| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @file 5 * Rules specific functions that expose content_access' API. 6 */ 7 8 9 /** 10 * Adds the role based settings to the form. 11 */ 12 function content_access_rules_role_based_form($settings, &$form) { 13 module_load_include('inc', 'content_access', 'content_access.admin'); 14 $form['#includes'][] = './'. drupal_get_path('module', 'content_access') .'/content_access.admin.inc'; 15 $form += content_access_role_based_form($settings); 16 } 17 18 /** 19 * Parse submitted settings for per-node form into internal format. 20 */ 21 function content_access_parse_settings(&$settings, $form, $form_state) { 22 foreach (_content_access_get_operations() as $op) { 23 $settings[$op] = array_keys(array_filter($form_state['values'][$op])); 24 } 25 } 26 27 /** 28 * Verifies that per content settings are activated for the given node. 29 */ 30 function _content_access_rules_check_setting($node) { 31 if (!content_access_get_settings('per_node', $node->type)) { 32 rules_log(t("Can't set per content permissions for content type @type. Make sure to have per content settings activated for content types you want to alter access control for.", array('@type' => node_get_types('name', $node->type))), TRUE); 33 return FALSE; 34 } 35 return TRUE; 36 } 37 38 39 /** 40 * Implementation of hook_action_info(). 41 */ 42 function content_access_rules_action_info() { 43 $items = array( 44 'content_access_action_grant_node_permissions' => array( 45 'label' => t('Grant content permissions by role'), 46 'arguments' => array( 47 'node' => array('type' => 'node', 'label' => t('Content')), 48 ), 49 'module' => 'Content access', 50 ), 51 'content_access_action_revoke_node_permissions' => array( 52 'label' => t('Revoke content permissions by role'), 53 'arguments' => array( 54 'node' => array('type' => 'node', 'label' => t('Content')), 55 ), 56 'module' => 'Content access', 57 ), 58 'content_access_action_reset_node_permissions' => array( 59 'label' => t('Reset content permissions'), 60 'arguments' => array( 61 'node' => array('type' => 'node', 'label' => t('Content')), 62 ), 63 'module' => 'Content access', 64 ), 65 ); 66 if (module_exists('acl')) { 67 $items += array( 68 'content_access_action_acl_grant_access' => array( 69 'label' => t('Grant access for a user'), 70 'arguments' => array( 71 'node' => array('type' => 'node', 'label' => t('Content')), 72 'user' => array('type' => 'user', 'label' => t('User')), 73 ), 74 'module' => 'Content access', 75 ), 76 'content_access_action_acl_revoke_access' => array( 77 'label' => t('Revoke access for a user'), 78 'arguments' => array( 79 'node' => array('type' => 'node', 'label' => t('Content')), 80 'user' => array('type' => 'user', 'label' => t('User')), 81 ), 82 'module' => 'Content access', 83 ), 84 ); 85 } 86 return $items; 87 } 88 89 /** 90 * Action implementation: Grant permissions for a node. 91 */ 92 function content_access_action_grant_node_permissions($node, $settings) { 93 if (_content_access_rules_check_setting($node)) { 94 $ca_settings = array(); 95 foreach (_content_access_get_operations() as $op) { 96 $settings += array($op => array()); 97 $ca_settings[$op] = array_keys(array_flip(content_access_per_node_setting($op, $node)) + array_flip($settings[$op])); 98 } 99 content_access_save_per_node_settings($node, $ca_settings); 100 // A following node_save() updates the grants for us. 101 return array('node' => $node); 102 } 103 } 104 105 function content_access_action_grant_node_permissions_form($settings, &$form) { 106 content_access_rules_role_based_form($settings, $form); 107 } 108 function content_access_action_grant_node_permissions_submit(&$settings, $form, $form_state) { 109 content_access_parse_settings($settings, $form, $form_state); 110 } 111 112 /** 113 * Action implementation: Revoke permissions for a node. 114 */ 115 function content_access_action_revoke_node_permissions($node, $settings) { 116 if (_content_access_rules_check_setting($node)) { 117 $ca_settings = array(); 118 foreach (_content_access_get_operations() as $op) { 119 $settings += array($op => array()); 120 $ca_settings[$op] = array_diff(content_access_per_node_setting($op, $node), $settings[$op]); 121 } 122 content_access_save_per_node_settings($node, $ca_settings); 123 // A following node_save() updates the grants for us. 124 return array('node' => $node); 125 } 126 } 127 128 function content_access_action_revoke_node_permissions_form($settings, &$form) { 129 content_access_rules_role_based_form($settings, $form); 130 } 131 function content_access_action_revoke_node_permissions_submit(&$settings, $form, $form_state) { 132 content_access_parse_settings($settings, $form, $form_state); 133 } 134 135 136 /** 137 * Action implementation: Grant access for a user. 138 */ 139 function content_access_action_acl_grant_access($node, $user, $settings) { 140 if (_content_access_rules_check_setting($node)) { 141 module_load_include('inc', 'content_access', 'content_access.admin'); 142 foreach ($settings['ops'] as $op) { 143 $acl_id = content_access_get_acl_id($node, $op); 144 acl_add_user($acl_id, $user->uid); 145 acl_node_add_acl($node->nid, $acl_id, $op == 'view', $op == 'update', $op == 'delete', content_access_get_settings('priority', $node->type)); 146 } 147 // A following node_save() updates the grants for us. 148 return array('node' => $node); 149 } 150 } 151 152 function content_access_action_acl_grant_access_form($settings, &$form) { 153 $settings += array('ops' => array('view', 'update', 'delete')); 154 $form['settings']['ops'] = array( 155 '#type' => 'checkboxes', 156 '#options' => drupal_map_assoc(array('view', 'update', 'delete')), 157 '#title' => t('Operations to grant access for'), 158 '#default_value' => $settings['ops'], 159 ); 160 } 161 function content_access_action_acl_grant_access_submit(&$settings, $form, $form_state) { 162 $settings['ops'] = array_filter($settings['ops']); 163 } 164 function content_access_action_acl_grant_access_help() { 165 return t("Note that this action is not going to revoke access for not chosen operations."); 166 } 167 function content_access_action_acl_grant_access_label($settings, $argument_labels) { 168 return t('Grant access for @user.', $argument_labels); 169 } 170 171 172 /** 173 * Action implementation: Revoke access for a user. 174 */ 175 function content_access_action_acl_revoke_access($node, $user, $settings) { 176 if (_content_access_rules_check_setting($node)) { 177 module_load_include('inc', 'content_access', 'content_access.admin'); 178 foreach ($settings['ops'] as $op) { 179 $acl_id = content_access_get_acl_id($node, $op); 180 acl_remove_user($acl_id, $user->uid); 181 } 182 // A following node_save() updates the grants for us. 183 return array('node' => $node); 184 } 185 } 186 187 function content_access_action_acl_revoke_access_form($settings, &$form) { 188 $settings += array('ops' => array('view', 'update', 'delete')); 189 $form['settings']['ops'] = array( 190 '#type' => 'checkboxes', 191 '#options' => drupal_map_assoc(array('view', 'update', 'delete')), 192 '#title' => t('Operations to revoke access for'), 193 '#default_value' => $settings['ops'], 194 ); 195 } 196 function content_access_action_acl_revoke_access_submit(&$settings, $form, $form_state) { 197 $settings['ops'] = array_filter($settings['ops']); 198 } 199 function content_access_action_acl_revoke_access_help() { 200 return t("Note that this action is only able to revoke access that has been previously granted with the help of the content access module."); 201 } 202 function content_access_action_acl_revoke_access_label($settings, $argument_labels) { 203 return t('Revoke access for @user.', $argument_labels); 204 } 205 206 207 /** 208 * Action implementation: Reset permissions for a node. 209 */ 210 function content_access_action_reset_node_permissions($node) { 211 if (_content_access_rules_check_setting($node)) { 212 content_access_delete_per_node_settings($node); 213 // A following node_save() updates the grants for us. 214 return array('node' => $node); 215 } 216 } 217 218 /** 219 * Implementation of hook_condition_info(). 220 */ 221 function content_access_rules_condition_info() { 222 return array( 223 'content_access_condition_check_permissions' => array( 224 'label' => t('Check role based settings'), 225 'arguments' => array( 226 'node' => array('type' => 'node', 'label' => t('Content')), 227 ), 228 'help' => t('Evaluates to TRUE, if content access allows all selected operations for the given roles.'), 229 'module' => 'Content access', 230 ), 231 ); 232 } 233 234 /** 235 * Condition implementation: Check if node has permissions. 236 * 237 * @note 238 * This will only check for the existence of permissions, not the 239 * absence of. I.e. a rule that checks just for authenticated write 240 * will return TRUE for a node that allows authenticated and anonymous 241 * write. 242 */ 243 function content_access_condition_check_permissions($node, $settings) { 244 if (_content_access_rules_check_setting($node)) { 245 // Compare our settings with node's settings. 246 foreach (_content_access_get_operations() as $op) { 247 $settings += array($op => array()); 248 $expect_roles = $settings[$op]; 249 $current_roles = content_access_per_node_setting($op, $node); 250 if (count(array_diff($expect_roles, $current_roles)) != 0) { 251 return FALSE; 252 } 253 } 254 return TRUE; 255 } 256 return FALSE; 257 } 258 259 function content_access_condition_check_permissions_form($settings, &$form) { 260 content_access_rules_role_based_form($settings, $form); 261 } 262 function content_access_condition_check_permissions_submit(&$settings, $form, $form_state) { 263 content_access_parse_settings($settings, $form, $form_state); 264 } 265 266 /** 267 * Automatically upgrade the old 'content_access_action_set_node_permissions' to an 268 * 'content_access_action_grant_node_permissions' action. 269 */ 270 function content_access_action_set_node_permissions_upgrade(&$element) { 271 $element['#name'] = 'content_access_action_grant_node_permissions'; 272 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |