2

Within Drupal 7 I am trying to modify the page myserver.com/admin/people/permissions/roles so that a custom role which I have added cannot be edited. In other words, I'd like it to be locked like the anonymous and administrator roles.

I have found one possible solution at http://drupal.org/node/397480 but I think it is a Drupal 6 fix.

I've tried modifying the render array for the content directly, but also can't figure that out quite right.

Here is the closest I've been able to get it. Any suggestions or solutions will be much appreciated.

function access_group_preprocess_page(&$variables) {
  //check to see if the current page matches the path specified
  $match_path = explode('/', 'admin/people/permissions/roles');
  $match = TRUE;
  for ($i = 0; $i < count($match_path); $i++) {
    if ($match_path[$i] != arg($i)) {
      $match = FALSE;
      break;
    }
  }

  //if there is a match, modify the page
  if($match){
    //define the locked roles
    $locked_roles = array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID,);
    $access_group_role = user_role_load_by_name(ACCESS_GROUP_ROLE);
    if ($access_group_role) $locked_roles[] = $access_group_role->rid;
    kpr($locked_roles);

    kpr($variables);

    //recreate the render array for the table
    $header = array(
        t('Name'),
        array(
            'data' => t('Operations'),
            'colspan' => 2
        )
    );
    foreach (user_roles() as $rid => $name) {
      $edit_permissions = l(t('edit permissions'), 'admin/user/permissions/'. $rid);
      if (!in_array($rid, $locked_roles)) {
        $rows[] = array($name, l(t('edit role'), 'admin/user/roles/edit/'. $rid), $edit_permissions);
      }else{
        $rows[] = array($name . " <em>(locked)</em>", '', $edit_permissions);
      }
    }

    //$form = user_admin_role(array(), array(), );
    //$rows[] = array(drupal_render($form['name']), array('data' => drupal_render($form['submit']), 'colspan' => 2));
    $form = $variables['page']['content']['system_main'];
    $rows[] = array(
      array(
          'data' => drupal_render($form['name']) . drupal_render($form['add]']),
          'colspan' => 3,
      ),
    );


    $output = drupal_render_children($form);
    $output .= theme('table', array(
        'header' => $header,
        'rows' => $rows,
    ));

    $variables['page']['content'] = $output;
  }
}

1 Answer 1

1

Unfortunately this behaviour is hard coded into the theme_user_admin_roles() function, so you'll need to override that theme function to remove the link and add the locked label. You'll also need to override the access check for the role edit/deletion page so it can't be visited directly.

This is the theme override you'll need to use (it's exactly the same as theme_user_admin_roles() except it has an extra test for your custom role):

function MYTHEME_user_admin_roles($variables) {
  $my_role_id = function_to_get_custom_role_id();

  $form = $variables['form'];

  $header = array(t('Name'), t('Weight'), array(
      'data' => t('Operations'),
      'colspan' => 2,
    ));
  foreach (element_children($form['roles']) as $rid) {
    $name = $form['roles'][$rid]['#role']->name;
    $row = array();
    if (in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID, $my_role_id))) {
      $row[] = t('@name <em>(locked)</em>', array('@name' => $name));
      $row[] = drupal_render($form['roles'][$rid]['weight']);
      $row[] = '';
      $row[] = l(t('edit permissions'), 'admin/people/permissions/' . $rid);
    }
    else {
      $row[] = check_plain($name);
      $row[] = drupal_render($form['roles'][$rid]['weight']);
      $row[] = l(t('edit role'), 'admin/people/permissions/roles/edit/' . $rid);
      $row[] = l(t('edit permissions'), 'admin/people/permissions/' . $rid);
    }
    $rows[] = array(
      'data' => $row,
      'class' => array('draggable'),
    );
  }
  $rows[] = array(array(
      'data' => drupal_render($form['name']) . drupal_render($form['add']),
      'colspan' => 4,
      'class' => 'edit-name',
    ));

  drupal_add_tabledrag('user-roles', 'order', 'sibling', 'role-weight');

  $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'user-roles')));
  $output .= drupal_render_children($form);

  return $output;
}

And this is the code for the access override (needs to go in a custom module)

function MYMODULE_menu_alter(&$items) {
  $items['admin/people/permissions/roles/edit/%user_role']['access callback'] = 'MYMODULE_user_roles_edit';
  $items['admin/people/permissions/roles/delete/%user_role']['access callback'] = 'MYMODULE_user_roles_edit';
}

function MYMODULE_user_roles_edit($role) {
  $my_role_id = function_to_get_custom_role_id();

  if ($role->rid == DRUPAL_ANONYMOUS_RID || 
    $role->rid == DRUPAL_AUTHENTICATED_RID ||
    $role->rid == $my_role_id) {
    return FALSE;
  }

  return user_access('administer permissions');
}
1
  • Thanks for the answer and explanation. I was approaching this in the entirely wrong direction. Thanks for helping me expand my Drupal knowledge.
    – James
    Jun 19, 2012 at 14:54

Your Answer

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

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