Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_edit_permissions.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 
44 {
45 
46 
52  function HIPO_Job_Edit_Permissions($code_name='')
53  {
54  $this->HIPO_Job($code_name);
55 
56  }//end constructor
57 
58 
68  public static function paintConfig(&$o, $class, $write_access)
69  {
70  // metadata regeneration threshhold HIPO config entry
71  $o->openField(translate('permissions_threshold'));
72 
73  if ($write_access) {
74  text_box($class.'[SQ_HIPO_PERMISSIONS_THRESHOLD]', SQ_HIPO_PERMISSIONS_THRESHOLD, 5);
75  echo ' '.translate('assets');
76  $o->note(translate('hipo_value_divided_by_permission'));
77  } else {
78  echo SQ_HIPO_PERMISSIONS_THRESHOLD;
79  echo ' '.translate('assets');
80  }
81 
82  $o->closeField();
83 
84  }//end paintConfig()
85 
86 
93  public static function getConfigVars()
94  {
95  return Array(
96  'SQ_HIPO_PERMISSIONS_THRESHOLD' => Array('editable' => 1, 'default' => 1),
97  );
98 
99  }//end getConfigVars()
100 
101 
108  function getCodeName()
109  {
110  return parent::getCodeName().'-'.md5(serialize($this->_running_vars['permission_changes']));
111 
112  }//end getCodeName()
113 
114 
121  function getHipoName()
122  {
123  return translate('hipo_name_edit_permissions');
124 
125  }//end getHipoName()
126 
127 
136  {
137  return Array(
138  Array(
139  'name' => translate('hipo_updating_permissions'),
140  'function_call' => Array(
141  'process_function' => 'processPermissions',
142  ),
143  'running_mode' => 'server',
144  'auto_step' => true,
145  'allow_cancel' => true,
146  'percent_done' => 0,
147  'complete' => false,
148  'message' => '',
149  ),
150  );
151 
152  }//end getInitialStepData()
153 
154 
164  {
165  if (SQ_HIPO_PERMISSIONS_THRESHOLD == 0) return 0;
166  if (!isset($this->_running_vars['todo_assetids'])) {
167  return 0;
168  }
169  return (($this->_running_vars['total_changes']) / (SQ_HIPO_PERMISSIONS_THRESHOLD / count($this->_running_vars['permission_changes'])) * 100);
170 
171  }//end getThresholdPercentageRequired()
172 
173 
180  function freestyle()
181  {
182  while (!empty($this->_running_vars['todo_assetids'])) {
183  if (!$this->processPermissions($this->_steps[0], get_class($this))) {
184  return FALSE;
185  }
186  }
187  return TRUE;
188 
189  }
190 
191 
198  function prepare()
199  {
200  if (empty($this->_running_vars['permission_changes'])) {
201  trigger_localised_error('HIPO0014', E_USER_WARNING);
202  return '';
203  }
204 
205  $this->_running_vars['done_changes'] = 0;
206  $this->_running_vars['total_changes'] = 0;
207 
208  if (empty($this->_running_vars['permission_changes'])) {
209  // no schema changes, so do nothing
210  $this->_running_vars['todo_assetids'] = Array();
211  } else if (empty($this->_running_vars['permission_screen_assetid'])) {
212  $this->_running_vars['todo_assetids'] = Array();
213  foreach ($this->_running_vars['permission_changes'] as $perm_change) {
214  $assetids = $perm_change['assetids'];
215  foreach ($assetids as $assetid) {
216  $asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($assetid));
217 
218  if (empty($asset_info)) {
219  trigger_localised_error('HIPO0013', E_USER_WARNING, $assetid);
220  return '';
221  }
222 
223  if ((!array_get_index($perm_change, 'cascades', TRUE)) || array_get_index($perm_change, 'dependants_only')) {
224  $child_assets = $GLOBALS['SQ_SYSTEM']->am->getDependantChildren($assetid);
225  } else {
226  $child_assets = $GLOBALS['SQ_SYSTEM']->am->getChildren($assetid);
227  }
228 
229  // add the parent asset to the list too
230  $child_assets[$assetid] = Array (
231  0 => Array (
232  'type_code' => $asset_info[$assetid]['type_code'],
233  ),
234  );
235 
236  $child_assets = array_reverse($child_assets, TRUE);
237 
238  // Now assign permission changes to each asset
239  foreach ($child_assets as $child_assetid => $child_asset) {
240  $type_code = $child_asset[0]['type_code'];
241 
242  $this_todo =& $this->_running_vars['todo_assetids'][$child_assetid];
243  $this_todo['type_code'] = $type_code;
244  $this_todo['permission_changes'][] = $perm_change;
245  $this->_running_vars['total_changes']++;
246  }
247  }
248  }
249  } else {
250  // Job from static permission screen
251  $need_all = false;
252  foreach ($this->_running_vars['permission_changes'] as $perm_change) {
253  if (!((!array_get_index($perm_change, 'cascades', TRUE)) || array_get_index($perm_change, 'dependants_only'))) {
254  $need_all = true;
255  }
256  }//end foreach
257 
258  $assetid = $this->_running_vars['permission_screen_assetid'];
259  $asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($assetid));
260  if (empty($asset_info)) {
261  trigger_localised_error('HIPO0013', E_USER_WARNING, $assetid);
262  return '';
263  }
264 
265  if ($need_all) {
266  $children = $GLOBALS['SQ_SYSTEM']->am->getChildren($assetid);
267  } else {
268  $children = $GLOBALS['SQ_SYSTEM']->am->getDependantChildren($assetid);
269  }
270 
271  // Add the parent asset to the list too
272  $todo_assetids = Array(
273  $assetid => Array(
274  'type_code' => $asset_info[$assetid]['type_code'],
275  ),
276  );
277  foreach($children as $todo_assetid => $info) {
278  if (!isset($todo_assetids[$todo_assetid])) {
279  $todo_assetids[$todo_assetid]['type_code'] = $info[0]['type_code'];
280  }
281  }//end foreach
282  unset($dep_children);
283  unset($children);
284 
285  $this->_running_vars['todo_assetids'] = $todo_assetids;
286  $this->_running_vars['total_changes'] = count($todo_assetids);
287  }
288 
289  return parent::prepare();
290 
291  }//end prepare()
292 
293 
303  function processPermissions(&$step_data, $prefix)
304  {
305  if (!empty($this->_running_vars['todo_assetids'])) {
306  reset($this->_running_vars['todo_assetids']);
307  $assetid = key($this->_running_vars['todo_assetids']);
308  $asset_type = $this->_running_vars['todo_assetids'][$assetid]['type_code'];
309  $perm_changes = empty($this->_running_vars['permission_screen_assetid']) ? $this->_running_vars['todo_assetids'][$assetid]['permission_changes'] : $this->_running_vars['permission_changes'];
310  unset($this->_running_vars['todo_assetids'][$assetid]);
311  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $asset_type);
312 
313  if (!is_null($asset) ) {
314  // check we can acquire lock and change permissions, otherwise silently pass by
315  if ($GLOBALS['SQ_SYSTEM']->am->acquireLock($assetid, 'permissions')) {
316  if ($asset->adminAccess('permissions')) {
317  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
318  $this->_running_vars['done_changes']++;
319 
320  foreach ($perm_changes as $set_data) {
321  $perm = $set_data['permission'];
322  $perm_name = permission_type_name($perm);
323 
324  if ($set_data['userid']) {
325  // use getAssetInfo() because deleting permissions
326  // held by deleted users (eg.LDAP) will cause an assertion
327  $user_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($set_data['userid']), 'user', false);
328 
329  // check that the passed userid is a user or user_group
330  if (empty($user_info)) {
331  $user_name = 'Unknown User';
332  } else {
333  $user_name = $user_info[$set_data['userid']]['name'];
334  }
335  } else {
336  $user_name = 'General Public';
337  }
338  if ($set_data['previous_access'] === null) {
339  // to edit this permission, there must not currently be a permission set
340  // at all - either grant OR deny - for this user
341  $current = $GLOBALS['SQ_SYSTEM']->am->getPermission($asset->id, $perm, !(bool)$set_data['granted'], FALSE, FALSE, TRUE, TRUE);
342  if (isset($current[$set_data['userid']])) {
343  $new_access = ($set_data['granted']) ? translate('grant') : translate('revoke');
344  $current_access = ($current[$set_data['userid']]) ? translate('granted') : translate('revoked');
345  $this->_addError(translate('hipo_cannot_modify_permission', $new_access, $perm_name, $user_name, $asset->name, $current_access), true);
346  continue;
347  }
348  } else {
349  // we need to have a permission set and the access level be the same
350  $current = $GLOBALS['SQ_SYSTEM']->am->getPermission($asset->id, $perm, null, false, false, true);
351  if (!isset($current[$set_data['userid']])) {
352  continue;
353  } else if ($current[$set_data['userid']] != $set_data['previous_access']) {
354  $new_access = ($set_data['previous_access']) ? translate('grant') : translate('revoke');
355  $current_access = ($set_data['previous_access']) ? translate('revoked') : translate('granted');
356  $this->_addError(translate('hipo_cannot_modify_permission', $new_access, $perm_name, $user_name, $asset->name, $current_access), true);
357  continue;
358  }
359 
360  }
361 
362  switch ($set_data['granted']) {
363  case -1 :
364  // delete a permission
365  $step_data['message'] = htmlentities('Removing '.$perm_name.' permission from "'.$asset->name.'" (#'.$asset->id.')', ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
366  $GLOBALS['SQ_SYSTEM']->am->deletePermission($asset->id, $set_data['userid'], $perm);
367  break;
368 
369  case 0 :
370  case 1 :
371  // deny a permission
372  // grant a permission
373  $step_data['message'] = htmlentities(($set_data['granted'] ? 'Apply' : 'Deny').'ing '.$perm_name.' permission to "'.$asset->name.'" (#'.$asset->id.')', ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
374  $GLOBALS['SQ_SYSTEM']->am->setPermission($asset->id, $set_data['userid'], $perm, $set_data['granted'], $set_data['cascades']);
375  break;
376  }
377  }
378 
379  // If there are multiple schema changes to make, say we are
380  // (otherwise use more specific message set above)
381  if (count($perm_changes) > 1) {
382  $step_data['message'] = htmlentities('Making '.count($perm_changes).' permission changes to '.$asset->name.' (Id: #'.$asset->id.')', ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
383  } else if (count($perm_changes) == 0) {
384  $step_data['message'] = translate('hipo_skipping_asset', $assetid);
385  }
386  $GLOBALS['SQ_SYSTEM']->am->releaseLock($assetid, 'permissions');
387  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
388  unset($asset);
389 
390  } else {
391  // we do not have admin access
392  trigger_localised_error('SYS0111', E_USER_WARNING, $asset->name);
393  }
394  } else {
395  // we do not acquire lock
396  trigger_localised_error('SYS0100', E_USER_WARNING, $asset->name);
397  }
398  } else {
399  $step_data['message'] = translate('hipo_skipping_asset', $assetid);
400  $this->_addError(translate('hipo_skipping_permission', $assetid));
401  }
402 
403  }//end if
404 
405  if (empty($this->_running_vars['todo_assetids'])) {
406  $step_data['percent_done'] = 100;
407  $step_data['complete'] = TRUE;
408  } else {
409  $step_data['percent_done'] = ($this->_running_vars['done_changes'] / $this->_running_vars['total_changes']) * 100;
410  $step_data['complete'] = FALSE;
411  }
412 
413  return true;
414 
415  }//end processPermissions()
416 
417 
418 
419 }//end class
420 
421 ?>