Projet

Général

Profil

Paste
Télécharger (4,61 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / includes / entity-access.inc @ 7e72b748

1
<?php
2

    
3
/**
4
 * @file
5
 * Provides various callbacks for the whole core module integration.
6
 * This is a copy of Entity API's functionality for use when Entity API isn't
7
 * Enabled, and only works on view functions.
8
 */
9

    
10
/**
11
 * Core hack to include entity api-esque 'access callback' functions to core
12
 * entities without needing to rely on entity api.
13
 * Exception: We don't touch file entity. You must have entity API enabled to
14
 * view files.
15
 */
16
function _ctools_entity_access(&$entity_info, $entity_type) {
17
  // If the access callback is already set, don't change anything.
18
  if (isset($entity_info['access callback'])) {
19
    return;
20
  }
21

    
22
  switch ($entity_type) {
23
    case 'node':
24
      // Sad panda, we don't use Entity API, lets manually add access callbacks.
25
      $entity_info['access callback'] = 'ctools_metadata_no_hook_node_access';
26
      break;
27

    
28
    case 'user':
29
      $entity_info['access callback'] = 'ctools_metadata_user_access';
30
      break;
31

    
32
    case 'comment':
33
      if (module_exists('comment')) {
34
        $entity_info['access callback'] = 'ctools_metadata_comment_access';
35
      }
36
      break;
37

    
38
    case 'taxonomy_term':
39
      if (module_exists('taxonomy')) {
40
        $entity_info['access callback'] = 'ctools_metadata_taxonomy_access';
41
      }
42
      break;
43

    
44
    case 'taxonomy_vocabulary':
45
      if (module_exists('taxonomy')) {
46
        $entity_info['access callback'] = 'ctools_metadata_taxonomy_access';
47
      }
48
      break;
49
  }
50
}
51

    
52
/**
53
 * Access callback for the node entity.
54
 *
55
 * This function does not implement hook_node_access(), thus it may not be
56
 * called ctools_metadata_node_access().
57
 *
58
 * @see entity_access()
59
 *
60
 * @param $op
61
 *   The operation being performed. One of 'view', 'update', 'create' or
62
 *   'delete'.
63
 * @param $node
64
 *   A node to check access for. Must be a node object. Must have nid,
65
 *   except in the case of 'create' operations.
66
 * @param $account
67
 *   The user to check for. Leave it to NULL to check for the global user.
68
 *
69
 * @throws EntityMalformedException
70
 *
71
 * @return bool
72
 *   TRUE if access is allowed, FALSE otherwise.
73
 */
74
function ctools_metadata_no_hook_node_access($op, $node = NULL, $account = NULL) {
75
  // First deal with the case where a $node is provided.
76
  if (isset($node)) {
77
    // If a non-default revision is given, incorporate revision access.
78
    $default_revision = node_load($node->nid);
79
    if ($node->vid !== $default_revision->vid) {
80
      return _node_revision_access($node, $op, $account);
81
    }
82
    else {
83
      return node_access($op, $node, $account);
84
    }
85
  }
86
  // No node is provided. Check for access to all nodes.
87
  if (user_access('bypass node access', $account)) {
88
    return TRUE;
89
  }
90
  if (!user_access('access content', $account)) {
91
    return FALSE;
92
  }
93
  if ($op == 'view' && node_access_view_all_nodes($account)) {
94
    return TRUE;
95
  }
96
  return FALSE;
97
}
98

    
99
/**
100
 * Access callback for the user entity.
101
 */
102
function ctools_metadata_user_access($op, $entity = NULL, $account = NULL, $entity_type) {
103
  $account = isset($account) ? $account : $GLOBALS['user'];
104
  // Grant access to the users own user account and to the anonymous one.
105
  if (isset($entity) && $op != 'delete' && (($entity->uid == $account->uid && $entity->uid) || (!$entity->uid && $op == 'view'))) {
106
    return TRUE;
107
  }
108
  if (user_access('administer users', $account) || user_access('access user profiles', $account) && $op == 'view' && $entity->status) {
109
    return TRUE;
110
  }
111
  return FALSE;
112
}
113

    
114
/**
115
 * Access callback for the comment entity.
116
 */
117
function ctools_metadata_comment_access($op, $entity = NULL, $account = NULL) {
118
  // When determining access to a comment, if comment has an associated node,
119
  // the user must be able to view the node in order to access the comment.
120
  if (isset($entity->nid)) {
121
    if (!node_access('view', node_load($entity->nid), $account)) {
122
      return FALSE;
123
    }
124
  }
125

    
126
  // Comment administrators are allowed to perform all operations on all
127
  // comments.
128
  if (user_access('administer comments', $account)) {
129
    return TRUE;
130
  }
131

    
132
  // Unpublished comments can never be accessed by non-admins.
133
  if (isset($entity->status) && $entity->status == COMMENT_NOT_PUBLISHED) {
134
    return FALSE;
135
  }
136

    
137
  if (user_access('access comments', $account) && $op == 'view') {
138
    return TRUE;
139
  }
140
  return FALSE;
141
}
142

    
143
/**
144
 * Access callback for the taxonomy entities.
145
 */
146
function ctools_metadata_taxonomy_access($op, $entity = NULL, $account = NULL, $entity_type) {
147
  if ($entity_type == 'taxonomy_vocabulary') {
148
    return user_access('administer taxonomy', $account);
149
  }
150
  if (user_access('administer taxonomy', $account) || user_access('access content', $account) && $op == 'view') {
151
    return TRUE;
152
  }
153
  return FALSE;
154
}