Answer the question
In order to leave comments, you need to log in
Error checking for role in yii1 controller?
Good afternoon,
Organized access to controllers based on roles as indicated in the documentation:
yiiframework.ru/doc/cookbook/ru/access.rbac.file
<?php
class UserIdentity extends CUserIdentity
{
protected $_id;
public function authenticate()
{
$user = Users::model()->find('LOWER(email)=?',
[
strtolower($this->username)
]);
if(
$user->confirm &&
(
($user === null) || (md5($this->password) !== $user->password)
)
)
$this->errorCode = self::ERROR_USERNAME_INVALID;
else
{
$this->_id = $user->id;
$this->username = $user->name;
$this->setState('id', $user->id);
$this->setState('email', $user->email);
$this->setState('role', $user->role_id);
$this->setState('sex', $user->sex_id);
$this->setState('adress', $user->adress);
$this->setState('postcode', $user->postcode);
$this->setState('role_name', $user->role->name);
$this->setState('telephone', $user->telephone);
$this->setState('country', $user->country);
$this->setState('city', $user->city);
$this->errorCode = self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId(){
return $this->_id;
}
}
<?php
class WebUser extends CWebUser
{
private $_model = null;
public $loginUrl = ['users/login'];
private function getModel()
{
if (!$this->isGuest && $this->_model === null)
{
$this->_model = Users::model()->findByPk($this->id); // , $criteria
}
return $this->_model;
}
function getRole()
{
if($user = $this->getModel())
{
// в таблице User есть поле role
return $user->role_id;
}
}
public function getEmail()
{
if ($user = $this->getModel())
return $user->email;
}
function getRoleName()
{
if($user = $this->getModel())
return $user->role->name;
}
public function getSex()
{
if ($user = $this->getModel())
return $user->sex_id;
}
public function getTelephone()
{
if ($user = $this->getModel())
return $user->telephone;
}
function getId()
{
if($user = $this->getModel())
return $user->id;
}
public function getCountry()
{
if ($user = $this->getModel())
return $user->country;
}
public function getCity()
{
if ($user = $this->getModel())
return $user->city;
}
public function getAdress()
{
if ($user = $this->getModel())
return $user->adress;
}
public function getPostcode()
{
if ($user = $this->getModel())
return $user->postcode;
}
}
class PhpAuthManager extends CPhpAuthManager
{
public function init()
{
if($this->authFile === null)
$this->authFile = Yii::getPathOfAlias('application.config.auth').'.php';
parent::init();
if(!Yii::app()->user->isGuest)
{
$this->assign
(
Yii::app()->user->role
, Yii::app()->user->id
);
}
}
}
return
[
'guest' =>
[
'type' => CAuthItem::TYPE_ROLE,
'description' => 'Guest',
'bizRule' => null,
'data' => null
],
'2' =>
[
'type' => CAuthItem::TYPE_ROLE,
'description' => 'customer',
'children' =>
[
'guest', // унаследуемся от гостя
],
'bizRule' => null,
'data' => null
],
'3' =>
[
'type' => CAuthItem::TYPE_ROLE,
'description' => 'moderator',
'children' =>
[
'customer', // позволим модератору всё, что позволено пользователю
],
'bizRule' => null,
'data' => null
],
'4' =>
[
'type' => CAuthItem::TYPE_ROLE,
'description' => 'admin',
'children' =>
[
'moderator', // позволим модератору всё, что позволено пользователю
],
'bizRule' => null,
'data' => null
],
];
const ID_CUSTOMER = 2;
const ID_MODERATOR = 3;
const ID_ADMIN = 4;
public function accessRules()
{
return
[
['allow',
'actions'=>
[
'show',
'index',
'view',
'admin',
'toggle',
'update'
],
'roles' => [ Users::ID_ADMIN ],
],
['deny',
'users'=>['*'],
],
];
}
echo Users::ID_ADMIN .':'.Yii::app()->user->role;
die();
if(Yii::app()->user->checkAccess('4'))
echo "hello, I'm administrator";
else
echo Users::ID_ADMIN .':'.Yii::app()->user->role;
die();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question