I am french so my english is not very good...
I had the following problem :
- I put a limit for 5 users on a role and I create the 5 users
- if I want to modify or delete a user, even with another role, I obtain the message "the limit has been reached" .
To resolve this problem, I have modified the code, in the function role_limits_user :
...
case 'validate':
foreach ($roles as $rid => $role) {
//My modification : role is controlled only if it is checked ($role<>0) and if the user has not the role
if ($role !=0){
if (is_array($account->roles)&&!array_key_exists($rid, $account->roles) ){ //end of modification
// Get the existing role count from the database.
$role_count = role_limits_get_role_count($rid);
// We have to load the roles to get a meaningful name for our message.
$user_roles = user_roles(TRUE);
$role_name = $user_roles[$rid];
// If the limit is more than 0, which indicates unlimited.
if (role_limits_get_limit($rid)) {
if ($role_count == role_limits_get_limit($rid)) {
form_set_error('roles', t('The @role role cannot be granted to this user because the limit has been reached.', array('@role' => $role_name)));
}
}
}
}
}
break;
...
Comments
Comment #1
webkenny commentedThank you very much for this excellent code. Would you possibly be able to create a patch for the purpose? Thanks.
Comment #2
nlahm commentedHere is my files. I have modified 'validate' and 'update' (see comments with "Patch Aciade")
Comment #3
webkenny commentedFantastic, I will review this straight away. Great work, nlahm. Appreciated.
Comment #4
callisto commentedI can confirm that this code resolves the issue for me as well.
I enabled a limit on a single role, but other admins reported that no role changes of any kind were functioning after that point. A patch or new release would be most appreciated.
Thanks!
Comment #5
webkenny commentedOk, not a problem. I will roll the patch into a new release this week. Sorry. Things crazy for me lately.
Comment #6
micheScenario:
- Role limit had been reached.
- Edited account of someone who already had the role.
- Received limit error.
Applied patch!
Test cases:
- Same as above, but didn't receive error :)
- Tried giving role to a new person, received error, as expected.
Marked as "reviewed and tested by the community"
Nice work nlahm!
Comment #7
webkenny commentedThanks for the work, folks. I'll commit this to DEV tomorrow morning. Running out of gas, atm. :)
Comment #8
webkenny commentedCommitted to the 6.x DEV branch. Still needs back-porting to 5.x [EDIT, Patch didn't come through for some reason. :)]
Comment #9
webkenny commentedReleased to 1.9
Comment #11
pav commentedGetting the same issue with the D7 version.
I limited the number of administrators to 1. Now, whenever I try to save any user (without attempting to grant him the administrator role), I get:
In fact, I cannot make any changes to existing user roles (neither grant nor remove existing roles). Changes to other user account fields are saved successfully.
Love the idea of this module, would really like to use it. Can anybody help?
Thanks,
Pav
Comment #12
webkenny commentedHi there. When you need to reopen an issue, just move the status to "active" and its more likely the maintainer (me, in this case) will see it. :) Currently out of my home country right now on vacation but I'll have a look at the issue this week. Thanks for letting me know.
Comment #13
Anonymous (not verified) commentedIs there any progress on this topic? I used this module but I had to disable it because it confuses the customers to see the error regarding the administration error.
Comment #14
bernman commentedI've needed to solve this issue myself and it looks like wrapping $edit['roles'] in array_filter() in role_limits_user_presave() does the trick by removing any roles in the array that are not selected such that we have:
The subsequent if statement could probably do with some cleaning up but it seems to work as is.
Comment #15
bernman commentedI've taken a pass at cleaning up the function a little bit and a truly minor optimization of the loop.
Comment #16
webkenny commented@bernman, thank you! Do you want to roll that into a patch and I can get it into the module with credit for you?
Comment #17
bernman commentedSorry for the delay in rolling the patch. There's some other areas in the code where the messages sent to the user are not being translated. But that would be for another issue :)
Comment #18
bernman commentedMoving to unassigned. Needs review.
Comment #19
bernman commented@webkenny, any chance this could get rolled into a 1.2 release? It's been a while with no issues reported against the patch.
Comment #20
bernman commentedI've rerolled the patch against 7.x-1.x to test for changes in user roles otherwise $edit['roles'] is empty and calling array_filter() will fail.