Jonathon Posted August 22, 2013 Share Posted August 22, 2013 Hi Larry, Looking at your code to assign a user. You take if ($user->type === 'admin') { $auth->assign('admin',$user->id); } elseif ($user->type === 'author') { $auth->assign('author',$user->id); } elseif ($user->type === 'public') { $auth->assign('public',$user->id); } and condense it to: public function afterSave() { if(!Yii::app()->authManager->isAssigned( $this->type,$this->id)){ Yii::app()->authManager->assign($this->type, $this->id); } returnparent::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? Thanks Jonathon Link to comment Share on other sites More sharing options...
Larry Posted August 24, 2013 Share Posted August 24, 2013 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 comment Share on other sites More sharing options...
Jonathon Posted August 24, 2013 Author Share Posted August 24, 2013 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 comment Share on other sites More sharing options...
Jonathon Posted August 24, 2013 Author Share Posted August 24, 2013 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 comment Share on other sites More sharing options...
Larry Posted August 31, 2013 Share Posted August 31, 2013 That looks okay to me. Is it working for you? Link to comment Share on other sites More sharing options...
Jonathon Posted August 31, 2013 Author Share Posted August 31, 2013 Yes seems to be fine. So hopefully it will be useful to others. Thanks Larry Jonathon Link to comment Share on other sites More sharing options...
Recommended Posts