中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

在yii中新增一個用戶驗證的方法詳解

1.為什么要新增一個用戶驗證:
因為我要將網站后臺和前臺做在同一個yii的應用中.但是前臺也包含有會員的管理中心.而這兩個用戶驗證是完全不同的,所以需要兩個不同登陸頁面,要將用戶信息保存在不同的cookie或session中.所以需要在一個應用中增加一個用戶驗證

2.yii的用戶驗證:
在自定義用戶驗證前,我們首先要弄清楚yii的驗證和授權方式.
為了驗證一個用戶,我們需要定義一個有驗證邏輯的驗證類.在yii中這個類需要實現IUserIdentity接口,不同的類就可以實現不同的驗證方 法.網站登陸一般需要驗證的就是用戶名和密碼,yii提供了CUserIdentity類,這個類一般用于驗證用戶名和密碼的類.繼承后我們需要重寫其中 的authenticate()方法來實現我們自己的驗證方法.具體代碼如下:
php代碼
復制代碼 代碼如下:
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; 
        } 
        return !$this->errorCode; 
    } 
    public function getId() 
    { 
        return $this->_id; 
    } 
}

在用戶登陸時則調用如下代碼:
php代碼
復制代碼 代碼如下:
// 使用提供的用戶名和密碼登錄用戶 
$identity=new UserIdentity($username,$password); 
if($identity->authenticate()) 
    Yii::app()->user->login($identity); 
else
    echo $identity->errorMessage;

用戶退出時,則調用如下代碼:
php代碼
復制代碼 代碼如下:
// 注銷當前用戶 
Yii::app()->user->logout();
 其中的user是yii的一個components.需要在protected/config/main.php中定義

php代碼
復制代碼 代碼如下:
'user'=>array( 
    // enable cookie-based authentication 
    'allowAutoLogin'=>true, 
        'loginUrl' => array('site/login'), 
),

這里我們沒有指定user的類名.因為在yii中默認user為CWebUser類的實例.
我 們現在已經實現了用戶的登陸驗證和退出.但是現在無論是否登陸,用戶都能訪問所有的action,所以下一步我們要對用戶訪問進行授權.在yii里是通過 Access Control Filter即訪問控制過濾器來實現用戶授權的.我們看一下一個簡單的帶有訪問控制的Controller:
php代碼
復制代碼 代碼如下:
class AdminDefaultController extends CController 
{  
    public function filters() 
        { 
            return array('accessControl'); 
        } 
        public function accessRules() 
        { 
            return array( 
                array( 
                    'allow', 
                    'users' => array('@'), 
                ), 
                array( 
                    'deny', 
                    'users' => array('*') 
                ), 
            ); 
        } 
}

我們在filters方法中設置具體的filter.我們可以看到在filters方法返回的array里有accessControl參數,在CController類中有一個filterAccessControl方法:
php代碼
復制代碼 代碼如下:
public function filterAccessControl($filterChain) 

    $filter=new CAccessControlFilter; 
    $filter->setRules($this->accessRules()); 
    $filter->filter($filterChain); 
}

在里面新建了一個CAccessControlFilter實例,并且在setRules時傳入了accessRules()方法返回的參數.
$filter->filter($filterChain)則是繼續調用其它filter.
而所有具體的授權規則則是定義在accessRules中:
php代碼
復制代碼 代碼如下:
public function accessRules() 
    { 
        return array( 
            array('deny', 
                'actions'=>array('create', 'edit'), 
                'users'=>array('?'), 
            ), 
            array('allow', 
                'actions'=>array('delete'), 
                'roles'=>array('admin'), 
            ), 
            array('deny', 
                'actions'=>array('delete'), 
                'users'=>array('*'), 
            ), 
        ); 
    }

具體規則參見yii的手冊.
3.新增一個驗證體系:
首先我們從CWebUser繼承一個CAdminUser:
php代碼
復制代碼 代碼如下:
class CAdminWebUser extends CWebUser 

    public $loginUrl = array('admin/admin/login'); 
}

我們需要把他放置到components中
如果是全局應用則通過protected/config/main.php的components小節:
php代碼
復制代碼 代碼如下:
'user'=>array( 
    // enable cookie-based authentication 
        'class' => 'CAdminUser', 
    'allowAutoLogin'=>true, 
       'loginUrl' => array('site/login'), 
),

如果是在modules中則在模塊類的init方法中添加如下代碼:
php代碼
復制代碼 代碼如下:
$this->setComponents(array( 
       'adminUser' => array( 
                'class' => 'CAdminWebUser', 
                'allowAutoLogin' => false, 
        ) 
));

最后調用方式
php代碼
復制代碼 代碼如下:
//全局應用 
Yii::app()->getComponent('adminUser'); 
//在模塊中 
Yii::app()->controller->module->getComponent('adminUser');

但僅僅這樣還不夠,我們還需要修改Controller的filter,我們需要自定義一個filter,來實現另一個用戶的驗證和授權
第一步自定義一個filter:
php代碼
復制代碼 代碼如下:
class CAdminAccessControlFilter extends CAccessControlFilter 

    protected function preFilter($filterChain) 
    { 
        $app=Yii::app(); 
        $request=$app->getRequest(); 
        $user = Yii::app()->controller->module->getComponent('adminUser'); 
        $verb=$request->getRequestType(); 
        $ip=$request->getUserHostAddress(); 

        foreach($this->getRules() as $rule) 
        { 
            if(($allow=$rule->isUserAllowed($user,$filterChain->controller,$filterChain->action,$ip,$verb))>0) // allowed 
                break; 
            else if($allow<0) // denied 
            { 
                $this->accessDenied($user); 
                return false; 
            } 
        } 
        return true; 
    } 
}

再重寫CController類的filterAccessController方法
php代碼
復制代碼 代碼如下:
public function filterAccessControl($filterChain) 

    $filter = new CAdminAccessControlFilter(); 
    $filter->setRules($this->accessRules()); 
    $filter->filter($filterChain); 

//在這里我們使用自定義的filter類替換了原來的filter

OK,到這里我們就可以在此Controller的accessRules()中指定adminUser的授權了

php技術在yii中新增一個用戶驗證的方法詳解,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 涩涩视频网站在线观看 | 成人做爰69片免费观看 | 午夜影院网站 | 欧美亚洲第一区 | 羞羞的视频在线看 | 在线色网站 | 91精品国产综合久久福利软件 | 国产在线a | 亚洲手机视频在线 | 不卡av电影在线播放 | 欧美高清性xxxxhd | 日韩一区二区在线视频 | 精品入口麻豆88视频 | 国产资源在线视频 | 亚洲一区二区三区免费在线观看 | 亚洲毛片在线 | 成人福利在线 | 亚洲91精品| 日本久久精品 | 国产福利视频 | 日韩精品免费看 | 精品国产乱码久久久久久图片 | 9久9久9久女女女九九九一九 | 欧美激情精品久久久久久免费 | 日本一区二区三区免费观看 | 欧美日韩免费在线 | 久久久久久亚洲国产精品 | 羞羞视频一区二区 | 国产高清毛片 | 黄色片网站国产 | 精品少妇一区二区三区在线播放 | 国产视频第一页 | 2019天天干天天操 | 可以免费看的毛片 | 亚洲三级免费看 | 欧美日韩在线一区二区 | 亚洲福利一区二区 | 欧美h版| 国产亚洲精品a | 伊人久久国产 | 国产日韩欧美 |