[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/node_permissions_grid/ -> node_permissions_grid.admin.inc (source)

   1  <?php
   2  // $Id: node_permissions_grid.admin.inc,v 1.2 2009/07/24 14:06:31 joachim Exp $
   3  
   4  /**
   5   * @file
   6   * Admin page callback file for the node permissions grid pages.
   7   */
   8  
   9  /**
  10   * @file
  11   *
  12   * This module works by extracting permissions of the form:
  13   *     ACTION PREFIX content-type name CONTENT SUFFIX
  14   * eg: create        page              content
  15   * eg: edit any      blog              entry
  16   */
  17  
  18  /**
  19   * Define action prefix strings for permissions.
  20   *
  21   * This sets the order in the grid.
  22   */
  23  function action_prefixes() {
  24    return array(
  25      'create',
  26      'edit own',
  27      'delete own',
  28      'edit any',
  29      'delete any',
  30    );
  31  }
  32  
  33  /**
  34   * Define content suffix strings for permissions.
  35   */
  36  function content_suffixes() {
  37    return array(
  38      'content', 
  39      'entry', 'entries', // for blog module
  40      'topic', 'topics',  // for forum module
  41    );
  42  }
  43  
  44  /**
  45   * Menu callback: administer permissions.
  46   */
  47  function node_permissions_grid_admin_content_perm($form_state, $rid = NULL) {
  48    // Some information about content permissions
  49    // the actions. these are the prefixes for permission names.
  50    $action_prefixes = action_prefixes();
  51    $action_prefixes_regexp = implode('|', $action_prefixes);
  52  
  53    // The content words. these are the suffixes.
  54    $content_suffixes = content_suffixes();
  55    $content_suffixes_regexp = implode('|', $content_suffixes);
  56  
  57    if (is_numeric($rid)) {
  58      $result = db_query('SELECT r.rid, p.perm FROM {role} r LEFT JOIN {permission} p ON r.rid = p.rid WHERE r.rid = %d', $rid);
  59    }
  60    else {
  61      $form['#theme'] = 'node_permissions_grid_admin_roles';
  62      return $form;
  63    }
  64  
  65    // Compile role array:
  66    // Add a comma at the end so when searching for a permission, we can
  67    // always search for "$perm," to make sure we do not confuse
  68    // permissions that are substrings of each other.
  69    while ($role = db_fetch_object($result)) {
  70      $role_permissions[$role->rid] = $role->perm .',';
  71    }
  72  
  73    $role_names = user_roles();
  74    if (is_numeric($rid)) {
  75      $role_names = array($rid => $role_names[$rid]);
  76    }
  77  
  78    // Render role/permission overview:
  79    $options = array();
  80    foreach (module_list(FALSE, FALSE, TRUE) as $module) {
  81      if ($permissions = module_invoke($module, 'perm')) {
  82  
  83        $form['permission'][] = array(
  84          '#value' => $module, // the module label.
  85        );
  86  
  87        foreach ($permissions as $perm) {
  88          $matches = array();
  89  
  90          // Get only the perms we care about.
  91          preg_match("/^($action_prefixes_regexp) (.+) ($content_suffixes_regexp)\$/", $perm, $matches);
  92          if ($matches) {
  93            // have (whole string, action, content type).
  94            //dsm($matches);
  95            $action  = $matches[1];
  96            $content = $matches[2];
  97            // autovivify so we don't clobber in another loop iteration
  98            $form['permission'][$content]['#value'] = $content; // the content type label
  99  
 100            // the last one of these to be stashed end up in form_values... but never mind.
 101            $form['permission'][$content]['perms']['#type']    = 'value';
 102            $form['permission'][$content]['perms']['#value'][] = $perm;
 103  
 104            $content_types[$content] = $content;
 105            $options[$perm] = '';
 106            if (strpos($role_permissions[$rid], $perm .',') !== FALSE) {
 107              $status[$rid][] = $perm;
 108            }
 109  
 110          } // if matches
 111          else {
 112            $other_permissions[] = $perm;
 113          }
 114        } // foreach perm
 115  
 116        if (is_numeric(key(array_slice($form['permission'], -1, 1, TRUE)))) {
 117          // if the last key in the permissions array is numeric, then it's a module label
 118          // and that means that this module gave us no interesting permissions
 119          // so remove it.
 120          array_pop($form['permission']);
 121        }
 122      }
 123    }
 124  
 125    // Have to build checkboxes here after checkbox arrays are built
 126    foreach ($role_names as $rid => $name) {
 127      $form['checkboxes'][$rid] = array('#type' => 'checkboxes', '#options' => $options, '#default_value' => isset($status[$rid]) ? $status[$rid] : array());
 128  
 129      $form['role_names'][$rid] = array('#value' => $name, '#tree' => TRUE);
 130  
 131      $form['other_perms'][$rid] = array(
 132        '#type' => 'value',
 133        '#tree' => TRUE,
 134      );
 135      foreach ($other_permissions as $perm) {
 136        // a perm we don't care about. stash its current value anyway for merging on submit.
 137        if (strpos($role_permissions[$rid], $perm .',') !== FALSE) {
 138          $form['other_perms'][$rid][$perm]['#type']  = 'value';
 139          $form['other_perms'][$rid][$perm]['#value'] = $perm;
 140        }
 141      }
 142    }
 143    foreach ($action_prefixes as $action) {
 144      $form['action_names'][$action] = array('#value' => $action, '#tree' => TRUE);
 145    }
 146  
 147    $form['submit'] = array('#type' => 'submit', '#value' => t('Save permissions'));
 148  
 149    return $form;
 150  }
 151  
 152  
 153  /**
 154   * Theme the administer permissions page.
 155   */
 156  function theme_node_permissions_grid_admin_content_perm($form) {
 157  /*
 158  $form['permission'] is either:
 159    - numeric keys that are module name values
 160    - content type keys that hold:
 161      - numeric is the content type label
 162  
 163  */
 164    #dsm($form['permission']);
 165  
 166    $action_prefixes = action_prefixes();
 167  
 168    $roles = user_roles();
 169    foreach (element_children($form['role_names']) as $rid) {
 170      $output .= '<h3>Content permissions for ' . drupal_render($form['role_names'][$rid]) . '</h3>';
 171      foreach (element_children($form['permission']) as $key) {
 172        // Don't take form control structures
 173        // We are going through an array whose keys are either:
 174        //  - numeric, and represents a label for a module
 175        //  - a string, which is the content name for one or more permissions, eg 'blog', 'story'
 176        #dsm($key);
 177        if (is_array($form['permission'][$key])) {
 178          $row = array();
 179          if (is_numeric($key)) {
 180            // Module name: make a row for this module.
 181            $module_name = $form['permission'][$key]['#value'];
 182            #dsm($module_name);
 183            $row[] = array('data' => t('@module module', array('@module' => drupal_render($form['permission'][$key]))), 'class' => 'module', 'id' => 'module-'. $module_name, 'colspan' => 6);
 184          }
 185          else {
 186            // The content name: this begins the row.
 187            $content_name = $form['permission'][$key]['#value'];
 188            #dsm($content_suffix);
 189  
 190            $row[] = array('data' => drupal_render($form['permission'][$key]), 'class' => 'permission');
 191            // The perms are stored. Get them to match up with the right checkbox item, then render it.
 192  
 193            // These come in the order the module defined them (probably?)
 194            // We want them in the same order as the $action_prefixes array.
 195            $perms_ordered = array();
 196            foreach ($form['permission'][$key]['perms']['#value'] as $perm) {
 197              // create an arrray that looks like
 198              //action string => full perm name
 199              $pos = strpos($perm, $content_name);
 200              $action_prefix = substr($perm, 0, $pos-1);
 201              $perms_by_prefix[$action_prefix] = $perm;
 202            }
 203  
 204            #dsm($perms_by_prefix);
 205            // Iterate over this array to get the right order.
 206            foreach ($action_prefixes as $action_prefix) {
 207              $perm = $perms_by_prefix[$action_prefix];
 208              if (is_array($form['checkboxes'][$rid][$perm])) {
 209                $row[] = array('data' => drupal_render($form['checkboxes'][$rid][$perm]), 'class' => 'checkbox', 'title' => $roles[$rid] .' : '. t($perm));
 210              }
 211              else {
 212                $row[] = '';
 213              }
 214            }
 215  
 216          }
 217          $rows[] = $row;
 218        }
 219      }
 220    }
 221    $header[] = (t('Content'));
 222    foreach ($action_prefixes as $action) {
 223      $header[] = array('data' => drupal_render($form['action_names'][$action]), 'class' => 'checkbox');
 224    }
 225    $output .= theme('table', $header, $rows, array('id' => 'permissions'));
 226    $output .= drupal_render($form);
 227    return $output;
 228  }
 229  
 230  
 231  /**
 232   * Submit the form.
 233   */
 234  function node_permissions_grid_admin_content_perm_submit($form, &$form_state) {
 235    // Save permissions:
 236    #dsm($form_state);
 237    $result = db_query('SELECT * FROM {role}');
 238    while ($role = db_fetch_object($result)) {
 239      if (isset($form_state['values'][$role->rid])) {
 240        $form_state['values'][$role->rid] = array_filter($form_state['values'][$role->rid]);
 241        // This contains the permissions featured on the form as well as all the others the role had set.
 242        // eg, 'access content'.
 243        #dsm($form_state['values'][$role->rid]);
 244  
 245        // Delete, so if we clear every checkbox we reset that role;
 246        // otherwise permissions are active and denied everywhere.
 247        db_query('DELETE FROM {permission} WHERE rid = %d', $role->rid);
 248        $form_state['values'][$role->rid] = array_filter($form_state['values'][$role->rid]);
 249        if (count($form_state['values'][$role->rid])) {
 250          db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $role->rid, implode(', ', array_keys($form_state['values'][$role->rid])));
 251        }
 252      }
 253    }
 254  
 255    drupal_set_message(t('The changes have been saved.'));
 256  
 257    // Clear the cached pages
 258    cache_clear_all();
 259  }
 260  
 261  /**
 262   * Output for main page: role list.
 263   *
 264   * Switching the form's theme function to bring us here is kinda hacky.
 265   * If core provided a role_load we could switch to using that in hook_menu.
 266   */
 267  function theme_node_permissions_grid_admin_roles($form) {
 268    $header = array(t('Name'), array('data' => t('Operations'), 'colspan' => 2));
 269    foreach (user_roles() as $rid => $name) {
 270      $edit_permissions = l(t('edit node permissions'), 'admin/user/node_permissions/'. $rid);
 271      if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
 272        $rows[] = array($name, l(t('edit role'), 'admin/user/roles/edit/'. $rid), $edit_permissions);
 273      }
 274      else {
 275        $rows[] = array($name, t('locked'), $edit_permissions);
 276      }
 277    }
 278  
 279    $output = drupal_render($form);
 280    $output .= theme('table', $header, $rows);
 281  
 282    return $output;
 283  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7