public function authenticate() { /* @var $user_record UsersYii */ //if have login and username if ($this->username != null && $this->password != null) { //find user by login $user_record = UsersYii::model()->findByAttributes(array('login' => $this->username)); if ($user_record === null) { $this->errorCode = self::ERROR_USERNAME_INVALID; } elseif ($user_record->password !== $this->password) { $this->errorCode = self::ERROR_PASSWORD_INVALID; } else { $token = AdminFunctions::GenerateString(12); $user_record->user_id = $token; $user_record->update(); $this->_id = $user_record->id; $this->setState('token', $user_record->user_id); $this->setState('role', $user_record->role); $this->setState('name', $user_record->name); $this->setState('email', $user_record->email); $this->errorCode = self::ERROR_NONE; } } elseif ($this->token != null) { //find user by token $user_record = UsersYii::model()->findByAttributes(array('user_id' => $this->token)); if ($user_record === null) { $this->errorCode = self::ERROR_UNKNOWN_IDENTITY; } else { $this->_id = $user_record->id; $this->setState('token', $user_record->user_id); $this->setState('role', $user_record->role); $this->setState('name', $user_record->name); $this->setState('email', $user_record->email); $this->errorCode = self::ERROR_NONE; } } else { $this->errorCode = self::ERROR_UNKNOWN_IDENTITY; } return !$this->errorCode; }