示例#1
0
 /**
  * Handle a registration request for the application.
  *
  * @param RegisterFormRequest $request
  * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|void
  */
 public function postRegister(RegisterFormRequest $request, UserRoleApi $userRoleApi)
 {
     $redirect = false;
     $errorMsg = '';
     // Start transaction
     DB::beginTransaction();
     // Register new user
     try {
         $user = $this->user->where('email', '=', $request->email)->first();
         $userId = isset($user->id) ? $user->id : false;
         // Save user
         if (!$userId) {
             $this->user->first_name = $request->first_name;
             $this->user->last_name = $request->last_name;
             $this->user->email = $request->email;
             $this->user->password = bcrypt($request->password);
             $this->user->active = 1;
             $this->user->save();
             $userId = $this->user->id;
         }
         // Roles that were selected to be registered
         $selectedRoles = explode(',', $request->get('role'));
         // Find all roles for this user
         $userRoles = $this->user->findOrNew($userId)->roles();
         $existingRoles = [];
         foreach ($userRoles->get() as $user) {
             $existingRoles[] = $user->role_id;
         }
         // Identical user with all the roles, throw error
         if (isset($userId) && $userRoles->count() == 3) {
             throw new Exception(config('testplanner.messages.users.identical_user'));
         }
         // Throw error if role already exists
         if (count(array_diff($selectedRoles, $existingRoles)) == 0) {
             throw new Exception(config('testplanner.messages.users.identical_role'));
         }
         // Add user's role
         $userRoleApi->addRoles($userId, $selectedRoles);
     } catch (\Exception $e) {
         $errorMsg = $e->getMessage();
         $redirect = true;
     } catch (QueryException $e) {
         $errorMsg = $e->getErrors();
         $redirect = true;
     } catch (ModelNotFoundException $e) {
         $errorMsg = $e->getErrors();
         $redirect = true;
     }
     // Redirect if errors
     if ($redirect) {
         // Rollback
         DB::rollback();
         // Log specific technical message
         Tools::log($errorMsg, array_except($request->all(), ['_token', 'created_from', 'created_to', 'password', 'password_confirmation']));
         // Return JSON error response
         return response()->json(['type' => 'error', 'msg' => config('testplanner.messages.users.new_error')]);
     }
     // Commit all changes
     DB::commit();
     // Flash message so it could be shown once redirected by AJAX call
     Session::flash('flash_success', config('testplanner.messages.users.new'));
     // Return JSON success message and redirect url
     return response()->json(['type' => 'success', 'redirect_url' => url('user/all')]);
 }
 /**
  * Handle a registration request for the application.
  *
  * @param RegisterFormRequest $request
  * @return \Illuminate\Http\Response
  */
 public function postRegister(RegisterFormRequest $request)
 {
     $user = $this->create($request->all());
     $this->userLogin($user);
     return $this->authenticated($request, $user);
 }
 public function register(RegisterFormRequest $request)
 {
     User::create($request->all());
     return redirect('admin/users');
 }