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

webkenny’s picture

Thank you very much for this excellent code. Would you possibly be able to create a patch for the purpose? Thanks.

nlahm’s picture

StatusFileSize
new8.88 KB

Here is my files. I have modified 'validate' and 'update' (see comments with "Patch Aciade")

webkenny’s picture

Assigned: Unassigned » webkenny
Status: Active » Needs review

Fantastic, I will review this straight away. Great work, nlahm. Appreciated.

callisto’s picture

I 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!

webkenny’s picture

Ok, not a problem. I will roll the patch into a new release this week. Sorry. Things crazy for me lately.

miche’s picture

Status: Needs review » Reviewed & tested by the community

Scenario:
- 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!

webkenny’s picture

Thanks for the work, folks. I'll commit this to DEV tomorrow morning. Running out of gas, atm. :)

webkenny’s picture

Version: 6.x-1.8 » 6.x-1.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)
StatusFileSize
new0 bytes

Committed to the 6.x DEV branch. Still needs back-porting to 5.x [EDIT, Patch didn't come through for some reason. :)]

webkenny’s picture

Status: Patch (to be ported) » Fixed

Released to 1.9

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

pav’s picture

Version: 6.x-1.x-dev » 7.x-1.1

Getting 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:

The administrator role cannot be granted to the user because the limit has been reached.

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

webkenny’s picture

Status: Closed (fixed) » Active

Hi 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.

Anonymous’s picture

Issue summary: View changes

Is 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.

bernman’s picture

I'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:

function role_limits_user_presave( &$edit, &$account, $category) {
  $roles = array_filter($edit['roles']);
  if ($roles != 0) {
    foreach ($roles as $rid => $role) {
      // If the limit is more than 0, which indicates unlimited.
      $user_role = user_role_load($rid);
      if (role_limits_get_limit($rid)) {
        // If we don't find the role in the account, it is being added.
        if (!array_key_exists($rid, $account->roles)) {
          $role_count = role_limits_get_role_count($rid);
          if ($role_count == role_limits_get_limit($rid)) {
            // Reset the user roles back to the existing set.
            $edit['roles'] = $account->roles;
            drupal_set_message(t('The @role role cannot be granted to the user because the limit has been reached.', array('@role' => $user_role->name, '@user' => $account->name)), 'error');
          }
        }
      }
    }
  }
  role_limits_check_registration();
}

The subsequent if statement could probably do with some cleaning up but it seems to work as is.

bernman’s picture

I've taken a pass at cleaning up the function a little bit and a truly minor optimization of the loop.

function role_limits_user_presave(&$edit, &$account, $category) {
  $roles = array_filter($edit['roles']);
  if (!empty($roles)) {
    foreach ($roles as $rid => $role) {
      // If the limit is more than 0, which indicates unlimited.
      if ($role_limit = role_limits_get_limit($rid)) {
        // If we don't find the role in the account, it is being added.
        if (!array_key_exists($rid, $account->roles)) {
          $role_count = role_limits_get_role_count($rid);
          if ($role_count == $role_limit) {
            // Reset the user roles back to the existing set.
            $edit['roles'] = $account->roles;
            // Display unable to grant role message to the user.
            $user_role = user_role_load($rid);
            drupal_set_message(t('The @role role cannot be granted to the user because the limit has been reached.', array('@role' => $user_role->name, '@user' => $account->name)), 'error');
          }
        }
      }
    }
  }
  role_limits_check_registration();
}
webkenny’s picture

@bernman, thank you! Do you want to roll that into a patch and I can get it into the module with credit for you?

bernman’s picture

Assigned: webkenny » bernman
Status: Active » Needs review
StatusFileSize
new1.69 KB

Sorry 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 :)

bernman’s picture

Assigned: bernman » Unassigned

Moving to unassigned. Needs review.

bernman’s picture

@webkenny, any chance this could get rolled into a 1.2 release? It's been a while with no issues reported against the patch.

bernman’s picture

I'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.