Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_set_permission.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
18 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
19 require_once SQ_SYSTEM_ROOT.'/core/attributes/parameter_map/parameter_map.inc';
20 
33 {
34 
35 
55  public static function execute($settings, &$state)
56  {
57  // check required settings
58  if (!isset($settings['permission'])) return FALSE;
59 
60  $parameter_map_value = array_get_index($settings, 'user_map', serialize(Array()));
61  $atr_parameter_map = new Asset_Attribute_Parameter_Map(0, $parameter_map_value);
62  $userid = $atr_parameter_map->getParameterValue('user');
63 
64  if (!empty($userid)) {
65  // ensure that the value from the parameter map is a valid asset id
66  $user_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo($userid, Array('user', 'user_group'), FALSE);
67  if (empty($user_info)) return FALSE;
68  } else {
69  // no user id has been supplied by the parameter map, so use the static userid
70  $userid = $settings['userid'];
71  }
72 
73  // if we still have no userid, bail.
74  if (empty($userid)) return FALSE;
75 
76  if (empty($state['assetid'])) return FALSE;
77  $granted = array_get_index($settings, 'granted', TRUE);
78  $dependant_only = array_get_index($settings, 'dependants_only', FALSE);
79  $dependant_parents = array_get_index($settings, 'dependant_parents', FALSE);
80  $cascade_to_new = array_get_index($settings, 'cascade_to_new', TRUE);
81 
82  // If permission change fails, fail silently
83  $mute_error = array_get_index($settings, 'mute_error', FALSE);
84 
85  // if dependant_parents is turned on, look for all our dependant parents
86  // and change their statuses, instead of our own
87  $target_assets = Array();
88  if ($dependant_parents) {
89  // get only the top level
90  $target_assets = $GLOBALS['SQ_SYSTEM']->am->getDependantParents($state['assetid'], '', TRUE, FALSE);
91  if (!empty($target_assets)) {
92  // returns Array(0 => Assetid), so flip the array
93  $target_assets = array_flip($target_assets);
94  foreach ($target_assets as $assetid => $null) {
95  $target_assets[$assetid] = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
96  }
97  }
98  } else {
99  $target_assets[$state['assetid']] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
100  }
101 
102  // need to use a hipo job to set status (handles dependant children)
103  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
104  $hipo_assets = Array();
105  foreach ($target_assets as $assetid => $asset) {
106  $hipo_assets[] = $assetid;
107  }
108  $vars['permission_changes'] = Array(
109  Array(
110  'permission' => $settings['permission'],
111  'granted' => $granted,
112  'userid' => $userid,
113  'previous_access' => NULL,
114  'dependants_only' => $dependant_only,
115  'cascades' => $cascade_to_new,
116  'assetids' => $hipo_assets,
117  ),
118  );
119  $vars['dependants_only'] = $dependant_only;
120 
121  $status_errors = $hh->freestyleHipo('hipo_job_edit_permissions', $vars);
122  if (!$mute_error && !empty($status_errors)) return FALSE;
123 
124  return Array(
125  'assetid' => $state['assetid'],
126  'userid' => $userid,
127  'permission' => $settings['permission'],
128  'granted' => $granted,
129  'cascades' => $cascade_to_new,
130  );
131 
132  }//end execute()
133 
134 
145  public static function getInterface($settings, $prefix, $write_access=FALSE)
146  {
147  $current_schemaid = array_get_index($settings, 'schemaid', 0);
148  $action = array_get_index($settings, 'action', 1);
149  $granted = array_get_index($settings, 'granted', 1);
150  $permission = array_get_index($settings, 'permission', SQ_PERMISSION_READ);
151  $userid = array_get_index($settings, 'userid', 0);
152  $dependants_only = array_get_index($settings, 'dependants_only', FALSE);
153  $dependant_parents = array_get_index($settings, 'dependant_parents', FALSE);
154  $cascade_to_new = array_get_index($settings, 'cascade_to_new', TRUE);
155 
156  ob_start();
157 
158  // attribute friendly prefix
159  $new_prefix = str_replace(array('[',']'), '_', $prefix);
160  hidden_field($prefix.'[new_prefix]', $new_prefix);
161 
162  // print the grant/deny selection
163  if ($write_access) {
164  combo_box($prefix.'[granted]',
165  Array (
166  '1' => translate('grant'),
167  '0' => translate('deny'),
168  '-1' => translate('delete'),
169  ),
170  FALSE,
171  $granted);
172  } else {
173  echo '<b>';
174  switch ($granted) {
175  case 1:
176  echo translate('grant');
177  break;
178  case 0:
179  echo translate('deny');
180  break;
181  case -1:
182  echo translate('delete');
183  break;
184  }
185  echo '</b> ';
186  }
187 
188  // print the permission level selection
190 
191  if ($write_access) {
192  combo_box($prefix.'[permission]', $permissions, FALSE, $permission);
193  } else {
194  echo '<b>'.$permissions[$permission].'</b>';
195  }
196  echo ' '.translate('access_for').' ';
197 
198  // print the asset finder
199  if ($write_access) {
200  asset_finder(
201  $prefix.'[userid]',
202  $userid,
203  Array(
204  'user' => 'D',
205  'user_group' => 'D',
206  )
207  );
208  } else {
209  $user = NULL;
210  if ($userid > 0) {
211  $user = $GLOBALS['SQ_SYSTEM']->am->getAsset($userid);
212  }
213  if (is_null($user)) {
214  echo '<b>['.translate('trigger_no_user_selected').']</b>';
215  } else {
216  echo '<b>'.$user->name.'</b>';
217  }
218  }
219 
220  $parameter_map_value = array_get_index($settings, 'user_map', serialize(Array()));
221 
222  $atr_parameter_map = new Asset_Attribute_Parameter_Map(0, $parameter_map_value);
223 
224  $atr_parameter_map->setParameter('user', 'Userid or Groupid');
225  echo $atr_parameter_map->paint($new_prefix.'_parameter_map', !$write_access);
226 
227  // Only cascade to our dependants (and not all our children)?
228  if ($write_access) {
229  echo '<br />';
230  check_box($prefix.'[dependants_only]', 1, $dependants_only);
231  echo translate('trigger_set_permission_dependants_only');
232  } else {
233  echo '<img src="'.sq_web_path('lib').'/web/images/'.($dependants_only ? 'tick' : 'cross').'.gif" alt="'.($dependants_only ? translate('yes') : translate('no')).'" /> ';
234  echo translate('trigger_set_permission_dependants_only');
235  }
236 
237  // Look for dependant parents (and change them instead)?
238  if ($write_access) {
239  echo '<br />';
240  check_box($prefix.'[dependant_parents]', 1, $dependant_parents);
241  echo translate('trigger_set_permission_dependant_parents');
242  } else {
243  echo '<br />';
244  echo '<img src="'.sq_web_path('lib').'/web/images/'.($dependant_parents ? 'tick' : 'cross').'.gif" alt="'.($dependant_parents ? translate('yes') : translate('no')).'" /> ';
245  echo translate('trigger_set_permission_dependant_parents');
246  }
247 
248  // Cascade to new children
249  if ($write_access) {
250  echo '<br />';
251  check_box($prefix.'[cascade_to_new]', 1, $cascade_to_new);
252  echo translate('cascade_permission_to_new_children');
253  } else {
254  echo '<br />';
255  echo '<img src="'.sq_web_path('lib').'/web/images/'.($cascade_to_new ? 'tick' : 'cross').'.gif" alt="'.($cascade_to_new ? translate('yes') : translate('no')).'" /> ';
256  echo translate('cascade_permission_to_new_children');
257  }
258 
259  return ob_get_clean();
260 
261  }//end getInterface()
262 
263 
275  public static function processInterface(&$settings, $request_data)
276  {
277  $new_prefix = str_replace(array('[',']'), '_', array_get_index($request_data, 'new_prefix', ''));
278 
279  // check if the permission is valid
280  $permission = array_get_index($request_data, 'permission', FALSE);
281  if (!$permission) return 'Permission not specified';
282 
284  if (isset($valid_permissions[$permission])) {
285  $settings['permission'] = $permission;
286  } else {
287  return 'Specified permission is invalid';
288  }
289 
290  // access must be either 1 or 0 or -1
291  $granted = array_get_index($request_data, 'granted', FALSE);
292  if ($granted < -1 || $granted > 1) {
293  return 'Access parameter is invalid';
294  }
295  $settings['granted'] = $granted;
296 
297  // The selected userid/groupid must be valid.
298  $user_data = array_get_index($request_data, 'userid', FALSE);
299  $userid = $user_data['assetid'];
300 
301  if (!empty($userid)) {
302  $user = $GLOBALS['SQ_SYSTEM']->am->getAsset($userid);
303  if (is_null($user)) {
304  return 'Specified user is not a valid asset';
305  }
306  }
307  $settings['userid'] = $userid;
308 
309  $atr_parameter_map = new Asset_Attribute_Parameter_Map();
310  $atr_parameter_map->process($new_prefix.'_parameter_map');
311  $settings['user_map'] = $atr_parameter_map->value;
312 
313  // Check we have a static id OR some parameter map settings. The param map settings
314  // can't really be validated, but checking for something means we ensure they've at least been
315  // reminded to configure it.
316  $user_map = unserialize($settings['user_map']);
317  if (empty($settings['userid']) && (empty($user_map))) {
318  return 'No user or group id provided';
319  }
320 
321  // dependants_only
322  $dependants_only = array_get_index($request_data, 'dependants_only', FALSE);
323  $settings['dependants_only'] = ($dependants_only == '1') ? TRUE : FALSE;
324 
325  // dependant_parents
326  $dependant_parents = array_get_index($request_data, 'dependant_parents', FALSE);
327  $settings['dependant_parents'] = ($dependant_parents == '1') ? TRUE : FALSE;
328 
329  // dependant_parents
330  $cascade_to_new = array_get_index($request_data, 'cascade_to_new', FALSE);
331  $settings['cascade_to_new'] = ($cascade_to_new == '1') ? TRUE : FALSE;
332 
333  return FALSE;
334 
335  }//end processInterface()
336 
337 
344  public static function _getPermissionList()
345  {
346  return Array(
347  SQ_PERMISSION_READ => 'read',
348  SQ_PERMISSION_WRITE => 'write',
349  SQ_PERMISSION_ADMIN => 'admin',
350  );
351 
352  }//end _getPermissionList()
353 
354 
364  public static function getLocks($settings, &$state)
365  {
366  return Array($state['assetid'] => Array('permissions'));
367 
368  }//end getLocks()
369 
370 
380  public static function addSettingUserid($value, $settings)
381  {
382 
383  $settings['userid'] = $value;
384  return $settings;
385 
386  }//end addSettingUserid()
387 
388 
389 }//end class
390 
391 ?>