Example #1
0
 public function login()
 {
     try {
         $data = Input::all();
         $credentials = array('email' => $data['email'], 'password' => $data['password']);
         $user = Sentry::authenticate($credentials, false);
         $groups = Sentry::getUser()->getGroups();
         $is_admin = 0;
         foreach ($groups as $v) {
             if ($v->is_admin == 1) {
                 $is_admin = 1;
             }
         }
         if ($is_admin == 0) {
             Sentry::logout();
             return Response::json(['status' => false, 'error' => '账户非管理员']);
         }
         // Authenticate the user
         return Response::json(['status' => $user ? true : false]);
     } catch (Cartalyst\Sentry\Users\LoginRequiredException $e) {
         return Response::json(['status' => false, 'error' => '请输入完整字段']);
     } catch (Cartalyst\Sentry\Users\PasswordRequiredException $e) {
         return Response::json(['status' => false, 'error' => '请输入密码']);
     } catch (Cartalyst\Sentry\Users\WrongPasswordException $e) {
         return Response::json(['status' => false, 'error' => '密码错误,请重试']);
     } catch (Cartalyst\Sentry\Users\UserNotFoundException $e) {
         return Response::json(['status' => false, 'error' => '用户不存在']);
     } catch (Cartalyst\Sentry\Users\UserNotActivatedException $e) {
         return Response::json(['status' => false, 'error' => '用户暂未激活']);
     }
 }
 /**
  * Authenticate with Sentry.
  *
  * @param array $credentials
  * @param bool $remember
  *
  * @return array
  */
 public function authenticate($credentials, $remember = NULL)
 {
     $response = ['authenticated' => false, 'message' => ''];
     /*
      * Try to log in the user with sentry
      */
     try {
         Sentry::authenticate($credentials, $remember);
         $response['authenticated'] = true;
         /*
          * Credentials were valid, return authenticated response
          */
         return $response;
     } catch (WrongPasswordException $e) {
         $response['message'] = 'Username or Password is incorrect.';
     } catch (UserNotActivatedException $e) {
         $response['message'] = 'Your account has not been activated.
             Please follow the link you were emailed to activate your account.';
     } catch (UserSuspendedException $e) {
         $response['message'] = 'Your account has been suspended. Please try again later.';
     } catch (UserBannedException $e) {
         $response['message'] = 'Your account has been permanently banned.';
     } catch (UserExistsException $e) {
         $response['message'] = 'Username or Password is incorrect.';
     } catch (UserNotFoundException $e) {
         $response['message'] = 'Username or Password is incorrect.';
     }
     return $response;
 }
Example #3
0
 /**
  * Attempt to authenticate user with given credentials.
  *
  * @param array $credentials
  * @return bool
  */
 public function auth(array $credentials)
 {
     $remember = isset($credentials['remember']) ? true : false;
     try {
         Sentry::authenticate(array('email' => $credentials['email'], 'password' => $credentials['password']), $remember);
     } catch (LoginRequiredException $e) {
         $this->error = 'Email field is required.';
         return false;
     } catch (PasswordRequiredException $e) {
         $this->error = 'Password field is required.';
         return false;
     } catch (WrongPasswordException $e) {
         $this->error = 'Wrong email or password, try again.';
         return false;
     } catch (UserNotFoundException $e) {
         $this->error = 'Wrong email or password, try again.';
         return false;
     } catch (UserNotActivatedException $e) {
         $this->error = 'This account has not been activated yet, please check your mailbox.';
         return false;
     } catch (UserSuspendedException $e) {
         $this->error = 'This account is suspended!';
         return false;
     } catch (UserBannedException $e) {
         $this->error = 'This account is banned!';
         return false;
     }
     return true;
 }
Example #4
0
 public function doLogin(Request $request)
 {
     if ($request->has('email') and $request->has('password')) {
         $outputMessage = array();
         try {
             $email = $request->input('email');
             $password = $request->input('password');
             $remember = false;
             if ($request->has('remember')) {
                 $remember = true;
             }
             $user = Sentry::authenticate(array('email' => $email, 'password' => $password), $remember);
             return redirect(\Config::get('app.settings.url.admin_dashboard'));
         } catch (\Cartalyst\Sentry\Users\LoginRequiredException $e) {
             $outputMessage[] = array("type" => "alert", "msg" => "Login field is required.");
         } catch (\Cartalyst\Sentry\Users\PasswordRequiredException $e) {
             $outputMessage[] = array("type" => "alert", "msg" => "Password field is required.");
         } catch (\Cartalyst\Sentry\Users\WrongPasswordException $e) {
             $outputMessage[] = array("type" => "alert", "msg" => "Wrong password, try again.");
         } catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
             $outputMessage[] = array("type" => "alert", "msg" => "User was not found.");
         } catch (\Cartalyst\Sentry\Users\UserNotActivatedException $e) {
             $outputMessage[] = array("type" => "alert", "msg" => "User is not activated.");
         } catch (\Cartalyst\Sentry\Throttling\UserSuspendedException $e) {
             $outputMessage[] = array("type" => "alert", "msg" => "User is suspended.");
         } catch (\Cartalyst\Sentry\Throttling\UserBannedException $e) {
             $outputMessage[] = array("type" => "alert", "msg" => "User is banned.");
         }
         return view('users.login')->with('messages', $outputMessage);
     } else {
         $outputMessage[] = array("type" => "alert", "msg" => "Login and password field is required.");
         return view('users.login')->with('messages', $outputMessage);
     }
 }
Example #5
0
 public function authen()
 {
     // Gather Sanitized Input
     $input = array('email' => Input::get('email'), 'password' => Input::get('password'), 'rememberMe' => Input::get('rememberMe'));
     // Set Validation Rules
     $rules = array('email' => 'required|min:4|max:255' . ($input['email'] !== "administrator" ? "|email" : ""), 'password' => 'required|min:6');
     //Run input validation
     $v = Validator::make($input, $rules);
     if ($v->fails()) {
         // Validation has failed
         return Redirect::route('admin.signin')->withErrors($v)->withInput();
     } else {
         try {
             // Set login credentials
             $credentials = array('email' => $input['email'], 'password' => $input['password']);
             // Try to authenticate the user
             Sentry::authenticate($credentials, $input['rememberMe']);
         } catch (UserNotFoundException $e) {
             Session::flash('error', 'Invalid username or password.');
             return Redirect::route('admin.signin')->withErrors($v)->withInput();
         } catch (UserNotActivatedException $e) {
             Session::flash('error', 'You have not yet activated this account.');
             return Redirect::route('admin.signin')->withErrors($v)->withInput();
         }
         //Login was succesful.
         return Redirect::route("admin.home");
     }
 }
Example #6
0
 public function attempt($arguments = array())
 {
     try {
         $result = Sentry::authenticate($arguments, false);
         return $this->login($result->id, array_get($arguments, 'remember'));
     } catch (Exception $e) {
         return false;
     }
 }
 public function doLogin()
 {
     parse_str(Input::get('filds'), $filds);
     $validator = Validator::make($filds, $this->auth_rules);
     if ($validator->fails()) {
         return Response::json(array('status' => 'error', "errors_messages" => implode("<br>", $validator->messages()->all())));
     }
     try {
         $user = Sentry::authenticate(array('email' => $filds['email'], 'password' => $filds['password'], 'activated' => "1"));
         return Response::json(array('status' => 'ok', "ok_messages" => "Вы успешно авторизованы"));
     } catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
         return Response::json(array('status' => 'error', "errors_messages" => "Пользователь не найден"));
     }
 }
Example #8
0
 public function postLogin()
 {
     try {
         // Set login credentials
         $credentials = array('email' => Input::get('email'), 'password' => Input::get('password'));
         // Try to authenticate the user
         Sentry::authenticate($credentials, Input::get('remember'));
         return Redirect::to('/');
     } catch (Exception $e) {
         $exception = get_class($e);
         $msg = Lang::has("sentry.{$exception}") ? Lang::get("sentry.{$exception}") : $e->getMessage();
         Session::flash('error', $msg);
     }
     return Redirect::to('/login');
 }
Example #9
0
 /**
  * @param $data
  * @return bool
  * @throws AuthenticatorException
  */
 public function attempt($data)
 {
     $valid = $this->loginValidator->isValid($data);
     if (!$valid) {
         $this->setValidationErrors($this->loginValidator->getMessages());
         throw new ValidationException("Validation error");
     }
     try {
         $user = Sentry::authenticate(['email' => $data['identifier'], 'password' => $data['password']], empty($data['remember']) ? false : true);
         return true;
     } catch (UserNotFoundException $e) {
         throw new InvalidCredentialsException("User not found");
     } catch (WrongPasswordException $e) {
         throw new InvalidCredentialsException("Password wrong!");
     }
 }
Example #10
0
 /**
  * Authenticate a user
  * @param  array $credentials
  * @param  bool  $remember    Remember the user
  * @return mixed
  */
 public function login(array $credentials, $remember = false)
 {
     try {
         Sentry::authenticate($credentials, $remember);
         return false;
     } catch (LoginRequiredException $e) {
         return 'Login field is required.';
     } catch (PasswordRequiredException $e) {
         return 'Password field is required.';
     } catch (WrongPasswordException $e) {
         return 'Wrong password, try again.';
     } catch (UserNotFoundException $e) {
         return 'User was not found.';
     } catch (UserNotActivatedException $e) {
         return 'User is not activated.';
     } catch (UserSuspendedException $e) {
         return 'User is suspended.';
     } catch (UserBannedException $e) {
         return 'User is banned.';
     }
 }
Example #11
0
 public function dologin()
 {
     try {
         $credentials = array('email' => Input::get('username'), 'password' => Input::get('password'));
         if (!Input::get('remember')) {
             Sentry::authenticate($credentials);
         } else {
             Sentry::authenticate($credentials, true);
         }
     } catch (Cartalyst\Sentry\Users\LoginRequiredException $e) {
         $this->errors = '请填写必填项.';
     } catch (Cartalyst\Sentry\Users\UserNotFoundException $e) {
         $this->errors = '用户名或密码错误.';
     } catch (Cartalyst\Sentry\Users\UserNotActivatedException $e) {
         $this->errors = '用户暂未激活,请前往邮箱' . link_to((new Uinfo())->getMail($credentials['email']), '激活', ['target' => '_blank']);
     } catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e) {
         $time = $throttle->getSuspensionTime();
         $this->errors = "User is suspended for [{$time}] minutes.";
     } catch (Cartalyst\Sentry\Throttling\UserBannedException $e) {
         $this->errors = 'User is banned.';
     }
     return Redirect::route('auth.login')->withErrors($this->errors);
 }
Example #12
0
 /**
  * Handle logging in / logging out a user.
  *
  * @return Response
  */
 public function login()
 {
     $status = 401;
     try {
         // Set login credentials
         $credentials = array('email' => Request::getUser(), 'password' => Request::getPassword());
         // Try to authenticate the user
         $response = Sentry::authenticate($credentials, false);
         $status = 200;
     } catch (\Cartalyst\Sentry\Users\LoginRequiredException $e) {
         $response = array('message' => 'Provided information is not valid.', 'errors' => array(array('field' => 'email', 'message' => 'Login field is required.')));
     } catch (\Cartalyst\Sentry\Users\PasswordRequiredException $e) {
         $response = array('message' => 'Provided information is not valid.', 'errors' => array(array('field' => 'password', 'message' => 'Password field is required.')));
     } catch (\Cartalyst\Sentry\Users\WrongPasswordException $e) {
         $response = array('message' => 'Provided information is not valid.', 'errors' => array(array('field' => 'password', 'message' => 'Wrong password, try again.')));
     } catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
         $response = array('message' => 'User was not found.');
     } catch (\Cartalyst\Sentry\Users\UserNotActivatedException $e) {
         $response = array('message' => 'Your account is not yet activated.');
     } catch (\Cartalyst\Sentry\Throttling\UserSuspendedException $e) {
         $response = array('message' => 'Your account is suspended.');
     } catch (\Cartalyst\Sentry\Throttling\UserBannedException $e) {
         $response = array('message' => 'Your account is banned.');
     }
     // Get current client
     $client = API::getClient();
     // Logging in user
     if ($status == 200) {
         $clientEndpoint = $client->endpoint;
         $clientScopeIds = API::getResource()->getScopeIds();
         $clientScopes = API::getResource()->getScopes();
         $scopes = array();
         if (!empty($clientScopeIds)) {
             foreach ($clientScopeIds as $id) {
                 $scopes[] = array('id' => $id);
             }
         }
         unset($clientScopeIds);
         if (!is_array($clientScopes)) {
             $clientScopes = array();
         }
         // Create a new client endpoint if not exist
         if (!is_object($clientEndpoint)) {
             $redirectUri = Request::getSchemeAndHttpHost();
             $clientEndpoint = OauthClientEndpoint::create(array('client_id' => $client->id, 'redirect_uri' => $redirectUri));
         } else {
             $redirectUri = $clientEndpoint->redirect_uri;
         }
         // Create a new authorization code
         $authCode = API::newAuthorizeRequest('user', $response->id, array('client_id' => $client->id, 'redirect_uri' => $redirectUri, 'scopes' => $scopes));
         // Authorize the client to a user
         if (!empty($authCode)) {
             $params = array('grant_type' => 'authorization_code', 'client_id' => $client->id, 'client_secret' => $client->secret, 'redirect_uri' => $redirectUri, 'code' => $authCode, 'scope' => implode(',', $clientScopes), 'state' => time());
             $authorizationResponse = API::performAccessTokenFlow(false, $params);
             if (array_key_exists('status', $authorizationResponse)) {
                 $status = $authorizationResponse['status'];
                 $headers = $authorizationResponse['headers'];
                 unset($authorizationResponse['status']);
                 unset($authorizationResponse['headers']);
                 return API::resourceJson($authorizationResponse, $status, $headers);
             }
             // Merge user data with the new authorization data
             $authorizationResponse['user'] = new UserTemplate($response);
             $response = $authorizationResponse;
             unset($authorizationResponse);
         } else {
             $response = array('message' => 'There was a problem while logging you in, please try again or contact customer support.');
             $status = 500;
         }
         unset($scopes);
         unset($clientScopes);
         // Logout user
     } else {
         $user = null;
         try {
             $user = Sentry::getUser();
         } catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
         }
         if (!is_null($user) and !is_null($client)) {
             // Cleanup OAuth session
             $session = new FluentSession();
             $session->deleteSession($client->id, 'user', $user->getId());
             unset($session);
             // Logout user via sentry
             Sentry::logout();
         }
         unset($user);
     }
     return API::resourceJson($response, $status);
 }
 public function loginsubmit()
 {
     $input = Input::all();
     $rules = array('password' => array('required'), 'email' => array('required'));
     $validation = Validator::make(Input::all(), $rules);
     if ($validation->fails()) {
         return Redirect::to('user/login');
     }
     $credentials = array('email' => $input['email'], 'password' => $input['password']);
     try {
         $user = Sentry::findUserByLogin($input['email']);
     } catch (UserNotFoundException $e) {
         Session::flash('message', 'User or Password not match.');
         return Redirect::to('user/login');
     }
     //check password
     if (!$user->checkPassword($input['password'])) {
         Session::flash('message', 'User or Password not match.');
         return Redirect::to('user/login');
     }
     $throttle = Sentry::findThrottlerByUserId($user['id']);
     //check Suspended
     if ($suspended = $throttle->isSuspended()) {
         Session::flash('message', ' User is Suspended.');
         return Redirect::to('user/login');
     }
     //check banned
     if ($banned = $throttle->isBanned()) {
         Session::flash('message', 'User banned.');
         return Redirect::to('user/login');
     }
     //check deactive
     if (!$user->isActivated()) {
         Session::flash('message', 'User not activated.');
         return Redirect::to('user/login');
     }
     if (isset($input['rememberme']) && $input['rememberme'] == 1) {
         Sentry::authenticateAndRemember($credentials);
     } else {
         Sentry::authenticate($credentials, false);
     }
     if ($user->hasAccess('dashboard')) {
         return Redirect::to('manager/videos');
     } else {
         return Redirect::to('/');
     }
 }