FusionDirectory
class_BaseSelectorAttribute.inc
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2012-2016 FusionDirectory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
25 {
26  private $baseSelector = NULL;
27  private $orig_dn = NULL;
28  private $ou = NULL;
29 
36  function __construct ($ou, $label = NULL, $desc = NULL)
37  {
38  if ($label === NULL) {
39  $label = _('Base');
40  }
41  if ($desc === NULL) {
42  $desc = _('Object base');
43  }
44  parent::__construct($label, $desc, 'base', FALSE, '', 'base');
45  $this->setInLdap(FALSE);
46  $this->ou = $ou;
47  }
48 
49  function setManagedAttributes (array $dontcare)
50  {
51  trigger_error('method setManagedAttributes is not supported for BaseSelectorAttribute');
52  }
53 
54  function setParent (&$plugin)
55  {
56  parent::setParent($plugin);
57  if (is_object($this->plugin)) {
58  /* Do base conversation */
59  if ($this->plugin->is_template) {
60  $this->ou = 'ou=templates,'.$this->ou;
61  }
62  if ($this->plugin->dn == 'new') {
63  $ui = get_userinfo();
64  $this->setValue($ui->getCurrentBase());
65  } else {
66  $this->setValue(dn2base($this->plugin->dn, $this->ou));
67  }
68  $this->orig_dn = $this->plugin->dn;
69  /* Instanciate base selector */
70  $this->initialValue = $this->value;
71  $this->baseSelector = new baseSelector($this->plugin->get_allowed_bases(), $this->value);
72  $this->baseSelector->setSubmitButton(FALSE);
73  $this->baseSelector->setHeight(300);
74  $this->baseSelector->update(TRUE);
75  }
76  }
77 
78  function loadPostValue ()
79  {
80  }
81 
82  function applyPostValue ()
83  {
84  if (!$this->disabled && $this->isVisible()
85  && ($this->plugin->acl_is_moveable($this->value) || ($this->plugin->dn == 'new'))) {
86  /* Refresh base */
87  if (!$this->baseSelector->update()) {
88  if ($this->plugin->dn == 'new') {
89  $error = new SimplePluginPermissionError($this, msgPool::permCreate());
90  } else {
91  $error = new SimplePluginPermissionError($this, msgPool::permMove($this->plugin->dn));
92  }
93  $error->display();
94  }
95  if ($this->value != $this->baseSelector->getBase()) {
96  $this->setValue($this->baseSelector->getBase());
97  }
98  }
99  }
100 
101  function check ()
102  {
103  $error = parent::check();
104  if (!empty($error)) {
105  return $error;
106  } else {
107  /* Check if we are allowed to create/move this user */
108  if (($this->orig_dn == 'new') && !$this->plugin->acl_is_createable($this->value)) {
110  } elseif (
111  ($this->orig_dn != 'new') &&
112  ($this->plugin->dn != $this->orig_dn) &&
113  !$this->plugin->acl_is_moveable($this->value)) {
114  return new SimplePluginPermissionError($this, msgPool::permMove($this->plugin->dn));
115  }
116  // Check if a wrong base was supplied
117  if (!$this->baseSelector->checkLastBaseUpdate()) {
118  return new SimplePluginCheckError($this, msgPool::check_base());
119  }
120  }
121  }
122 
123  function checkValue ($value)
124  {
125  if (!is_string($value) && (!is_object($value) || !method_exists($value, '__toString'))) {
126  throw new InvalidValueException(_('Base field value should always be a string'));
127  }
128  }
129 
130  function setValue ($value)
131  {
132  parent::setValue($value);
133  if (is_object($this->plugin) &&
134  ($this->baseSelector !== NULL) &&
135  ($this->baseSelector->getBase() !== $this->value)
136  ) {
137  $this->baseSelector->setBase($this->value);
138  }
139  }
140 
141  function getValue ()
142  {
143  return $this->value;
144  }
145 
146  function renderFormInput (): string
147  {
148  $smarty = get_smarty();
149  $smarty->assign('usePrototype', 'true');
150  if ($this->disabled) {
151  $display = $this->renderInputField(
152  'text', '',
153  [
154  'value' => $this->getValue()
155  ]
156  );
157  } else {
158  $display = '{literal}'.$this->baseSelector->render().'{/literal}';
159  }
160  return $this->renderAcl($display);
161  }
162 
163  function getHtmlId (): string
164  {
165  if (isset($this->baseSelector)) {
166  return $this->baseSelector->getInputHtmlId();
167  } else {
168  return '';
169  }
170  }
171 
172  function serializeAttribute (array &$attributes, bool $form = TRUE)
173  {
174  if (!$form || $this->visible) {
175  parent::serializeAttribute($attributes, $form);
176 
177  $attributes[$this->getLdapName()]['choices'] = $this->baseSelector->getBases();
178  }
179  }
180 }
setSubmitButton($flag)
Set a new flag to the submit button.
Exception class which can be thrown if an attribute is set to a value with a non-compatible type...
__construct($ou, $label=NULL, $desc=NULL)
The constructor of BaseSelectorAttribute.
update(bool $force=FALSE)
Update the base.
& get_userinfo()
Return the current userinfo object.
Definition: functions.inc:312
Class Base Selector.
static permCreate($name='')
Display that we have no permission to create an object.
checkLastBaseUpdate()
Check the last base value updated.
& get_smarty()
Get global smarty object.
Definition: functions.inc:324
Error returned by check method of SimplePlugin.
static check_base()
Display error when checking the base.
dn2base($dn, $ou=NULL)
Return the base of a given DN.
Definition: functions.inc:614
setHeight($value)
Set a new value of the member height.
getBase()
Accessor of the base.
This class allow to handle easily an Base selector attribute.
static permMove($name='')
Display that we have no permission to move an object.
renderAcl(string $display)
Add ACL information around display.
getInputHtmlId()
Returns id of the html field.
getBases()
Accessor of the bases.
This class allow to handle easily any kind of LDAP attribute.
setBase(string $base)
Set a new value of the member base.