Ejemplo n.º 1
0
 /**
  * Create and add elements to this form
  *
  * @param   array   $formData   The data sent by the user
  */
 public function createElements(array $formData)
 {
     // TODO(jom): Fetching already existing members to prevent the user from mistakenly creating duplicate
     // memberships (no matter whether the data source permits it or not, a member does never need to be
     // added more than once) should be kept at backend level (GroupController::fetchUsers) but this does
     // not work currently as our ldap protocol stuff is unable to handle our filter implementation..
     $members = $this->backend->select()->from('group_membership', array('user_name'))->where('group_name', $this->groupName)->fetchColumn();
     $filter = empty($members) ? Filter::matchAll() : Filter::not(Filter::where('user_name', $members));
     $users = $this->ds->select()->from('user', array('user_name'))->applyFilter($filter)->fetchColumn();
     if (!empty($users)) {
         $this->addElement('multiselect', 'user_name', array('multiOptions' => array_combine($users, $users), 'label' => $this->translate('Backend Users'), 'description' => $this->translate('Select one or more users (fetched from your user backends) to add as group member'), 'class' => 'grant-permissions'));
     }
     $this->addElement('textarea', 'users', array('required' => empty($users), 'label' => $this->translate('Users'), 'description' => $this->translate('Provide one or more usernames separated by comma to add as group member')));
     $this->setTitle(sprintf($this->translate('Add members for group %s'), $this->groupName));
     $this->setSubmitLabel($this->translate('Add'));
 }
Ejemplo n.º 2
0
 protected function applyChanges($changes)
 {
     $filter = $this->filter;
     $pairs = array();
     $addTo = null;
     $add = array();
     foreach ($changes as $k => $v) {
         if (preg_match('/^(column|value|sign|operator)((?:_new)?)_([\\d-]+)$/', $k, $m)) {
             if ($m[2] === '_new') {
                 if ($addTo !== null && $addTo !== $m[3]) {
                     throw new \Exception('F...U');
                 }
                 $addTo = $m[3];
                 $add[$m[1]] = $v;
             } else {
                 $pairs[$m[3]][$m[1]] = $v;
             }
         }
     }
     $operators = array();
     foreach ($pairs as $id => $fs) {
         if (array_key_exists('operator', $fs)) {
             $operators[$id] = $fs['operator'];
         } else {
             $f = $filter->getById($id);
             $f->setColumn($fs['column']);
             if ($f->getSign() !== $fs['sign']) {
                 if ($f->isRootNode()) {
                     $filter = $f->setSign($fs['sign']);
                 } else {
                     $filter->replaceById($id, $f->setSign($fs['sign']));
                 }
             }
             $f->setExpression($fs['value']);
         }
     }
     krsort($operators, version_compare(PHP_VERSION, '5.4.0') >= 0 ? SORT_NATURAL : SORT_REGULAR);
     foreach ($operators as $id => $operator) {
         $f = $filter->getById($id);
         if ($f->getOperatorName() !== $operator) {
             if ($f->isRootNode()) {
                 $filter = $f->setOperatorName($operator);
             } else {
                 $filter->replaceById($id, $f->setOperatorName($operator));
             }
         }
     }
     if ($addTo !== null) {
         if ($addTo === '0') {
             $filter = Filter::expression($add['column'], $add['sign'], $add['value']);
         } else {
             $parent = $filter->getById($addTo);
             $f = Filter::expression($add['column'], $add['sign'], $add['value']);
             if (isset($add['operator'])) {
                 switch ($add['operator']) {
                     case 'AND':
                         if ($parent->isExpression()) {
                             if ($parent->isRootNode()) {
                                 $filter = Filter::matchAll(clone $parent, $f);
                             } else {
                                 $filter = $filter->replaceById($addTo, Filter::matchAll(clone $parent, $f));
                             }
                         } else {
                             $parent->addFilter(Filter::matchAll($f));
                         }
                         break;
                     case 'OR':
                         if ($parent->isExpression()) {
                             if ($parent->isRootNode()) {
                                 $filter = Filter::matchAny(clone $parent, $f);
                             } else {
                                 $filter = $filter->replaceById($addTo, Filter::matchAny(clone $parent, $f));
                             }
                         } else {
                             $parent->addFilter(Filter::matchAny($f));
                         }
                         break;
                     case 'NOT':
                         if ($parent->isExpression()) {
                             if ($parent->isRootNode()) {
                                 $filter = Filter::not(Filter::matchAll($parent, $f));
                             } else {
                                 $filter = $filter->replaceById($addTo, Filter::not(Filter::matchAll($parent, $f)));
                             }
                         } else {
                             $parent->addFilter(Filter::not($f));
                         }
                         break;
                 }
             } else {
                 $parent->addFilter($f);
             }
         }
     }
     return $filter;
 }