Hello,
I’v created my own WebUser class which extending the /yii/web/User. I wrote some function, to check if logged user is an administrator or not. When o tried to call my webuser function in behaviors i’v got the following error message:
Call to a member function checkAccess() on a non-object
This is my modified behavior function in frontend/SiteController.php
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
//'class' => '\common\components\AccessControl',
'only' => ['logout', 'signup','index'],
'rules' => [
[
'actions' => ['signup'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout','index'],
'allow' => true,
'roles' => ['$user->getIsAdmin'],//call webuser function here
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
my Weuser Class
<?php
namespace common\components;
use Yii;
use yii\web\IdentityInterface;
use yii\web\User as CoreUser;
use yii\db\Expression;
/**
* User component
*/
class WebUser extends CoreUser
{
/**
* @inheritdoc
*/
public $identityClass = 'common\models\User';
/**
* @inheritdoc
*/
public $enableAutoLogin = true;
/**
* @inheritdoc
*/
public $loginUrl = ["/user/login"];
/**
* Check if user is logged in
*
* @return bool
*/
public function getIsLoggedIn()
{
return !$this->getIsGuest();
} /**
* Check if user is logged in
*
* @return bool
*/
public function getIsAdmin()
{
return $this->identity->isAdmin();
//return $this->getIsAdmin();
}
/**
* @inheritdoc
*/
public function afterLogin($identity, $cookieBased, $duration)
{
/** @var \amnah\yii2\user\models\User $identity */
// $identity->updateLoginMeta();
parent::afterLogin($identity, $cookieBased, $duration);
}
/**
* Get user's display name
*
* @param string $default
* @return string
*/
public function getDisplayName($default = "")
{
/** @var \amnah\yii2\user\models\User $user */
$user = $this->getIdentity();
return $user ? $user->getDisplayName($default) : "";
}
/**
* Check if user can do $permissionName.
* If "authManager" component is set, this will simply use the default functionality.
* Otherwise, it will use our custom permission system
*
* @param string $permissionName
* @param array $params
* @param bool $allowCaching
* @return bool
*/
// public function can($permissionName, $params = [], $allowCaching = true)
// {
// // check for auth manager to call parent
// $auth = Yii::$app->getAuthManager();
// if ($auth) {
// return parent::can($permissionName, $params, $allowCaching);
// }
// // otherwise use our own custom permission (via the role table)
// /** @var \amnah\yii2\user\models\User $user */
// $user = $this->getIdentity();
// print_r($permissionName);exit;
// return $user ? $user->can($permissionName) : false;
// }
}
User model
class User extends ActiveRecord implements IdentityInterface
{
const STATUS_DELETED = 0;
const STATUS_ACTIVE = 10;
const ROLE_USER = 10;
const ROLE_ADMINISTRATOR = 15;
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%user}}';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
TimestampBehavior::className(),
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
['status', 'default', 'value' => self::STATUS_ACTIVE],
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
['role', 'default', 'value' => self::ROLE_USER],
['role', 'in', 'range' => [self::ROLE_USER]],
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function isAdmin()
{
$user = static::findOne(['id' => $this->id, 'status' => self::STATUS_ACTIVE]);
if(!$user)
{
return FALSE;
}
else
{
return $user->role == self::ROLE_ADMINISTRATOR ? TRUE : FALSE;
}
}
...
...
...
}