Yii身份验证和授权

时间:2016-09-05 07:40:16

标签: yii

我按照本文中的步骤http://www.yiiframework.com/doc/guide/1.1/en/topics.auth如何在我的网站上创建登录和注册系统,但我不明白我应该把这个代码放在哪里,在什么文件中?

$identity=new UserIdentity($username,$password);
if($identity->authenticate())
    Yii::app()->user->login($identity);
else
    echo $identity->errorMessage;
......
// Logout the current user
Yii::app()->user->logout();

1 个答案:

答案 0 :(得分:0)

首先你应该像这样创建一个LoginForm。

<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class LoginForm extends CFormModel {

    public $username;
    public $password;
    public $rememberMe;
    public $qrcode;
    private $_identity;

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules() {
        return array(
            // username and password are required
            array('username, password', 'required'),
            // rememberMe needs to be a boolean
            array('rememberMe', 'boolean'),
            // password needs to be authenticated
            array('password', 'authenticate'),
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            //'rememberMe'=>'Remember me next time',
            'rememberMe' => Yii::t('default', 'Remember me next time'),
            'username' => Yii::t('default', 'Username'),
            'password' => Yii::t('default', 'Password'),
        );
    }

    /**
     * Authenticates the password.
     * This is the 'authenticate' validator as declared in rules().
     */
    public function authenticate($attribute, $params) {
        if (!$this->hasErrors()) {
            $this->_identity = new UserIdentity($this->username, $this->password);
            if (!$this->_identity->authenticate())
                $this->addError('password', Yii::t('default', 'Incorrect username or password'));
        }
    }

    /**
     * Logs in the user using the given username and password in the model.
     * @return boolean whether login is successful
     */
    public function login() {
        if ($this->_identity === null) {
            $this->_identity = new UserIdentity($this->username, $this->password);
            // Yii::app()->user->setState("password", $this->password);
            //$_SESSION['password'] = $this->password;
            $this->_identity->authenticate();
        }
        if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
            $duration = $this->rememberMe ? 3600 * 24 * 30 : 0; // 30 days
            Yii::app()->user->login($this->_identity, $duration);
            return true;
        } else
            return false;
    }

}

第二个像这样创建文件UserIdentity。

<?php

/**  * UserIdentity represents the data needed to identity a user.
 * * It contains the authentication method that checks if the provided
 * * data can identity the user.
 */
class UserIdentity extends CUserIdentity {

    private $_id;
    public $user;
    public $usertype;

    public function authenticate() {

            $user = User::model()->find('LOWER(username)=? or easiio_id=?', array(strtolower($this->username), $this->username));

        if ($user === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        }  else {
            //date_default_timezone_set("America/Los_angeles");
            $this->_id = $user->id;
            $this->usertype = $user->status;
            $this->user = $user;
            $this->username = $user->username;
            $this->setState("user", $user);
            $this->setState('username', $user->username);
            $this->setState('password', $user->password);
            $this->setState('org', $user->org_id);

            $user->saveAttributes(array(
                'lastlogin' => date("Y-m-d H:i:s", time()),
            ));
            $this->errorCode = self::ERROR_NONE;
        }
        return $this->errorCode == self::ERROR_NONE;
    }

    public function getId() {
        return $this->_id;
    }

}

第三次登录