/[drupal]/contributions/modules/aclfield/aclfield.inc
ViewVC logotype

Contents of /contributions/modules/aclfield/aclfield.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Tue Apr 17 02:26:32 2007 UTC (2 years, 7 months ago) by mfredrickson
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +69 -1 lines
File MIME type: text/x-php
added a formatter for the field to display what the perms are
1 <?php
2
3 /*** User.module ***/
4
5 // A grant for each user
6 function user_node_grants($account, $op) {
7 $grants['user_id'][] = $account->uid;
8 $grants['user_author'][] = $account->uid;
9 foreach(array_keys($account->roles) as $role) {
10 $grants['user_role'][] = $role;
11 }
12 $grants['user_all'][] = true;
13 return $grants;
14 }
15
16 // returns pretty names for realms
17 function user_node_grants_info() {
18 return array (
19 'user_all' => t('Everyone'),
20 'user_author' => t('Content Author'),
21 'user_id' => t('User Names'),
22 'user_role' => t('User Roles')
23 );
24 }
25
26 // returns a string explaining this grant and realm
27 function user_node_grants_display($realm, $data = NULL) {
28 switch($realm) {
29 case 'user_all':
30 return t("Everyone");
31
32 case 'user_author':
33 return t('This content\'s author');
34
35 case 'user_id':
36 $names = array();
37 dprint_r($data);
38 foreach($data['users'] as $id) {
39 if ($user = user_load(array('uid' => $id))) {
40 dprint_r($user);
41 $names[$id] = theme('username', $user);
42 }
43 }
44 dprint_r("After");
45 if (!empty($names)) {
46 return aclfield_array_to_text($names);
47 }
48 break;
49 case 'user_role':
50 $roles = user_roles();
51 $priv_roles = array();
52 foreach($data['roles'] as $rid) {
53 $priv_roles[] = $roles[$rid];
54 }
55 if (!empty($priv_roles)) {
56 return t('Users in the '). aclfield_array_to_text($priv_roles) . ' ' . t('roles');
57 }
58 break;
59 }
60 }
61
62 function user_grant_form($grant, $data = NULL) {
63 //dprint_r("in grant form, data is:");
64 //dprint_r($data);
65 $form = array();
66 // TODO define a better form to pick a user ID. perhaps we can hijack views?
67 switch ($grant) {
68 case 'user_id':
69 $res = db_query('SELECT uid, name FROM {users} WHERE uid > 0');
70 $users = array();
71 while ($row = db_fetch_object($res)) {
72 $users[$row->uid] = $row->name;
73 }
74 $form['users'] = array(
75 '#type' => 'select',
76 // '#title' => t('Users in the following roles'),
77 '#options' => $users,
78 '#multiple' => true,
79 '#default_value' => $data['users'],
80 );
81 break;
82
83 case 'user_all':
84 case 'user_author':
85 break;
86
87 case 'user_role':
88 $roles = user_roles();
89 $form['roles'] = array(
90 '#type' => 'select',
91 '#title' => t('Users in the following roles'),
92 '#options' => $roles,
93 '#multiple' => true,
94 '#default_value' => $data['roles'],
95 );
96
97 break;
98 }
99
100 return $form;
101 }
102
103 function user_grant_process_node($grant, $node, $data) {
104 $grants = array();
105
106 switch($grant) {
107 case 'user_author':
108 $grants[] = $node->uid;
109 break;
110
111 case 'user_id':
112 $grants = $data['users'];
113 break;
114
115 case 'user_role':
116 $grants = $data['roles'];
117 break;
118
119 case 'user_all':
120 $grants[] = 1;
121 break;
122
123 }
124
125 return $grants;
126 }
127
128 /*** Organic Groups ***/
129
130 function og_node_grants_info() {
131 return array (
132 'og_subscriber' => t('Group Subscribers'),
133 'og_admin' => t('Group Administrators')
134 );
135 }
136
137 function og_grant_form($grant, $data = NULL) {
138 $form = array();
139 $groups = og_all_groups_options();
140 $groups['THIS_NODE'] = t('This Content\'s Groups');
141 ksort($groups); // sort this node to top
142
143 $form['groups'] = array (
144 '#type' => 'select',
145 '#title' => t('In the following groups'),
146 '#options' => $groups,
147 '#multiple' => true,
148 '#default_value' => $data['groups'],
149 );
150
151 return $form;
152 }
153
154 function og_node_grants_display($realm, $data = NULL) {
155 $groups = array();
156
157 foreach($data['groups'] as $nid) {
158 if ($nid == 'THIS_NODE') {
159 $groups[] = t('this content\'s groups');
160 }
161 elseif ($node = node_load($nid)) {
162 // perhaps replace with an sql_rewrite version that just pulls nids and titles?
163 $groups[] = t('members of ') . l($node->title, 'node/'. $node->nid);
164 }
165 }
166
167 $group_string = aclfield_array_to_text($groups);
168
169 // idea: call default node formatter on each nid?
170 /* something like:
171 if exists node reference:
172 call nodereference_formatter on a fudged up version of the data
173 else
174 do it ourselves?
175 */
176
177 switch($realm) {
178 case 'og_subscriber':
179 return t("Subscribers in ") . $group_string;
180 case 'og_admin':
181 return t("Administrators in ") . $group_string;
182 }
183 }
184
185 // BUG this depends on the patch in http://drupal.org/node/111836 to work
186 function og_grant_process_node($grant, $node, $data) {
187 $grants = array();
188
189 // return an empty array if no groups are selected
190 if (!is_array($data['groups'])) { return $grants; }
191
192 // iterate through the selected groups and return the node ids as grant ids
193 foreach(array_keys($data['groups']) as $gid) {
194 // expand the THIS_NODE placeholder into the list of groups
195 if ($gid == 'THIS_NODE' && is_array($node->og_groups)) {
196 foreach ($node->og_groups as $node_gid) {
197 $grants[] = $node_gid;
198 }
199 } else {
200 // we've already found the group ids, just return them here
201 $grants[] = $gid;
202 }
203
204 }
205
206 return $grants;
207 }
208
209 /* More to add
210 - buddy lists
211 -- select all
212 -- select from
213 - node reference
214 - user reference
215 */
216
217 // implementing this until the og_grant bug listed above works
218 // TODO comment out this function
219 function aclfield_node_grants($account, $op) {
220 if (function_exists('og_get_subscriptions')) {
221 // get subscriptions
222 if ($subscriptions = og_get_subscriptions($account->uid)) {
223 foreach ($subscriptions as $key => $val) {
224 // og now has two realms, subscribers and administrators
225 $grants['og_subscriber'][] = $key;
226 if ($val['is_admin']) {
227 $grants['og_admin'][] = $key;
228 }
229 }
230 }
231 return $grants ? $grants : array();
232 }
233 }

  ViewVC Help
Powered by ViewVC 1.1.2