public function createUser(RegisterFormRequest $request) { $userName = $request->get('name'); $email = new UserEmail($request->get('email')); $password = new PasswordEncryption($request->get('password')); $userDTO = new UserRegistrationDTO($userName, $email, $password); $userRegistrationService = new UserRegistrationService($userDTO); $user = $userRegistrationService->register(); Auth::attempt(['email' => $email->getEmail(), 'password' => $request->get('password')]); return redirect('/dashboard'); }
/** * 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); }
/** * 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')]); }
/** * Store a newly created user in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(RegisterFormRequest $request) { $user_chk = User::find($request->get('username')); if ($user_chk != null) { $error = 1; $error_msg = "User already existed."; return response()->json(['error' => $error, 'error_msg' => $error_msg]); } else { $error = 0; $error_msg = "Registration completed."; $user = new User(); $user->username = $request->get('username'); $user->password = sha1($request->get('password')); $user->first_name = $request->get('first_name'); $user->last_name = $request->get('last_name'); $user->email = $request->get('email'); $user->save(); return response()->json(['error' => $error, 'error_msg' => $error_msg]); } }
public function register(RegisterFormRequest $request) { User::create($request->all()); return redirect('admin/users'); }