Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_edit_roles.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 
46 {
47 
48 
54  function HIPO_Job_Edit_Roles($code_name='')
55  {
56  $this->HIPO_Job($code_name);
57 
58  }//end constructor
59 
60 
70  public static function paintConfig(&$o, $class, $write_access)
71  {
72  $o->openField(translate('edit_role_threshold'));
73 
74  if ($write_access) {
75  text_box($class.'[SQ_HIPO_EDIT_ROLE_THRESHOLD]', SQ_HIPO_EDIT_ROLE_THRESHOLD, 5);
76  } else {
77  echo SQ_HIPO_EDIT_ROLE_THRESHOLD;
78  }
79  echo ' '.translate('assets');
80 
81  $o->closeField();
82 
83  }//end paintConfig()
84 
85 
92  public static function getConfigVars()
93  {
94  return Array(
95  'SQ_HIPO_EDIT_ROLE_THRESHOLD' => Array('editable' => 1, 'default' => 1),
96  );
97 
98  }//end getConfigVars()
99 
100 
107  function getCodeName()
108  {
109  return parent::getCodeName().'-'.md5(implode('-',array_keys($this->_running_vars['assets'])));
110 
111  }//end getCodeName()
112 
113 
120  function getHipoName()
121  {
122  return translate('hipo_name_edit_roles');
123 
124  }//end getHipoName()
125 
126 
135  {
136  return Array(
137  Array(
138  'name' => translate('hipo_updating_roles'),
139  'function_call' => Array(
140  'process_function' => 'processRoles',
141  ),
142  'running_mode' => 'server',
143  'auto_step' => TRUE,
144  'allow_cancel' => TRUE,
145  'percent_done' => 0,
146  'complete' => FALSE,
147  'message' => FALSE,
148  ),
149  );
150 
151  }//end getInitialStepData()
152 
153 
163  {
164  if (SQ_HIPO_EDIT_ROLE_THRESHOLD == 0) return 0;
165  if (!isset($this->_running_vars['todo_assetids']) || !isset($this->_running_vars['role_changes'])) {
166  return 0;
167  }
168 
169  //total userids in 'add' and 'delete' role changes - include duplicated userids because we are counting the number of calls to setRole and deleteRole methods
170  $total_userids = 0;
171  foreach ($this->_running_vars['role_changes'] as $action_data) {
172  foreach ($action_data as $userids) {
173  $total_userids += count($userids);
174  }
175  }
176 
177  //we have to multiply with the number of users because we want to run runQueuedJobs() instead of freestyle() in hipo job
178  //when we want to set role for 1 asset but 1000 users. It will take time in this case
179  return ((count($this->_running_vars['todo_assetids']) * $total_userids / SQ_HIPO_EDIT_ROLE_THRESHOLD) * 100);
180 
181  }//end getThresholdPercentageRequired()
182 
183 
190  function freestyle()
191  {
192  while (!empty($this->_running_vars['todo_assetids'])) {
193  if (!$this->processRoles($this->_steps[0], get_class($this))) {
194  return FALSE;
195  }
196  }
197  return TRUE;
198 
199  }//end freestyle()
200 
201 
208  function prepare()
209  {
210  if (empty($this->_running_vars['assets'])) {
211  trigger_localised_error('HIPO0075', E_USER_WARNING);
212  return '';
213  }
214 
215  if (empty($this->_running_vars['role_changes'])) {
216  $this->_running_vars['role_changes'] = Array();
217  }
218 
219  $assets = $this->_running_vars['assets'];
220  $child_assets = Array();
221  for (reset($assets); NULL !== ($assetid = key($assets)); next($assets)) {
222  $asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($assetid));
223  if (empty($asset_info)) {
224  trigger_localised_error('HIPO0013', E_USER_WARNING, $assetid);
225  return '';
226  }
227 
228  if (array_get_index($this->_running_vars, 'dependants_only')) {
229  $child_assets += $GLOBALS['SQ_SYSTEM']->am->getDependantChildren($assetid);
230  } else {
231  $child_assets += $GLOBALS['SQ_SYSTEM']->am->getChildren($assetid);
232  }
233 
234  $child_assets[$assetid] = Array (
235  0 => Array (
236  'type_code' => $asset_info[$assetid]['type_code'],
237  ),
238  );
239  }
240 
241  $child_assets = array_reverse($child_assets,TRUE);
242  $this->_running_vars['todo_assetids'] = $child_assets;
243  $this->_running_vars['done_assetids'] = Array();
244  return parent::prepare();
245 
246  }//end prepare()
247 
248 
258  function processRoles(&$step_data, $prefix)
259  {
260  if (!empty($this->_running_vars['todo_assetids'])) {
261  reset($this->_running_vars['todo_assetids']);
262  $assetid = key($this->_running_vars['todo_assetids']);
263  $asset_type = $this->_running_vars['todo_assetids'][$assetid][0]['type_code'];
264  unset($this->_running_vars['todo_assetids'][$assetid]);
265  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $asset_type);
266 
267  if (!is_null($asset)) {
268  // check we can acquire lock and change roles, otherwise silently pass by
269  if ($GLOBALS['SQ_SYSTEM']->am->acquireLock($assetid, 'roles')) {
270  if ($asset->adminAccess('roles')) {
271  foreach ($this->_running_vars['role_changes'] as $action => $action_data) {
272  foreach ($action_data as $roleid => $userids) {
273  foreach ($userids as $userid) {
274  switch ($action) {
275  case 'add':
276  $GLOBALS['SQ_SYSTEM']->am->setRole($asset->id, $roleid, $userid);
277  break;
278  case 'delete':
279  $GLOBALS['SQ_SYSTEM']->am->deleteRole($asset->id, $roleid, $userid);
280  break;
281  }
282  }
283  }
284  }
285  $step_data['message'] = translate('hipo_edit_role', htmlentities($asset->name, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET));
286  $GLOBALS['SQ_SYSTEM']->am->releaseLock($assetid, 'roles');
287  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
288  unset($asset);
289  } else {
290  // we do not have admin access
291  trigger_localised_error('SYS0313', E_USER_WARNING, $asset->name);
292  }
293  } else {
294  // we do not acquire lock
295  trigger_localised_error('SYS0100', E_USER_WARNING, $asset->name);
296  }
297  } else {
298  $step_data['message'] = translate('hipo_skipping_asset', $assetid);
299  $this->_addError(translate('hipo_skipping_role', $assetid));
300  }
301 
302  // add this assetid to the done array so we dont do it again
303  $this->_running_vars['done_assetids'][] = $assetid;
304 
305  }//end if
306 
307  if (empty($this->_running_vars['todo_assetids'])) {
308  $step_data['percent_done'] = 100;
309  $step_data['complete'] = TRUE;
310  unset($this->_running_vars['done_assetids']);
311  } else {
312  $total = count($this->_running_vars['todo_assetids']) + count($this->_running_vars['done_assetids']);
313  $step_data['percent_done'] =(count($this->_running_vars['done_assetids']) / $total) * 100;
314  $step_data['complete'] = FALSE;
315  }
316 
317  return TRUE;
318 
319  }//end processRoles()
320 
321 
332  function getLockAssetDetails(&$job_type, &$running_vars, &$options)
333  {
334  $job_type = 'hipo_job_acquire_locks';
335  $options['auto_complete'] = TRUE;
336 
337  $running_vars = Array(
338  'assetids' => array_keys($this->_running_vars['assets']),
339  'lock_type' => Array('roles'),
340  'forceably_acquire' => FALSE,
341  'dependants_only' => isset($this->_running_vars['dependants_only']),
342  );
343 
344  }//end getLockAssetDetails()
345 
346 
356  function releaseLocks(&$step_data, $prefix)
357  {
358  if (!isset($this->_running_vars['re_lock_assets'])) {
359  $this->_running_vars['re_lock_assets'] = $this->_running_vars['assets'];
360  }
361 
362  $assetid = key($this->_running_vars['re_lock_assets']);
363  unset($this->_running_vars['re_lock_assets'][$assetid]);
364 
365  $children = $GLOBALS['SQ_SYSTEM']->am->getDependantChildren($assetid);
366 
367  foreach ($children as $dep_child_id => $dep_child_data) {
368  $GLOBALS['SQ_SYSTEM']->am->releaseLock($dep_child_id, 'roles');
369  }
370 
371  $asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($assetid));
372 
373  $step_data['message'] = translate('hipo_release_locks', $asset_info[$assetid]['name']).translate('hipo_release_locks_dependents');
374 
375  $GLOBALS['SQ_SYSTEM']->am->releaseLock($assetid, 'all');
376 
377  $this->_running_vars['done_assetids'][] = $assetid;
378 
379  if (empty($this->_running_vars['re_lock_assets'])) {
380  unset($this->_running_vars['done_assetids']);
381  $step_data['percent_done'] = 100;
382  $step_data['complete'] = TRUE;
383  } else {
384  $step_data['percent_done'] = (count($this->_running_vars['done_assetids']) / count($this->_running_vars['assets'])) * 100;
385  $step_data['complete'] = FALSE;
386  }
387 
388  return TRUE;
389 
390  }//end releaseLocks()
391 
392 
393 }//end class
394 
395 ?>