Example #1
1
 public function indexAction()
 {
     $request = $this->getRequest();
     $user = new User();
     $this->connexionForm->bind($user);
     if ($request->isPost()) {
         $data = $request->getPost();
         $this->connexionForm->setData($data);
         if ($this->connexionForm->isValid()) {
             /** @var User $user */
             $user = $this->connexionForm->getData();
             $adapter = $this->authenticationService->getAdapter();
             $adapter->setIdentityValue($user->getUsername());
             $adapter->setCredentialValue($user->getPassword());
             $result = $this->authenticationService->authenticate();
             if ($result->isValid()) {
                 $this->flashMessenger()->addSuccessMessage($this->getTranslation('FORM_SUCCESS_LOGIN'));
                 return $this->redirect()->toRoute('admin/posts');
             }
         }
         $this->flashMessenger()->addErrorMessage($this->getTranslation('FORM_ERROR_LOGIN'));
         return $this->redirect()->toRoute('admin');
     }
     return new ViewModel(array('form' => $this->connexionForm));
 }
Example #2
0
 /**
  * @param $data
  * @return mixed
  */
 public function login($data)
 {
     /** @var ObjectRepository $adapter */
     $adapter = $this->authService->getAdapter();
     $adapter->setIdentityValue($data->login);
     $adapter->setCredentialValue($data->password);
     $result = $adapter->authenticate();
     switch ($result->getCode()) {
         case Result::SUCCESS:
             $identity = $result->getIdentity();
             $this->authService->getStorage()->write($identity);
             if (isset($data->rememberMe)) {
                 $this->sessionManager->rememberMe(1209600);
             }
             $toJson['user'] = $identity->toArray();
             $toJson['session_id'] = $this->sessionManager->getId();
             $toJson['success'] = true;
             return json_decode(json_encode($toJson));
         case Result::FAILURE_IDENTITY_NOT_FOUND:
             return ['success' => false, 'message' => 'User not found.'];
         case Result::FAILURE_CREDENTIAL_INVALID:
             return ['success' => false, 'message' => 'Invalid password'];
         default:
             return ['success' => false, 'message' => 'Error while login'];
     }
 }
 public function authenticateAction()
 {
     if ($this->identity()) {
         return $this->redirect()->toRoute($this->routes['redirect']['name'], $this->routes['redirect']['params'], $this->routes['redirect']['options'], $this->routes['redirect']['reuseMatchedParams']);
     }
     $form = new SigninForm();
     $form->setAttribute('action', $this->url()->fromRoute($this->routes['authenticate']['name'], $this->routes['authenticate']['params'], $this->routes['authenticate']['options'], $this->routes['authenticate']['reuseMatchedParams']));
     $request = $this->getRequest();
     if ($request->isPost()) {
         $post = $request->getPost();
         $form->setData($post);
         if ($form->isValid()) {
             $authAdapter = $this->authenticationService->getAdapter();
             $authAdapter->setIdentityValue($form->get('username')->getValue());
             $authAdapter->setCredentialValue(sha1(sha1($form->get('password')->getValue())));
             $authResult = $this->authenticationService->authenticate();
             if ($authResult->isValid()) {
                 $identity = $authResult->getIdentity();
                 $authStorage = $this->authenticationService->getStorage();
                 if ($form->get('remember-me')->getValue() == 1) {
                     $authStorage->setRememberMe(1);
                 }
                 $authStorage->write($identity);
                 $this->flashMessenger()->addSuccessMessage(_('Sign in with success!'));
                 return $this->redirect()->toRoute($this->routes['redirect']['name'], $this->routes['redirect']['params'], $this->routes['redirect']['options'], $this->routes['redirect']['reuseMatchedParams']);
             } else {
                 $this->flashMessenger()->addErrorMessage(_('Username or password is invalid.'));
             }
         }
     }
     return $this->redirect()->toRoute($this->routes['signin']['name'], $this->routes['signin']['params'], $this->routes['signin']['options'], $this->routes['signin']['reuseMatchedParams']);
 }
Example #4
0
 public function __construct(AuthenticationService $authService)
 {
     parent::__construct('login');
     $this->filter = new InputFilter();
     $email = new Element\Email('email');
     $email->setAttribute('required', true);
     $email->setAttribute('placeholder', 'Email Address');
     $this->add($email);
     $emailFilter = new Input('email');
     $emailFilter->setRequired(true);
     $this->filter->add($emailFilter);
     $password = new Element\Password('password');
     $password->setAttribute('required', true);
     $password->setAttribute('placeholder', 'Password');
     $this->add($password);
     $passwordFilter = new Input('password');
     $passwordFilter->setRequired(true);
     $passwordFilter->getValidatorChain()->attach(new AuthValidator\Authentication(array('message' => 'Invalid email address or password', 'service' => $authService, 'adapter' => $authService->getAdapter(), 'identity' => 'email', 'credential' => 'password')));
     $this->filter->add($passwordFilter);
     $buttons = new Form('buttons');
     $buttons->setOption('twb-layout', 'inline');
     $buttons->setAttribute('class', 'form-group');
     $submit = new Element\Submit('submit');
     $submit->setAttribute('class', 'btn-primary pull-right');
     $submit->setOption('glyphicon', 'log-in');
     $submit->setLabel('Log In');
     $buttons->add($submit);
     $forgot = new Element\Submit('forgot');
     $forgot->setAttribute('formnovalidate', true);
     $forgot->setAttribute('class', 'btn-warning pull-right');
     $forgot->setOption('glyphicon', 'question-sign');
     $forgot->setLabel('Forgot Password');
     $buttons->add($forgot);
     $this->add($buttons);
 }
 /**
  * Handle authentication
  *
  * @param  GetResponseEvent $event
  * @throws RuntimeException
  */
 public function onKernelRequest(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     if (!$this->isRequestNeedProcessing($request) || $this->authentication->hasIdentity()) {
         return;
     }
     /* @var $adapter \SilexCMF\ZendAuthentication\Adapter\Http\HttpBasedAdapter */
     $adapter = $this->authentication->getAdapter();
     if (!$adapter || !$adapter instanceof HttpBasedAdapter) {
         throw new RuntimeException(sprintf('Unsupported adapter type %s', is_object($adapter) ? get_class($adapter) : gettype($adapter)));
     }
     $result = $this->authentication->authenticate();
     if (!$result->isValid()) {
         $event->setResponse($adapter->getResponse());
     }
 }
Example #6
0
 public function loginAction()
 {
     if ($this->authenticationService->hasIdentity()) {
         return $this->redirect()->toRoute('home');
     }
     $this->layout('layout/layout-blank');
     $resultModel = new JsonResultModel();
     if ($this->getRequest()->isPost()) {
         $jsonData = $this->getRequest()->getPost('login');
         $data = Json::decode($jsonData, Json::TYPE_ARRAY);
         // If you used another name for the authentication service, change it here
         $adapter = $this->authenticationService->getAdapter();
         $adapter->setIdentityValue($data['username']);
         $adapter->setCredentialValue($data['password']);
         $authResult = $this->authenticationService->authenticate();
         //@todo remember me
         if ($authResult->isValid()) {
             if ($data['rememberMe']) {
                 $this->authenticationService->getStorage()->getManager()->rememberMe(36000);
             }
             return $resultModel;
         } else {
             $resultModel->addErrors('password', '登录名或密码错误');
             return $resultModel;
         }
     }
 }
Example #7
0
 public function __invoke(Request $req, Response $res)
 {
     if ($req->isPost()) {
         $adapter = $this->authService->getAdapter();
         if ($adapter instanceof ValidatableAdapterInterface) {
             $adapter->setIdentity($req->getParam('identity'));
             $adapter->setCredential($req->getParam('credential'));
         }
         $result = $this->authService->authenticate($adapter);
         if (!$result->isValid()) {
             $this->flash->addMessage('danger', reset($result->getMessages()));
             return $res->withRedirect($req->getUri());
         }
         return $res->withRedirect($this->successUrl);
     }
     return $this->view->render($res, 'user/login.twig', []);
 }
Example #8
0
 /**
  * Authentificate user by username/password pair
  *
  * @param string $username
  * @param string $password
  * @param boolean $remember
  * @return \Zend\Authentication\Result
  * @throws \Exception
  */
 public function authentificate($username, $password, $remember = false)
 {
     $adapter = $this->authService->getAdapter();
     if (!$adapter instanceof \Zend\Authentication\Adapter\DbTable) {
         throw new \Exception('invalid auth adapter type');
     }
     $adapter->setIdentity($username)->setCredential($password);
     $result = $this->authService->authenticate();
     if ($result->getCode() == \Zend\Authentication\Result::SUCCESS) {
         if ($remember) {
             $this->sessionContainer->getManager()->rememberMe();
         }
         $this->sessionContainer->userEntity = (array) $adapter->getResultRowObject();
     } else {
         $this->sessionContainer->userEntity = null;
     }
     return $result;
 }
Example #9
0
 /**
  * Test if authentication is valid
  * 
  * @see \Zend\Validator\ValidatorInterface::isValid()
  * @param mixed $value
  * @param array $context
  * @return boolean
  */
 public function isValid($value, array $context = null)
 {
     $context = (array) $context;
     if (!array_key_exists($this->identity, $context) || empty($context[$this->identity])) {
         //             $this->error(self::IDENTITY_EMPTY);
         return false;
     }
     if (!array_key_exists($this->credential, $context) || empty($context[$this->credential])) {
         //             $this->error(self::CREDENTIAL_EMPTY);
         return false;
     }
     $authAdapter = $this->auth->getAdapter();
     $authAdapter->setIdentity($context[$this->identity]);
     $authAdapter->setCredential($context[$this->credential]);
     $result = $this->authResult = $this->auth->authenticate();
     if (!$result->isValid()) {
         $this->error($result->getCode());
         return false;
     }
     return true;
 }
 /**
  * authenticate
  *
  * Perform the authentication.
  *
  * @param  mixed  $data  The authentication data.
  *
  * @return boolean
  *
  * @throws \InvalidArgumentException  If $data is not an array.
  */
 public function authenticate($data)
 {
     if (!is_array($data)) {
         throw new \InvalidArgumentException(sprintf('The \'data\' argument must be of type \'array\'; \'%s\' given in \'%s\'.', gettype($data), __METHOD__));
     }
     $eventManager = $this->getEventManager();
     /** @var AuthenticationEvent $event */
     $event = $this->getEvent();
     $event->setName(AuthenticationEvent::EVENT_PREPARE)->setAdapter($this->auth->getAdapter())->setStorage($this->auth->getStorage())->setParams($data);
     // trigger authentication 'prepare' event
     $eventManager->trigger($event);
     // trigger the authentication event
     $event->setName(AuthenticationEvent::EVENT_AUTHENTICATE);
     $eventManager->trigger($event);
     if ($this->hasIdentity()) {
         $event->setName(AuthenticationEvent::EVENT_SUCCESS);
         $eventManager->trigger($event);
         return true;
     }
     $event->setName(AuthenticationEvent::EVENT_FAILURE);
     $eventManager->trigger($event);
     return false;
 }
Example #11
0
 public static function authenticateUser($user, $pass, AuthenticationService $authService)
 {
     $authService->getAdapter()->setIdentity($user)->setCredential($pass);
     $result = $authService->authenticate();
     return $result;
 }
Example #12
0
 /**
  * Authenticates user
  *
  * @param  string                                         $email    User email
  * @param  string                                         $password User password
  * @return Zend\Authentication\Result
  * @throws Zend\Authentication\Exception\RuntimeException
  */
 public function authenticate($email, $password)
 {
     $adapter = $this->auth->getAdapter();
     $adapter->setCredentials($email, $password);
     return $this->auth->authenticate();
 }