/[drupal]/contributions/modules/subuser/subuser.module
ViewVC logotype

Contents of /contributions/modules/subuser/subuser.module

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


Revision 1.7 - (show annotations) (download) (as text)
Thu Oct 15 07:45:29 2009 UTC (5 weeks, 5 days ago) by boombatower
Branch: MAIN
CVS Tags: DRUPAL-6--1-2, HEAD
Branch point for: DRUPAL-6--1
Changes since 1.6: +7 -3 lines
File MIME type: text/x-php
Standardize hook_user() view array.
1 <?php
2 // $Id: subuser.module,v 1.6 2009/10/14 03:54:15 boombatower Exp $
3 /**
4 * @file
5 * Allows users of a particular role to create sub user account in another role.
6 *
7 * Copyright 2008-2009 by Jimmy Berry ("boombatower", http://drupal.org/user/214218)
8 */
9
10 /*
11 * Variables loaded as constants.
12 */
13 define('SUBUSER_PARENT', variable_get('subuser_parent', 'Parent'));
14 define('SUBUSER_LIST', variable_get('subuser_list', 'Subusers'));
15 define('SUBUSER_CREATE', variable_get('subuser_create', 'Create subuser'));
16 define('SUBUSER_ADMINISTER', variable_get('subuser_administer', 'Administer subusers'));
17
18 /**
19 * Implementation of hook_menu().
20 */
21 function subuser_menu() {
22 $items = array();
23
24 $items['admin/settings/subuser'] = array(
25 'title' => 'Subuser',
26 'description' => 'Define what, if any, roles are assigned to a new subuser.',
27 'page callback' => 'drupal_get_form',
28 'page arguments' => array('subuser_settings_form'),
29 'access arguments' => array('administer subuser settings'),
30 'file' => 'subuser.pages.inc'
31 );
32 $items['user/%user/subuser/create'] = array(
33 'title' => variable_get('subuser_create', 'Create subuser'),
34 'page callback' => 'drupal_get_form',
35 'page arguments' => array('subuser_create_form', 1),
36 'access arguments' => array('create subuser'),
37 'type' => MENU_CALLBACK,
38 'file' => 'subuser.pages.inc'
39 );
40 $items['subuser/switch/%'] = array(
41 'title' => 'Switch subuser',
42 'page callback' => 'subuser_switch_user',
43 'page arguments' => array(2),
44 'access callback' => 'subuser_switch_user_access',
45 'access arguments' => array(2),
46 'type' => MENU_CALLBACK,
47 );
48
49 return $items;
50 }
51
52 /**
53 * Implementation of hook_perm().
54 */
55 function subuser_perm() {
56 return array(
57 'create subuser',
58 'switch subuser',
59 'administer subuser settings',
60 'administer subusers',
61 );
62 }
63
64 /**
65 * Implementation of hook_menu_link_alter().
66 *
67 * Allow the logout link to be altered.
68 *
69 * @see subuser_translated_menu_link_alter().
70 */
71 function subuser_menu_link_alter(&$item, $menu) {
72 if ($item['link_path'] == 'logout') {
73 $item['options']['alter'] = TRUE;
74 }
75 }
76
77 /**
78 * Implementation of hook_translated_menu_link_alter().
79 *
80 * If currently running as a child user change the "Log out" link to
81 * "Log out (return)".
82 */
83 function subuser_translated_menu_link_alter(&$item, $map) {
84 if ($item['href'] == 'logout' && isset($_SESSION['subuser_uid'])) {
85 $item['title'] = t('Log out (return)');
86 $item['href'] = 'subuser/switch/' . $_SESSION['subuser_uid'];
87 }
88 }
89
90 /**
91 * Implementation of hook_menu_link_alter().
92 */
93 function subuser_menu_alter(&$items) {
94 $items['user/%user_category/edit']['access callback'] = 'subuser_user_edit_access';
95 $items['admin/user/user']['access callback'] = 'subuser_administer_users_access';
96 $items['admin/user/user']['title callback'] = 'subuser_administer_users_title';
97 }
98
99 /**
100 * Modified version of user_edit_access() for 'administer subusers' logic.
101 */
102 function subuser_user_edit_access($account) {
103 // Condition from user_edit_access().
104 if ((($GLOBALS['user']->uid == $account->uid) || user_access('administer users')) && $account->uid > 0) {
105 return TRUE;
106 }
107
108 // Check if user can administer subusers and the user being editted is a
109 // subuser of the active user.
110 if (user_access('administer subusers') &&
111 db_result(db_query('SELECT uid FROM {user_relationship} WHERE uid = %d AND parent_id = %d', $account->uid, $GLOBALS['user']->uid))) {
112 return TRUE;
113 }
114 return FALSE;
115 }
116
117 /**
118 * Access callback for admin/user/user page.
119 *
120 * If user has 'administer users' or 'administer subusers' then allow them to
121 * view the administer users page. Filter the view to only the user's they are
122 * a parent of if they do not have the 'administer users' permission.
123 *
124 * @return boolean TRUE access granted, otherwise FALSE.
125 */
126 function subuser_administer_users_access() {
127 if (user_access('administer users')) {
128 return TRUE;
129 }
130
131 if (user_access('administer subusers')) {
132 global $user;
133
134 if (!isset($_SESSION['user_overview_filter'])) {
135 $_SESSION['user_overview_filter'] = array();
136 }
137
138 // Look for the subuser filter and ensure it is set to the current user if
139 // found, otherwise it will be added bellow.
140 $found = FALSE;
141 foreach ($_SESSION['user_overview_filter'] as $index => $filter) {
142 list($key, $value) = $filter;
143
144 if ($key == 'subuser') {
145 $_SESSION['user_overview_filter'][$index][1] = $user->uid;
146 $found = TRUE;
147 break;
148 }
149 }
150
151 // Explicitly add the filter.
152 if (!$found) {
153 $_SESSION['user_overview_filter'][] = array(
154 'subuser',
155 $user->uid,
156 );
157 }
158 return TRUE;
159 }
160 return FALSE;
161 }
162
163 /**
164 * Title callback for admin/user/user page.
165 *
166 * Set the title to the custom subuser administer title when user has
167 * 'administer subusers' and not 'administer users' permission.
168 *
169 * @return string Either default title or custom subuser title.
170 */
171 function subuser_administer_users_title() {
172 if (!user_access('administer users') && user_access('administer subusers')) {
173 return t(SUBUSER_ADMINISTER);
174 }
175 return t('Users');
176 }
177
178 /**
179 * Check if the user has permission to switch the specified user.
180 *
181 * Pass cases:
182 * - Super user.
183 * - Returning to parent account.
184 * - The user is a parent of the user being switched to.
185 *
186 * @param integer $uid User ID being switched to.
187 * @return boolean Access granted.
188 */
189 function subuser_switch_user_access($uid) {
190 global $user;
191
192 if ($user->uid == 1 ||
193 (isset($_SESSION['subuser_uid']) && $uid == $_SESSION['subuser_uid']) ||
194 db_result(db_query('SELECT uid FROM {user_relationship} WHERE uid = %d AND parent_id = %d', $uid, $user->uid))) {
195 return TRUE;
196 }
197 return FALSE;
198 }
199
200 /**
201 * Switch from a parent user to a subuser (or child user).
202 *
203 * @param $uid The user id to switch to.
204 */
205 function subuser_switch_user($uid) {
206 global $user;
207
208 if ($uid) {
209 $_SESSION['subuser_uid'] = ((isset($_SESSION['subuser_uid']) && $uid == $_SESSION['subuser_uid']) ? NULL : $user->uid);
210 $user = user_load($uid);
211 }
212 drupal_goto('user/' . $uid);
213 }
214
215 /**
216 * Implementation of hook_user().
217 */
218 function subuser_user($op, &$edit, &$account, $category = NULL) {
219 switch ($op) {
220 case 'insert':
221 if (isset($edit['origin']) && $edit['origin'] == 'subuser') {
222 db_query('INSERT INTO {user_relationship} (parent_id, uid)
223 VALUES (%d, %d)', $edit['parent_user'], $account->uid);
224 }
225 break;
226 case 'delete':
227 db_query('DELETE FROM {user_relationship} WHERE uid = %d', $account->uid);
228 break;
229 case 'view':
230 $parent = db_fetch_object(db_query('SELECT parent_id
231 FROM {user_relationship}
232 WHERE uid = %d', $account->uid));
233 if ($parent) {
234 // Display link to parent user if available.
235 $parent = user_load($parent->parent_id);
236 $account->content['subuser_parent'] = array(
237 '#type' => 'user_profile_item',
238 '#title' => t(SUBUSER_PARENT),
239 '#value' => theme('username', $parent),
240 '#weight' => 10,
241 );
242 }
243
244 // The parent user should either have access to create subusers, or have
245 // existing subusers.
246 $create = user_access('create subuser');
247 $administer = user_access('administer subusers');
248 $view = views_get_view('subusers');
249 if ($create || (isset($view->results) && $view->results)) {
250 $view = views_embed_view('subusers');
251
252 if ($create) {
253 $links[] = l(t(SUBUSER_CREATE), 'user/' . $account->uid . '/subuser/create');
254 }
255 if ($administer) {
256 $links[] = l(t(SUBUSER_ADMINISTER), 'admin/user/user');
257 }
258
259 $output = implode(' | ', $links);
260 $output .= '<br />' . $view;
261
262 $account->content['subuser'] = array(
263 '#type' => 'user_profile_category',
264 '#title' => t(SUBUSER_LIST),
265 '#weight' => 11,
266 );
267 $account->content['subuser']['list'] = array(
268 '#type' => 'user_profile_item',
269 '#value' => $output,
270 '#weight' => 11,
271 );
272 }
273 break;
274 }
275 }
276
277 /**
278 * Implementation of hook_views_api().
279 */
280 function subuser_views_api() {
281 return array(
282 'api' => 2,
283 );
284 }
285
286 /**
287 * Implementation of hook_views_data().
288 */
289 function subuser_views_data() {
290 $data['user_relationship']['table']['group'] = t('Relationship');
291
292 $data['user_relationship']['table']['join']['users'] = array(
293 'left_field' => 'uid',
294 'field' => 'uid',
295 );
296
297 $data['user_relationship']['rid'] = array(
298 'title' => t('Relationship: ID'),
299 'help' => t('The relationship id'),
300 );
301 $data['user_relationship']['uid'] = array(
302 'title' => t('Uid'),
303 'help' => t('The ID of the Sub User.'),
304 'field' => array(
305 'handler' => 'views_handler_field_user',
306 'click sortable' => TRUE,
307 ),
308 'argument' => array(
309 'handler' => 'views_handler_argument_user_uid',
310 'name field' => 'title',
311 'numeric' => TRUE,
312 'validate type' => 'uid',
313 ),
314 'filter' => array(
315 'handler' => 'views_handler_filter_numeric',
316 ),
317 'sort' => array(
318 'handler' => 'views_handler_sort',
319 ),
320 );
321 $data['user_relationship']['parent_id'] = array(
322 'title' => t('Parent Id'),
323 'help' => t('The ID of the Parent User.'),
324 'field' => array(
325 'handler' => 'views_handler_field_user',
326 'click sortable' => TRUE,
327 ),
328 'argument' => array(
329 'handler' => 'views_handler_argument_user_uid',
330 'name field' => 'title',
331 'numeric' => TRUE,
332 'validate type' => 'uid',
333 ),
334 'filter' => array(
335 'handler' => 'views_handler_filter_numeric',
336 ),
337 'sort' => array(
338 'handler' => 'views_handler_sort',
339 ),
340 );
341 return $data;
342 }

  ViewVC Help
Powered by ViewVC 1.1.2