diff -urpN old/acl.module new/acl.module --- old/acl.module 2007-11-18 01:32:42.000000000 +0530 +++ new/acl.module 2007-12-24 00:52:30.343750000 +0530 @@ -9,6 +9,10 @@ * other without having to actually know much about them, and so that * ACLs can easily co-exist with the existing node_access system. */ + +if (module_exists('workflow_ng')) { + include_once(drupal_get_path('module', 'acl') .'/acl_workflow_ng.inc'); +} /** * Create a new ACL. @@ -20,14 +24,23 @@ function acl_create_new_acl($module, $na } /** - * Delete an existing ACL. + * Delete an existing ACL. + * @param $write_grants + * If TRUE it will write the node grants to the database */ -function acl_delete_acl($acl_id) { +function acl_delete_acl($acl_id, $write_grants = FALSE) { db_query("DELETE FROM {acl} WHERE acl_id = %d", $acl_id); - db_query("DELETE FROM {acl_user} WHERE acl_id = %d", $acl_id); - db_query("DELETE FROM {acl_node} WHERE acl_id = %d", $acl_id); + db_query("DELETE FROM {acl_user} WHERE acl_id = %d", $acl_id); + if ($write_grants) { + $result = db_query("SELECT * FROM {acl_node} WHERE acl_id = %d", $acl_id); + while ($data = db_fetch_object($result)) { + $node = node_load($data->nid); + node_access_acquire_grants($node); + } + } + db_query("DELETE FROM {acl_node} WHERE acl_id = %d", $acl_id); } - + /** * Add the specified UID to an ACL. */ @@ -177,14 +190,20 @@ function acl_node_add_acl($nid, $acl_id, } /** - * Remove an ACL completely from a node. + * Remove an ACL completely from a node. + * + * @return + * Return TRUE if there was actaully a node deleted from ACL. */ function acl_node_remove_acl($nid, $acl_id) { - db_query("DELETE FROM {acl_node} WHERE acl_id = %d AND nid = %d", $acl_id, $nid); + db_query("DELETE FROM {acl_node} WHERE acl_id = %d AND nid = %d", $acl_id, $nid); + return (boolean)db_affected_rows(); } /** - * Clear all of a module's ACL's from a node. + * Clear all of a module's ACL's from a node. + * @return + * TRUE if there was actaully ACL(s) deleted from a node. */ function acl_node_clear_acls($nid, $module) { $result = db_query("SELECT acl_id FROM {acl} WHERE module = '%s'", $module); @@ -193,15 +212,24 @@ function acl_node_clear_acls($nid, $modu } if ($acls) { db_query("DELETE FROM {acl_node} WHERE nid = %d AND acl_id in (%s)", $nid, - implode(',', $acls)); + implode(',', $acls)); + return (boolean)db_affected_rows(); } } /** - * Gets the id of an acl + * Gets the id of an acl + * @param $auto_create + * If TRUE it will create a new acl if an acl not found + * @return + * The acl */ -function acl_get_id_by_name($module, $name) { - return db_result(db_query("SELECT acl_id FROM {acl} WHERE module = '%s' AND name = '%s'", $module, $name)); +function acl_get_id_by_name($module, $name, $auto_create = FALSE) { + $acl_id = db_result(db_query("SELECT acl_id FROM {acl} WHERE module = '%s' AND name = '%s'", $module, $name)); + if (($auto_create) && (!($acl_id))) { + $acl_id = acl_create_new_acl('acl', $name); + } + return $acl_id; } /** diff -urpN old/acl_workflow_ng.inc new/acl_workflow_ng.inc --- old/acl_workflow_ng.inc 1970-01-01 05:30:00.000000000 +0530 +++ new/acl_workflow_ng.inc 2007-12-24 21:28:56.546875000 +0530 @@ -0,0 +1,297 @@ + array( + '#label' => t('Add user to ACL'), + '#arguments' => array( + 'user' => array('#entity' => 'user', '#label' => t('User which will be added to the ACL')), + ), + '#module' => t('ACL'), + ), + 'acl_workflow_ng_remove_user_from_acl' => array( + '#label' => t('Remove user from ACL'), + '#arguments' => array( + 'user' => array('#entity' => 'user', '#label' => t('User which will be removed from the ACL')), + ), + '#module' => t('ACL'), + ), + 'acl_workflow_ng_add_node_to_acl' => array( + '#label' => t('Add node to ACL'), + '#arguments' => array( + 'node' => array('#entity' => 'node', '#label' => t('Content')), + ), + '#module' => t('ACL'), + ), + 'acl_workflow_ng_remove_node_from_acl' => array( + '#label' => t('Remove node from ACL'), + '#arguments' => array( + 'node' => array('#entity' => 'node', '#label' => t('Content')), + ), + '#module' => t('ACL'), + ), + 'acl_workflow_ng_delete_whole_acl' => array( + '#label' => t('Delete ACL(s) from node'), + '#arguments' => array( + 'node' => array('#entity' => 'node', '#label' => t('Content')), + ), + '#module' => t('ACL'), + ), + 'acl_workflow_ng_delete_acl' => array( + '#label' => t('Delete ACL'), + '#module' => t('ACL'), + ), + ); +} + + +/* + * Action: Add a user to ACL + */ +function acl_workflow_ng_add_user_to_acl($user, $settings, &$arguments, &$log) { + $token = workflow_ng_token_replace_all(array('acl'), $settings, $arguments, $log); + //Make sure ACL exists, or create a new one + $acl_id = acl_get_id_by_name('acl', $token['acl'], TRUE); + acl_add_user($acl_id, $user->uid); +} + +/* + * Form builder for action - Add a user to ACL form + * + * @ingroup forms + * @see acl_workflow_ng_add_user_to_acl_submit + * + */ +function acl_workflow_ng_add_user_to_acl_form($settings = array(), $argument_info) { + $form = array(); + $form['acl'] = array( + '#type' => 'textfield', + '#title' => t('ACL name'), + '#description' => t('Enter the name of the ACL. You may have a single user/ node assigned to several ACL lists'), + '#default_value' => $settings['acl'], + '#required' => TRUE, + ); + workflow_ng_token_replacement_help($form, $argument_info); + return $form; +} + +function acl_workflow_ng_add_user_to_acl_submit($form_id, $form_values) { + $token = workflow_ng_token_get_settings(array('acl'), $form_values); + $settings = array('acl' => $form_values['acl']); + return $token + $settings; +} + +/* + * Action: Remove a user from ACL + */ +function acl_workflow_ng_remove_user_from_acl($user, $settings, &$arguments, &$log) { + //Make sure ACL exists + $token = workflow_ng_token_replace_all(array('acl'), $settings, $arguments, $log); + $acl_id = acl_get_id_by_name('acl', $token['acl'], TRUE); + if ($acl_id) { + acl_remove_user($acl_id, $user->uid); + } +} + +/* + * Form builder for action - Remove a user from ACL + * + * @ingroup forms + * @see acl_workflow_ng_remove_user_from_acl_submit + */ + +function acl_workflow_ng_remove_user_from_acl_form($settings = array(), $argument_info) { + $form = array(); + $form['acl'] = array( + '#type' => 'textfield', + '#title' => t('ACL name'), + '#description' => t('Enter the name of the ACL.'), + '#default_value' => $settings['acl'], + '#required' => TRUE, + ); + workflow_ng_token_replacement_help($form, $argument_info); + return $form; +} + +function acl_workflow_ng_remove_user_from_acl_submit($form_id, $form_values) { + $token = workflow_ng_token_get_settings(array('acl'), $form_values); + $settings = array('acl' => $form_values['acl']); + return $token + $settings; +} + + +/* + * Action: Add a node to ACL + */ +function acl_workflow_ng_add_node_to_acl($node, $settings, &$arguments, &$log) { + //Make sure ACL exists, or create a new one + $token = workflow_ng_token_replace_all(array('acl'), $settings, $arguments, $log); + $acl_id = acl_get_id_by_name('acl', $token['acl'], TRUE); + + //TODO: Check if node not already in ACL + acl_node_add_acl($node->nid, $acl_id, (boolean)$settings['access_permissions']['view'], (boolean)$settings['access_permissions']['edit'], (boolean)$settings['access_permissions']['delete']); + //Make sure new node access grants are written. + return array('node' => $node); +} + +/* + * Form builder for action - Add a node from ACL + * + * @ingroup forms + * @see acl_workflow_ng_add_node_to_acl_submit + */ +function acl_workflow_ng_add_node_to_acl_form($settings = array(), $argument_info) { + $form = array(); + $form['acl'] = array( + '#type' => 'textfield', + '#title' => t('ACL name'), + '#description' => t('Enter the name of the ACL.'), + '#default_value' => $settings['acl'], + '#required' => TRUE, + ); + workflow_ng_token_replacement_help($form, $argument_info); + $form['access_permissions'] = array( + '#type' => 'checkboxes', + '#title' => t('Access permissions'), + '#description' => t('Select the operations users will have access rights to, when added to the ACL.'), + '#default_value' => $settings['access_permissions'], + '#options' => array( + 'view' => t('View'), + 'edit' => t('Edit'), + 'delete' => t('Delete'), + ), + ); + return $form; +} + +function acl_workflow_ng_add_node_to_acl_submit($form_id, $form_values) { + $token = workflow_ng_token_get_settings(array('acl'), $form_values); + $settings = array('acl' => $form_values['acl'], 'access_permissions' => $form_values['access_permissions']); + return $token + $settings; +} + +/* + * Action: Remove a node from ACL + */ +function acl_workflow_ng_remove_node_from_acl($node, $settings, &$arguments, &$log) { + //Make sure ACL exists + $token = workflow_ng_token_replace_all(array('acl'), $settings, $arguments, $log); + $acl_id = acl_get_id_by_name('acl', $token['acl']); + if ($acl_id) { + $node_exists = acl_node_remove_acl($node->nid, $acl_id); + if ($node_exists) { + //Make sure new node access grants are written. + return array('node' => $node); + } + } +} + +/* + * Form builder for action - Remove a node from ACL + * + * @ingroup forms + * @see acl_workflow_ng_remove_node_from_acl_submit + */ +function acl_workflow_ng_remove_node_from_acl_form($settings = array(), $argument_info) { + $form = array(); + $form['acl'] = array( + '#type' => 'textfield', + '#title' => t('ACL name'), + '#description' => t('Enter the name of the ACL.'), + '#default_value' => $settings['acl'], + '#required' => TRUE, + ); + workflow_ng_token_replacement_help($form, $argument_info); + return $form; +} + +function acl_workflow_ng_remove_node_from_acl_submit($form_id, $form_values) { + $token = workflow_ng_token_get_settings(array('acl'), $form_values); + $settings = array('acl' => $form_values['acl']); + return $token + $settings; +} + +/* + * Action: Delete ACL(s) from node + */ +function acl_workflow_ng_delete_whole_acl($node, $settings, &$arguments, &$log) { + //Make sure ACL exists + $node_exists = acl_node_clear_acls($node->nid, 'acl'); + if ($node_exists) { + //Make sure new node access grants are written. + return array('node' => $node); + } +} + + +/* + * Action: Delete ACL + */ +function acl_workflow_ng_delete_acl($settings, &$arguments, &$log) { + //Make sure ACL exists + $token = workflow_ng_token_replace_all(array('acl'), $settings, $arguments, $log); + $acl_id = acl_get_id_by_name('acl', $token['acl']); + if ($acl_id) { + //ACL will take care of writing the node grants to the database. + acl_delete_acl($acl_id, TRUE); + } +} + +/* + * Form builder for action - Delete ACL + * + * @ingroup forms + * @see acl_workflow_ng_delete_acl_submit + */ +function acl_workflow_ng_delete_acl_form($settings = array(), $argument_info) { + $form = array(); + $form['acl'] = array( + '#type' => 'textfield', + '#title' => t('ACL name'), + '#description' => t('Enter the name of the ACL.'), + '#default_value' => $settings['acl'], + '#required' => TRUE, + ); + workflow_ng_token_replacement_help($form, $argument_info); + return $form; +} + +function acl_workflow_ng_delete_acl_submit($form_id, $form_values) { + $token = workflow_ng_token_get_settings(array('acl'), $form_values); + $settings = array('acl' => $form_values['acl']); + return $token + $settings; +} + +/* + * Used by the ACL module + */ +function acl_enabled() { + return !acl_disabling(); +} + +/* + * Remembers if we have disabled access + */ +function acl_disabling($set = NULL) { + static $disabling = FALSE; + + if (isset($set)) { + $disabling = $set; + } + return $disabling; +} + + + +