Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi Larry,


Looking at your code to assign a user. You take

if ($user->type === 'admin') {
} elseif ($user->type === 'author') {
} elseif ($user->type === 'public') {

and condense it to:

public function afterSave() {

I was a little unsure as to how to adjust this in the following situation as my table structure is different.


I have a User table and a user_role table, in my user table I have column user_role_fk that relates back the the user_role table.


The user_role table has 2 columns (id, role)


[id - role ]

1 - standard

2 - editor

3 - moderator


And the permissions I created were,

- standardPermissions

- editorPersimissions

- moderatorPermissions.


How would I update your code in my situation?






Link to post
Share on other sites

Unless I misunderstand you, it sounds like it'd be the same, but the role types come from a database. They'd still end up in a User property. That property could still be used to assign permissions as in my example. No? Or am I misunderstanding the difference here?

Link to post
Share on other sites

Ahh ok.


Well I tried it

protected function afterSave()
if (!Yii::app()->authManager->isAssigned($this->user_role_fk,$this->id)) {
  Yii::app()->authManager->assign($this->user_role_fk, $this->id);
return parent::afterSave();

And I am getting: CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails


I have noticed that  CDbCommand->execute(array(":itemname" => "3", ":userid" => "13", ":bizrule" => null, ":data" => "N;"))


:itemName I don't think should be 3.  Shouldn't itemName be the permission name i.e. standardPermissions etc?


The isAssigned() takes the first parameter as the itemName. In my situation I only have the itemNames defined in the `authitem` table. So $this->user_role_fk would only relate the a numerical key in my user table that related to a type of user in my user_role table. 


The user_role table doesn't have the itemName in either. It just has a few roles in it as shown above. 

Link to post
Share on other sites

I may fixed this:



protected function afterSave()
if ($this->user_role_fk == 1) { // standard
if (!Yii::app()->authManager->isAssigned('standardPermissions',$this->id)) {
Yii::app()->authManager->assign('standardPermissions', $this->id);
} elseif ($this->user_role_fk  == 2) { // moderator
if (!Yii::app()->authManager->isAssigned('editorPermissions',$this->id)) {
Yii::app()->authManager->assign('editorPermissions', $this->id);
} elseif ($this->user_role_fk  == 3) { // editor
if (!Yii::app()->authManager->isAssigned('moderatorPermissions',$this->id)) {
Yii::app()->authManager->assign('moderatorPermissions', $this->id);
return parent::afterSave();


Does that seem about right?

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...