Esempio n. 1
0
 /**
  * Perhaps subject can be omitted, and defaults to a value that will allow the an action
  * for all subjects? Eg, display.
  */
 public static function add($role, $action, $subject, $subject_id = 0, array $options = array())
 {
     if (!Backend::getDB('default')) {
         return false;
     }
     //Loop through arrays
     if (is_array($role)) {
         $result = 0;
         foreach ($role as $one_role) {
             if (self::add($one_role, $action, $subject, $subject_id, $options)) {
                 $result++;
             }
         }
         return $result;
     }
     if (is_array($action)) {
         $result = 0;
         foreach ($action as $one_action) {
             if (self::add($role, $one_action, $subject, $subject_id, $options)) {
                 $result++;
             }
         }
         return $result;
     }
     if (is_array($subject)) {
         $result = 0;
         foreach ($subject as $one_subject) {
             if (self::add($role, $role, $one_subject, $subject_id, $options)) {
                 $result++;
             }
         }
         return $result;
     }
     if (is_array($subject_id)) {
         $options = $subject_id;
         $subject_id = 0;
     }
     $control = array_key_exists('control', $options) ? $options['control'] : '100';
     $system = array_key_exists('system', $options) ? $options['system'] : 0;
     $data = array('role' => $role, 'action' => $action, 'subject' => class_for_url($subject), 'subject_id' => $subject_id, 'control' => $control, 'system' => $system, 'active' => 1);
     $permission = new PermissionObj();
     if ($permission->replace($data) !== false) {
         Backend::addSuccess('Added permission to ' . $action . ' for ' . $role);
         $result = true;
     } else {
         Backend::addError('Could not add permission to ' . $action . ' for ' . $role);
         $result = false;
     }
     return $result;
 }
Esempio n. 2
0
 public function post_permissions($component = false)
 {
     $parameters = array();
     $query = new DeleteQuery('Permission');
     $query->filter("`role` != 'nobody'")->filter("`role` != 'superadmin'");
     if ($component) {
         $query->filter('`subject` = :component');
         $parameters[':component'] = class_for_url($component);
     }
     $result = $query->execute($parameters);
     if ($result === false) {
         Backend::addError('Could not empty permissions table');
         return false;
     }
     $permission = new PermissionObj();
     $count = 0;
     foreach (Controller::getPayload() as $key => $roles) {
         if (strpos($key, '::') === false) {
             continue;
         }
         list($subject, $action) = explode('::', $key, 2);
         foreach ($roles as $role => $value) {
             $data = array('subject' => $subject, 'action' => $action, 'role' => $role);
             if ($permission->replace($data)) {
                 $count++;
             }
         }
     }
     return $count;
 }