comparison sites/all/modules/ctools/plugins/access/node_access.inc @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ff03f76ab3fe
1 <?php
2
3 /**
4 * @file
5 * Plugin to provide access control based upon node type.
6 */
7
8 /**
9 * Plugins are described by creating a $plugin array which will be used
10 * by the system that includes this file.
11 */
12 $plugin = array(
13 'title' => t("Node: accessible"),
14 'description' => t('Control access with built in Drupal node access test.'),
15 'callback' => 'ctools_node_access_ctools_access_check',
16 'default' => array('type' => 'view'),
17 'settings form' => 'ctools_node_access_ctools_access_settings',
18 'settings form submit' => 'ctools_node_access_ctools_access_settings_submit',
19 'summary' => 'ctools_node_access_ctools_access_summary',
20 'required context' => array(
21 new ctools_context_required(t('User'), 'user'),
22 new ctools_context_required(t('Node'), 'node'),
23 ),
24 );
25
26 /**
27 * Settings form for the 'by node_access' access plugin
28 */
29 function ctools_node_access_ctools_access_settings($form, &$form_state, $conf) {
30 $form['settings']['type'] = array(
31 '#title' => t('Operation'),
32 '#type' => 'radios',
33 '#options' => array(
34 'view' => t('View'),
35 'update' => t('Update'),
36 'delete' => t('Delete'),
37 'create' => t('Create nodes of the same type'),
38 ),
39 '#description' => t('Using built in Drupal node access rules, determine if the user can perform the selected operation on the node.'),
40 '#default_value' => $conf['type'],
41 );
42 return $form;
43 }
44
45 /**
46 * Check for access.
47 */
48 function ctools_node_access_ctools_access_check($conf, $context) {
49 // As far as I know there should always be a context at this point, but this
50 // is safe.
51 list($user_context, $node_context) = $context;
52 if (empty($node_context) || empty($node_context->data) || empty($node_context->data->type)) {
53 return FALSE;
54 }
55
56 if (empty($user_context) || empty($user_context->data)) {
57 return FALSE;
58 }
59
60 if ($conf['type'] == 'create') {
61 return node_access('create', $node_context->data->type, $user_context->data);
62 }
63 else {
64 return node_access($conf['type'], $node_context->data, $user_context->data);
65 }
66 }
67
68 /**
69 * Provide a summary description based upon the checked node_accesss.
70 */
71 function ctools_node_access_ctools_access_summary($conf, $context) {
72 list($user_context, $node_context) = $context;
73 $replacement = array('@user' => $user_context->identifier, '@node' => $node_context->identifier);
74
75 switch ($conf['type']) {
76 case 'view':
77 return t('@user can view @node.', $replacement);
78
79 case 'update':
80 return t('@user can edit @node.', $replacement);
81
82 case 'delete':
83 return t('@user can delete @node.', $replacement);
84
85 case 'create':
86 return t('@user can create nodes of the same type as @node.', $replacement);
87 }
88 }
89