YII學習第十天,訪問控制filter

2021-09-01 15:34:15 字數 2206 閱讀 3422

在controller.php 

注:filters是yii的ccontroller.php中定義的方法,而controller.php是繼承ccontroller.php的。此處,相當於覆蓋父類中的filters方法。而所有的xxcontroller.php又繼承自controller.php,顯然都可以定義authlessactions()方法覆蓋父類中相應方法。 

public function filteraccessauth($filterchain) 

elseif(!in_array($this->getaction()->getid(), $this->authlessactions()) && $this->current_user && $this->current_user->ispasswordexpired())

if(!in_array($this->getaction()->getid(), $this->authlessactions()) && $this->current_user && $this->current_user->hi_id == null)

$filterchain->run();

} public function filters()

public function authlessactions()

批註1:yii中使用filter規則。

for method-based filters, a method named 'filterxyz($filterchain)' in the controller class will be executed, where 'xyz' stands for the filter name as specified in filters(). note, inside the filter method, you must call $filterchain->run() if the action should be executed. otherwise, the filtering process would stop at this filter. 

批註2:另一種用法(隱式用法) 

在1.1.7中,public function filters()  

也即accesscontrol對應下面這個方法 

public void filteraccesscontrol(cfilterchain $filterchain) 

class postcontroller extends ccontroller

}

所以不難看出filter至少有兩種型別,一種是accessauth,另一種是accesscontrol。

批註3:當使用者訪問index.php?r=site/login 

首先,對映到sitecontroller.php中 

而在sitecontroller.php中,覆蓋了controller.php的public function authlessactions()方法,不再是空陣列, 

而是return array('login', 'logout', 'error', 'page', 'bashjs'); 

批註4:在actionlogin方法裡 

if(isset($_post['loginform']))

// 檢查有沒有設定hi

if($this->current_user->hi_id == null)

}}

根據最前面的方法in_array($this->getaction()->getid(), $this->authlessactions())可以判斷該action是否在不用授權陣列中。

其中,$this->getaction()是乙個很大的物件,$this->getaction()->getid()是login。 

批註5:關於未登入前,應該走下面這個邏輯 

} 即注釋掉改行後,project/不會自動跳轉到project/index.php?r=site/login頁面。 

最後,如果使用者登陸成功後,使用者跳轉到site/index,而其定義為outsource/index,當outsourcecontroller.php執行時,又一次執行controller.php,此時的authlessactions陣列為空,則in_array()為false,取反為true。執行檢查上次修改密碼超過30天和是否已新增hi。 

學習第十天

一 介面 jdk1.8及之後新增了2中可以定義存在方法體的方法 預設方法 default關鍵字修飾的方法 使用 通過實現類物件使用 靜態方法 使用 通過介面名去呼叫 二 單例模式 保證類只能存在乙個例項 餓漢式 先建立物件,然後需要的人要這個物件,保證永遠使用的都是這個建立好的物件 執行緒安全的,效...

python學習第十天

class student count 0 def init self,name,age,address self.name name self.age age self.address address student.count 1 k print k w open a.txt w encodin...

菜鳥學習第十天

1.字串最大的特點 一旦初始化就不可以改變。不可改變的字串內容而不是指向字串的引用 2.string s abc 其中s是乙個類型別變數,abc 是乙個物件。3.string s1 abc 和string s2 new string abc s1 s2 和s1.equals s2 比較的是他們在記憶...