コード例 #1
0
ファイル: ExampleUserIdentity.php プロジェクト: jhuss/yii-usr
 /**
  * @inheritdoc
  */
 public function authenticate()
 {
     $record = User::model()->findByAttributes(array('username' => $this->username));
     $authenticated = $record !== null && $record->verifyPassword($this->password);
     $attempt = new UserLoginAttempt();
     $attempt->username = $this->username;
     $attempt->user_id = $record === null ? null : $record->id;
     $attempt->is_successful = $authenticated;
     $attempt->save();
     if (UserLoginAttempt::hasTooManyFailedAttempts($this->username, self::MAX_FAILED_LOGIN_ATTEMPTS, self::LOGIN_ATTEMPTS_COUNT_SECONDS)) {
         // this is the first check not to reveal if the specified user account exists or not
         $this->errorCode = self::ERROR_USER_LOCKED;
         $this->errorMessage = Yii::t('UsrModule.usr', 'User account has been locked due to too many failed login attempts. Try again later.');
     } elseif (!$authenticated) {
         $this->errorCode = self::ERROR_USERNAME_INVALID;
         $this->errorMessage = Yii::t('UsrModule.usr', 'Invalid username or password.');
     } elseif ($record->is_disabled) {
         $this->errorCode = self::ERROR_USER_DISABLED;
         $this->errorMessage = Yii::t('UsrModule.usr', 'User account has been disabled.');
     } else {
         if (!$record->is_active) {
             $this->errorCode = self::ERROR_USER_INACTIVE;
             $this->errorMessage = Yii::t('UsrModule.usr', 'User account has not been activated yet.');
         } else {
             $this->errorCode = self::ERROR_NONE;
             $this->errorMessage = '';
             $this->initFromUser($record);
             $record->saveAttributes(array('last_visit_on' => date('Y-m-d H:i:s')));
         }
     }
     return $this->getIsAuthenticated();
 }
コード例 #2
0
 /**
  * @inheritdoc
  */
 public function authenticate()
 {
     $record = Usuarios::model()->findByAttributes(array('usuario' => $this->usuario));
     $authenticated = $record !== null && $record->verificarContrasena($this->contrasena);
     $attempt = new UserLoginAttempt();
     $attempt->username = $this->usuario;
     $attempt->user_id = $record === null ? null : $record->usuario_id;
     $attempt->is_successful = $authenticated;
     $attempt->save();
     if (UserLoginAttempt::hasTooManyFailedAttempts($this->usuario, self::MAX_FAILED_LOGIN_ATTEMPTS, self::LOGIN_ATTEMPTS_COUNT_SECONDS)) {
         // this is the first check not to reveal if the specified user account exists or not
         $this->errorCode = self::ERROR_USER_LOCKED;
         $this->errorMessage = Yii::t('UsrModule.usr', 'La cuenta de usuario ha sido bloqueada temporalmente debido a demasiados intentos fallidos. Por favor intenta de nuevo más tarde.');
     } elseif (!$authenticated) {
         $this->errorCode = self::ERROR_USERNAME_INVALID;
         $this->errorMessage = Yii::t('UsrModule.usr', 'Usuario o contraseña invalido.');
     } elseif ($record->esta_deshabilitado) {
         $this->errorCode = self::ERROR_USER_DISABLED;
         $this->errorMessage = Yii::t('UsrModule.usr', 'Esta cuenta de usuario ha sido deshabilitada.');
     } else {
         if (!$record->esta_activo) {
             $this->errorCode = self::ERROR_USER_INACTIVE;
             $this->errorMessage = Yii::t('UsrModule.usr', 'Esta cuenta de usuario se encuentra inactiva.');
         } else {
             $this->errorCode = self::ERROR_NONE;
             $this->errorMessage = '';
             $this->initFromUser($record);
             $record->saveAttributes(array('ultima_visita_el' => date('Y-m-d H:i:s')));
             $auth = Yii::app()->authManager;
             $role = 'ente';
             switch ($record->enteOrgano->tipo) {
                 case 'S':
                     $role = 'admin';
                     break;
                 case 'O':
                     $role = 'organo';
                     break;
                 case 'E':
                     $role = 'ente';
                     break;
                 default:
                     # code...
                     break;
             }
             switch ($record->rol) {
                 case 'uel':
                     $role = 'uel';
                     break;
                 case 'presupuesto':
                     $role = 'presupuesto';
                     break;
                 case 'producto':
                     $role = 'producto';
                     break;
                 default:
                     # code...
                     break;
             }
             //echo print_r($record);
             //Yii::app()->end();
             if (!$auth->isAssigned($role, $this->_id)) {
                 if ($auth->assign($role, $this->_id)) {
                     Yii::app()->authManager->save();
                 }
             }
         }
     }
     return $this->getIsAuthenticated();
 }