authenticate方法,根据验证结果,返回各种错误码,包括
const ERROR_NONE=0; const ERROR_USERNAME_INVALID=1; const ERROR_PASSWORD_INVALID=2; const ERROR_UNKNOWN_IDENTITY=100;
这样,在判断验证结果的时候,直接读取自缩写的定义身份类的errorCode属性就可以判断了
class UserIdentity extends CUserIdentity { private $_id; public function authenticate() { $record=User::model()->findByAttributes(array('username'=>$this->username)); if($record===null) $this->errorCode=self::ERROR_USERNAME_INVALID; else if($record->password!==md5($this->password)) $this->errorCode=self::ERROR_PASSWORD_INVALID; else { $this->_id=$record->id; $this->setState('title', $record->title); $this->errorCode=self::ERROR_NONE; } 第 79 页 共 112 页 return !$this->errorCode; }