This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaggle.permissions.inc
185 lines (168 loc) · 5.61 KB
/
waggle.permissions.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Implement hook_permission().
*/
function waggle_permission() {
return array(
'access all stories' => array(
'title' => t('Access all stories'),
),
'access departmental user stories' => array(
'title' => t('Access departmental user stories'),
'description' => t('Grants access to any story submitted by a user in the same department'),
),
);
}
/**
* Implement hook_node_access().
*/
function waggle_node_access($node, $op, $account) {
if ($node->type != 'story')
return;
if ($op != 'view' && $op != 'update')
return;
if ($node->uid === $account->uid
|| user_access('access all stories', $account)) {
return NODE_ACCESS_ALLOW;
}
// Grant access to departmental managers.
if (user_access('access departmental user stories', $account)) {
//watchdog('waggle', 'node: <pre>' . print_r($node,TRUE) . '</pre>');
//watchdog('waggle', 'account: <pre>' . print_r($account,TRUE) . '</pre>');
$owner = user_load($node->uid);
$a = array_map(function($i) { return $i['value']; }, array_pop($owner->field_department));
$account = user_load($account->uid);
$b = array_map(function($i) { return $i['value']; }, array_pop($account->field_department));
/*watchdog('waggle', 'nid: ' . $node->nid . ' $a: ' . print_r($a,TRUE));
watchdog('waggle', 'nid: ' . $node->nid . ' $b: ' . print_r($b,TRUE));*/
$intersection = array_intersect($a, $b);
if (!empty($intersection)) {
return NODE_ACCESS_ALLOW;
}
}
// Grant access only to associated users.
foreach ($node->field_associated_users as $lang => $user_map) {
foreach ($user_map as $associated_user) {
if ($associated_user['uid'] === $account->uid) {
return NODE_ACCESS_ALLOW;
}
}
}
// Grant also to associated roles.
foreach ($node->field_role_visibility as $lang => $roles_granted) {
foreach ($roles_granted as $rgk => $role) {
foreach ($account->roles as $rid => $user_role) {
if ($rid == $role['value']) {
return NODE_ACCESS_ALLOW;
}
}
}
}
return NODE_ACCESS_DENY;
}
/**
* Implement hook_node_grants().
*/
function waggle_node_grants($account, $op) {
$grants = array();
if ($op == 'view' || $op == 'update') {
if (user_access('access all stories', $account)) {
$grants['waggle_all_stories'] = array(0);
} else {
$grants['waggle_story'] = array($account->uid);
$grants['waggle_own_story'] = array($account->uid);
$grants['waggle_role_story'] = array();
$account = user_load($account->uid);
foreach ($account->roles as $rid => $role) {
// TODO: Hardcoded to handle department manager role differently, that's not ideal
if ($rid != 11) {
$grants['waggle_role_story'][] = $rid;
} else {
if (user_access('access departmental user stories', $account)) {
if (isset($account->field_department_term['und'])) {
$grants['waggle_department'] = array();
foreach ($account->field_department_term['und'] as $dep_tid) {
$grants['waggle_department'][] = $dep_tid['tid'];
}
}
}
}
}
}
}
watchdog('waggle', 'granting for user ' . $account->uid . ': <pre>' . print_r($grants,true) . '</pre>');
return $grants;
}
/**
* Implement hook_node_access_records().
*/
function waggle_node_access_records($node) {
if ($node->type == 'story') {
if (isset($node->field_associated_users['und'])) {
foreach ($node->field_associated_users['und'] as $delta => $account) {
if (isset($account['uid'])) {
$grants[] = array(
'realm' => 'waggle_story',
'gid' => $account['uid'],
'grant_view' => $node->status,
'grant_update' => 1,
'grant_delete' => 0,
'priority' => 1,
);
}
}
}
if (isset($node->field_role_visibility['und'])) {
foreach ($node->field_role_visibility['und'] as $delta => $role) {
// TODO: Not hardcode 11 (tid for Departmental managers)
if (isset($role) && $role != 11) {
$grants[] = array(
'realm' => 'waggle_role_story',
'gid' => $role,
'grant_view' => $node->status,
'grant_update' => 1,
'grant_delete' => 0,
'priority' => 1,
);
}
}
}
if ($node->uid != 0) {
$grants[] = array(
'realm' => 'waggle_own_story',
'gid' => $node->uid,
'grant_view' => $node->status,
'grant_update' => 1,
'grant_delete' => 0,
'priority' => 1,
);
// TODO: Department manager role is hardcoded to 11, make this configurable
if (isset($node->field_role_visibility['und']) && in_array(array('value' => 11), $node->field_role_visibility['und'])) {
$owner_account = user_load($node->uid);
if (isset($owner_account->field_department_term['und'])) {
foreach ($owner_account->field_department_term['und'] as $delta => $department_term) {
if (isset($department_term)) {
$grants[] = array(
'realm' => 'waggle_department',
'gid' => $department_term['tid'],
'grant_view' => $node->status,
'grant_update' => 1,
'grant_delete' => 0,
'priority' => 1,
);
}
}
}
}
}
$grants[] = array(
'realm' => 'waggle_all_stories',
'gid' => 0,
'grant_view' => 1,
'grant_update' => 1,
'grant_delete' => 0,
'priority' => 1,
);
return $grants;
}
}