I am working on a user handling system. In order for the administrator to assign roles to the users. I want to include an array in the users AR model containing the assigned roles for the the current user. I get the roles for the current user from the auth system by doing this:
Where getRolesList in the model returns an array of all available roles assigned in RBAC.
This all works fine except for that the attribute rolesArray is not added to the attribute array of the User model. So when the new selection of roles are posted the rolesArray is available in the _POST array but not in the attributes array of the user.
Am I approaching this in the right way? What is best practise if you want to add attributes to an AR model object(I don't really like to put the attribute rolesArray as public)?
ps: The next step is to assign or revoke the roles dependng on if they are ticked or not.
Indirect modification of overloaded property User::$attributes has no effect
Maybe there is another way to add the rolesArray to the attribute array.
Quote
Why you don't like declaring rolesArray as a public member of your AR class?
Well it might be an inheritance from my Java background. I would set the attribute as private and create public getters and setters for it. Since this is already done using magic getters and setters in the CActiveRecord file my problem would be solved if I found a way to add the rolesArray to the attributes array.
I have tried it and it seems to work. I also tried it setting rolesArray to to be private. That didn’t work, I get this message
User does not have attribute “rolesArray”.
, but I guess that is by design.
I was running Xdebug in Eclipse and I was watching the attributes array. That confused me because I couldn't see rolesArray be added to the attributes array. I tried to use getAttribute and it returned what I expected so it is all fine now.
I also tried to use getAttributes after using setAttribute. Get attributes did not return the attribute rolesArray. The description for getAttributes is:
Quote
names of attributes whose value needs to be returned. If this is true (default), then all attribute values will be returned, including those that are not loaded from DB (null will be returned for those attributes). If this is null, all attributes except those that are not loaded from DB will be returned.
Since I called without parameters I am expecting to get all the attributes back including rolesArray. Is this a bug?
Yes, because the default implementation simply returns all table columns except the primary key. Since rolesArray is not one of them, you need to manually declare it. (this is for security reason)
Yes, because the default implementation simply returns all table columns except the primary key. Since rolesArray is not one of them, you need to manually declare it. (this is for security reason)
This worked perfectly fine. This in combination with the scenario based validation will give me exactly what I was looking for.
This is what the safeAttributes method looks like now:
public function safeAttributes()
{
$customSafeAttrArray = array('rolesArray');
$currentSafeAttributes = parent::safeAttributes();
return array_merge($customSafeAttrArray, $currentSafeAttributes);
}
Thanks guys Yii is looking more promising by the minute.