You are here

config_perms.module in Custom Permissions 6.2

Adds more granular permissions for items under 'administer site configuration'.

Development by Doc (Sjoerd Arendsen) sjoerd@optixdesigns.com www.optixdesigns.com

Initial development by mrthumpz (Todd Humphrey) todd@iplanitglobal.com www.iplanitglobal.com

File

config_perms.module
View source
<?php

/**
 * @file
 * Adds more granular permissions for items under 'administer site configuration'.
 *
 * Development by Doc (Sjoerd Arendsen) sjoerd@optixdesigns.com
 * www.optixdesigns.com
 * 
 * Initial development by mrthumpz (Todd Humphrey) todd@iplanitglobal.com
 * www.iplanitglobal.com
 */

/**
 * Load all permissions
 */
function config_perms_perms($machine_name = NULL) {

  // Load current perms
  $perms = cache_get('config_perms');

  // Rebuild if not there
  if (!$perms) {
    config_perms_cache_rebuild();
    $perms = cache_get('config_perms');
  }
  return $machine_name ? $perms->data[$machine_name] : $perms->data;
}

/**
 * Configure permission names for urls:
 */
function config_perms_cache_rebuild() {
  $perms = config_perms_load();
  cache_set('config_perms', $perms);
}

/*******************************************************************************
 * Hook Functions
 ******************************************************************************/

/*

/**
* Implementation of hook_perm
* Administer -> User management -> Permissions
*/
function config_perms_perm() {
  $permissions = array();
  $perms = config_perms_perms();

  //$perms += config_perms_persistant();
  foreach ($perms as $perm) {
    if ($perm->status) {
      $permissions[] = check_plain($perm->name);
    }
  }
  return $permissions;
}

/**
 * Implementation of hook_flush_caches().
 */
function config_perms_flush_caches() {
  cache_clear_all('config_perms', 'cache');
}

/**
 * Implementation of hook_theme();.
 */
function config_perms_theme() {
  return array(
    'config_perms_form' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'config_perms.admin.inc',
    ),
  );
}

/**
 * Implementation of hook_menu().
*/
function config_perms_menu() {
  $items = array();
  $items['config_perms/ahah'] = array(
    'type' => MENU_CALLBACK,
    'page callback' => 'config_perms_ahah',
    'page arguments' => array(),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'config_perms.admin.inc',
  );
  $items['admin/settings/config_perms'] = array(
    'title' => 'Site Configuration Permissions',
    'description' => 'Adds more granular permissions for items under \'administer site configuration\'',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'config_perms_admin_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'config_perms.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
* Implementation of hook_menu_alter().
*/
function config_perms_menu_alter(&$items) {
  foreach (config_perms_perms() as $perm) {
    foreach ($perm->path as $path) {
      if ($items[$path]) {
        $items[$path]['access callback'] = 'config_perms_access_callback';
        $items[$path]['access arguments'] = array(
          $perm,
        );
      }
    }
  }

  // administer themes:
  // TODO

  /*
  foreach (list_themes() as $theme) {
    $items['admin/build/themes/settings/'. $theme->name]['access callback'] = 'config_perms_themes_access';
    $items['admin/build/themes/settings/'. $theme->name]['access arguments'] = array($theme);
  }
  */
}

/**
* Access callback
*/
function config_perms_access_callback($perm) {
  return user_access(check_plain($perm->name));
}

/*******************************************************************************
 * Ctools
 ******************************************************************************/

/*

/**
* Load all permissions
*/
function config_perms_load() {
  $perms = array();
  if (module_exists('ctools')) {
    ctools_include('export');
    $perms = ctools_export_load_object('config_perms');
  }
  else {
    $result = db_query("SELECT * FROM {config_perms}");
    while ($perm = db_fetch_object($result)) {
      $perm->path = unserialize($perm->path);
      $perms[$perm->machine_name] = $perm;
    }
  }
  return $perms;
}

/**
 * Inserts or updates a perm into the database.
 *
 * @param $preset
 *   The perm object to be inserted.
 */
function config_perms_save($perm) {
  $perm = (object) $perm;

  // Generate machine name
  $perm->machine_name = config_perms_generate_machine_name($perm->name);
  if ($perm->pid) {
    drupal_write_record('config_perms', $perm, 'pid');
  }
  else {
    drupal_write_record('config_perms', $perm);
  }
  return TRUE;
}

/**
 * Peristant permissions
 */
function config_perms_persistant() {
  $persistant = array(
    array(
      'name' => 'administer themes',
      'status' => 1,
      'paths' => array(),
    ),
    array(
      'name' => 'administer enabled themes',
      'status' => 1,
      'paths' => array(),
    ),
  );
  foreach ($persistant as $p) {
    $perms[config_perms_generate_machine_name($p['name'])] = (object) $p;
  }
  return $perms;
}

/**
* Delete a perm.
*
* @param $name
*   A perm object, or a perms name
*/
function config_perms_delete($perm) {
  $pid = is_object($perm) ? $perm->pid : $perm;
  db_query('DELETE FROM {config_perms} WHERE pid = %d', $pid);
  if (module_exists('ctools')) {

    // Clear the Ctools export API cache.
    ctools_include('export');
    ctools_export_load_object_reset('config_perms');
  }
}

/**
 * Generate a machine name given a string
 *
 */
function config_perms_generate_machine_name($string) {

  //return preg_replace('/[^[:alnum:]_]/', '_', strtolower(trim($string) ) );
  return strtolower(preg_replace('/[^a-zA-Z0-9_]+/', '_', $string));
}
function config_perms_parse_path($path) {
  if (is_array($path)) {
    $string = implode("\n", $path);
    return $string;
  }
  else {
    $path = str_replace(array(
      "\r\n",
      "\n\r",
      "\n",
      "\r",
    ), "\n", $path);
    $parts = explode("\n", $path);
    return $parts;
  }
}

Functions

Namesort descending Description
config_perms_access_callback Access callback
config_perms_cache_rebuild Configure permission names for urls:
config_perms_delete Delete a perm.
config_perms_flush_caches Implementation of hook_flush_caches().
config_perms_generate_machine_name Generate a machine name given a string
config_perms_load
config_perms_menu Implementation of hook_menu().
config_perms_menu_alter Implementation of hook_menu_alter().
config_perms_parse_path
config_perms_perm
config_perms_perms Load all permissions
config_perms_persistant Peristant permissions
config_perms_save Inserts or updates a perm into the database.
config_perms_theme Implementation of hook_theme();.