/** * Attempt to register a new user. * * @return \Illuminate\Http\Response */ public function postRegister() { if (!Config::get('credentials.regallowed')) { return Redirect::route('account.register'); } $input = Binput::only(['first_name', 'last_name', 'email', 'password', 'password_confirmation']); $val = UserRepository::validate($input, array_keys($input)); if ($val->fails()) { return Redirect::route('account.register')->withInput()->withErrors($val->errors()); } $this->throttler->hit(); try { unset($input['password_confirmation']); $user = Credentials::register($input); if (!Config::get('credentials.activation')) { $mail = ['url' => URL::to(Config::get('credentials.home', '/')), 'email' => $user->getLogin(), 'subject' => Config::get('app.name') . ' - Welcome']; Mail::queue('credentials::emails.welcome', $mail, function ($message) use($mail) { $message->to($mail['email'])->subject($mail['subject']); }); $user->attemptActivation($user->getActivationCode()); $user->addGroup(Credentials::getGroupProvider()->findByName('Users')); return Redirect::to(Config::get('credentials.home', '/'))->with('success', 'Your account has been created successfully. You may now login.'); } $code = $user->getActivationCode(); $mail = ['url' => URL::to(Config::get('credentials.home', '/')), 'link' => URL::route('account.activate', ['id' => $user->id, 'code' => $code]), 'email' => $user->getLogin(), 'subject' => Config::get('app.name') . ' - Welcome']; Mail::queue('credentials::emails.welcome', $mail, function ($message) use($mail) { $message->to($mail['email'])->subject($mail['subject']); }); return Redirect::to(Config::get('credentials.home', '/'))->with('success', 'Your account has been created. Check your email for the confirmation link.'); } catch (UserExistsException $e) { return Redirect::route('account.register')->withInput()->withErrors($val->errors())->with('error', 'That email address is taken.'); } }
/** * Activate an existing user. * * @param int $id * @param string $code * * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException * * @return \Illuminate\Http\Response */ public function getActivate($id, $code) { if (!$id || !$code) { throw new BadRequestHttpException(); } try { $user = Credentials::getUserProvider()->findById($id); if (!$user->attemptActivation($code)) { return Redirect::to(Config::get('credentials.home', '/'))->with('error', 'There was a problem activating this account. Please contact support.'); } $user->addGroup(Credentials::getGroupProvider()->findByName('Users')); return Redirect::route('account.login')->with('success', 'Your account has been activated successfully. You may now login.'); } catch (UserNotFoundException $e) { return Redirect::to(Config::get('credentials.home', '/'))->with('error', 'There was a problem activating this account. Please contact support.'); } catch (UserAlreadyActivatedException $e) { return Redirect::route('account.login')->with('warning', 'You have already activated this account. You may want to login.'); } }
/** * Run the database seeding. * * @return void */ public function run() { DB::table('groups')->truncate(); // users $permissions = ['user' => 1, 'edit' => 0, 'blog' => 0, 'mod' => 0, 'admin' => 0]; $group = ['name' => 'Users', 'permissions' => $permissions]; Credentials::getGroupProvider()->create($group); // editors $permissions = ['user' => 1, 'edit' => 1, 'blog' => 0, 'mod' => 0, 'admin' => 0]; $group = ['name' => 'Editors', 'permissions' => $permissions]; Credentials::getGroupProvider()->create($group); // bloggers $permissions = ['user' => 1, 'edit' => 0, 'blog' => 1, 'mod' => 0, 'admin' => 0]; $group = ['name' => 'Bloggers', 'permissions' => $permissions]; Credentials::getGroupProvider()->create($group); // moderators $permissions = ['user' => 1, 'edit' => 0, 'blog' => 0, 'mod' => 1, 'admin' => 0]; $group = ['name' => 'Moderators', 'permissions' => $permissions]; Credentials::getGroupProvider()->create($group); // admins $permissions = ['user' => 1, 'edit' => 1, 'blog' => 1, 'mod' => 1, 'admin' => 1]; $group = ['name' => 'Admins', 'permissions' => $permissions]; Credentials::getGroupProvider()->create($group); }
/** * Add the user by email to a group. * * @param string $email * @param string $group * * @return void */ protected function matchUser($email, $group) { return Credentials::getUserProvider()->findByLogin($email)->addGroup(Credentials::getGroupProvider()->findByName($group)); }
/** * Attempt to login the specified user. * * @return \Illuminate\Http\Response */ public function postLogin() { $remember = Binput::get('rememberMe'); $input = Binput::only(['email', 'password']); $rules = UserRepository::rules(array_keys($input)); $rules['password'] = '******'; $val = UserRepository::validate($input, $rules, true); if ($val->fails()) { return Redirect::route('account.login')->withInput()->withErrors($val->errors()); } $this->throttler->hit(); try { $throttle = Credentials::getThrottleProvider()->findByUserLogin($input['email']); $throttle->check(); Credentials::authenticate($input, $remember); } catch (WrongPasswordException $e) { return Redirect::route('account.login')->withInput()->withErrors($val->errors())->with('error', 'Your password was incorrect.'); } catch (UserNotFoundException $e) { return Redirect::route('account.login')->withInput()->withErrors($val->errors())->with('error', 'That user does not exist.'); } catch (UserNotActivatedException $e) { if (Config::get('credentials::activation')) { return Redirect::route('account.login')->withInput()->withErrors($val->errors())->with('error', 'You have not yet activated this account.'); } else { $throttle->user->attemptActivation($throttle->user->getActivationCode()); $throttle->user->addGroup(Credentials::getGroupProvider()->findByName('Users')); return $this->postLogin(); } } catch (UserSuspendedException $e) { $time = $throttle->getSuspensionTime(); return Redirect::route('account.login')->withInput()->withErrors($val->errors())->with('error', "Your account has been suspended for {$time} minutes."); } catch (UserBannedException $e) { return Redirect::route('account.login')->withInput()->withErrors($val->errors())->with('error', 'You have been banned. Please contact support.'); } return Redirect::intended(Config::get('credentials.home', '/')); }