| 1 |
<?php
|
| 2 |
// $Id: workflow_access.module,v 1.6 2006/09/09 17:30:12 merlinofchaos Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file workflow_access.module
|
| 6 |
*
|
| 7 |
* This module is a demo of the na_arbitrator module, to create access permissions
|
| 8 |
* that exist only for workflows.
|
| 9 |
*
|
| 10 |
*/
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implementation of hook_help
|
| 14 |
*/
|
| 15 |
function workflow_access_help($section) {
|
| 16 |
switch($section) {
|
| 17 |
case 'admin/modules#description':
|
| 18 |
return t('workflow access allows role-based access permissions to workflows.');
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
| 22 |
/**
|
| 23 |
* This function supplies the workflow access grants. workflow_access simply uses
|
| 24 |
* roles as ACLs, so rids translate directly to gids.
|
| 25 |
*/
|
| 26 |
function workflow_access_node_grants($user, $op) {
|
| 27 |
$grants['workflow_access'] = array_keys($user->roles);
|
| 28 |
$grants['workflow_access_owner'] = array($user->uid);
|
| 29 |
return $grants;
|
| 30 |
}
|
| 31 |
|
| 32 |
/**
|
| 33 |
* Implementation of hook_node_access_grants
|
| 34 |
*
|
| 35 |
* Returns a list of grant records for the passed in node object.
|
| 36 |
*/
|
| 37 |
function workflow_access_node_access_grants($node) {
|
| 38 |
$sid = db_result(db_query("SELECT sid FROM {workflow_node} WHERE nid = %d", $node->nid));
|
| 39 |
if (is_numeric($sid)) {
|
| 40 |
$result = db_query('SELECT * FROM {workflow_access} WHERE sid = %d', $sid);
|
| 41 |
while ($grant = db_fetch_object($result)) {
|
| 42 |
if ($grant->rid == -1) {
|
| 43 |
$realm = 'workflow_access_owner';
|
| 44 |
$gid = $node->uid;
|
| 45 |
}
|
| 46 |
else {
|
| 47 |
$realm = 'workflow_access';
|
| 48 |
$gid = $grant->rid;
|
| 49 |
}
|
| 50 |
$realm = $grant->rid != -1 ? 'workflow_access' : 'workflow_access_owner';
|
| 51 |
$gid =
|
| 52 |
$grants[] = array('realm' => $realm, 'gid' => $gid, 'grant_view' => $grant->grant_view, 'grant_update' => $grant->grant_update, 'grant_delete' => $grant->grant_delete);
|
| 53 |
}
|
| 54 |
}
|
| 55 |
return $grants;
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 59 |
* Implementation of hook_form_alter()
|
| 60 |
*
|
| 61 |
* Remove inaccessible workflows from the node form.
|
| 62 |
*/
|
| 63 |
function workflow_access_form_alter($form_id, &$form) {
|
| 64 |
if ($form_id == 'workflow_edit') {
|
| 65 |
workflow_access_workflow_form($form_id, $form);
|
| 66 |
}
|
| 67 |
|
| 68 |
// hunmonk's module dependency check: see http://drupal.org/node/54463
|
| 69 |
if ($form_id == 'system_modules' && !$_POST) {
|
| 70 |
workflow_access_system_module_validate($form);
|
| 71 |
}
|
| 72 |
}
|
| 73 |
|
| 74 |
/**
|
| 75 |
* hunmonk's module dependency check: see http://drupal.org/node/54463
|
| 76 |
*/
|
| 77 |
function workflow_access_system_module_validate(&$form) {
|
| 78 |
$module = 'workflow_access';
|
| 79 |
$dependencies = array('na_arbitrator', 'workflow');
|
| 80 |
foreach ($dependencies as $dependency) {
|
| 81 |
if (!in_array($dependency, $form['status']['#default_value'])) {
|
| 82 |
$missing_dependency = TRUE;
|
| 83 |
$missing_dependency_list[] = $dependency;
|
| 84 |
}
|
| 85 |
}
|
| 86 |
if (in_array($module, $form['status']['#default_value']) && isset($missing_dependency)) {
|
| 87 |
db_query("UPDATE {system} SET status = 0 WHERE type = 'module' AND name = '%s'", $module);
|
| 88 |
$key = array_search($module, $form['status']['#default_value']);
|
| 89 |
unset($form['status']['#default_value'][$key]);
|
| 90 |
drupal_set_message(t('The module %module was deactivated--it requires the following disabled/non-existent modules to function properly: %dependencies', array('%module' => $module, '%dependencies' => implode(', ', $missing_dependency_list))), 'error');
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
| 94 |
/**
|
| 95 |
* Add our items to the workflow edit form.
|
| 96 |
*/
|
| 97 |
function workflow_access_workflow_form($form_id, &$form) {
|
| 98 |
$wid = $form['wid']['#value'];
|
| 99 |
$rids = array('-1' => t('author'));
|
| 100 |
$result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name");
|
| 101 |
while ($obj = db_fetch_object($result)) {
|
| 102 |
$rids[$obj->rid] = $obj->name;
|
| 103 |
}
|
| 104 |
|
| 105 |
$states = workflow_get_states($wid);
|
| 106 |
|
| 107 |
$form['workflow_access'] = array('#type' => 'fieldset',
|
| 108 |
'#title' => t('Access control'),
|
| 109 |
'#collapsible' => TRUE,
|
| 110 |
'#tree' => TRUE,
|
| 111 |
);
|
| 112 |
|
| 113 |
foreach ($states as $sid => $state) {
|
| 114 |
$view = $update = $delete = array();
|
| 115 |
|
| 116 |
$result = db_query("SELECT * from {workflow_access} where sid=%d", $sid);
|
| 117 |
if (db_num_rows($result) == 0) {
|
| 118 |
$view = array(1, 2);
|
| 119 |
}
|
| 120 |
|
| 121 |
while ($fa = db_fetch_object($result)) {
|
| 122 |
if ($fa->grant_view) {
|
| 123 |
$view[] = $fa->rid;
|
| 124 |
}
|
| 125 |
if ($fa->grant_update) {
|
| 126 |
$update[] = $fa->rid;
|
| 127 |
}
|
| 128 |
if ($fa->grant_delete) {
|
| 129 |
$delete[] = $fa->rid;
|
| 130 |
}
|
| 131 |
}
|
| 132 |
|
| 133 |
$form['workflow_access'][$sid] = array('#type' => 'fieldset',
|
| 134 |
'#title' => $state,
|
| 135 |
'#collapsible' => TRUE,
|
| 136 |
'#tree' => TRUE,
|
| 137 |
);
|
| 138 |
|
| 139 |
$form['workflow_access'][$sid]['view'] = array('#type' => 'checkboxes',
|
| 140 |
'#options' => $rids,
|
| 141 |
'#default_value' => $view,
|
| 142 |
'#title' => t('Roles who can view posts in this state'),
|
| 143 |
'#prefix' => "<table width=100% border=0><tr><td>",
|
| 144 |
);
|
| 145 |
|
| 146 |
$form['workflow_access'][$sid]['update'] = array('#type' => 'checkboxes',
|
| 147 |
'#options' => $rids,
|
| 148 |
'#default_value' => $update,
|
| 149 |
'#title' => t('Roles who can edit posts in this state'),
|
| 150 |
'#prefix' => "</td><td>",
|
| 151 |
);
|
| 152 |
|
| 153 |
$form['workflow_access'][$sid]['delete'] = array('#type' => 'checkboxes',
|
| 154 |
'#options' => $rids,
|
| 155 |
'#default_value' => $delete,
|
| 156 |
'#title' => t('Roles who can delete posts in this state'),
|
| 157 |
'#prefix' => "</td><td>",
|
| 158 |
'#suffix' => "</td></tr></table>",
|
| 159 |
);
|
| 160 |
}
|
| 161 |
// Move some stuff down so our block goes in a nice place.
|
| 162 |
$form['submit']['#weight'] = 10;
|
| 163 |
|
| 164 |
$form['#submit']['workflow_access_form_submit'] = current($form['#submit']);
|
| 165 |
$form['#submit']['workflow_access_form_submit'][0] = 'workflow_access_form_submit';
|
| 166 |
}
|
| 167 |
|
| 168 |
function workflow_access_form_submit($form_id, $form_values) {
|
| 169 |
foreach($form_values['workflow_access'] as $sid => $access) {
|
| 170 |
if (!is_numeric($sid)) {
|
| 171 |
continue; // ignore keys that ain't us.
|
| 172 |
}
|
| 173 |
$grants = array();
|
| 174 |
|
| 175 |
db_query("DELETE FROM {workflow_access} WHERE sid = %d", $sid);
|
| 176 |
foreach($access['view'] as $rid => $checked) {
|
| 177 |
if ($rid == -1) {
|
| 178 |
$realm = 'workflow_access_owner';
|
| 179 |
$gid = $node->uid;
|
| 180 |
}
|
| 181 |
else {
|
| 182 |
$realm = 'workflow_access';
|
| 183 |
$gid = $rid;
|
| 184 |
}
|
| 185 |
$grants[] = array(
|
| 186 |
'realm' => $realm,
|
| 187 |
'gid' => $gid,
|
| 188 |
'grant_view' => (bool) $checked,
|
| 189 |
'grant_update' => (bool) $access['update'][$rid],
|
| 190 |
'grant_delete' => (bool) $access['delete'][$rid],
|
| 191 |
);
|
| 192 |
|
| 193 |
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]);
|
| 194 |
}
|
| 195 |
|
| 196 |
// mass update
|
| 197 |
$result = db_query("SELECT n.nid FROM {node} n LEFT JOIN {workflow_node} wn ON wn.nid = n.nid WHERE wn.sid = %d", $sid);
|
| 198 |
while ($node = db_fetch_object($result)) {
|
| 199 |
na_arbitrator_acquire_grants(node_load($node->nid), $grants, 'workflow_access');
|
| 200 |
}
|
| 201 |
}
|
| 202 |
}
|
| 203 |
|
| 204 |
/**
|
| 205 |
* be sure to update grants when a node changes workflow; we had at one
|
| 206 |
* point set that to call node_save, but that turned out to be the
|
| 207 |
* wrong behavior.
|
| 208 |
*/
|
| 209 |
function workflow_access_workflow($op, $old_sid, $sid, $node) {
|
| 210 |
if ($op == 'transition post' && $old_sid != $sid) {
|
| 211 |
na_arbitrator_acquire_grants($node);
|
| 212 |
}
|
| 213 |
}
|