Hi. I’m preety new to Yii and I need help.
I’ve got a problem with RBAC and adding Roles to Users.
This is my Project.php file (without redundant code):
class Project extends TrackStarActiveRecord
{
public $fixtures=array(
'projects'=>'Project',
'users'=>'User',
'projUsrAssign'=>':tbl_project_user_assignment',
'projUserRole'=>':tbl_project_user_role',
'authAssign'=>':AuthAssignment',
);
[b]...[/b]
public function getUserOptions()
{
$usersArray = CHtml::listData($this->users, 'id', 'username');
return $usersArray;
}
public function UserRoleAssignment()
{
$project = $this->$project->id;
$user = $this->users->id;
$this->assertEquals(1,$project->associateUserToRole('owner',$user->id));
$this->assertEquals(1,$project->removeUserFromRole('owner',$user->id));
}
public function associateUserToRole($role, $userId)
{
$sql = "INSERT INTO tbl_project_user_role (project_id, user_id, role) VALUES (:projectId, :userId, :role)";
$command = Yii::app()->db->createCommand($sql);
$command->bindValue(":projectId", $this->id, PDO::PARAM_INT);
$command->bindValue(":userId", $userId, PDO::PARAM_INT);
$command->bindValue(":role", $role, PDO::PARAM_STR);
return $command->execute;
}
public function removeUserFromRole($role, $userId)
{
$sql = "DELETE 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", $userId, PDO::PARAM_INT);
$command->bindValue(":role", $role, PDO::PARAM_STR);
return $command->execute();
}
public function isInRole()
{
$row1 = $this->projUserRole['row1'];
Yii::app()->user->setId($row1['user_id']);
$project=Project::model()->findByPk($row1['project_id']);
$this->assertTrue($project->isUserInRole('member'));
}
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;
}
public static function getUserRoleOptions()
{
return CHtml::listData(Yii::app()->authManager->getRoles(),'name', 'name');
}
public function associateUserToProject($user)
{
$sql = "INSERT INTO tbl_project_user_assignment (project_id, user_id) VALUES (:projectId, :userId)";
$command = Yii::app()->db->createCommand($sql);
$command->bindValue(":projectId", $this->id, PDO::PARAM_INT);
$command->bindValue(":userId", $user->id, PDO::PARAM_INT);
return $command->execute();
}
public function isUserInProject($user)
{
$sql = "SELECT user_id FROM tbl_project_user_assignment WHERE project_id=:projectId AND user_id=:userId";
$command = Yii::app()->db->createCommand($sql);
$command->bindValue(":projectId", $this->id, PDO::PARAM_INT);
$command->bindValue(":userId", $user->id, PDO::PARAM_INT);
return $command->execute()==1 ? true : false;
}
public function testUserAccessBasedOnProjectRole()
{
$row1 = $this->projUserRole['row1'];
Yii::app()->user->setId($row1['user_id']);
$project=Project::model()->findByPk($row1['project_id']);
$auth = Yii::app()->authManager;
$bizRule='return isset($params["project"]) && $params["project"]->isUserInRole("member");';
$auth->assign('member',$row1['user_id'], $bizRule);
$params=array('project'=>$project);
$this->assertTrue(Yii::app()->user->checkAccess('updateIssue',$params));
$this->assertTrue(Yii::app()->user->checkAccess('readIssue',$params));
$this->assertFalse(Yii::app()->user->checkAccess('updateProject',$params));
//now ensure the user does not have any access to a project they are not associated with
$project=Project::model()->findByPk(1);
$params=array('project'=>$project);
$this->assertFalse(Yii::app()->user->checkAccess('updateIssue',$params));
$this->assertFalse(Yii::app()->user->checkAccess('readIssue',$params));
$this->assertFalse(Yii::app()->user->checkAccess('updateProject',$params));
}
}
And ProjectController.php (just 1 function):
public function actionAdduser($id)
{
$form=new ProjectUserForm;
$project = $this->loadModel($id);
// collect user input data
if(isset($_POST['ProjectUserForm']))
{
$form->attributes=$_POST['ProjectUserForm'];
$form->project = $project;
// validate user input and set a sucessfull flassh message if valid
if($form->validate())
{
Yii::app()->user->setFlash('success',$form->username ." has been added to the project." );
$form=new ProjectUserForm;
}
}
// display the add user form
$users = User::model()->findAll();
$usernames=array();
foreach($users as $user)
{
$usernames[]=$user->username;
}
$form->project = $project;
$this->render('adduser',array('model'=>$form,'usernames'=>$usernames));
}
Everything just like in the book. It adds relation between project and user (user now is in project, can be added to owner of issue etc.) But still tbl_project_user_role isn’t really created - there are no record created by function [font=“Courier New”]associateUserToRole[/font]. So I can assign User to a project, but i can’t assign their roles. What am I doing wrong?
EDIT: Using Yii 1.1.12
EDIT1: OK, google doesn’t hurt. Missing () after execute method.