I’m rather new to the framework and working on getting basic foundation functionality squared away. I’m trying to get CPhpAuthManager working to use some basic RBAC functionality in my first site, but something peculiar seems to be happening. CPhpAuthManager seems to be loaded fine; Yii reads in my auth.php and does its formatting business to the file and all. I currently have only one role defined, and one user with that role set, but when I log in, the basic ‘is this working’ test code I threw into the main view is behaving the opposite of how I would expect it to.
I have a role ‘member’ defined in auth.php, my user is set as a member, but the code Yii::app()->user->checkAccess(‘member’) seems to be returning true only when I am logged out.
All of the relevant code (I’m pretty sure) is as follows:
components/UserIdentity.php
<?php
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$username = strtolower($this->username);
$user = User::model()->find('LOWER(username)=?', array($username));
if ($user === null)
$this->errorCode = self::ERROR_USERNAME_INVALID;
else if (!$user->validatePassword($this->password))
$this->errorCode = self::ERROR_PASSWORD_INVALID;
else
{
$auth = Yii::app()->authManager;
if (!$auth->isAssigned($user->role, $this->_id))
{
if ($auth->assign($user->role, $this->_id))
{
Yii::app()->authManager->save();
}
}
$this->_id = $user->id;
$this->username = $user->username;
$this->errorCode = self::ERROR_NONE;
}
return $this->errorCode == self::ERROR_NONE;
}
public function getId()
{
return $this->_id;
}
}
data/auth.php
<?php
return array
(
'member' => array
(
'type' => CAuthItem::TYPE_ROLE,
'description' => 'General User',
'bizRule' => '',
'data' => '',
),
);
snippet from config/main.php
'authManager'=>array
(
'class' => 'CPhpAuthManager'
),
snippet from views/layouts/main.php
<p><?php if (Yii::app()->user->checkAccess('member')) echo "member"; ?></p>
I appreciate any help you can provide for me with this. I’m starting to wade through the Yii code to try to better understand what happens in Yii::app()->user->checkAccess() but it’s not exactly easy for one who is new to this framework.