Debian Codegraph @

lib/modules/account.inc

raw
1 <?php
2 /*
3
4 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
5 Copyright (C) 2003 - 2006 Tilo Lutz
6 2005 - 2026 Roland Gruber
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 /**
24 * Manages the object class "account" for users and hosts.
25 *
26 * @package modules
27 *
28 * @author Tilo Lutz
29 * @author Roland Gruber
30 * @author Michael Duergner
31 */
32
33 use LAM\TYPES\ConfiguredType;
34
35 /**
36 * Manages the object class "account" for users and hosts.
37 *
38 * @package modules
39 */
40 class account extends baseModule {
41
42 /**
43 * {@inheritDoc}
44 */
45 public function can_manage(): bool {
46 return in_array($this->get_scope(), ["host", "user"]);
47 }
48
49 /**
50 * {@inheritDoc}
51 */
52 public function get_metaData(): array {
53 $return = [];
54 // icon
55 $return['icon'] = 'user.svg';
56 // alias name
57 $return["alias"] = _('Account');
58 // this is a base module
59 $return["is_base"] = true;
60 // LDAP filter
61 $return["ldap_filter"] = ['or' => "(objectClass=account)"];
62 // RDN attribute
63 $return["RDN"] = ["uid" => "low"];
64 // module dependencies
65 $return['dependencies'] = ['depends' => [], 'conflicts' => []];
66 // managed object classes
67 $return['objectClasses'] = ['account'];
68 // LDAP aliases
69 $return['LDAPaliases'] = ['userid' => 'uid'];
70 // managed attributes
71 $return['attributes'] = ['uid', 'description'];
72 // available PDF fields
73 $return['PDF_fields'] = [
74 'description' => _('Description')
75 ];
76 // help Entries
77 $return['help'] = [
78 'uidHost' => [
79 "Headline" => _("Host name"), 'attr' => 'uid',
80 "Text" => _("Host name of the host which should be created. Valid characters are: a-z,A-Z,0-9, .-_$. Host names are always ending with $. If last character is not $ it will be added. If host name is already used host name will be expanded with a number. The next free number will be used.")
81 ],
82 'descriptionHost' => [
83 "Headline" => _("Description"), 'attr' => 'description',
84 "Text" => _("Host description. If left empty host name will be used.")
85 ],
86 'uidUser' => [
87 "Headline" => _("User name"), 'attr' => 'uid',
88 "Text" => _("User name of the user who should be created. Valid characters are: a-z,A-Z,0-9, @.-_.")
89 ],
90 'descriptionUser' => [
91 "Headline" => _("Description"), 'attr' => 'description',
92 "Text" => _("User description. If left empty user name will be used.")
93 ]
94 ];
95 // upload columns
96 $descriptionHelpId = ($this->get_scope() === 'user') ? 'descriptionUser' : 'descriptionHost';
97 $return['upload_columns'][] = [
98 'name' => 'account_description',
99 'description' => _('Description'),
100 'help' => $descriptionHelpId,
101 'example' => ''
102 ];
103 return $return;
104 }
105
106 /**
107 * Returns if the Unix module is active for the current account type.
108 *
109 * @return bool Unix active
110 */
111 private function isUnixActive() {
112 if ($this->getAccountContainer() != null) {
113 return $this->getAccountContainer()->getAccountModule('posixAccount') != null;
114 }
115 return false;
116 }
117
118 /**
119 * {@inheritDoc}
120 */
121 protected function load_Messages(): void {
122 $this->messages['uid'][0] = ['ERROR', _('User name'), _('User name contains invalid characters. Valid characters are: a-z, A-Z, 0-9 and .-_ !')];
123 $this->messages['uid'][1] = ['ERROR', _('Account %s:') . ' posixAccount_userName', _('User name contains invalid characters. Valid characters are: a-z, A-Z, 0-9 and .-_ !')];
124 $this->messages['uid'][2] = ['WARN', _('User name'), _('You are using capital letters. This can cause problems because Windows is not case-sensitive.')];
125 $this->messages['uid'][3] = ['ERROR', _('User name'), _('User name contains invalid characters. Valid characters are: a-z, A-Z, 0-9 and .-_ !')];
126 }
127
128 /**
129 * {@inheritDoc}
130 */
131 public function module_complete(): bool {
132 return $this->isUnixActive() || !empty($this->attributes['uid'][0]);
133 }
134
135 /**
136 * {@inheritDoc}
137 */
138 public function getButtonStatus(): string {
139 if (!$this->getAccountContainer()->isNewAccount) {
140 // check if account is based on our object class
141 $objectClasses = $this->getAccountContainer()->attributes_orig['objectClass'];
142 if (is_array($objectClasses) && !in_array('account', $objectClasses)) {
143 return "disabled";
144 }
145 }
146 return "enabled";
147 }
148
149 /**
150 * {@inheritDoc}
151 */
152 public function save_attributes(): array {
153 // skip saving if account is based on another structural object class
154 if (!$this->getAccountContainer()->isNewAccount && !in_array('account', $this->getAccountContainer()->attributes_orig['objectClass'])) {
155 return [];
156 }
157 return $this->getAccountContainer()->save_module_attributes($this->attributes, $this->orig);
158 }
159
160 /**
161 * {@inheritDoc}
162 */
163 public function process_attributes(): array {
164 $errors = [];
165 // Load attributes
166 $this->attributes['description'][0] = $_POST['description'];
167 // user name if no posixAccount
168 if (!$this->isUnixActive()) {
169 $this->attributes['uid'][0] = $_POST['uid'];
170 if (!get_preg($this->attributes['uid'][0], '!upper')) {
171 $errors[] = $this->messages['uid'][2];
172 }
173 if (!get_preg($this->attributes['uid'][0], 'username')) {
174 $errors[] = $this->messages['uid'][3];
175 }
176 }
177 return $errors;
178 }
179
180 /**
181 * {@inheritDoc}
182 */
183 public function display_html_attributes(): htmlElement|array {
184 $container = new htmlResponsiveRow();
185 // user name if no posixAccount
186 if (!$this->isUnixActive()) {
187 $title = _('User name');
188 if ($this->get_scope() == 'host') {
189 $title = _('Host name');
190 }
191 $uidInput = $this->addSimpleInputTextField($container, 'uid', $title, true);
192 $uidInput->setFieldMaxLength(100);
193 $uidHelpId = ($this->get_scope() === 'user') ? 'uidUser' : 'uidHost';
194 $uidInput->setHelpId($uidHelpId);
195 }
196 // description
197 $descriptionInput = $this->addSimpleInputTextField($container, 'description', _('Description'));
198 $descriptionHelpId = ($this->get_scope() === 'user') ? 'descriptionUser' : 'descriptionHost';
199 $descriptionInput->setHelpId($descriptionHelpId);
200 return $container;
201 }
202
203 /**
204 * {@inheritDoc}
205 */
206 public function get_pdfFields(string $typeId): array {
207 $fields = parent::get_pdfFields($typeId);
208 if (!$this->isUnixActive()) {
209 $fields['uid'] = _('User name');
210 }
211 return $fields;
212 }
213
214 /**
215 * {@inheritDoc}
216 */
217 public function get_pdfEntries(array $pdfKeys, string $typeId): array {
218 $return = [];
219 $this->addSimplePDFField($return, 'description', _('Description'));
220 $this->addSimplePDFField($return, 'uid', _('User name'));
221 return $return;
222 }
223
224 /**
225 * {@inheritDoc}
226 */
227 public function get_uploadColumns(array $selectedModules, ConfiguredType $type): array {
228 $return = parent::get_uploadColumns($selectedModules, $type);
229 if (!in_array('posixAccount', $selectedModules)) {
230 $uidHelpId = ($this->get_scope() === 'user') ? 'uidUser' : 'uidHost';
231 $return[] = [
232 'name' => 'account_uid',
233 'description' => _('User name'),
234 'help' => $uidHelpId,
235 'required' => true
236 ];
237 }
238 return $return;
239 }
240
241 /**
242 * {@inheritDoc}
243 */
244 public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array {
245 $messages = [];
246 for ($i = 0; $i < count($rawAccounts); $i++) {
247 // add object class
248 if (!in_array("account", $partialAccounts[$i]['objectClass'])) {
249 $partialAccounts[$i]['objectClass'][] = "account";
250 }
251 // description
252 if (isset($rawAccounts[$i][$ids['account_description']]) && ($rawAccounts[$i][$ids['account_description']] !== '')) {
253 $partialAccounts[$i]['description'] = $rawAccounts[$i][$ids['account_description']];
254 }
255 elseif (isset($ids['account_uid']) && isset($rawAccounts[$i][$ids['account_uid']])) {
256 $partialAccounts[$i]['description'] = $rawAccounts[$i][$ids['account_uid']];
257 }
258 elseif (isset($partialAccounts[$i]['uid'])) {
259 $partialAccounts[$i]['description'] = $partialAccounts[$i]['uid'];
260 }
261 if (!in_array('posixAccount', $selectedModules)) {
262 // user name
263 $this->mapSimpleUploadField($rawAccounts, $ids, $partialAccounts, $i, 'account_uid', 'uid', 'username', $this->messages['uid'][1], $messages);
264 }
265 }
266 return $messages;
267 }
268
269 /**
270 * @inheritDoc
271 */
272 public function getListAttributeDescriptions(ConfiguredType $type): array {
273 return [
274 "description" => _("Description"),
275 'uid' => _("User name")
276 ];
277 }
278
279 }