/[drupal]/contributions/modules/nodeaccess/nodeaccess.module
ViewVC logotype

Diff of /contributions/modules/nodeaccess/nodeaccess.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.4.2.7.2.9, Mon Jul 21 21:43:46 2008 UTC revision 1.4.2.7.2.10, Mon Jul 21 21:51:50 2008 UTC
# Line 4  Line 4 
4  /**  /**
5   * Implementation of hook_help().   * Implementation of hook_help().
6   */   */
7  function nodeaccess_help($section) {  function nodeaccess_help($path, $arg) {
8    if (preg_match('|^node/\d+/grant|', $section)) {    switch ($path) {
9      return '<small>' . t('You can set grants per users. Enter a name or a partial name in the box and click Search or press return. You need to check the Keep? checkbox if you want to keep the user for granting. Note that user grants are additional to those coming from roles.') . '</small>';      case 'node/%/grant':
10          return '<small>' . t('You can set grants per users. Enter a name or a partial name in the box and click Search or press return. You need to check the Keep? checkbox if you want to keep the user for granting. Note that user grants are additional to those coming from roles.') . '</small>';
11    }    }
12  }  }
13    
14  /**  /**
15   * Implementation of hook_menu().   * Implementation of hook_menu().
16   */   */
17  function nodeaccess_menu($may_cache) {  function nodeaccess_menu() {
18    $items = array();    $items['admin/user/nodeaccess'] = array(
19    if ($may_cache) {      'title' => 'Nodeaccess',
20      $items[] = array('path' => 'admin/user/nodeaccess',      'description' => 'Change default settings for the Nodeaccess module.',
21                       'title' => t('Nodeaccess'),      'page callback' => 'nodeaccess_admin',
22                       'callback' => 'nodeaccess_admin',      'access arguments' => array('administer nodeaccess')
23                       'access' => user_access('administer nodeaccess'),    );
24                       'description' => t('Change default settings for the Nodeaccess module.'));    $items['node/%node/grant'] = array(
25    }      'title' => 'Grant',
26    else {      'page callback' => 'nodeaccess_grants',
27      if (arg(0) == 'node' && is_numeric(arg(1))) {      'page arguments' => array(1),
28        $node = node_load(arg(1));      'access callback' => 'nodeaccess_access',
29        if (nodeaccess_access('grant', $node)) {      'access arguments' => array('grant', 1),
30          $items[] = array('path' => 'node/'. $node->nid .'/grant',      'weight' => 5,
31                           'title' => t('Grant'),      'type' => MENU_LOCAL_TASK
32                           'callback' => 'nodeaccess_grants',    );
                          'callback arguments' => $node->nid,  
                          'access' => nodeaccess_access('grant', $node),  
                          'weight' => 5,  
                          'type' => MENU_LOCAL_TASK);  
       }  
     }  
   }  
33    return $items;    return $items;
34  }  }
35    
# Line 49  function nodeaccess_perm() { Line 43  function nodeaccess_perm() {
43  /**  /**
44   * Implementation of hook_access().   * Implementation of hook_access().
45   */   */
46  function nodeaccess_access($op, $node) {  function nodeaccess_access($op, $node, $account = NULL) {
47    global $user;    global $user;
48    
49      if (!$node) {
50        return FALSE;
51      }
52      // If no user object is supplied, the access check is for the current user.
53      if (empty($account)) {
54        $account = $user;
55      }
56    $allowed_types = variable_get('nodeaccess-types', array());    $allowed_types = variable_get('nodeaccess-types', array());
57    if ($op == 'grant') {    if ($op == 'grant') {
58      if ($node->nid && $allowed_types[$node->type] &&      if ($node->nid && $allowed_types[$node->type] &&
59          (user_access('grant node permissions') ||          (user_access('grant node permissions', $account) ||
60           (user_access('grant editable node permissions') && node_access('update', $node)) ||           (user_access('grant editable node permissions', $account) && node_access('update', $node, $account)) ||
61           (user_access('grant deletable node permissions') && node_access('delete', $node)) ||           (user_access('grant deletable node permissions', $account) && node_access('delete', $node, $account)) ||
62           (user_access('grant own node permissions') && ($user->uid == $node->uid)))) {           (user_access('grant own node permissions', $account) && ($account->uid == $node->uid)))) {
63        return TRUE;        return TRUE;
64      }      }
65    }    }
# Line 65  function nodeaccess_access($op, $node) { Line 67  function nodeaccess_access($op, $node) {
67  }  }
68    
69  /**  /**
70     * Implementation of hook_theme().
71     */
72    function nodeaccess_theme($existing = NULL, $type = NULL, $theme = NULL, $path = NULL) {
73      return array(
74        'nodeaccess_admin_form_roles' => array('arguments' => array('form')),
75        'nodeaccess_admin_form_types' => array('arguments' => array('form')),
76        'nodeaccess_grants_form' => array('arguments' => array('form'))
77      );
78    }
79    
80    /**
81   * Menu callback. Draws the admin page.   * Menu callback. Draws the admin page.
82   */   */
83  function nodeaccess_admin() {  function nodeaccess_admin() {
# Line 74  function nodeaccess_admin() { Line 87  function nodeaccess_admin() {
87  /**  /**
88   * Menu callback. Draws the admin page.   * Menu callback. Draws the admin page.
89   */   */
90  function nodeaccess_admin_form($form_values = NULL) {  function nodeaccess_admin_form(&$form_state) {
91    // Set defaults from variable_get.    // Set defaults from variable_get.
92    $show = variable_get('nodeaccess-types', array());    $show = variable_get('nodeaccess-types', array());
93    $roles = nodeaccess_get_role_aliases();    $roles = nodeaccess_get_role_aliases();
# Line 164  function nodeaccess_admin_form($form_val Line 177  function nodeaccess_admin_form($form_val
177  /**  /**
178   * Submit function for nodeaccess_admin_form.   * Submit function for nodeaccess_admin_form.
179   */   */
180  function nodeaccess_admin_form_submit($form_id, $form_values) {  function nodeaccess_admin_form_submit($form, &$form_state) {
181      $form_values = $form_state['values'];
182    // Save priority.    // Save priority.
183    variable_set('nodeaccess-priority', $form_values['priority']);    variable_set('nodeaccess-priority', $form_values['priority']);
184    // Save preserve.    // Save preserve.
# Line 209  function nodeaccess_admin_form_submit($f Line 223  function nodeaccess_admin_form_submit($f
223    variable_set('nodeaccess_authors', $author_prefs);    variable_set('nodeaccess_authors', $author_prefs);
224    // Save allowed node types.    // Save allowed node types.
225    variable_set('nodeaccess-types', $allowed_types);    variable_set('nodeaccess-types', $allowed_types);
226    node_access_rebuild();    node_access_needs_rebuild(TRUE);
227    drupal_set_message(t('Grants saved.'));    drupal_set_message(t('Grants saved.'));
228  }  }
229    
# Line 259  function theme_nodeaccess_admin_form_typ Line 273  function theme_nodeaccess_admin_form_typ
273  /**  /**
274   * Menu callback. Draws the grant tab.   * Menu callback. Draws the grant tab.
275   */   */
276  function nodeaccess_grants($nid) {  function nodeaccess_grants($node) {
   $node = node_load($nid);  
277    drupal_set_title(check_plain($node->title));    drupal_set_title(check_plain($node->title));
278    return drupal_get_form('nodeaccess_grants_form', $node->nid);    return drupal_get_form('nodeaccess_grants_form', $node);
279  }  }
280    
281  /**  /**
282   * Menu callback. Draws the grant tab.   * Menu callback. Draws the grant tab.
283   */   */
284  function nodeaccess_grants_form($nid, $form_values = NULL) {  function nodeaccess_grants_form(&$form_state, $node) {
285    $node = node_load($nid);    $form_values =& $form_state['values'];
   $form_values = $_POST;  
286    if (!$form_values) {    if (!$form_values) {
287      $form_values = array();      $form_values = array();
288      // Load all roles.      // Load all roles.
# Line 284  function nodeaccess_grants_form($nid, $f Line 296  function nodeaccess_grants_form($nid, $f
296        $form_values['uid'][$account->uid] = array('name' => $account->name, 'keep' => 1, 'grant_view' => $account->grant_view, 'grant_update' => $account->grant_update, 'grant_delete' => $account->grant_delete);        $form_values['uid'][$account->uid] = array('name' => $account->name, 'keep' => 1, 'grant_view' => $account->grant_view, 'grant_update' => $account->grant_update, 'grant_delete' => $account->grant_delete);
297      }      }
298    } else {    } else {
     // Delete unkept users.  
     if (is_array($form_values['uid'])) {  
       foreach ($form_values['uid'] as $uid => $row) {  
         if (!$row['keep']) {  
           unset($form_values['uid'][$uid]);  
         }  
       }  
     }  
     if (!$form_values['uid']) {  
       unset($form_values['uid']);  
     }  
299      // Perform search.      // Perform search.
300      if ($form_values['keys']) {      if ($form_values['keys']) {
301        $sql = "SELECT uid, name FROM {users} WHERE name LIKE '%%%s%%'";        $sql = "SELECT uid, name FROM {users} WHERE name LIKE '%%%s%%'";
# Line 368  function nodeaccess_grants_form($nid, $f Line 369  function nodeaccess_grants_form($nid, $f
369        $form['uid'][$key]['keep'] = array('#type' => 'checkbox', '#default_value' => $field['keep']);        $form['uid'][$key]['keep'] = array('#type' => 'checkbox', '#default_value' => $field['keep']);
370        if ($allowed_grants['view']) {        if ($allowed_grants['view']) {
371          $form['uid'][$key]['grant_view'] = array('#type' => 'checkbox', '#default_value' => $field['grant_view']);          $form['uid'][$key]['grant_view'] = array('#type' => 'checkbox', '#default_value' => $field['grant_view']);
         // Because of a bug in the form API, dynamic checkboxes  
         // must be checked explicitly.  
         if ($field['grant_view']) {  
           $form['uid'][$key]['grant_view']['#attributes'] = array('checked' => 'checked');  
         }  
372        } else if ($preserve) {        } else if ($preserve) {
         // Dynamic hidden fields work fine.  
373          $form['uid'][$key]['grant_view'] = array('#type' => 'hidden', '#value' => $field['grant_view']);          $form['uid'][$key]['grant_view'] = array('#type' => 'hidden', '#value' => $field['grant_view']);
374        }        }
375        if ($allowed_grants['edit']) {        if ($allowed_grants['edit']) {
376          $form['uid'][$key]['grant_update'] = array('#type' => 'checkbox', '#default_value' => $field['grant_update']);          $form['uid'][$key]['grant_update'] = array('#type' => 'checkbox', '#default_value' => $field['grant_update']);
         if ($field['grant_update']) {  
           $form['uid'][$key]['grant_update']['#attributes'] = array('checked' => 'checked');  
         }  
377        } else if ($preserve) {        } else if ($preserve) {
378          $form['uid'][$key]['grant_update'] = array('#type' => 'hidden', '#value' => $field['grant_update']);          $form['uid'][$key]['grant_update'] = array('#type' => 'hidden', '#value' => $field['grant_update']);
379        }        }
380        if ($allowed_grants['delete']) {        if ($allowed_grants['delete']) {
381          $form['uid'][$key]['grant_delete'] = array('#type' => 'checkbox', '#default_value' => $field['grant_delete']);          $form['uid'][$key]['grant_delete'] = array('#type' => 'checkbox', '#default_value' => $field['grant_delete']);
         if ($field['grant_delete']) {  
           $form['uid'][$key]['grant_delete']['#attributes'] = array('checked' => 'checked');  
         }  
382        } else if ($preserve) {        } else if ($preserve) {
383          $form['uid'][$key]['grant_delete'] = array('#type' => 'hidden', '#value' => $field['grant_delete']);          $form['uid'][$key]['grant_delete'] = array('#type' => 'hidden', '#value' => $field['grant_delete']);
384        }        }
# Line 404  function nodeaccess_grants_form($nid, $f Line 393  function nodeaccess_grants_form($nid, $f
393      $form['keys'] = array('#type' => 'textfield', '#default_value' => $form_values['keys'], '#size' => 40);      $form['keys'] = array('#type' => 'textfield', '#default_value' => $form_values['keys'], '#size' => 40);
394    }    }
395    
396    $form['search'] = array('#type' => 'button', '#value' => t('Search'));    $form['search'] = array('#type' => 'submit', '#value' => t('Search'));
397    
398    $form['submit'] = array('#type' => 'submit', '#value' => t('Save Grants'));    $form['submit'] = array('#type' => 'submit', '#value' => t('Save Grants'));
399    
# Line 414  function nodeaccess_grants_form($nid, $f Line 403  function nodeaccess_grants_form($nid, $f
403  /**  /**
404   * Validate function for nodeaccess_grants_form.   * Validate function for nodeaccess_grants_form.
405   */   */
406  function nodeaccess_grants_form_validate($form_id, $form_values) {  function nodeaccess_grants_form_validate($form, &$form_state) {
407    global $form_values;    $form_values =& $form_state['values'];
408    // Delete unkept users.    // Delete unkept users.
409    if (is_array($form_values['uid'])) {    if (is_array($form_values['uid'])) {
410      foreach ($form_values['uid'] as $uid => $row) {      foreach ($form_values['uid'] as $uid => $row) {
# Line 427  function nodeaccess_grants_form_validate Line 416  function nodeaccess_grants_form_validate
416    if (!$form_values['uid']) {    if (!$form_values['uid']) {
417      unset($form_values['uid']);      unset($form_values['uid']);
418    }    }
419      if ($form_state['clicked_button']['#id'] == 'edit-search') {
420        $form_state['rebuild'] = TRUE;
421      } else {
422        unset($form_state['rebuild']);
423      }
424  }  }
425    
426  /**  /**
427   * Submit function for nodeaccess_grants_form.   * Submit function for nodeaccess_grants_form.
428   */   */
429  function nodeaccess_grants_form_submit($form_id, $form_values) {  function nodeaccess_grants_form_submit($form, &$form_state) {
430    global $form_values;    $form_values =& $form_state['values'];
431    global $user;    global $user;
432    $grants = array();    $grants = array();
433    $nid = $form_values['nid'];    $nid = $form_values['nid'];
# Line 570  function nodeaccess_node_grants($account Line 564  function nodeaccess_node_grants($account
564   */   */
565  function nodeaccess_nodeapi(&$node, $op) {  function nodeaccess_nodeapi(&$node, $op) {
566    switch ($op) {    switch ($op) {
567        case 'insert':
568          // New node, write default permissions.
569          $grants = nodeaccess_node_access_records($node);
570          foreach(array('uid', 'rid', 'author') as $type) {
571            $realm = 'nodeaccess_' . $type;
572            node_access_write_grants($node, $grants, $realm);
573          }
574          // Done, default permissions are not written into nodeaccess.
575        break;
576        case 'update':
577          // Node author may have changed, overwrite old record.
578          $author_prefs = variable_get('nodeaccess_authors', array());
579          // Array is prepopulated with grant values.
580          $grant = $author_prefs[$node->type];
581          $grant['gid'] = $node->uid;
582          $grant['realm'] = 'nodeaccess_author';
583          $grants = array();
584          $grants[] = $grant;
585          node_access_write_grants($node, $grants, 'nodeaccess_author');
586          // Done, author permissions are not written into nodeaccess.
587        break;
588      case 'delete':      case 'delete':
589        // Deleting node, delete related permissions.        // Deleting node, delete related permissions.
590        db_query('DELETE FROM {nodeaccess} WHERE nid = %d', $node->nid);        db_query('DELETE FROM {nodeaccess} WHERE nid = %d', $node->nid);
# Line 632  function nodeaccess_node_access_records( Line 647  function nodeaccess_node_access_records(
647   * Implementation of hook_enable().   * Implementation of hook_enable().
648   */   */
649  function nodeaccess_enable() {  function nodeaccess_enable() {
   node_access_rebuild();  
650  }  }
651    
652  /**  /**
# Line 640  function nodeaccess_enable() { Line 654  function nodeaccess_enable() {
654   */   */
655  function nodeaccess_disable() {  function nodeaccess_disable() {
656    nodeaccess_disabling(TRUE);    nodeaccess_disabling(TRUE);
   node_access_rebuild();  
657  }  }
658    
659  function nodeaccess_disabling($set = NULL) {  function nodeaccess_disabling($set = NULL) {
# Line 688  function nodeaccess_node_type($op, $info Line 701  function nodeaccess_node_type($op, $info
701        $author_prefs = variable_get('nodeaccess_authors', array());        $author_prefs = variable_get('nodeaccess_authors', array());
702        $author_prefs[$info->type] = array('grant_view' => 1, 'grant_update' => 1, 'grant_delete' => 1);        $author_prefs[$info->type] = array('grant_view' => 1, 'grant_update' => 1, 'grant_delete' => 1);
703        variable_set('nodeaccess_authors', $author_prefs);        variable_set('nodeaccess_authors', $author_prefs);
704        node_access_rebuild();        node_access_needs_rebuild(TRUE);
705      break;      break;
706    }    }
707  }  }

Legend:
Removed from v.1.4.2.7.2.9  
changed lines
  Added in v.1.4.2.7.2.10

  ViewVC Help
Powered by ViewVC 1.1.2