0

I need to create a permission that allows any user to create other users without allowing them to manage all users (ie without granting them 'administer users' permission).

My approach was altering menu 'admin/people/create' but it - for unknown reason - doesn't work. Even though the permission for the menu is granted the menu item with link for is invisible and when I go to the specific url it - if I don't have 'administer users' permission - just redirects me to current user profile page.

    /**
 * Implements hook_menu_alter().
 */
function fm_users_more_menu_alter(&$items) {    
  $add_new_user = &$items['admin/people/create'];

  unset($add_new_user['access callback']);
  unset($add_new_user['access arguments']);

  $add_new_user['access callback'] = 'fmu_add_user_access_callback';
  $add_new_user['access arguments'] = array(1);
}

/**
 * Callback that manages access to create user page.
 */
function fmu_add_user_access_callback($arguement) {
  dsm('test23');
  return true;
}

It does the same - redirects you to current user profile page - with User register form.

  $register_new_user = &$items['user/register'];

  unset($register_new_user['access callback']);
  $register_new_user['access callback'] = 'fmu_register_user_access_callback';

/**
 * Callback that manages access to create user page.
 */
function fmu_register_user_access_callback() {
  dsm('test23');
  if (user_register_access() || user_access('create users under self')) {
    return true;
  }
  else {
    return false;
  }
}

2 Answers 2

1

Don't forget to also give those same users the permission to access administration pages -- otherwise they'll be denied anytime they try to access any URI beginning with admin.

Also, I don't think it's necessary (or even desirable) to unset the menu items like that... You should just be able to do this:

function fm_users_more_menu_alter(&$items) {    
  $items['admin/people/create']['access callback'] = 'fmu_add_user_access_callback';
  $items['admin/people/create']['access arguments'] = array(1);
}

Finally, remember to rebuild the menus after making such a change.

3
  • It doesn't help, it still redirects me to current user profile page without any apparent reason.
    – Petrroll
    Commented Sep 29, 2013 at 18:48
  • Yikes. Without giving them that permission? I don't think so... Well, not without compromising a whole lot of security. And actually I just looked, and if you look at lines 3752-3768 of modules/user/user.module you'll see that in order for anyone to access the user registration form, they have to either be anonymous or have the permission administer users. You should also see why it's redirecting you to the user page on line 3767. :/
    – jerdiggity
    Commented Sep 29, 2013 at 18:56
  • Yep, I see it know (actually had found it before read your comment. Now I need to figure out how to surpass it. //I might have found an answer to that here: api.drupal.org/api/drupal/modules!user!user.module/function/…
    – Petrroll
    Commented Sep 29, 2013 at 19:01
0

I've found out where the problem was, inside user register form there's this piece of code that - if a user is logged in but is not a administrator - redirects you to your profile page.

  // If we aren't admin but already logged on, go to the user page instead.
  if (!$admin && $user->uid) {
    drupal_goto('user/' . $user->uid);
  }

More information can be found here: https://api.drupal.org/api/drupal/modules!user!user.module/function/user_register_form/7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.