cakephp菜鳥筆記-auth組件簡單說明

Auth是簡單的登陸組件,一般都是先建立user表,字段username,password,如果不是這樣就必須另行顯式說明。

<?php
class AppController extends Controller {
 
    var $components = array('Auth','Session');
    
 function beforeFilter() {
        //Configure AuthComponent配置Auth組件,Auth組件採用SHA1的加密方法。
        $this->Auth->allowedActions = array('display','logout','login');
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
       $this->Auth->loginRedirect = array('controller' => 'products', 'action' => 'index');
  if(!$this->Session->check('lang'))
  {
   $this->Session->write('lang',0);
  }
  date_default_timezone_set('Asia/Chongqing');
    }
}

現在app_controller進行基本的設置,還要建立user的model和controller,建立login和louout方法。它使用SHA1進行加密。可以更改成md5。

進行以上設置後,加入user記錄後,就沒有被Auth->allowedActions的就會被禁止。
function beforeFilter() {
  parent::beforeFilter();
  $this->Auth->allow('*');
 }

一般在每個controller使用這個來進行allow的方法設置,放行一些方法,免得被禁止權限。
這只是簡單的Auth組件使用,更詳細的權限使用還需要使用ACL。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章