You are here

user_permissions.module in User Permissions 6

File

user_permissions.module
View source
<?php

/**
 * Implementation of hook_theme().
 */
function user_permissions_theme() {
  return array(
    'user_permissions_profile_permissions_form' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'function' => 'theme_user_admin_perm',
    ),
    'user_admin_new_role' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'function' => 'theme_user_permissions_user_admin_new_role',
    ),
  );
}

/**
 * Implementation of hook_menu().
 */
function user_permissions_menu() {
  $items['user/%user/permissions'] = array(
    'title' => 'User Permissions',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'user_permissions_profile_permissions_form',
      1,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer permissions',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'user.admin.inc',
    'file path' => drupal_get_path('module', 'user'),
  );
  return $items;
}

/**
 * Create form on user's profile for assigning permissions
 */
function user_permissions_profile_permissions_form($form_state, $user) {
  $role_name = '_user_role_' . $user->uid;
  $rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", $role_name));
  if ($rid) {
    $form = user_admin_perm($form_state, $rid);
  }
  else {
    $rid = DRUPAL_ANONYMOUS_RID;
    $form = user_admin_perm($form_state, $rid);
    $form['checkboxes'][$rid]['#default_value'] = array();
  }
  $form['role_names'][$rid]['#value'] = 'Enable?';
  $form['role_name'] = array(
    '#type' => 'hidden',
    '#value' => $role_name,
  );
  $form['rid'] = array(
    '#type' => 'hidden',
    '#value' => $rid,
  );
  return $form;
}

/**
 * Handle submission of user_permissions_profile_permissions_form
 */
function user_permissions_profile_permissions_form_submit($form, &$form_state) {

  // if the user has a role created by this module, then update the permissions for this role
  // otherwise create the new role with the new permissions if any permissions were given
  $rid = $form_state["values"]["rid"];
  $uid = (int) substr_replace($form_state["values"]["role_name"], "", 0, 11);
  $perms = array_filter($form_state["values"][$rid]);
  if ($rid == DRUPAL_ANONYMOUS_RID) {
    if (!empty($perms)) {

      // create new role with the name $role_name
      $role_name = $form_state["values"]["role_name"];
      db_query("INSERT INTO {role} (name) VALUES ('%s')", $role_name);
      $rid = db_last_insert_id("role", "rid");
      db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)", $uid, $rid);
      db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, implode(', ', array_keys($perms)));
    }
  }
  else {
    db_query("DELETE FROM {permission} WHERE rid = %d", $rid);
    if (!empty($perms)) {
      db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, implode(', ', array_keys($perms)));
    }
    else {

      // if $perms is empty, delete all permission/role information
      // related to this role to reduce database clutter
      db_query("DELETE FROM {users_roles} WHERE uid = %d and rid = %d", $uid, $rid);
      db_query("DELETE FROM {role} WHERE rid = %d", $rid);
    }
  }
}

/**
 * Alter user_filter_form
 */
function user_permissions_form_user_filter_form_alter(&$form, &$form_state) {
  if (is_array($form['filters']['status']['role']['#options'])) {
    $role_count = $user_role_count = 0;
    foreach ($form['filters']['status']['role']['#options'] as $key => $value) {
      $role_count++;
      if (strpos($value, "_user_role_") !== FALSE) {
        $user_role_count++;
        unset($form['filters']['status']['role']['#options'][$key]);
      }
    }
    if ($role_count == $user_role_count) {
      unset($form['filters']['status']['role']);
      unset($form['filters']['filter']['#options']['role']);
    }
  }
}

/**
 * Alter user_admin_account
 */
function user_permissions_form_user_admin_account_alter(&$form, &$form_state) {
  if (is_array($form['options']['operation']['#options']['Add a role to the selected users']) && is_array($form['options']['operation']['#options']['Remove a role from the selected users'])) {
    $role_count = $user_role_count = 0;
    foreach ($form['options']['operation']['#options']['Add a role to the selected users'] as $key => $value) {
      $role_count++;
      if (strpos($value, "_user_role_") !== FALSE) {
        $user_role_count++;
        unset($form['options']['operation']['#options']['Add a role to the selected users'][$key]);
      }
    }
    foreach ($form['options']['operation']['#options']['Remove a role from the selected users'] as $key => $value) {
      if (strpos($value, "_user_role_") !== FALSE) {
        unset($form['options']['operation']['#options']['Remove a role from the selected users'][$key]);
      }
    }
    if ($role_count == $user_role_count) {
      unset($form['options']['operation']['#options']['Add a role to the selected users']);
      unset($form['options']['operation']['#options']['Remove a role from the selected users']);
    }
  }
}

/**
 * Alter user_admin_perm
 */
function user_permissions_form_user_admin_perm_alter(&$form, &$form_state) {
  $role_id_filter = array();
  foreach (user_roles() as $key => $value) {
    if (strpos($value, "_user_role_") !== FALSE) {
      $role_id_filter[] = $key;
    }
  }
  foreach ($form['checkboxes'] as $key => $value) {
    if (in_array($key, $role_id_filter)) {
      unset($form['checkboxes'][$key]);
    }
  }
  foreach ($form['role_names'] as $key => $value) {
    if (in_array($key, $role_id_filter)) {
      unset($form['role_names'][$key]);
    }
  }
}

/**
 * Theme user_admin_new_role
 */
function theme_user_permissions_user_admin_new_role($form) {
  $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, array(
      DRUPAL_ANONYMOUS_RID,
      DRUPAL_AUTHENTICATED_RID,
    ))) {
      if (strpos($name, '_user_role_') === FALSE) {
        $rows[] = array(
          $name,
          l(t('edit role'), 'admin/user/roles/edit/' . $rid),
          $edit_permissions,
        );
      }
    }
    else {
      $rows[] = array(
        $name,
        t('locked'),
        $edit_permissions,
      );
    }
  }
  $rows[] = array(
    drupal_render($form['name']),
    array(
      'data' => drupal_render($form['submit']),
      'colspan' => 2,
    ),
  );
  $output = drupal_render($form);
  $output .= theme('table', $header, $rows);
  return $output;
}

Functions

Namesort descending Description
theme_user_permissions_user_admin_new_role Theme user_admin_new_role
user_permissions_form_user_admin_account_alter Alter user_admin_account
user_permissions_form_user_admin_perm_alter Alter user_admin_perm
user_permissions_form_user_filter_form_alter Alter user_filter_form
user_permissions_menu Implementation of hook_menu().
user_permissions_profile_permissions_form Create form on user's profile for assigning permissions
user_permissions_profile_permissions_form_submit Handle submission of user_permissions_profile_permissions_form
user_permissions_theme Implementation of hook_theme().