/[drupal]/contributions/modules/workflow/workflow_access.module
ViewVC logotype

Diff of /contributions/modules/workflow/workflow_access.module

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

revision 1.3, Fri Aug 1 02:29:46 2008 UTC revision 1.4, Fri Aug 1 03:33:11 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: workflow_access.module,v 1.2 2008/04/02 16:44:07 jvandyk Exp $  // $Id: workflow_access.module,v 1.3 2008/08/01 02:29:46 jvandyk Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 7  Line 7 
7   */   */
8    
9  /**  /**
  * Implementation of hook_enable().  
  *  
  * Force a rebuild of the node access table when enabled to ensure that things are set up.  
  */  
 function workflow_access_enable() {  
   node_access_rebuild();  
 }  
   
 /**  
  * Implementation of hook_disable().  
  *  
  * Force a rebuild of the node access table when disabled to ensure  
  * that our entries are removed from the table.  
  */  
 function workflow_access_disable() {  
   workflow_access_disabling(TRUE);  
   node_access_rebuild();  
 }  
   
 /**  
  * Make sure we don't respond with grants when disabling ourselves.  
  */  
 function workflow_access_disabling($value = NULL) {  
   static $disabling = FALSE;  
   if (isset($value)) {  
     $disabling = $value;  
   }  
   return $disabling;  
 }  
   
 /**  
10   * Implementation of hook_node_grants().   * Implementation of hook_node_grants().
11   *   *
12   * Supply the workflow access grants. We are simply using   * Supply the workflow access grants. We are simply using
# Line 56  function workflow_access_node_grants($ac Line 25  function workflow_access_node_grants($ac
25   * Returns a list of grant records for the passed in node object.   * Returns a list of grant records for the passed in node object.
26   */   */
27  function workflow_access_node_access_records($node) {  function workflow_access_node_access_records($node) {
   if (workflow_access_disabling()) {  
     return;  
   }  
   
28    $grants = array();    $grants = array();
29    $sid = db_result(db_query("SELECT sid FROM {workflow_node} WHERE nid = %d", $node->nid));    $sid = db_result(db_query("SELECT sid FROM {workflow_node} WHERE nid = %d", $node->nid));
30    
# Line 95  function workflow_access_form_alter(&$fo Line 60  function workflow_access_form_alter(&$fo
60    // A list of roles available on the site and our    // A list of roles available on the site and our
61    // special -1 role used to represent the node author.    // special -1 role used to represent the node author.
62    // TODO i think there is an API call for this -- user_roles() perhaps?    // TODO i think there is an API call for this -- user_roles() perhaps?
63    $rids = array('-1' => t('author'));    $rids = array_merge(user_roles(), array('-1' => t('author')));
64    $result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name");  
65    while ($obj = db_fetch_object($result)) {    $form['workflow_access'] = array(
66      $rids[$obj->rid] = $obj->name;      '#type' => 'fieldset',
   }  
   
   $form['workflow_access'] = array('#type' => 'fieldset',  
67      '#title' => t('Access control'),      '#title' => t('Access control'),
68      '#collapsible' => TRUE,      '#collapsible' => TRUE,
69      '#tree' => TRUE,      '#tree' => TRUE,
# Line 119  function workflow_access_form_alter(&$fo Line 81  function workflow_access_form_alter(&$fo
81    
82      $result = db_query("SELECT * from {workflow_access} where sid = %d", $sid);      $result = db_query("SELECT * from {workflow_access} where sid = %d", $sid);
83    
     // Allow view grants by default for anonymous and authenticated users,  
     // if no grants were set up earlier.  
     if (db_num_rows($result) == 0) {  
       $view = array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID);  
     }  
   
84      while ($access = db_fetch_object($result)) {      while ($access = db_fetch_object($result)) {
85        if ($access->grant_view) {        if ($access->grant_view) {
86          $view[] = $access->rid;          $view[] = $access->rid;
# Line 137  function workflow_access_form_alter(&$fo Line 93  function workflow_access_form_alter(&$fo
93        }        }
94      }      }
95    
96        // Allow view grants by default for anonymous and authenticated users,
97        // if no grants were set up earlier.
98        if (empty($view)) {
99          $view = array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID);
100        }
101    
102      // TODO better tables using a #theme function instead of direct #prefixing      // TODO better tables using a #theme function instead of direct #prefixing
103      $form['workflow_access'][$sid] = array(      $form['workflow_access'][$sid] = array(
104        '#type' => 'fieldset',        '#type' => 'fieldset',
# Line 175  function workflow_access_form_alter(&$fo Line 137  function workflow_access_form_alter(&$fo
137  /**  /**
138   * Store permission settings for workflow states.   * Store permission settings for workflow states.
139   */   */
140  function workflow_access_form_submit($form_id, $form_values) {  function workflow_access_form_submit($form, $form_state) {
141    foreach ($form_values['workflow_access'] as $sid => $access) {    foreach ($form_state['values']['workflow_access'] as $sid => $access) {
142      // Ignore irrelevant keys.      // Ignore irrelevant keys.
143      if (!is_numeric($sid)) {      if (!is_numeric($sid)) {
144        continue;        continue;
# Line 188  function workflow_access_form_submit($fo Line 150  function workflow_access_form_submit($fo
150        $grants[] = array(        $grants[] = array(
151          'realm'        => ($rid == -1) ? 'workflow_access_owner' : 'workflow_access',          'realm'        => ($rid == -1) ? 'workflow_access_owner' : 'workflow_access',
152          'gid'          => ($rid == -1) ? $node->uid : $rid,          'gid'          => ($rid == -1) ? $node->uid : $rid,
153          'grant_view'   => (bool) $checked,          'grant_view'   => (bool)$checked,
154          'grant_update' => (bool) $access['update'][$rid],          'grant_update' => (bool)$access['update'][$rid],
155          'grant_delete' => (bool) $access['delete'][$rid],          'grant_delete' => (bool)$access['delete'][$rid],
156        );        );
157    
158        db_query("INSERT INTO {workflow_access} (sid, rid, grant_view, grant_update, grant_delete) VALUES (%d, %d, %d, %d, %d)", $sid, $rid, (bool) $checked, (bool) $access['update'][$rid], (bool) $access['delete'][$rid]);        db_query("INSERT INTO {workflow_access} (sid, rid, grant_view, grant_update, grant_delete) VALUES (%d, %d, %d, %d, %d)", $sid, $rid, (bool)$checked, (bool)$access['update'][$rid], (bool)$access['delete'][$rid]);
159      }      }
160    
161      // Update all nodes having some workflow state to reflect new settings.      // Update all nodes having same workflow state to reflect new settings.
162      $result = db_query("SELECT n.nid FROM {node} n LEFT JOIN {workflow_node} wn ON wn.nid = n.nid WHERE wn.sid = %d", $sid);      $result = db_query("SELECT n.nid FROM {node} n LEFT JOIN {workflow_node} wn ON wn.nid = n.nid WHERE wn.sid = %d", $sid);
163      while ($node = db_fetch_object($result)) {      while ($node = db_fetch_object($result)) {
164        // TODO: this only works with workflow_access realm, not the workflow_access_owner realm?!        // TODO: this only works with workflow_access realm, not the workflow_access_owner realm?!
165        node_access_write_grants(node_load($node->nid), $grants, 'workflow_access');        node_access_write_grants($node, $grants, 'workflow_access');
166      }      }
167    }    }
168    drupal_set_message(t('Workflow access permissions updated.'));    drupal_set_message(t('Workflow access permissions updated.'));

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

  ViewVC Help
Powered by ViewVC 1.1.2