コード例 #1
0
 /**
  * Authenticates a user.
  * The example implementation makes sure if the username and password
  * are both 'demo'.
  * In practical applications, this should be changed to authenticate
  * against some persistent user identity storage (e.g. database).
  * @return boolean whether authentication succeeds.
  */
 public function authenticate()
 {
     $user = User::model()->with(array('userGroup'))->find('username = ? AND password = ?', array($this->username, $this->password));
     if ($user === null) {
         $this->errorCode = self::ERROR_USERNAME_INVALID;
     } else {
         $this->user_id = $user->user_id;
         $this->username = $user->username;
         //save date time when login
         $user->last_login = date('Y-m-d H:i:s');
         $user->save();
         $auth = Yii::app()->authManager;
         if (!$auth->isAssigned($user->userGroup->role, $this->user_id)) {
             if ($auth->assign($user->userGroup->role, $this->user_id)) {
                 Yii::app()->authManager->save();
             }
         }
         // Save log for login
         $log = new LogLogin();
         $log->user_id = $user->user_id;
         $log->login_time = $user->last_login;
         $log->ip = $_SERVER['REMOTE_ADDR'];
         $log->browser = $_SERVER['HTTP_USER_AGENT'];
         $log->save();
         //////////////////////
         $this->errorCode = self::ERROR_NONE;
     }
     return $this->errorCode == self::ERROR_NONE;
 }