1

I am working in Drupal 6. I have created a role "admin users" with permissions for edit and delete users but I need that this role "admin users" cannot edit/delete the user admin. I have installed the User Protect module but I cannot to configure it correctly for the role "admin users" can not modify it.

1 Answer 1

1

I had to do it one day and I did like that. But in my case the permission is edit user 1. The code simply remove the user from the list. Feel free to change as you want.

<?php
function mymodule_menu_alter(&$items) {
  $items['admin/user/permissions']['access callback'] = 'mymodule_user_access';
}

function mymodule_user_access($string, $account = NULL, $reset = FALSE) {
  if ($string == 'administer permissions' && user_access('edit user 1')) {
    return TRUE;
  } else {
    return FALSE;
  }
}

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  $form['#validate'][] = 'mymodule_user_profile_form_validate';
}

function mymodule_user_profile_form_validate(&$form, &$form_state) {
  $account = $form_state['values']['_account'];
  if(!user_access('edit user 1') && $account->uid == 1) {
    form_set_error('category', t('You does not have right to edit this user.'));
  }
}

function mymodule_perm() {
  return array(
    'edit user 1',
  );
}

function mymodule_theme() {
  return array(
    'mymodule_user_admin_account' => array(
      'arguments' => array('form' => NULL),
    ),
  );
}

function mymodule_theme_registry_alter(&$theme_registry) {
  unset($theme_registry['user_admin_account']['file']);
  $theme_registry['user_admin_account']['function'] = 'theme_mymodule_user_admin_account';
  $theme_registry['user_admin_account']['include files'] = array();
  $theme_registry['user_admin_account']['theme path'] = drupal_get_path('module', 'mymodule');
  $theme_registry['user_admin_account']['theme paths'][] = drupal_get_path('module', 'mymodule');
}

function theme_mymodule_user_admin_account($form) {
  // Overview table:
  $header = array(
    theme('table_select_header_cell'),
    array('data' => t('Username'), 'field' => 'u.name'),
    array('data' => t('Status'), 'field' => 'u.status'),
    t('Roles'),
    array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
    array('data' => t('Last access'), 'field' => 'u.access'),
    t('Operations')
  );

  $output = drupal_render($form['options']);
  if (isset($form['name']) && is_array($form['name'])) {
    foreach (element_children($form['name']) as $key) {
      if(!user_access('edit user 1') && $key == 1) {
        unset($form['accounts'][$key]);
        unset($form['name'][$key]);
        unset($form['status'][$key]);
        unset($form['roles'][$key]);
        unset($form['member_for'][$key]);
        unset($form['last_access'][$key]);
        unset($form['operations'][$key]);
      }

      $rows[] = array(
        drupal_render($form['accounts'][$key]),
        drupal_render($form['name'][$key]),
        drupal_render($form['status'][$key]),
        drupal_render($form['roles'][$key]),
        drupal_render($form['member_for'][$key]),
        drupal_render($form['last_access'][$key]),
        drupal_render($form['operations'][$key]),
      );
    }
  }
  else {
    $rows[] = array(array('data' => t('No users available.'), 'colspan' => '7'));
  }

  $output .= theme('table', $header, $rows);
  if ($form['pager']['#value']) {
    $output .= drupal_render($form['pager']);
  }

  $output .= drupal_render($form);

  return $output;
}

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.