Debian Codegraph @

lib/types/group.inc

raw
1 <?php
2
3 use LAM\TYPES\ConfiguredType;
4 use LAM\TYPES\TypeManager;
5
6 /*
7
8 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
9 Copyright (C) 2005 - 2026 Roland Gruber
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
25 */
26
27 /**
28 * The account type for group accounts (e.g. Unix and Samba).
29 *
30 * @package types
31 * @author Roland Gruber
32 * @author Ludek Finstrle
33 */
34
35 /**
36 * The account type for group accounts (e.g. Unix and Samba).
37 *
38 * @package types
39 */
40 class group extends baseType {
41
42 /**
43 * Constructs a new group type object.
44 *
45 * @param ConfiguredType $type configuration
46 */
47 public function __construct($type) {
48 parent::__construct($type);
49 $this->LABEL_CREATE_ANOTHER_ACCOUNT = _('Create another group');
50 $this->LABEL_BACK_TO_ACCOUNT_LIST = _('Back to group list');
51 if ($this->getType() != null) {
52 $modules = $this->getType()->getModules();
53 if (in_array('organizationalRole', $modules)) {
54 $this->LABEL_CREATE_ANOTHER_ACCOUNT = _('Create another role');
55 $this->LABEL_BACK_TO_ACCOUNT_LIST = _('Back to role list');
56 }
57 }
58 }
59
60 /**
61 * {@inheritDoc}
62 */
63 public function getAlias(): string {
64 if ($this->getType() != null) {
65 $modules = $this->getType()->getModules();
66 if (in_array('organizationalRole', $modules)) {
67 return _('Roles');
68 }
69 }
70 return _("Groups");
71 }
72
73 /**
74 * {@inheritDoc}
75 */
76 public function getDescription(): string {
77 return _("Group accounts (e.g. Unix and Samba)");
78 }
79
80 /**
81 * {@inheritDoc}
82 */
83 public function getIcon(): string {
84 return 'group.svg';
85 }
86
87 /**
88 * {@inheritDoc}
89 */
90 public function getListClassName(): string {
91 return lamGroupList::class;
92 }
93
94 /**
95 * {@inheritDoc}
96 */
97 public function getDefaultListAttributes(): string {
98 return "#cn;#gidNumber;#memberUID;#description";
99 }
100
101 /**
102 * {@inheritDoc}
103 */
104 public function getTitleBarTitle(accountContainer $container): ?string {
105 // get attributes
106 $attributes = null;
107 if ($container->getAccountModule('posixGroup') != null) {
108 $attributes = $container->getAccountModule('posixGroup')->getAttributes();
109 }
110 elseif ($container->getAccountModule('rfc2307bisPosixGroup') != null) {
111 $attributes = $container->getAccountModule('rfc2307bisPosixGroup')->getAttributes();
112 }
113 elseif ($container->getAccountModule('windowsGroup') != null) {
114 $attributes = $container->getAccountModule('windowsGroup')->getAttributes();
115 }
116 $gonAttributes = null;
117 if ($container->getAccountModule('groupOfNames') != null) {
118 $gonAttributes = $container->getAccountModule('groupOfNames')->getAttributes();
119 }
120 elseif ($container->getAccountModule('groupOfUniqueNames') != null) {
121 $gonAttributes = $container->getAccountModule('groupOfUniqueNames')->getAttributes();
122 }
123 elseif ($container->getAccountModule('organizationalRole') != null) {
124 $gonAttributes = $container->getAccountModule('organizationalRole')->getAttributes();
125 }
126 // check if a group name is set
127 if (isset($attributes['gid'][0])) {
128 return htmlspecialchars($attributes['gid'][0]);
129 }
130 // check if a common name is set
131 if (isset($attributes['cn'][0])) {
132 return htmlspecialchars($attributes['cn'][0]);
133 }
134 if (isset($gonAttributes['cn'][0])) {
135 return htmlspecialchars($gonAttributes['cn'][0]);
136 }
137 // new entry
138 if ($container->isNewAccount) {
139 return _("New group");
140 }
141 // fall back to default
142 return parent::getTitleBarTitle($container);
143 }
144
145 /**
146 * {@inheritDoc}
147 */
148 public function getTitleBarSubtitle(accountContainer $container): string {
149 // get attributes
150 $attributes = null;
151 if ($container->getAccountModule('posixGroup') != null) {
152 $attributes = $container->getAccountModule('posixGroup')->getAttributes();
153 }
154 elseif ($container->getAccountModule('rfc2307bisPosixGroup') != null) {
155 $attributes = $container->getAccountModule('rfc2307bisPosixGroup')->getAttributes();
156 }
157 elseif ($container->getAccountModule('windowsGroup') != null) {
158 $attributes = $container->getAccountModule('windowsGroup')->getAttributes();
159 }
160 $gonAttributes = null;
161 if ($container->getAccountModule('groupOfNames') != null) {
162 $gonAttributes = $container->getAccountModule('groupOfNames')->getAttributes();
163 }
164 elseif ($container->getAccountModule('groupOfUniqueNames') != null) {
165 $gonAttributes = $container->getAccountModule('groupOfUniqueNames')->getAttributes();
166 }
167 elseif ($container->getAccountModule('organizationalRole') != null) {
168 $gonAttributes = $container->getAccountModule('organizationalRole')->getAttributes();
169 }
170 // check if a description can be shown
171 if (($attributes != null) && isset($attributes['description'][0])) {
172 return htmlspecialchars($attributes['description'][0]);
173 }
174 if (($gonAttributes != null) && isset($gonAttributes['description'][0])) {
175 return htmlspecialchars($gonAttributes['description'][0]);
176 }
177 return '';
178 }
179
180 }
181
182 /**
183 * Generates the list view.
184 *
185 * @package lists
186 * @author Roland Gruber
187 *
188 */
189 class lamGroupList extends lamList {
190
191 /** @var array<string, string[]> $primary_hash primary group members hash */
192 private array $primary_hash = [];
193 /** Controls if primary group members needs refresh */
194 private bool $refresh_primary = false;
195 /** ID for config option */
196 private const TRANS_PRIMARY_OPTION_NAME = "LG_TP";
197 /** specifies if primary group members are visible */
198 private bool $include_primary = false;
199 /** @var array<string, string> $suffixList LDAP suffix of membership types */
200 private array $suffixList = [];
201
202 /**
203 * Constructor
204 *
205 * @param ConfiguredType $type account type
206 */
207 public function __construct(ConfiguredType $type) {
208 parent::__construct($type);
209 $this->labels = [
210 'nav' => _("Group count: %s"),
211 'error_noneFound' => _("No groups found!"),
212 'newEntry' => _("New group"),
213 'deleteEntry' => _("Delete selected groups")
214 ];
215 $modules = $this->type->getModules();
216 if (in_array('organizationalRole', $modules)) {
217 $this->labels = [
218 'nav' => _("Role count: %s"),
219 'error_noneFound' => _("No roles found!"),
220 'newEntry' => _("New role"),
221 'deleteEntry' => _("Delete selected roles")
222 ];
223 }
224 // build suffix list for account types
225 $typeManager = new TypeManager();
226 $scopes = ['user', 'gon', 'group'];
227 $types = $typeManager->getConfiguredTypesForScopes($scopes);
228 foreach ($types as $type) {
229 $suffix = $type->getSuffix();
230 // stop if suffixes are not unique
231 if (isset($this->suffixList[$suffix])) {
232 $this->suffixList = [];
233 break;
234 }
235 if (!empty($suffix)) {
236 $this->suffixList[$suffix] = $type->getId();
237 }
238 }
239 }
240
241 /**
242 * {@inheritDoc}
243 */
244 protected function listGetParams(): void {
245 parent::listGetParams();
246 // generate list primary group members
247 // after parent::listGetParams is $this->refresh set to correct value
248 if ($this->include_primary && !$this->refresh && ($this->refresh_primary || (count($this->primary_hash) === 0))) {
249 $this->groupRefreshPrimary();
250 }
251 }
252
253 /**
254 * {@inheritDoc}
255 * @see lamList::getTableCellContent()
256 */
257 protected function getTableCellContent(&$entry, &$attribute): ?htmlElement {
258 if ($attribute == "memberuid") {
259 // $gid is used for linking primary group members
260 $gid = -1;
261 $use_primary = false;
262 if ($this->include_primary == "on") {
263 // Get the gid number
264 if (isset($entry['gidnumber']) && is_array($entry['gidnumber'])) {
265 $gid = $entry['gidnumber'][0];
266 }
267 $use_primary = (($gid >= 0) && (count($this->primary_hash) > 0)
268 && isset($this->primary_hash[$gid])
269 && ($this->primary_hash[$gid] !== []));
270 }
271 if (!$use_primary) {
272 if (!isset($entry[$attribute]) || !is_array($entry[$attribute]) || (count($entry[$attribute]) < 1)) {
273 return null;
274 }
275 // sort array
276 sort($entry[$attribute]);
277 }
278 // make a link for each member of the group
279 $linklist = [];
280 if ($use_primary) {
281 $primary_hash = $this->primary_hash[$gid];
282 // merge primary members into secondary ones
283 $primaryvals = array_flip(array_values($primary_hash));
284 // test if group has some secondary members
285 $attr = (isset($entry[$attribute]) && is_array($entry[$attribute])) ? array_merge($primary_hash, $entry[$attribute]) : $primary_hash;
286 // sort array
287 sort($attr);
288
289 // make a link for each member of the group
290 for ($d = 0; $d < count($attr); $d++) {
291 $separator = ($d < (count($attr) - 1)) ? ';' : '';
292 $cssClasses = ($d >= htmlShowMore::MAXIMUM_SIZE_LARGE) ? [htmlShowMore::CLASS_SHOW_MORE] : [];
293 $user = $attr[$d]; // user name
294 if (isAccountTypeHidden('user')) {
295 $linklist[$d] = new htmlOutputText($user . $separator);
296 }
297 elseif (isset($primaryvals[$user])) {
298 $linklist[$d] = new htmlLink($user . $separator, "userlink.php?user='" . $user . "'");
299 $cssClasses[] = 'bold';
300 }
301 else {
302 $linklist[$d] = new htmlLink($user . $separator, "userlink.php?user='" . $user . "'");
303 }
304 $linklist[$d]->setCSSClasses($cssClasses);
305 }
306 }
307 else {
308 // make a link for each member of the group
309 for ($d = 0; $d < count($entry[$attribute]); $d++) {
310 $separator = ($d < (count($entry[$attribute]) - 1)) ? ';' : '';
311 $cssClasses = ($d >= htmlShowMore::MAXIMUM_SIZE_LARGE) ? [htmlShowMore::CLASS_SHOW_MORE] : [];
312 $user = $entry[$attribute][$d]; // user name
313 if (!isAccountTypeHidden('user')) {
314 $linklist[$d] = new htmlLink($user . $separator, "userlink.php?user='" . $user . "'");
315 }
316 else {
317 $linklist[$d] = new htmlOutputText($user . $separator);
318 }
319 $linklist[$d]->setCSSClasses($cssClasses);
320 }
321 }
322 if (count($linklist) > htmlShowMore::MAXIMUM_SIZE_LARGE) {
323 array_splice($linklist, htmlShowMore::MAXIMUM_SIZE_LARGE, 0, [new htmlShowMore()]);
324 $linklist[] = new htmlShowLess();
325 }
326 return new htmlDiv(null, new htmlGroup($linklist), [htmlShowMore::CLASS_SHOW_MORE_ROOT]);
327 }
328 // print all other attributes
329 else {
330 return parent::getTableCellContent($entry, $attribute);
331 }
332 }
333
334 /**
335 * {@inheritDoc}
336 */
337 protected function listRefreshData(): void {
338 parent::listRefreshData();
339 if ($this->include_primary) {
340 $this->groupRefreshPrimary();
341 }
342 }
343
344 /**
345 * Refreshes the primary group members list.
346 */
347 private function groupRefreshPrimary(): void {
348 $this->refresh_primary = false;
349 // return unless some entries
350 if (count($this->ldapEntries) <= 0) {
351 return;
352 }
353
354 $scope = "user";
355 // configure search filter
356 $module_filter = get_ldap_filter($scope); // basic filter is provided by modules
357 $attrs = ["uid"];
358 for ($i = 0; $i < count($this->ldapEntries); $i++) {
359 if (empty($this->ldapEntries[$i]['gidnumber'][0])) {
360 continue;
361 }
362 $gid = $this->ldapEntries[$i]['gidnumber'][0];
363 $filter = "(&(&" . $module_filter . ")(gidNumber=" . $gid . "))";
364 $entries = searchLDAPByFilter($filter, $attrs, [$scope]);
365 for ($j = 0; $j < count($entries); $j++) {
366 $this->primary_hash[$gid][$j] = $entries[$j]['uid'][0];
367 }
368 }
369 }
370
371 /**
372 * {@inheritDoc}
373 */
374 protected function listGetAllConfigOptions(): array {
375 $options = parent::listGetAllConfigOptions();
376 $options[] = new lamBooleanListOption(_('Show primary group members as normal group members'), self::TRANS_PRIMARY_OPTION_NAME);
377 return $options;
378 }
379
380 /**
381 * {@inheritDoc}
382 */
383 protected function listConfigurationChanged(): void {
384 parent::listConfigurationChanged();
385 $tpOption = $this->listGetConfigOptionByID(self::TRANS_PRIMARY_OPTION_NAME);
386 $use_primary = $this->include_primary;
387 $this->include_primary = ($tpOption instanceof lamBooleanListOption) && $tpOption->isSelected();
388 if (!$use_primary && $this->include_primary) {
389 $this->refresh_primary = true;
390 }
391 }
392
393 /**
394 * {@inheritDoc}
395 * @see lamList::getAdditionalLDAPAttributesToRead()
396 */
397 protected function getAdditionalLDAPAttributesToRead(): array {
398 $attrs = $this->type->getAttributes();
399 $ret = ['objectclass'];
400 $countLen = strlen('_count');
401 foreach ($attrs as $attr) {
402 $attrName = $attr->getAttributeName();
403 if (strrpos($attrName, '_count') === (strlen($attrName) - $countLen)) {
404 $ret[] = substr($attrName, 0, strlen($attrName) - $countLen);
405 }
406 }
407 return $ret;
408 }
409
410 }