1
|
<?php
|
2
|
/**
|
3
|
* @file
|
4
|
* Adds UUID functionality to the nodeaccess module.
|
5
|
*/
|
6
|
|
7
|
/**
|
8
|
* Implements hook_entity_uuid_load().
|
9
|
*/
|
10
|
function nodeaccess_uuid_entity_uuid_load(&$entities, $entity_type) {
|
11
|
if ($entity_type !== 'node') {
|
12
|
return;
|
13
|
}
|
14
|
|
15
|
foreach ($entities as &$entity) {
|
16
|
// Load the information.
|
17
|
$query = db_select('node_access', 'na');
|
18
|
$query->fields('na');
|
19
|
$query->addField('r', 'name', 'role_name');
|
20
|
$query->addField('u', 'uuid', 'user_uuid');
|
21
|
$query->addExpression("CONCAT(na.nid, '-', na.gid, '-', na.realm)", 'unique_key');
|
22
|
$query->leftJoin('role', 'r', "na.realm='nodeaccess_rid' AND r.rid=na.gid");
|
23
|
$query->leftJoin('users', 'u', "na.realm='nodeaccess_uid' AND u.uid=na.gid");
|
24
|
$query->condition('na.nid', $entity->nid);
|
25
|
|
26
|
$results = $query->execute()
|
27
|
->fetchAllAssoc('unique_key');
|
28
|
|
29
|
$entity->nodeaccess = $results;
|
30
|
}
|
31
|
}
|
32
|
|
33
|
/**
|
34
|
* Implements hook_entity_uuid_save().
|
35
|
*/
|
36
|
function nodeaccess_uuid_entity_uuid_save($entity, $entity_type) {
|
37
|
if ($entity_type !== 'node' || empty($entity->nodeaccess)) {
|
38
|
return;
|
39
|
}
|
40
|
|
41
|
$nid = entity_get_id_by_uuid($entity_type, array($entity->uuid));
|
42
|
|
43
|
if (empty($nid)) {
|
44
|
return;
|
45
|
}
|
46
|
|
47
|
$values['nid'] = reset($nid);
|
48
|
|
49
|
// Store the roles and users so that we don't have to continually load them.
|
50
|
$roles = drupal_static(__FUNCTION__ . '_roles', array());
|
51
|
$users = drupal_static(__FUNCTION__ . '_users', array());
|
52
|
|
53
|
foreach ($entity->nodeaccess as $nodeaccess) {
|
54
|
switch ($nodeaccess['realm']) {
|
55
|
case 'nodeaccess_rid':
|
56
|
$role_name = $nodeaccess['role_name'];
|
57
|
|
58
|
if (empty($roles[$role_name])) {
|
59
|
$role = user_role_load_by_name($role_name);
|
60
|
|
61
|
if (!empty($role)) {
|
62
|
$roles[$role_name] = $role->rid;
|
63
|
}
|
64
|
// Note: assumed to be TRUE, but local settings can exclude the action
|
65
|
// in which case the grant would end up being skipped.
|
66
|
elseif(variable_get('nodeaccess_uuid_create_missing_roles', TRUE)) {
|
67
|
// Lets create the role.
|
68
|
$role = new stdClass();
|
69
|
$role->name = $role_name;
|
70
|
user_role_save($role);
|
71
|
$roles[$role_name] = $role->rid;
|
72
|
}
|
73
|
else {
|
74
|
break;
|
75
|
}
|
76
|
}
|
77
|
|
78
|
$nodeaccess['gid'] = $roles[$role_name];
|
79
|
$values['rid'][$nodeaccess['gid']] = $nodeaccess;
|
80
|
break;
|
81
|
|
82
|
case 'nodeaccess_uid':
|
83
|
if (empty($users[$nodeaccess['user_uuid']])) {
|
84
|
$user = entity_get_id_by_uuid('user', array($nodeaccess['user_uuid']));
|
85
|
|
86
|
if (empty($user[$nodeaccess['user_uuid']])) {
|
87
|
break;
|
88
|
}
|
89
|
|
90
|
$users[$nodeaccess['user_uuid']] = $user[$nodeaccess['user_uuid']];
|
91
|
}
|
92
|
|
93
|
$nodeaccess['gid'] = $users[$nodeaccess['user_uuid']];
|
94
|
$values['uid'][$nodeaccess['gid']] = $nodeaccess;
|
95
|
break;
|
96
|
}
|
97
|
}
|
98
|
|
99
|
$form_state = array(
|
100
|
'values' => $values,
|
101
|
);
|
102
|
|
103
|
_nodeaccess_grants_form_submit(array(), $form_state);
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* Implements hook_entity_dependencies().
|
108
|
*/
|
109
|
function nodeaccess_uuid_entity_dependencies($entity, $entity_type) {
|
110
|
if ($entity_type !== 'node') {
|
111
|
return;
|
112
|
}
|
113
|
|
114
|
$query = db_select('node_access', 'na')
|
115
|
->condition('na.nid', $entity->nid)
|
116
|
->condition('na.realm', 'nodeaccess_uid');
|
117
|
$join_alias = $query->join('users', 'u', '(na.gid = u.uid AND na.realm = :realm)', array(':realm' => 'nodeaccess_uid'));
|
118
|
$query->fields($join_alias);
|
119
|
|
120
|
$results = $query
|
121
|
->execute()
|
122
|
->fetchAllAssoc('uid');
|
123
|
|
124
|
$dependencies = array();
|
125
|
|
126
|
if (!empty($results)) {
|
127
|
foreach ($results as $result) {
|
128
|
$dependencies[] = array(
|
129
|
'type' => 'user',
|
130
|
'id' => $result->uid,
|
131
|
);
|
132
|
}
|
133
|
}
|
134
|
|
135
|
return $dependencies;
|
136
|
}
|