Example #1
0
 public function isValid(array $values)
 {
     $isValid = parent::isValid($values);
     if (!$isValid) {
         return false;
     }
     $users = new Users();
     $user = $users->fetch('email', $values['email']);
     if (!$user) {
         $this->setError('email', 'User does not exist');
         return false;
     }
     return true;
 }
Example #2
0
 public function isValid(array $values)
 {
     $isValid = parent::isValid($values);
     if (!$isValid) {
         return false;
     }
     $users = new Users();
     if ($users->fetch('email', $values['email'])) {
         $this->setError('email', 'Email is already in use');
         return false;
     }
     if ($users->fetch('username', $values['username'])) {
         $this->setError('username', 'Username already exists');
         return false;
     }
     if (!$this->isMatch()) {
         $this->setError('confirm', 'Passwords do not match');
         return false;
     }
     return true;
 }
Example #3
0
 public function authenticate($username, $password)
 {
     if ($this->hasIdentity()) {
         return true;
     }
     $users = new Users();
     if (($user = $users->authenticate($username, $password)) !== false) {
         $s = $this->session;
         $s->regenerate(true);
         $s->write('__auth', $user['username']);
         $s->write('__id', $user['id']);
         $s->write('__time', time());
         $token = md5(uniqid(rand(), true));
         $s->write('__token', $token);
         UserSession::create()->entry($user['id'], $token, session_id());
         $this->userInfo = $user;
         return $user;
     }
     session_write_close();
     return false;
 }
Example #4
0
 public function handle(Request $request, Response $response, array $config)
 {
     $auth = Auth::getInstance();
     if ($auth->hasIdentity()) {
         $this->redirect(UserSettings::create()->getAfterLoginPage($auth->getUserId()), $request, $response);
         return;
     }
     if ($request->isPost()) {
         $post = $request->getPost();
         $form = new \Access\Forms\Login('loginform', __DIR__ . '/../View/Login.php');
         if ($form->isValid($post)) {
             $users = new Users();
             $username = $form->getValue('username');
             $user = $users->fetch('username', $username);
             if (!$user) {
                 $form->setError('username', 'Invalid login');
             } else {
                 if ((int) $user['is_active'] === 0) {
                     $form->setError('username', 'User account has not been activated');
                 } else {
                     if ((int) $user['is_locked'] === 1) {
                         $form->setError('username', 'This user account has been locked');
                     } else {
                         $user = $auth->authenticate($username, $form->getValue('password'));
                         if ($auth->hasIdentity()) {
                             $this->redirect(UserSettings::create()->getAfterLoginPage($auth->getUserId()), $request, $response);
                             return;
                         } else {
                             $form->setError('username', 'Login is not successful. Please try again.');
                         }
                     }
                 }
             }
         }
         $response->setBody($form->getHTML($form->getValues(), $form->getErrors()));
         return;
     }
     $view = new View(__DIR__ . '/../View');
     $response->setBody($view->render('login'));
 }
Example #5
0
 /**
  * Set allowed resources for the user.
  * 
  * @return void
  */
 protected function setAllowedResources()
 {
     $auth = Auth::getInstance();
     if ($auth->hasIdentity()) {
         $users = new Users();
         $user = $users->fetch('id', $auth->getUserId());
         if ($user !== false) {
             $perms = new Permissions();
             $this->resources = $perms->getAllowedResources($user['id']);
         }
         $this->user = $user;
     }
     $this->isEstablished = true;
 }