Debian Codegraph @

lib/types/user.inc

raw
1 <?php
2
3 /*
4
5 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
6 Copyright (C) 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 /**
25 * The account type for user accounts (e.g. Unix, Samba and Kolab).
26 *
27 * @package types
28 * @author Roland Gruber
29 */
30
31 use LAM\TYPES\ConfiguredType;
32
33 /**
34 * The account type for user accounts (e.g. Unix, Samba and Kolab).
35 *
36 * @package types
37 */
38 class user extends baseType {
39
40 /**
41 * Constructs a new user type object.
42 *
43 * @param ConfiguredType $type configuration
44 */
45 public function __construct($type) {
46 parent::__construct($type);
47 $this->LABEL_CREATE_ANOTHER_ACCOUNT = _('Create another user');
48 $this->LABEL_BACK_TO_ACCOUNT_LIST = _('Back to user list');
49 }
50
51 /**
52 * {@inheritDoc}
53 */
54 public function getAlias(): string {
55 return _("Users");
56 }
57
58 /**
59 * {@inheritDoc}
60 */
61 public function getDescription(): string {
62 return _("User accounts (e.g. Unix, Samba and Kolab)");
63 }
64
65 /**
66 * {@inheritDoc}
67 */
68 public function getIcon(): string {
69 return 'user.svg';
70 }
71
72 /**
73 * {@inheritDoc}
74 */
75 public function getListClassName(): string {
76 return lamUserList::class;
77 }
78
79 /**
80 * {@inheritDoc}
81 */
82 public function getDefaultListAttributes(): string {
83 return "#uid;#givenName;#sn;#uidNumber;#gidNumber";
84 }
85
86 /**
87 * {@inheritDoc}
88 */
89 public function getTitleBarTitle(accountContainer $container): ?string {
90 $title = '';
91 // get attributes
92 $personalAttributes = [];
93 if ($container->getAccountModule('inetOrgPerson') != null) {
94 $personalAttributes = $container->getAccountModule('inetOrgPerson')->getAttributes();
95 }
96 elseif ($container->getAccountModule('windowsUser') != null) {
97 $personalAttributes = $container->getAccountModule('windowsUser')->getAttributes();
98 }
99 $accountAttributes = [];
100 if ($container->getAccountModule('account') != null) {
101 $accountAttributes = $container->getAccountModule('account')->getAttributes();
102 }
103 $sambaAttributes = [];
104 if ($container->getAccountModule('sambaSamAccount') != null) {
105 $sambaAttributes = $container->getAccountModule('sambaSamAccount')->getAttributes();
106 }
107 $unixAttributes = [];
108 if ($container->getAccountModule('posixAccount') != null) {
109 $unixAttributes = $container->getAccountModule('posixAccount')->getAttributes();
110 }
111 $mitKerberosAttributes = [];
112 if ($container->getAccountModule('mitKerberosStructural') != null) {
113 $mitKerberosAttributes = $container->getAccountModule('mitKerberosStructural')->getAttributes();
114 }
115 elseif ($container->getAccountModule('mitKerberos') != null) {
116 $mitKerberosAttributes = $container->getAccountModule('mitKerberos')->getAttributes();
117 }
118 // check if the first and last name can be shown
119 if (!empty($personalAttributes['sn'][0]) && !empty($personalAttributes['givenName'][0])) {
120 return $title . htmlspecialchars($personalAttributes['givenName'][0] . ' ' . $personalAttributes['sn'][0]);
121 }
122 // check if a display name is set
123 if (!empty($sambaAttributes['displayName'][0])) {
124 return $title . htmlspecialchars($sambaAttributes['displayName'][0]);
125 }
126 // check if a common name is set
127 if (!empty($personalAttributes['cn'][0])) {
128 return $title . htmlspecialchars($personalAttributes['cn'][0]);
129 }
130 if (!empty($unixAttributes['cn'][0])) {
131 return $title . htmlspecialchars($unixAttributes['cn'][0]);
132 }
133 // check if a username is set
134 if (!empty($unixAttributes['uid'][0])) {
135 return $title . htmlspecialchars($unixAttributes['uid'][0]);
136 }
137 if (!empty($personalAttributes['uid'][0])) {
138 return $title . htmlspecialchars($personalAttributes['uid'][0]);
139 }
140 if (!empty($accountAttributes['uid'][0])) {
141 return $title . htmlspecialchars($accountAttributes['uid'][0]);
142 }
143 if (!empty($mitKerberosAttributes['krbPrincipalName'][0])) {
144 return $title . htmlspecialchars($mitKerberosAttributes['krbPrincipalName'][0]);
145 }
146 if ($container->isNewAccount) {
147 return $title . _("New user");
148 }
149 // fall back to default
150 return $title . parent::getTitleBarTitle($container);
151 }
152
153 /**
154 * {@inheritDoc}
155 */
156 public function getTitleBarSubtitle(accountContainer $container): string {
157 $personalAttributes = null;
158 if ($container->getAccountModule('inetOrgPerson') != null) {
159 $personalAttributes = $container->getAccountModule('inetOrgPerson')->getAttributes();
160 }
161 elseif ($container->getAccountModule('windowsUser') != null) {
162 $personalAttributes = $container->getAccountModule('windowsUser')->getAttributes();
163 }
164 if ($personalAttributes == null) {
165 return '';
166 }
167 $subtitle = '';
168 $spacer = ' &nbsp;&nbsp;&nbsp; ';
169 // check if an email address can be shown
170 if (isset($personalAttributes['mail'][0]) && !empty($personalAttributes['mail'][0])) {
171 $subtitle .= '<a href="mailto:' . htmlspecialchars($personalAttributes['mail'][0]) . '">' . htmlspecialchars($personalAttributes['mail'][0]) . '</a>' . $spacer;
172 }
173 // check if an telephone number can be shown
174 if (isset($personalAttributes['telephoneNumber'][0]) && !empty($personalAttributes['telephoneNumber'][0])) {
175 $subtitle .= _('Telephone number') . ' ' . htmlspecialchars($personalAttributes['telephoneNumber'][0]) . $spacer;
176 }
177 // check if an mobile number can be shown
178 if (isset($personalAttributes['mobile'][0]) && !empty($personalAttributes['mobile'][0])) {
179 $subtitle .= _('Mobile number') . ' ' . htmlspecialchars($personalAttributes['mobile'][0]);
180 }
181 return $subtitle;
182 }
183
184 }
185
186 /**
187 * Generates the list view.
188 *
189 * @package lists
190 * @author Roland Gruber
191 *
192 */
193 class lamUserList extends lamList {
194
195 /** Controls if GID number is translated to group name */
196 private bool $trans_primary = false;
197
198 /**
199 * @var array<string, string> $trans_primary_hash translates GID to group name
200 */
201 private array $trans_primary_hash = [];
202
203 /** ID for config option to translate primary group GIDs to group names */
204 private const TRANS_PRIMARY_OPTION_NAME = "LU_TP";
205
206 /**
207 * Constructor
208 *
209 * @param ConfiguredType $type account type
210 */
211 public function __construct(ConfiguredType $type) {
212 parent::__construct($type);
213 $this->labels = [
214 'nav' => _("User count: %s"),
215 'error_noneFound' => _("No users found!"),
216 'newEntry' => _("New user"),
217 'deleteEntry' => _("Delete selected users")];
218 }
219
220 /**
221 * {@inheritDoc}
222 */
223 protected function listGetParams(): void {
224 parent::listGetParams();
225 // generate hash table for group translation
226 if ($this->trans_primary == "on" && !$this->refresh && (count($this->trans_primary_hash) === 0)) {
227 $this->refreshPrimaryGroupTranslation();
228 }
229 }
230
231 /**
232 * {@inheritDoc}
233 */
234 protected function listRefreshData(): void {
235 parent::listRefreshData();
236 // show group names
237 if ($this->trans_primary == "on") {
238 $this->refreshPrimaryGroupTranslation();
239 }
240 }
241
242 /**
243 * Refreshes the GID to group name cache.
244 */
245 protected function refreshPrimaryGroupTranslation(): void {
246 $this->trans_primary_hash = [];
247 $attrs = ["cn", "gidNumber"];
248 $entries = searchLDAPByAttribute(null, null, 'posixGroup', $attrs, ['group']);
249 $entryCount = count($entries);
250 for ($i = 0; $i < $entryCount; $i++) {
251 $this->trans_primary_hash[$entries[$i]['gidnumber'][0]] = $entries[$i]['cn'][0];
252 }
253 }
254
255 /**
256 * {@inheritDoc}
257 * @see lamList::getTableCellContent()
258 */
259 protected function getTableCellContent(&$entry, &$attribute): ?htmlElement {
260 // check if there is something to display at all
261 if (!isset($entry[$attribute]) || !is_array($entry[$attribute]) || (count($entry[$attribute]) < 1)) {
262 return parent::getTableCellContent($entry, $attribute);
263 }
264 // translate GID to group name
265 if (($attribute == "gidnumber") && ($this->trans_primary == "on")) {
266 if (isset($this->trans_primary_hash[$entry[$attribute][0]])) {
267 return new htmlOutputText($this->trans_primary_hash[$entry[$attribute][0]]);
268 }
269 else {
270 return parent::getTableCellContent($entry, $attribute);
271 }
272 }
273 // print all other attributes
274 else {
275 return parent::getTableCellContent($entry, $attribute);
276 }
277 }
278
279 /**
280 * {@inheritDoc}
281 */
282 protected function listGetAllConfigOptions(): array {
283 $options = parent::listGetAllConfigOptions();
284 $options[] = new lamBooleanListOption(_('Translate GID number to group name'), self::TRANS_PRIMARY_OPTION_NAME);
285 return $options;
286 }
287
288 /**
289 * {@inheritDoc}
290 */
291 protected function listConfigurationChanged(): void {
292 parent::listConfigurationChanged();
293 $tpOption = $this->listGetConfigOptionByID(self::TRANS_PRIMARY_OPTION_NAME);
294 $this->trans_primary = ($tpOption instanceof lamBooleanListOption) && $tpOption->isSelected();
295 }
296
297 /**
298 * Returns if the given attribute can be filtered.
299 * If filtering is not possible then no filter box will be displayed.
300 * <br>
301 * <br>The user list allows no filtering for photos.
302 *
303 * @param String $attr attribute name
304 * @return boolean filtering possible
305 */
306 protected function canBeFiltered($attr) {
307 return strtolower($attr) !== 'jpegphoto';
308 }
309
310 /**
311 * {@inheritDoc}
312 * @see lamList::isFilterMatching()
313 */
314 protected function isFilterMatching(&$data, $filterAttribute, $regex): bool {
315 if (($filterAttribute == 'gidnumber') && ($this->trans_primary == "on")) {
316 if (!isset($data[$filterAttribute])) {
317 return false;
318 }
319 if (!isset($this->trans_primary_hash[$data[$filterAttribute][0]])) {
320 return false;
321 }
322 return preg_match($regex, $this->trans_primary_hash[$data[$filterAttribute][0]]);
323 }
324 return parent::isFilterMatching($data, $filterAttribute, $regex);
325 }
326
327 }