/[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.10, Mon Jul 21 21:51:50 2008 UTC revision 1.4.2.7.2.11, Mon Jul 21 22:56:07 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: nodeaccess.module,v 1.4.2.7.2.8 2007/06/08 16:24:51 debtman7 Exp $  // $Id: nodeaccess.module,v 1.4.2.7.2.9 2008/07/21 21:43:46 mantyla Exp $
3    
4  /**  /**
5   * Implementation of hook_help().   * Implementation of hook_help().
6   */   */
7  function nodeaccess_help($path, $arg) {  function nodeaccess_help($section) {
8    switch ($path) {    if (preg_match('|^node/\d+/grant|', $section)) {
9      case 'node/%/grant':      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>';
       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>';  
10    }    }
11  }  }
12    
13  /**  /**
14   * Implementation of hook_menu().   * Implementation of hook_menu().
15   */   */
16  function nodeaccess_menu() {  function nodeaccess_menu($may_cache) {
17    $items['admin/user/nodeaccess'] = array(    $items = array();
18      'title' => 'Nodeaccess',    if ($may_cache) {
19      'description' => 'Change default settings for the Nodeaccess module.',      $items[] = array('path' => 'admin/user/nodeaccess',
20      'page callback' => 'nodeaccess_admin',                       'title' => t('Nodeaccess'),
21      'access arguments' => array('administer nodeaccess')                       'callback' => 'nodeaccess_admin',
22    );                       'access' => user_access('administer nodeaccess'),
23    $items['node/%node/grant'] = array(                       'description' => t('Change default settings for the Nodeaccess module.'));
24      'title' => 'Grant',    }
25      'page callback' => 'nodeaccess_grants',    else {
26      'page arguments' => array(1),      if (arg(0) == 'node' && is_numeric(arg(1))) {
27      'access callback' => 'nodeaccess_access',        $node = node_load(arg(1));
28      'access arguments' => array('grant', 1),        if (nodeaccess_access('grant', $node)) {
29      'weight' => 5,          $items[] = array('path' => 'node/'. $node->nid .'/grant',
30      'type' => MENU_LOCAL_TASK                           'title' => t('Grant'),
31    );                           'callback' => 'nodeaccess_grants',
32                             'callback arguments' => $node->nid,
33                             'access' => nodeaccess_access('grant', $node),
34                             'weight' => 5,
35                             'type' => MENU_LOCAL_TASK);
36          }
37        }
38      }
39    return $items;    return $items;
40  }  }
41    
# Line 43  function nodeaccess_perm() { Line 49  function nodeaccess_perm() {
49  /**  /**
50   * Implementation of hook_access().   * Implementation of hook_access().
51   */   */
52  function nodeaccess_access($op, $node, $account = NULL) {  function nodeaccess_access($op, $node) {
53    global $user;    global $user;
   
   if (!$node) {  
     return FALSE;  
   }  
   // If no user object is supplied, the access check is for the current user.  
   if (empty($account)) {  
     $account = $user;  
   }  
54    $allowed_types = variable_get('nodeaccess-types', array());    $allowed_types = variable_get('nodeaccess-types', array());
55    if ($op == 'grant') {    if ($op == 'grant') {
56      if ($node->nid && $allowed_types[$node->type] &&      if ($node->nid && $allowed_types[$node->type] &&
57          (user_access('grant node permissions', $account) ||          (user_access('grant node permissions') ||
58           (user_access('grant editable node permissions', $account) && node_access('update', $node, $account)) ||           (user_access('grant editable node permissions') && node_access('update', $node)) ||
59           (user_access('grant deletable node permissions', $account) && node_access('delete', $node, $account)) ||           (user_access('grant deletable node permissions') && node_access('delete', $node)) ||
60           (user_access('grant own node permissions', $account) && ($account->uid == $node->uid)))) {           (user_access('grant own node permissions') && ($user->uid == $node->uid)))) {
61        return TRUE;        return TRUE;
62      }      }
63    }    }
# Line 67  function nodeaccess_access($op, $node, $ Line 65  function nodeaccess_access($op, $node, $
65  }  }
66    
67  /**  /**
  * Implementation of hook_theme().  
  */  
 function nodeaccess_theme($existing = NULL, $type = NULL, $theme = NULL, $path = NULL) {  
   return array(  
     'nodeaccess_admin_form_roles' => array('arguments' => array('form')),  
     'nodeaccess_admin_form_types' => array('arguments' => array('form')),  
     'nodeaccess_grants_form' => array('arguments' => array('form'))  
   );  
 }  
   
 /**  
68   * Menu callback. Draws the admin page.   * Menu callback. Draws the admin page.
69   */   */
70  function nodeaccess_admin() {  function nodeaccess_admin() {
# Line 87  function nodeaccess_admin() { Line 74  function nodeaccess_admin() {
74  /**  /**
75   * Menu callback. Draws the admin page.   * Menu callback. Draws the admin page.
76   */   */
77  function nodeaccess_admin_form(&$form_state) {  function nodeaccess_admin_form($form_values = NULL) {
78    // Set defaults from variable_get.    // Set defaults from variable_get.
79    $show = variable_get('nodeaccess-types', array());    $show = variable_get('nodeaccess-types', array());
80    $roles = nodeaccess_get_role_aliases();    $roles = nodeaccess_get_role_aliases();
# Line 177  function nodeaccess_admin_form(&$form_st Line 164  function nodeaccess_admin_form(&$form_st
164  /**  /**
165   * Submit function for nodeaccess_admin_form.   * Submit function for nodeaccess_admin_form.
166   */   */
167  function nodeaccess_admin_form_submit($form, &$form_state) {  function nodeaccess_admin_form_submit($form_id, $form_values) {
   $form_values = $form_state['values'];  
168    // Save priority.    // Save priority.
169    variable_set('nodeaccess-priority', $form_values['priority']);    variable_set('nodeaccess-priority', $form_values['priority']);
170    // Save preserve.    // Save preserve.
# Line 223  function nodeaccess_admin_form_submit($f Line 209  function nodeaccess_admin_form_submit($f
209    variable_set('nodeaccess_authors', $author_prefs);    variable_set('nodeaccess_authors', $author_prefs);
210    // Save allowed node types.    // Save allowed node types.
211    variable_set('nodeaccess-types', $allowed_types);    variable_set('nodeaccess-types', $allowed_types);
212    node_access_needs_rebuild(TRUE);    node_access_rebuild();
213    drupal_set_message(t('Grants saved.'));    drupal_set_message(t('Grants saved.'));
214  }  }
215    
# Line 273  function theme_nodeaccess_admin_form_typ Line 259  function theme_nodeaccess_admin_form_typ
259  /**  /**
260   * Menu callback. Draws the grant tab.   * Menu callback. Draws the grant tab.
261   */   */
262  function nodeaccess_grants($node) {  function nodeaccess_grants($nid) {
263      $node = node_load($nid);
264    drupal_set_title(check_plain($node->title));    drupal_set_title(check_plain($node->title));
265    return drupal_get_form('nodeaccess_grants_form', $node);    return drupal_get_form('nodeaccess_grants_form', $node->nid);
266  }  }
267    
268  /**  /**
269   * Menu callback. Draws the grant tab.   * Menu callback. Draws the grant tab.
270   */   */
271  function nodeaccess_grants_form(&$form_state, $node) {  function nodeaccess_grants_form($nid, $form_values = NULL) {
272    $form_values =& $form_state['values'];    $node = node_load($nid);
273      $form_values = $_POST;
274    if (!$form_values) {    if (!$form_values) {
275      $form_values = array();      $form_values = array();
276      // Load all roles.      // Load all roles.
# Line 296  function nodeaccess_grants_form(&$form_s Line 284  function nodeaccess_grants_form(&$form_s
284        $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);
285      }      }
286    } else {    } else {
287        // Delete unkept users.
288        if (is_array($form_values['uid'])) {
289          foreach ($form_values['uid'] as $uid => $row) {
290            if (!$row['keep']) {
291              unset($form_values['uid'][$uid]);
292            }
293          }
294        }
295        if (!$form_values['uid']) {
296          unset($form_values['uid']);
297        }
298      // Perform search.      // Perform search.
299      if ($form_values['keys']) {      if ($form_values['keys']) {
300        $sql = "SELECT uid, name FROM {users} WHERE name LIKE '%%%s%%'";        $sql = "SELECT uid, name FROM {users} WHERE name LIKE '%%%s%%'";
# Line 369  function nodeaccess_grants_form(&$form_s Line 368  function nodeaccess_grants_form(&$form_s
368        $form['uid'][$key]['keep'] = array('#type' => 'checkbox', '#default_value' => $field['keep']);        $form['uid'][$key]['keep'] = array('#type' => 'checkbox', '#default_value' => $field['keep']);
369        if ($allowed_grants['view']) {        if ($allowed_grants['view']) {
370          $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']);
371            // Because of a bug in the form API, dynamic checkboxes
372            // must be checked explicitly.
373            if ($field['grant_view']) {
374              $form['uid'][$key]['grant_view']['#attributes'] = array('checked' => 'checked');
375            }
376        } else if ($preserve) {        } else if ($preserve) {
377            // Dynamic hidden fields work fine.
378          $form['uid'][$key]['grant_view'] = array('#type' => 'hidden', '#value' => $field['grant_view']);          $form['uid'][$key]['grant_view'] = array('#type' => 'hidden', '#value' => $field['grant_view']);
379        }        }
380        if ($allowed_grants['edit']) {        if ($allowed_grants['edit']) {
381          $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']);
382            if ($field['grant_update']) {
383              $form['uid'][$key]['grant_update']['#attributes'] = array('checked' => 'checked');
384            }
385        } else if ($preserve) {        } else if ($preserve) {
386          $form['uid'][$key]['grant_update'] = array('#type' => 'hidden', '#value' => $field['grant_update']);          $form['uid'][$key]['grant_update'] = array('#type' => 'hidden', '#value' => $field['grant_update']);
387        }        }
388        if ($allowed_grants['delete']) {        if ($allowed_grants['delete']) {
389          $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']);
390            if ($field['grant_delete']) {
391              $form['uid'][$key]['grant_delete']['#attributes'] = array('checked' => 'checked');
392            }
393        } else if ($preserve) {        } else if ($preserve) {
394          $form['uid'][$key]['grant_delete'] = array('#type' => 'hidden', '#value' => $field['grant_delete']);          $form['uid'][$key]['grant_delete'] = array('#type' => 'hidden', '#value' => $field['grant_delete']);
395        }        }
# Line 393  function nodeaccess_grants_form(&$form_s Line 404  function nodeaccess_grants_form(&$form_s
404      $form['keys'] = array('#type' => 'textfield', '#default_value' => $form_values['keys'], '#size' => 40);      $form['keys'] = array('#type' => 'textfield', '#default_value' => $form_values['keys'], '#size' => 40);
405    }    }
406    
407    $form['search'] = array('#type' => 'submit', '#value' => t('Search'));    $form['search'] = array('#type' => 'button', '#value' => t('Search'));
408    
409    $form['submit'] = array('#type' => 'submit', '#value' => t('Save Grants'));    $form['submit'] = array('#type' => 'submit', '#value' => t('Save Grants'));
410    
# Line 403  function nodeaccess_grants_form(&$form_s Line 414  function nodeaccess_grants_form(&$form_s
414  /**  /**
415   * Validate function for nodeaccess_grants_form.   * Validate function for nodeaccess_grants_form.
416   */   */
417  function nodeaccess_grants_form_validate($form, &$form_state) {  function nodeaccess_grants_form_validate($form_id, $form_values) {
418    $form_values =& $form_state['values'];    global $form_values;
419    // Delete unkept users.    // Delete unkept users.
420    if (is_array($form_values['uid'])) {    if (is_array($form_values['uid'])) {
421      foreach ($form_values['uid'] as $uid => $row) {      foreach ($form_values['uid'] as $uid => $row) {
# Line 416  function nodeaccess_grants_form_validate Line 427  function nodeaccess_grants_form_validate
427    if (!$form_values['uid']) {    if (!$form_values['uid']) {
428      unset($form_values['uid']);      unset($form_values['uid']);
429    }    }
   if ($form_state['clicked_button']['#id'] == 'edit-search') {  
     $form_state['rebuild'] = TRUE;  
   } else {  
     unset($form_state['rebuild']);  
   }  
430  }  }
431    
432  /**  /**
433   * Submit function for nodeaccess_grants_form.   * Submit function for nodeaccess_grants_form.
434   */   */
435  function nodeaccess_grants_form_submit($form, &$form_state) {  function nodeaccess_grants_form_submit($form_id, $form_values) {
436    $form_values =& $form_state['values'];    global $form_values;
437    global $user;    global $user;
438    $grants = array();    $grants = array();
439    $nid = $form_values['nid'];    $nid = $form_values['nid'];
# Line 564  function nodeaccess_node_grants($account Line 570  function nodeaccess_node_grants($account
570   */   */
571  function nodeaccess_nodeapi(&$node, $op) {  function nodeaccess_nodeapi(&$node, $op) {
572    switch ($op) {    switch ($op) {
     case 'insert':  
       // New node, write default permissions.  
       $grants = nodeaccess_node_access_records($node);  
       foreach(array('uid', 'rid', 'author') as $type) {  
         $realm = 'nodeaccess_' . $type;  
         node_access_write_grants($node, $grants, $realm);  
       }  
       // Done, default permissions are not written into nodeaccess.  
     break;  
     case 'update':  
       // Node author may have changed, overwrite old record.  
       $author_prefs = variable_get('nodeaccess_authors', array());  
       // Array is prepopulated with grant values.  
       $grant = $author_prefs[$node->type];  
       $grant['gid'] = $node->uid;  
       $grant['realm'] = 'nodeaccess_author';  
       $grants = array();  
       $grants[] = $grant;  
       node_access_write_grants($node, $grants, 'nodeaccess_author');  
       // Done, author permissions are not written into nodeaccess.  
     break;  
573      case 'delete':      case 'delete':
574        // Deleting node, delete related permissions.        // Deleting node, delete related permissions.
575        db_query('DELETE FROM {nodeaccess} WHERE nid = %d', $node->nid);        db_query('DELETE FROM {nodeaccess} WHERE nid = %d', $node->nid);
# Line 647  function nodeaccess_node_access_records( Line 632  function nodeaccess_node_access_records(
632   * Implementation of hook_enable().   * Implementation of hook_enable().
633   */   */
634  function nodeaccess_enable() {  function nodeaccess_enable() {
635      node_access_rebuild();
636  }  }
637    
638  /**  /**
# Line 654  function nodeaccess_enable() { Line 640  function nodeaccess_enable() {
640   */   */
641  function nodeaccess_disable() {  function nodeaccess_disable() {
642    nodeaccess_disabling(TRUE);    nodeaccess_disabling(TRUE);
643      node_access_rebuild();
644  }  }
645    
646  function nodeaccess_disabling($set = NULL) {  function nodeaccess_disabling($set = NULL) {
# Line 701  function nodeaccess_node_type($op, $info Line 688  function nodeaccess_node_type($op, $info
688        $author_prefs = variable_get('nodeaccess_authors', array());        $author_prefs = variable_get('nodeaccess_authors', array());
689        $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);
690        variable_set('nodeaccess_authors', $author_prefs);        variable_set('nodeaccess_authors', $author_prefs);
691        node_access_needs_rebuild(TRUE);        node_access_rebuild();
692      break;      break;
693    }    }
694  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.2