/** * 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.'); } }
public function testStoreSuccess() { $this->markTestSkipped('Tests requiring authentication are currently broken.'); Credentials::shouldReceive('getuser')->once()->andReturn((object) ['id' => 1]); $this->call('POST', 'pages', ['title' => 'New Page', 'nav_title' => 'Herro', 'slug' => 'foobar', 'icon' => '', 'body' => 'Why herro there!', 'css' => '', 'js' => '', 'show_title' => 'on', 'show_nav' => 'on']); $this->assertRedirectedTo('pages/foobar'); $this->assertSessionHas('success'); }
/** * Store a new post. * * @return \Illuminate\Http\Response */ public function store() { $input = array_merge(['user_id' => Credentials::getuser()->id], Binput::only(['title', 'summary', 'body'])); $val = PostRepository::validate($input, array_keys($input)); if ($val->fails()) { return Redirect::route('blog.posts.create')->withInput()->withErrors($val->errors()); } $post = PostRepository::create($input); return Redirect::route('blog.posts.show', ['posts' => $post->id])->with('success', 'Your post has been created successfully.'); }
/** * Store a new comment. * * @param int $postId * * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException * * @return \Illuminate\Http\JsonResponse */ public function store($postId) { $input = array_merge(Binput::only('body'), ['user_id' => Credentials::getuser()->id, 'post_id' => $postId, 'version' => 1]); if (CommentRepository::validate($input, array_keys($input))->fails()) { throw new BadRequestHttpException('Your comment was empty.'); } $this->throttler->hit(); $comment = CommentRepository::create($input); $contents = View::make('posts.comment', ['comment' => $comment, 'post_id' => $postId]); return Response::json(['success' => true, 'msg' => 'Comment created successfully.', 'contents' => $contents->render(), 'comment_id' => $comment->id], 201); }
/** * Store a new page. * * @return \Illuminate\Http\Response */ public function store() { $input = array_merge($this->getInput(), ['user_id' => Credentials::getuser()->id]); $val = PageRepository::validate($input, array_keys($input)); if ($val->fails()) { return Redirect::route('pages.create')->withInput()->withErrors($val->errors()); } $page = PageRepository::create($input); // write flash message and redirect return Redirect::route('pages.show', ['pages' => $page->slug])->with('success', 'Your page has been created successfully.'); }
/** * Store a new event. * * @return \Illuminate\Http\Response */ public function store() { $input = array_merge(['user_id' => Credentials::getuser()->id], Binput::only(['title', 'location', 'date', 'body'])); $val = EventRepository::validate($input, array_keys($input)); if ($val->fails()) { return Redirect::route('events.create')->withInput()->withErrors($val->errors()); } $input['date'] = Carbon::createFromFormat(Config::get('date.php_format'), $input['date']); $event = EventRepository::create($input); return Redirect::route('events.show', ['events' => $event->id])->with('success', 'Your event has been created successfully.'); }
/** * Run the database seeding. * * @return void */ public function run() { DB::table('users')->truncate(); $user = ['first_name' => 'CMS', 'last_name' => 'Admin', 'email' => '*****@*****.**', 'password' => 'password', 'activated' => 1, 'activated_at' => Carbon::now()]; Credentials::getUserProvider()->create($user); $user = ['first_name' => 'CMS', 'last_name' => 'Semi-Admin', 'email' => '*****@*****.**', 'password' => 'password', 'activated' => 1, 'activated_at' => Carbon::now()]; Credentials::getUserProvider()->create($user); $user = ['first_name' => 'CMS', 'last_name' => 'Moderator', 'email' => '*****@*****.**', 'password' => 'password', 'activated' => 1, 'activated_at' => Carbon::now()]; Credentials::getUserProvider()->create($user); $user = ['first_name' => 'CMS', 'last_name' => 'Blogger', 'email' => '*****@*****.**', 'password' => 'password', 'activated' => 1, 'activated_at' => Carbon::now()]; Credentials::getUserProvider()->create($user); $user = ['first_name' => 'CMS', 'last_name' => 'Editor', 'email' => '*****@*****.**', 'password' => 'password', 'activated' => 1, 'activated_at' => Carbon::now()]; Credentials::getUserProvider()->create($user); $user = ['first_name' => 'CMS', 'last_name' => 'User', 'email' => '*****@*****.**', 'password' => 'password', 'activated' => 1, 'activated_at' => Carbon::now()]; Credentials::getUserProvider()->create($user); }
/** * 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); }
/** * Queue the sending of the activation email. * * @return \Illuminate\Http\Response */ public function postResend() { $input = Binput::only('email'); $val = UserRepository::validate($input, array_keys($input)); if ($val->fails()) { return Redirect::route('account.resend')->withInput()->withErrors($val->errors()); } $this->throttler->hit(); try { $user = Credentials::getUserProvider()->findByLogin($input['email']); if ($user->activated) { return Redirect::route('account.resend')->withInput()->with('error', 'That user is already activated.'); } $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') . ' - Activation']; Mail::queue('credentials::emails.resend', $mail, function ($message) use($mail) { $message->to($mail['email'])->subject($mail['subject']); }); return Redirect::route('account.resend')->with('success', 'Check your email for your new activation email.'); } catch (UserNotFoundException $e) { return Redirect::route('account.resend')->with('error', 'That user does not exist.'); } }
/** * 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)); }
<?php /* * This file is part of Laravel Credentials. * * (c) Graham Campbell <*****@*****.**> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ use Illuminate\Support\Facades\Redirect; use GrahamCampbell\Credentials\Facades\Credentials; $router->filter('auth.admin', function ($route, $request) { if (Credentials::check()) { if (!Credentials::hasAccess('admin')) { return Redirect::to('/')->with('error', 'You do not have permission to login'); } } });
/** * Attempt to find the user id of the currently logged in user. * * @return int|null */ protected function getUserId() { if (Credentials::check()) { return Credentials::getUser()->id; } elseif (isset($this['user_id']) && $this['user_id']) { return $this['user_id']; } }
/** * Suspend an existing user. * * @param int $id * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException * * @return \Illuminate\Http\Response */ public function suspend($id) { try { $throttle = Credentials::getThrottleProvider()->findByUserId($id); $throttle->suspend(); } catch (UserNotFoundException $e) { throw new NotFoundHttpException('User Not Found', $e); } catch (UserSuspendedException $e) { $time = $throttle->getSuspensionTime(); return Redirect::route('users.suspend', ['users' => $id])->withInput()->with('error', "This user is already suspended for {$time} minutes."); } catch (UserBannedException $e) { return Redirect::route('users.suspend', ['users' => $id])->withInput()->with('error', 'This user has already been banned.'); } return Redirect::route('users.show', ['users' => $id])->with('success', 'The user has been suspended successfully.'); }
/** * Logout the specified user. * * @return \Illuminate\Http\Response */ public function getLogout() { Credentials::logout(); return Redirect::to(Config::get('credentials.home', '/')); }
/** * Removes the user from the given group. * * @param \Cartalyst\Sentry\Groups\GroupInterface $group * * @return bool */ public function removeGroup(GroupInterface $group) { RevisionRepository::create(['revisionable_type' => get_class($this), 'revisionable_id' => $this->getKey(), 'key' => 'removed_group', 'old_value' => null, 'new_value' => $group->getName(), 'user_id' => Credentials::getUser()->id]); return parent::removeGroup($group); }
/** * Reset the user's password. * * @param int $id * @param string $code * * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException * * @return \Illuminate\Http\Response */ public function getPassword($id, $code) { if (!$id || !$code) { throw new BadRequestHttpException(); } try { $user = Credentials::getUserProvider()->findById($id); $password = Str::random(); if (!$user->attemptResetPassword($code, $password)) { return Redirect::to(Config::get('credentials.home', '/'))->with('error', 'There was a problem resetting your password. Please contact support.'); } $mail = ['password' => $password, 'email' => $user->getLogin(), 'subject' => Config::get('app.name') . ' - New Password Information']; Mail::queue('credentials::emails.password', $mail, function ($message) use($mail) { $message->to($mail['email'])->subject($mail['subject']); }); return Redirect::to(Config::get('credentials.home', '/'))->with('success', 'Your password has been changed. Check your email for the new password.'); } catch (UserNotFoundException $e) { return Redirect::to(Config::get('credentials.home', '/'))->with('error', 'There was a problem resetting your password. Please contact support.'); } }
/** * Update the user's password. * * @return \Illuminate\Http\Response */ public function patchPassword() { $input = Binput::only(['password', 'password_confirmation']); $val = UserRepository::validate($input, array_keys($input)); if ($val->fails()) { return Redirect::route('account.profile')->withInput()->withErrors($val->errors()); } unset($input['password_confirmation']); $user = Credentials::getUser(); $this->checkUser($user); $mail = ['url' => URL::to(Config::get('credentials.home', '/')), 'email' => $user->getLogin(), 'subject' => Config::get('app.name') . ' - New Password Notification']; Mail::queue('credentials::emails.newpass', $mail, function ($message) use($mail) { $message->to($mail['email'])->subject($mail['subject']); }); $user->update($input); return Redirect::route('account.profile')->with('success', 'Your password has been updated successfully.'); }