| 1 |
<?php |
<?php |
| 2 |
// $Id: nodeaccess.module,v 1.4.2.7.2.10 2008/07/21 21:51:50 mantyla Exp $ |
// $Id: nodeaccess.module,v 1.4.2.7.2.9.2.1 2008/07/21 23:28:46 mantyla Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* Implementation of hook_help(). |
* Implementation of hook_help(). |
| 169 |
$form['nodeaccess'][$type]['roles'][$id]['grant_update'] = array('#type' => 'checkbox', '#default_value' => $opts[$id]['grant_update']); |
$form['nodeaccess'][$type]['roles'][$id]['grant_update'] = array('#type' => 'checkbox', '#default_value' => $opts[$id]['grant_update']); |
| 170 |
$form['nodeaccess'][$type]['roles'][$id]['grant_delete'] = array('#type' => 'checkbox', '#default_value' => $opts[$id]['grant_delete']); |
$form['nodeaccess'][$type]['roles'][$id]['grant_delete'] = array('#type' => 'checkbox', '#default_value' => $opts[$id]['grant_delete']); |
| 171 |
} |
} |
| 172 |
|
|
| 173 |
|
// Set the default permissions if userreference exists and is enabled on |
| 174 |
|
// the content type. |
| 175 |
|
if (module_exists('userreference')) { |
| 176 |
|
$type_info = content_types($type); |
| 177 |
|
$fields = $type_info['fields']; |
| 178 |
|
|
| 179 |
|
$userreference_perms = variable_get('nodeaccess_' . $type . '_userreference', array()); |
| 180 |
|
foreach ($fields as $field) { |
| 181 |
|
if ($field['type'] == 'userreference') { |
| 182 |
|
$form['nodeaccess'][$type]['userreference'][$field['field_name']]['name'] = array('#value' => $field['widget']['label']); |
| 183 |
|
$form['nodeaccess'][$type]['userreference'][$field['field_name']]['enabled'] = array('#type' => 'checkbox', '#default_value' => $userreference_perms[$field['field_name']]['enabled']); |
| 184 |
|
$form['nodeaccess'][$type]['userreference'][$field['field_name']]['grant_view'] = array('#type' => 'checkbox', '#default_value' => $userreference_perms[$field['field_name']]['grant_view']); |
| 185 |
|
$form['nodeaccess'][$type]['userreference'][$field['field_name']]['grant_update'] = array('#type' => 'checkbox', '#default_value' => $userreference_perms[$field['field_name']]['grant_update']); |
| 186 |
|
$form['nodeaccess'][$type]['userreference'][$field['field_name']]['grant_delete'] = array('#type' => 'checkbox', '#default_value' => $userreference_perms[$field['field_name']]['grant_delete']); |
| 187 |
|
} |
| 188 |
|
} |
| 189 |
|
} |
| 190 |
} |
} |
| 191 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save Grants')); |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save Grants')); |
| 192 |
return $form; |
return $form; |
| 237 |
$allowed_types[$type] = 0; |
$allowed_types[$type] = 0; |
| 238 |
} |
} |
| 239 |
$author_prefs[$type] = $form_values[$type]['author']; |
$author_prefs[$type] = $form_values[$type]['author']; |
| 240 |
|
// Also save userreference default permissions if enabled. |
| 241 |
|
if (module_exists('userreference') && isset($form_values[$type]['userreference'])) { |
| 242 |
|
$userreference_grants = array(); |
| 243 |
|
foreach ($form_values[$type]['userreference'] as $userreference_field => $val) { |
| 244 |
|
$userreference_grants[$userreference_field] = array('gid' => 'nodeaccess_uid', 'enabled' => $val['enabled'], 'grant_view' => $val['grant_view'], 'grant_update' => $val['grant_update'], 'grant_delete' => $val['grant_delete']); |
| 245 |
|
} |
| 246 |
|
variable_set('nodeaccess_' . $type . '_userreference', $userreference_grants); |
| 247 |
|
} |
| 248 |
} |
} |
| 249 |
variable_set('nodeaccess_authors', $author_prefs); |
variable_set('nodeaccess_authors', $author_prefs); |
| 250 |
// Save allowed node types. |
// Save allowed node types. |
| 293 |
|
|
| 294 |
$output .= theme('table', $header, array($row)); |
$output .= theme('table', $header, array($row)); |
| 295 |
$output .= '<small>' . t('The settings selected for the node author will define what permissions the node author has. This cannot be changed on individual node grants.') . '</small>'; |
$output .= '<small>' . t('The settings selected for the node author will define what permissions the node author has. This cannot be changed on individual node grants.') . '</small>'; |
| 296 |
|
|
| 297 |
|
if (module_exists('userreference') && isset($form['userreference'])) { |
| 298 |
|
$userreference_fields = element_children($form['userreference']); |
| 299 |
|
$header = array(t('User Reference Field'), t('Enable this field'), t('View'), t('Edit'), t('Delete')); |
| 300 |
|
$rows = array(); |
| 301 |
|
foreach ($userreference_fields as $userreference_field) { |
| 302 |
|
$row = array(); |
| 303 |
|
$row[] = drupal_render($form['userreference'][$userreference_field]['name']); |
| 304 |
|
$row[] = drupal_render($form['userreference'][$userreference_field]['enabled']); |
| 305 |
|
$row[] = drupal_render($form['userreference'][$userreference_field]['grant_view']); |
| 306 |
|
$row[] = drupal_render($form['userreference'][$userreference_field]['grant_update']); |
| 307 |
|
$row[] = drupal_render($form['userreference'][$userreference_field]['grant_delete']); |
| 308 |
|
$rows[] = $row; |
| 309 |
|
} |
| 310 |
|
$output .= theme('table', $header, $rows); |
| 311 |
|
$output .= '<small>' . t('If enabled, the value of the user reference field will be granted the associated permissions. If a user changes the value of the user reference field on a node, the associated user will be modified in the node-specific access table.') . '</small>'; |
| 312 |
|
} |
| 313 |
return $output; |
return $output; |
| 314 |
} |
} |
| 315 |
|
|
| 470 |
* Submit function for nodeaccess_grants_form. |
* Submit function for nodeaccess_grants_form. |
| 471 |
*/ |
*/ |
| 472 |
function nodeaccess_grants_form_submit($form, &$form_state) { |
function nodeaccess_grants_form_submit($form, &$form_state) { |
| 473 |
|
_nodeaccess_grants_form_submit($form, $form_state); |
| 474 |
|
drupal_set_message(t('Grants saved.')); |
| 475 |
|
} |
| 476 |
|
|
| 477 |
|
/** |
| 478 |
|
* Private function to submit the per-node grants table. We use this so any |
| 479 |
|
* user notifications aren't displayed when a userreference field causes the |
| 480 |
|
* updating. |
| 481 |
|
*/ |
| 482 |
|
function _nodeaccess_grants_form_submit($form, &$form_state) { |
| 483 |
$form_values =& $form_state['values']; |
$form_values =& $form_state['values']; |
| 484 |
global $user; |
global $user; |
| 485 |
$grants = array(); |
$grants = array(); |
| 511 |
db_query("INSERT INTO {nodeaccess} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, '%s', %d, %d, %d)", |
db_query("INSERT INTO {nodeaccess} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, '%s', %d, %d, %d)", |
| 512 |
$nid, $grant['gid'], $grant['realm'], $grant['grant_view'], $grant['grant_update'], $grant['grant_delete']); |
$nid, $grant['gid'], $grant['realm'], $grant['grant_view'], $grant['grant_update'], $grant['grant_delete']); |
| 513 |
} |
} |
|
|
|
|
drupal_set_message(t('Grants saved.')); |
|
| 514 |
} |
} |
| 515 |
|
|
| 516 |
/** |
/** |
| 522 |
|
|
| 523 |
// Roles table. |
// Roles table. |
| 524 |
$roles = element_children($form['rid']); |
$roles = element_children($form['rid']); |
| 525 |
if (count($roles) > 0) { |
if (count($roles) && count($allowed_roles)) { |
| 526 |
$header = array(); |
$header = array(); |
| 527 |
$header[] = t('Role'); |
$header[] = t('Role'); |
| 528 |
if ($allowed_grants['view']) { |
if ($allowed_grants['view']) { |
| 604 |
* Implementation of hook_node_grants(). |
* Implementation of hook_node_grants(). |
| 605 |
*/ |
*/ |
| 606 |
function nodeaccess_node_grants($account, $op) { |
function nodeaccess_node_grants($account, $op) { |
| 607 |
global $user; |
$roles = is_array($account->roles) ? array_keys($account->roles) : array(-1); |
| 608 |
|
return array('nodeaccess_rid' => $roles, 'nodeaccess_uid' => array($account->uid), 'nodeaccess_author' => array($account->uid)); |
| 609 |
|
|
|
$roles = is_array($user->roles) ? array_keys($user->roles) : array(-1); |
|
|
return array('nodeaccess_rid' => $roles, 'nodeaccess_uid' => array($user->uid), 'nodeaccess_author' => array($user->uid)); |
|
| 610 |
} |
} |
| 611 |
|
|
| 612 |
/** |
/** |
| 621 |
$realm = 'nodeaccess_' . $type; |
$realm = 'nodeaccess_' . $type; |
| 622 |
node_access_write_grants($node, $grants, $realm); |
node_access_write_grants($node, $grants, $realm); |
| 623 |
} |
} |
| 624 |
|
if (module_exists('userreference')) { |
| 625 |
|
$fields = variable_get('nodeaccess_' . $node->type . '_userreference', array()); |
| 626 |
|
foreach (array_keys($fields) as $field_name) { |
| 627 |
|
if (isset($node->$field_name)) { |
| 628 |
|
nodeaccess_insert_userreference($node); |
| 629 |
|
break; |
| 630 |
|
} |
| 631 |
|
} |
| 632 |
|
} |
| 633 |
// Done, default permissions are not written into nodeaccess. |
// Done, default permissions are not written into nodeaccess. |
| 634 |
break; |
break; |
| 635 |
case 'update': |
case 'update': |
| 642 |
$grants = array(); |
$grants = array(); |
| 643 |
$grants[] = $grant; |
$grants[] = $grant; |
| 644 |
node_access_write_grants($node, $grants, 'nodeaccess_author'); |
node_access_write_grants($node, $grants, 'nodeaccess_author'); |
| 645 |
|
if (module_exists('userreference')) { |
| 646 |
|
$fields = variable_get('nodeaccess_' . $node->type . '_userreference', array()); |
| 647 |
|
foreach (array_keys($fields) as $field_name) { |
| 648 |
|
if (isset($node->$field_name)) { |
| 649 |
|
$old_node = node_load($node->nid); |
| 650 |
|
// Delete the old user as it's changed. |
| 651 |
|
if ($node->$field_name != $old_node->$field_name) { |
| 652 |
|
nodeaccess_delete_userreference($old_node); |
| 653 |
|
nodeaccess_insert_userreference($node); |
| 654 |
|
} |
| 655 |
|
break; |
| 656 |
|
} |
| 657 |
|
} |
| 658 |
|
} |
| 659 |
// Done, author permissions are not written into nodeaccess. |
// Done, author permissions are not written into nodeaccess. |
| 660 |
break; |
break; |
| 661 |
case 'delete': |
case 'delete': |
| 804 |
return $success; |
return $success; |
| 805 |
} |
} |
| 806 |
|
|
| 807 |
?> |
/** |
| 808 |
|
* Insert userreference grants from a node. |
| 809 |
|
* @param $node |
| 810 |
|
* The node with enabled userreference grants to insert. |
| 811 |
|
*/ |
| 812 |
|
function nodeaccess_insert_userreference($node) { |
| 813 |
|
$form_values = _nodeaccess_get_grants($node); |
| 814 |
|
|
| 815 |
|
// Now, append or overwrite the uid with what was specified in the user |
| 816 |
|
// reference field. |
| 817 |
|
$fields = variable_get('nodeaccess_' . $node->type . '_userreference', array()); |
| 818 |
|
foreach ($fields as $field_name => $field) { |
| 819 |
|
foreach ($node->$field_name as $values) { |
| 820 |
|
// Add the settings for this UID. If the UID is zero, then the |
| 821 |
|
// userreference field is set to None. |
| 822 |
|
if ($values['uid'] > 0) { |
| 823 |
|
$user = user_load($values['uid']); |
| 824 |
|
$form_values['uid'][$user->uid] = array('name' => $user->name, 'keep' => 1, 'grant_view' => $field['grant_view'], 'grant_update' => $field['grant_update'], 'grant_delete' => $field['grant_delete']); |
| 825 |
|
} |
| 826 |
|
} |
| 827 |
|
} |
| 828 |
|
// Only do the changes if there are users to save. |
| 829 |
|
if (count($form_values['uid']) > 0) { |
| 830 |
|
$form_values['nid'] = $node->nid; |
| 831 |
|
$form_state = array('values' => $form_values); |
| 832 |
|
_nodeaccess_grants_form_submit(NULL, $form_state); |
| 833 |
|
} |
| 834 |
|
} |
| 835 |
|
|
| 836 |
|
/** |
| 837 |
|
* Delete all userreference user grants from a node. |
| 838 |
|
* |
| 839 |
|
* @param $node |
| 840 |
|
* The node with userreference fields that need to be deleted from the user |
| 841 |
|
* grants table. |
| 842 |
|
*/ |
| 843 |
|
function nodeaccess_delete_userreference($node) { |
| 844 |
|
$form_values = _nodeaccess_get_grants($node); |
| 845 |
|
// Now, append or overwrite the uid with what was specified in the user |
| 846 |
|
// reference field. |
| 847 |
|
$fields = variable_get('nodeaccess_' . $node->type . '_userreference', array()); |
| 848 |
|
foreach ($fields as $field_name => $field) { |
| 849 |
|
foreach ($node->$field_name as $values) { |
| 850 |
|
// Add the settings for this UID |
| 851 |
|
$user = user_load($values['uid']); |
| 852 |
|
unset($form_values['uid'][$user->uid]); |
| 853 |
|
} |
| 854 |
|
} |
| 855 |
|
$form_values['nid'] = $node->nid; |
| 856 |
|
$form_state = array('values' => $form_values); |
| 857 |
|
_nodeaccess_grants_form_submit(NULL, $form_state); |
| 858 |
|
} |
| 859 |
|
|
| 860 |
|
/** |
| 861 |
|
* Return the grants applied to a node object. This is used to generate values |
| 862 |
|
* for the Grants form. |
| 863 |
|
* |
| 864 |
|
* @param $node |
| 865 |
|
* The node to return grants for. |
| 866 |
|
* @return array |
| 867 |
|
* An array of grants with keys 'rid' for roles and 'uid' for users. |
| 868 |
|
*/ |
| 869 |
|
function _nodeaccess_get_grants($node) { |
| 870 |
|
$grants = array(); |
| 871 |
|
// Load all roles. |
| 872 |
|
$result = db_query("SELECT r.rid, nra.name, na.grant_view, na.grant_update, na.grant_delete FROM {role} r LEFT JOIN {nodeaccess_role_alias} nra ON r.rid = nra.rid LEFT JOIN {node_access} na ON r.rid = na.gid AND na.realm = '%s' AND na.nid = %d ORDER BY nra.weight, nra.name", 'nodeaccess_rid', $node->nid); |
| 873 |
|
while ($grant = db_fetch_object($result)) { |
| 874 |
|
$grants['rid'][$grant->rid] = array('name' => $grant->name, 'grant_view' => (boolean)$grant->grant_view, 'grant_update' => (boolean)$grant->grant_update, 'grant_delete' => (boolean)$grant->grant_delete); |
| 875 |
|
} |
| 876 |
|
// Load users from node_access. |
| 877 |
|
$result = db_query("SELECT uid, name, grant_view, grant_update, grant_delete FROM {node_access} LEFT JOIN {users} ON uid = gid WHERE nid = %d AND realm = '%s' ORDER BY name", $node->nid, 'nodeaccess_uid'); |
| 878 |
|
while ($account = db_fetch_object($result)) { |
| 879 |
|
$grants['uid'][$account->uid] = array('name' => $account->name, 'keep' => 1, 'grant_view' => $account->grant_view, 'grant_update' => $account->grant_update, 'grant_delete' => $account->grant_delete); |
| 880 |
|
} |
| 881 |
|
return $grants; |
| 882 |
|
} |