0
我在Yii中查看了rbac的文檔,並認爲我理解它是如何工作的,直到我真的嘗試過它。在Yii的rbac授權檢查不起作用(獲取未知屬性:app models Post :: createdBy)
這是用於檢查帖子的作者是否正在試圖獲得一個動作的授權規則:
class AuthorRule extends Rule
{
public $name = 'isAuthor';
/**
* @param string|integer $user the user ID.
* @param Item $item the role or permission that this rule is associated with
* @param array $params parameters passed to ManagerInterface::checkAccess().
* @return boolean a value indicating whether the rule permits the role or permission it is associated with.
*/
public function execute($user, $item, $params)
{
return isset($params['model']) ? $params['model']->createdBy == $user : false;
}
}
這是我想使用的規則和的Yii的RBAC:
public function actionUpdate($id)
{
$model = $this->findModel($id);
if (\Yii::$app->user->can('update', ['model' => $model])) {
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
}
不過,我得到這個,當我嘗試編輯一個帖子:
Getting unknown property: app\models\Post::createdBy
所以我想I H廣告代替createdBy與表Post中的列userId,我得到一個空白頁,這意味着它不起作用。所以我試圖猜測$用戶是什麼。
我也試過:
return isset($params['model']) ? $params['model']->userId == $user->id : false;
和我得到:Trying to get property of non-object.
我應該怎麼做,使其工作?該文件似乎建議你只需在控制器操作中插入條件以使其工作,但似乎並非如此。
VAR轉儲:
object(app\models\Post)[75]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=6)
'id' => int 1
'userId' => int 1
'title' => string 'test' (length=4)
'content' => string 'lol' (length=3)
'dateCreated' => null
'dateUpdated' => null
private '_oldAttributes' (yii\db\BaseActiveRecord) =>
array (size=6)
'id' => int 1
'userId' => int 1
'title' => string 'test' (length=4)
'content' => string 'lol' (length=3)
'dateCreated' => null
'dateUpdated' => null
private '_related' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_errors' (yii\base\Model) => null
private '_validators' (yii\base\Model) => null
private '_scenario' (yii\base\Model) => string 'default' (length=7)
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
null
我該如何輸出一個var_dump或者我在哪裏得到var_dump的輸出? – jarvan
在返回isset($ params ['model'])之前放置var轉儲? $ params ['model'] - > userId == $ user-> id:false;' – SilverFire
將輸出添加到問題 – jarvan