public function behaviors() { return [ 'access' => [ 'class' => AccessControl::className(), 'only' => ['about', 'contact'], 'rules' => [ [ 'allow' => true, 'actions' => ['about'], 'roles' => ['?'], ], [ 'allow' => true, 'actions' => ['contact', 'about'], 'roles' => ['@'], ], ], ], ]; }
allow ? 定義是否是一個"allow" 或 "deny"規則
actions ? 定義此規則匹配哪些動作
controllers ? 定義此規則匹配哪些控制器
roles ? 定義使用者角色匹配此規則。兩個特殊角色被識別 -
? ? guest使用者相匹配
@ ? 通過認證的使用者相匹配
ips ? 定義此規則匹配 IP 地址
verbs ? 定義請求方法(POST,GET,PUT等)的規則匹配
matchCallback ? 定義一個PHP回撥函式呼叫以檢查是否應適用於這一規則
denyCallback ? 定義一個PHP回撥函式,此規則拒絕存取時呼叫
public function actionAuth() { $password = "123456"; //generates password hasg $hash = Yii::$app->getSecurity()->generatePasswordHash($password); var_dump($hash);echo '<br/>'; //validates password hash if (Yii::$app->getSecurity()->validatePassword($password, $hash)) { echo "correct password"; } else { echo "incorrect password"; } //generate a token $key = Yii::$app->getSecurity()->generateRandomString(); var_dump($key);echo '<br/>'; //encrypt data with a secret key $encryptedData = Yii::$app->getSecurity()->encryptByPassword("mydata", $key); var_dump($encryptedData); //decrypt data with a secret key $data = Yii::$app->getSecurity()->decryptByPassword($encryptedData, $key); var_dump($data);echo '<br/>'; //hash data with a secret key $data = Yii::$app->getSecurity()->hashData("mygenuinedata", $key); var_dump($data); echo '<br/>'; //validate data with a secret key $data = Yii::$app->getSecurity()->validateData($data, $key); var_dump($data); }