Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
roles.inc
1 <?php
17 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
18 
38 function paintInlineRoles(&$owner, &$o, &$ei)
39 {
40  $prefix = $owner->getPrefix();
41 
42  $roles = $GLOBALS['SQ_SYSTEM']->am->getRole($owner->id);
43 
44  if (empty($roles)) return FALSE;
45 
46  $o->openSection(translate('roles'));
47  $o->openField('&nbsp;');
48  ?>
49  <table class="sq-backend-table">
50  <td class="sq-backend-table-header"><?php echo translate('roles'); ?></td>
51  <td class="sq-backend-table-header"><?php echo translate('users'); ?></td>
52  <td class="sq-backend-table-header"><?php echo translate('global'); ?></td>
53  <?php
54  foreach ($roles as $roleid => $userids) {
55  $global_role = FALSE;
56  if (in_array(0, $userids)) {
57  $userids = array_diff($userids, Array(0));
58  $global_role = TRUE;
59  }
60 
61  $userid_count = count($userids);
62  ?>
63  <tr>
64  <td class="sq-backend-table-cell-alt" rowspan="<?php echo $userid_count; ?>">
65  <?php echo get_asset_tag_line($roleid); ?>
66  </td>
67  <td class="sq-backend-table-cell-alt">
68  <?php
69  if (!empty($userids)) {
70  echo get_asset_tag_line(array_shift($userids));
71  }
72  ?>
73  </td>
74  <td class="sq-backend-table-cell-alt" rowspan="<?php echo $userid_count; ?>">
75  <img src="<?php echo sq_web_path('lib'); ?>/web/images/<?php echo ($global_role) ? 'tick' : 'cross'; ?>.gif" width="15" height="15" />
76  </td>
77  </tr>
78  <?php
79  foreach ($userids as $userid) {
80  ?>
81  <tr>
82  <td class="sq-backend-table-cell-alt">
83  <?php echo get_asset_tag_line($userid); ?>
84  </td>
85  </tr>
86  <?php
87  }
88  }//end foreach
89  ?>
90  </table><?php
91  $o->closeField();
92  $o->closeSection();
93 
94  return TRUE;
95 
96 }//end paintInlineRoles()
97 
98 
110 function paintRoles(&$owner, &$o, &$ei, $keywords=NULL)
111 {
112  $root = $ei->findScreen('static_screen_roles');
113  if (is_null($root)) return FALSE;
114 
115  $prefix = $owner->getPrefix();
116  $admin_access = $owner->adminAccess('roles');
117 
118  foreach ($root->section as $section) {
119 
120  // only do this if we are not painting a layout
121  if (is_null($keywords)) {
122  // if we are in limbo and this section is not to be printed in limbo - dont print it
123  if (SQ_IN_LIMBO && (!isset($section->attributes()->limbo_access) || (int)$section->attributes()->limbo_access == 0)) {
124  continue;
125  }
126  }
127 
128  $section_access = $ei->_getAccess($owner, $section, $prefix);
129  if (!($section_access & SQ_EI_READ)) {
130  continue;
131  }
132 
133  if (!is_null($keywords) && isset($keywords['sections'][strtolower(str_replace(' ', '_', $section->attributes()->name))])) {
134  ob_start();
135  }
136 
137  $o->openSection(array_get_index($section->attributes(), 'display_name', $section->attributes()->name));
138 
139  if (!($num_fields = count($section->children()))) {
140  continue;
141  }
142 
143  if (SQ_CONF_ENABLE_ROLES_PERM_SYSTEM == '0' && SQ_CONF_ENABLE_ROLES_WF_SYSTEM == '0') {
144  $o->openField('');
145  ob_start();
146  echo translate('roles_system_disabled');
147  ob_end_flush();
148  $o->closeField();
149  $o->closeSection();
150 
151  return $admin_access;
152  }
153 
154  foreach ($section->field as $field) {
155 
156  $field_access = $ei->_getAccess($owner, $field, $prefix);
157  if (!($field_access & SQ_EI_READ)) {
158  continue;
159  }
160 
161  $field_name = '';
162  if (!empty($field->display_name)) {
163  $field_name = (string) $field->display_name;
164  }
165 
166  $o->openField($field_name);
167  if (!is_null($keywords) && isset($keywords['fields'][strtolower($field->attributes()->code)])) {
168  ob_start();
169  }
170  switch (strtolower($field->attributes()->code)) {
171  case 'roles_current' :
172  $roles = $GLOBALS['SQ_SYSTEM']->am->getRole($owner->id);
173  if (empty($roles)) {
174  echo translate('no_roles_set');
175  } else {
176  ?>
177  <table class="sq-backend-table">
178  <td class="sq-backend-table-header"><?php echo translate('roles'); ?></td>
179  <td class="sq-backend-table-header"><?php echo translate('users'); ?></td>
180  <?php
181  if (SQ_CONF_ENABLE_GLOBAL_ROLES == '1') {
182  ?>
183  <td class="sq-backend-table-header"><?php echo translate('global'); ?></td>
184  <?php
185  }
186 
187  foreach ($roles as $roleid => $ids) {
188  $userids = Array();
189  foreach ($ids as $row => $userid) {
190  if ($GLOBALS['SQ_SYSTEM']->am->getAssetInfo($userid, Array('user', 'user_group'), FALSE)) {
191  $userids[] = $userid;
192  }
193  }
194  $global_role = FALSE;
195  if (in_array(0, $userids)) {
196  $userids = array_diff($userids, Array(0));
197  $global_role = TRUE;
198  }
199  if ($admin_access) {
200  ?>
201  <tr>
202  <td class="sq-backend-table-cell-alt">
203  <?php echo asset_finder($prefix.'_role_'.$roleid, $roleid, Array('role' => 'I')); ?>
204  </td>
205  <td class="sq-backend-table-cell-alt">
206  <?php echo multiple_asset_finder($prefix.'_userids_'.$roleid, $userids, Array('user' => 'D', 'user_group' => 'D')); ?>
207  </td>
208  <?php
209  if (SQ_CONF_ENABLE_GLOBAL_ROLES == '1') {
210  ?>
211  <td class="sq-backend-table-cell-alt">
212  <?php echo check_box($prefix.'_globally_assign_'.$roleid, 1, $global_role); ?>
213  </td>
214  <?php
215  }
216  ?>
217  </tr>
218  <?php
219  } else {
220  $userid_count = count($userids);
221  ?>
222  <tr>
223  <td class="sq-backend-table-cell-alt" rowspan="<?php echo $userid_count; ?>">
224  <?php echo get_asset_tag_line($roleid); ?>
225  </td>
226  <td class="sq-backend-table-cell-alt">
227  <?php
228  if (!empty($userids)) {
229  echo get_asset_tag_line(array_shift($userids));
230  }
231  ?>
232  </td>
233  <?php
234  if (SQ_CONF_ENABLE_GLOBAL_ROLES == '1') {
235  ?>
236  <td class="sq-backend-table-cell-alt" rowspan="<?php echo $userid_count; ?>">
237  <img src="<?php echo sq_web_path('lib'); ?>/web/images/<?php echo ($global_role) ? 'tick' : 'cross'; ?>.gif" width="15" height="15" />
238  </td>
239  <?php
240  }
241  ?>
242  </tr>
243  <?php
244  foreach ($userids as $userid) {
245  ?>
246  <tr>
247  <td class="sq-backend-table-cell-alt">
248  <?php echo get_asset_tag_line($userid); ?>
249  </td>
250  </tr>
251  <?php
252  }
253  }
254  }//end foreach
255  ?>
256  </table>
257  <?php
258  }//end else
259  break;
260  case 'roles_add' :
261  echo '<p>'.translate('select_a_role').'<br />';
262  echo asset_finder($prefix.'_role_new', 0, Array('role' => 'I')).'</p>';
263  echo '<p>'.translate('select_users_for_role').'<br />';
264  echo multiple_asset_finder($prefix.'_userids_new', Array(), Array('user' => 'D', 'user_group' => 'D')).'</p>';
265  if (SQ_CONF_ENABLE_GLOBAL_ROLES == '1') {
266  echo check_box($prefix.'_globally_assign_new').translate('allow_global_assignment');
267  }
268  break;
269  }//end switch
270 
271  if (!empty($field->note)) {
272  $o->note((string) $field->note);
273  }
274 
275  if (!is_null($keywords) && isset($keywords['fields'][strtolower((string)$field->attributes()->code)])) {
276  $ei->_tmp['layout_keyword_replacements'][$keywords['fields'][strtolower($field->attributes()->code)]] = ob_get_contents();
277  ob_end_flush();
278  }
279  $o->closeField();
280 
281  }//end for
282 
283  $o->closeSection();
284 
285  if ($admin_access) {
286  $kiddies = $GLOBALS['SQ_SYSTEM']->am->getChildren($owner->id);
287  if (count($kiddies)) {
288  $o->openSection(translate('cascade_roles'));
289  $o->openField(translate('note'));
290  $o->note(translate('cascade_roles_note', htmlspecialchars($owner->name, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET), count($kiddies)));
291  $o->closeField();
292  $o->openField('&nbsp;');
293  check_box($prefix.'_cascade_roles', TRUE, TRUE);
294  label(translate('cascade_roles_option'), $prefix.'_roles[cascade_changes]');
295  $o->closeField();
296  $o->closeSection();
297 
298  $o->openSection(translate('manually_cascade_roles'));
299  $o->openField(translate('note'));
300  $o->note(translate('manually_cascade_roles_note', htmlspecialchars($owner->name, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET), count($kiddies)));
301  $o->closeField();
302  $o->openField('&nbsp;');
303  check_box($prefix.'_manually_cascade_roles', TRUE);
304  label(translate('manually_cascade_roles_option'), $prefix.'_roles[manual_cascade]');
305  $o->closeField();
306  $o->closeSection();
307  }
308  }
309 
310  if (!is_null($keywords) && isset($keywords['sections'][strtolower(str_replace(' ', '_', $section->attributes()->name))])) {
311  $ei->_tmp['layout_keyword_replacements'][$keywords['sections'][strtolower(str_replace(' ', '_', $section->attributes()->name))]] = ob_get_contents();
312  ob_end_flush();
313  }
314 
315  }//end for
316 
317  return $admin_access;
318 
319 }//end paintRoles()
320 
321 
332 function processRoles(&$owner, &$o, &$ei)
333 {
334  if (!$owner->adminAccess('roles')) return FALSE;
335  $roles = $GLOBALS['SQ_SYSTEM']->am->getRole($owner->id);
336  $current_roles = Array();
337  foreach ($roles as $roleid => $ids) {
338  foreach ($ids as $row => $userid) {
339  $current_roles[$roleid][] = $userid;
340  }
341  }
342  $new_roles = array_get_index($_REQUEST, $owner->getPrefix().'_roles', Array());
343 
344  $prefix = $owner->getPrefix();
345 
346  $role_vars = Array();
347  foreach ($current_roles as $roleid => $userids) {
348  $currently_global = FALSE;
349  if (in_array(0, $userids)) {
350  $userids = array_diff($userids, Array(0));
351  $currently_global = TRUE;
352  }
353 
354  $tmp = array_get_index($_REQUEST, $prefix.'_role_'.$roleid, Array());
355  $roleid_new = array_get_index($tmp, 'assetid', 0);
356  $tmp = array_get_index($_REQUEST, $prefix.'_userids_'.$roleid, Array());
357 
358  $userids_new = Array();
359  $userids_keep = Array();
360 
361  foreach ($tmp as $tmp_values) {
362  if ($tmp_values['assetid']) {
363  if (!in_array($tmp_values['assetid'], $userids)) {
364  $userids_new[] = $tmp_values['assetid'];
365  } else {
366  $userids_keep[] = $tmp_values['assetid'];
367  }
368  }
369  }
370 
371  if (!$roleid_new) {
372  $role_vars['delete'][$roleid] = $userids;
373  } else if ($roleid_new != $roleid) {
374  $role_vars['delete'][$roleid] = $userids;
375  $role_vars['add'][$roleid_new] = array_merge($userids_new, $userids_keep);
376  } else {
377  $roles_diff = array_diff($userids, $userids_keep);
378  if (!empty($roles_diff)) {
379  $role_vars['delete'][$roleid] = $roles_diff;
380  }
381  if (!empty($userids_new)) {
382  $role_vars['add'][$roleid] = $userids_new;
383  }
384  }
385 
386  $globally_assigned = array_get_index($_REQUEST, $prefix.'_globally_assign_'.$roleid, FALSE);
387  if (!$globally_assigned && $currently_global) {
388  $role_vars['delete'][$roleid][] = 0;
389  } else if ($globally_assigned && !$currently_global) {
390  $role_vars['add'][$roleid][] = 0;
391  }
392  }//end foreach
393 
394  $roleid_new = array_get_index($_REQUEST, $prefix.'_role_new', FALSE);
395  $globally_assign_new = array_get_index($_REQUEST, $prefix.'_globally_assign_new', FALSE);
396  if ($roleid_new['assetid']) {
397  $userids_new = array_get_index($_REQUEST, $prefix.'_userids_new', Array());
398  foreach ($userids_new as $userid_new) {
399  if ($userid_new['assetid']) {
400  $role_vars['add'][$roleid_new['assetid']][] = $userid_new['assetid'];
401  }
402  }
403  if ($globally_assign_new) {
404  $role_vars['add'][$roleid_new['assetid']][] = 0;
405  }
406  }
407 
408  $cascade = array_get_index($_REQUEST, $prefix.'_cascade_roles', FALSE);
409  $manually_cascade = array_get_index($_REQUEST, $prefix.'_manually_cascade_roles', FALSE);
410 
411  if ($manually_cascade) {
412  foreach ($current_roles as $roleid => $userids) {
413  if (isset($role_vars['add'][$roleid])) {
414  $role_vars['add'][$roleid] += $userids;
415  } else {
416  $role_vars['add'][$roleid] = $userids;
417  }
418  }
419  }
420 
421  if (empty($role_vars['add']) && empty($role_vars['delete']) && !$manually_cascade) {
422  return;
423  }
424 
425  if (!empty($role_vars)) {
426  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
427  $vars = Array(
428  'assets' => Array(
429  $owner->id => Array(
430  'type_code' => $owner->type(),
431  ),
432  ),
433  'role_changes' => $role_vars,
434  );
435 
436  if (!$cascade) $vars['dependants_only'] = 1;
437 
438  $hh->queueHipo('hipo_job_edit_roles', $vars);
439  }
440 
441  return TRUE;
442 
443 }//end processRoles()
444 
445 
446 ?>