[SOLUTION]
I followed all of the advice in this thread, however checkAccess() was still returning false. I had a typo in the validate bizrule. I found it by comparing the code from inside MySQL server to what was submitted via the ProjectUserForm.verify method. I am reproducing my screens below, in case someone else finds it useful.
I am calling it from views/project/view/php like this:
if(Yii::app()->user->checkAccess('createUser',array('project'=>$model)))
{
$this->menu[]=array('label'=>'Add User to Project', 'url'=>array('adduser','id'=>$model->id));
}
I am logging in to the application as user: Test_User_Four
using this URL:
localhost/trackstar/index.php?r=project/view&id=2
Here is my tbl_user table:
here is my authassignment table:
this is from authItem table
this is from authitemchild
this is from tbl_project_user_role
It looks to me like Test_User_Four is the owner of project 2 and should be able to have access to createUser and see the link
here is my verify() function in ProjectUserForm.php which sets the bizRule
public function verify($atrribute, $params)
{
//Review LoginForm.authenticate() for further example.
//only verify if no other input errors present
if(!$this->hasErrors())
{
$user=User::model()->findByAttributes(array('username'=>$this->username));
if($this->project->isUserInProject($user))
{
$this->addError('username', 'This user has already been added to the project.');
}
else
{
$this->project->associateUserToProject($user);
$this->project->associateUserToRole($this->role, $user->id);
$auth=Yii::app()->authManager;
$bizRule='return isset($params["project"]) && params["project"]->isUserInRole("'.$this->role.'");';
$auth->assign($this->role,$user->id,$bizRule);
}
}
}
HERE’s THE PROBLEM
$bizRule='return isset($params["project"]) && params["project"]->isUserInRole("'.$this->role.'");';
//params missing a variable identifier $
//should be:
$bizRule='return isset($params["project"]) && $params["project"]->isUserInRole("'.$this->role.'");';
finally, here is the isUserInRole($role) function from the model Project.php
public function isUserinRole($role)
{
$sql = "SELECT role FROM tbl_project_user_role WHERE project_id=:projectId AND user_id=:userId AND role=:role";
$command = Yii::app()->db->createCommand($sql);
$command->bindValue(":projectId", $this->id, PDO::PARAM_INT);
$command->bindValue(":userId", Yii::app()->user->getId(), PDO::PARAM_INT);
$command->bindValue(":role", $role, PDO::PARAM_STR);
return (($command->execute()==1) ? true : false);
}
[s]Any idea of what I may be doing wrong or what else I can try to make this work?
[/s]If you go to your database and inspect the value in the ‘bizrule’ field in authassignment table you can see the typo more easily:
// this is bad
return isset($params["project"]) && params["project"]->isUserInRole("owner");
//this is good
return isset($params["project"]) && $params["project"]->isUserInRole("owner");
HTH