Esempio n. 1
0
 /**
  * Validate and add user
  * @param Request
  * @return User
  */
 public function addUser(Request $request)
 {
     $this->validate($request, ['username' => 'required', 'password' => 'required', 'email' => 'required|email']);
     $user = User::where('email', $request->input("email"))->orWhere('username', $request->input("username"))->first();
     if (empty($user)) {
         $user = new User();
         $user->name = $request->input("name", '');
         $user->username = $request->input("username");
         $user->email = $request->input("email");
         $user->role = $request->input("role", 2);
         $user->password = hash('sha1', $request->input("password"));
         $user->save();
         $userNew = User::where('username', $user->username)->first();
         $userNew->role_desc = Roles::where('role_id', $userNew->role)->first()->role_desc;
         return $this->respondWithCORS($userNew);
     } else {
         $error = new Notification();
         $error->notify("User already exist, please try different username and email.", 5100);
         return $this->respondWithCORS($error);
     }
 }
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store(ModelNewRequest $request)
 {
     try {
         $roles = $request->input('roles', []);
         $departments = $request->input('departments', []);
         $user_id = $request->input('user_id', null);
         $model = new User($request->all());
         try {
             DB::beginTransaction();
             $model->user_id = $user_id;
             $model->save();
             $model->roles()->sync($roles);
             $model->departments()->sync($departments);
             if ($request->hasFile('photo')) {
                 $uploaded_file = $request->file('photo');
                 if ($uploaded_file->isValid()) {
                     $picture = new Picture();
                     $picture->filename = $this->save_picture($model->name, $uploaded_file);
                     $picture->mime_type = $uploaded_file->getMimeType();
                     $picture->extension = $uploaded_file->guessExtension();
                     if (!$picture->extension) {
                         $picture->extension = $uploaded_file->getExtension();
                     }
                     $picture->user_id = $model->id;
                     $picture->save();
                 }
             }
             DB::commit();
             Flash::info(trans($this->resource_name . 'saved', ['model' => $this->model_name]));
             return redirect(route($this->show_route, [$model->id]));
         } catch (Exception $e) {
             DB::rollBack();
             throw $e;
         }
     } catch (Exception $e) {
         $errors = [];
         if ($e->getCode() == 23000) {
             $errors['email'] = trans($this->resource_name . 'duplicated_email');
         } else {
             Flash::error($e->getMessage());
         }
         return $request->response($errors);
     }
 }