안녕하세요 yii2로 홈페이지를 만들다가 문제가 생겨 이렇게 포스팅하게 되었습니다.
yii2의 보안 솔루션을 이용하여 홈페이지 권한 설정을 하다가 문제가 생겼습니다. 아무리 문제를 찾아보아도 찾을 수 없네요. 설마 yii2의 권한 체크 모듈이 틀릴리가 없을 같아서 이렇게 문제를 게시하게 되었습니다.
PostController의 설정
'access' => [
'class' => AccessControl::className(),
/*'ruleConfig' => [
'class' => AccessRule::className(),
],*/
'only' => [ 'index', 'view' , 'create', 'update', 'delete', 'deleteall' ],
'rules' => [
[
'actions' => [ 'list', 'view_content' ],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => [ 'create', 'index' ],
'allow' => true,
'roles' => ['author'],
],
[
'actions' => [ 'view' ],
'allow' => true,
'roles' => ['admin'],
],
[
'actions' => [ 'update', 'delete', 'deleteall' ],
'allow' => true,
'roles' => ['superadmin'],
],
],
은 이렇게 설정되어 있구요.
admin은 viewPost의 권한을 가지고 있고, author는 viewOwnPost의 권한을 가지고 있으며, viewOwnPost는 viewPost의 부모 관계입니다. 그리고 viewOwnPost의 rulename은 isAuthor입니다.
또한, AuthorRule은 아래와 같습니다.
namespace app\rbac;
use yii\rbac\Rule;
/**
* Checks if authorID matches user passed via params
*/
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)
{
if (isset($params['post'])) {
if ($params['post']->createdBy($user))
return true;
else
return false;
}
else {
return false;
}
}
}
이렇게 설정했는데, author권한을 가지고 있는 사용자가 상위 권한이 있는 글을 보려고 하는데, 권한 체크가 제대로 동작하지 않고, 글을 볼 수 있습니다.
제가 만든 권한 체크 모듈에 무슨 문제가 있나요?
권한 체크 루틴은 아래와 같습니다. 도와 주세요.
/**
* Checkl access Authority
*/
public function filterCheckAuthority($model = null)
{
$auth = Yii::$app->authManager;
$ci = $this->id;
$permission = $this->action->id . ucfirst($ci);
if (!$model || 'superadmin' == Yii::$app->user->role) {
if (Yii::$app->user->can($permission))
return true;
else
return false;
}
else {
if ('post' == $ci) {
$result = Yii::$app->user->can($permission, ['post' => $model], false);
if ($result) {
return true;
}
else
rerurn false;;
}
elseif ('media' == $ci) {
if (Yii::$app->user->can($permission, ['media' => $model]))
return true;
}
elseif ('comment' == $ci) {
if (Yii::$app->user->can($permission, ['comment' => $model]))
return true;
}
return false;
}
}