| 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) 2003 - 2006 Tilo Lutz |
| 10 |
2007 - 2026 Roland Gruber |
| 11 |
|
| 12 |
This program is free software; you can redistribute it and/or modify |
| 13 |
it under the terms of the GNU General Public License as published by |
| 14 |
the Free Software Foundation; either version 2 of the License, or |
| 15 |
(at your option) any later version. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, |
| 18 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
GNU General Public License for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License |
| 23 |
along with this program; if not, write to the Free Software |
| 24 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
*/ |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* Manages Samba 3 accounts for groups. |
| 30 |
* |
| 31 |
* @package modules |
| 32 |
* |
| 33 |
* @author Tilo Lutz |
| 34 |
* @author Roland Gruber |
| 35 |
* @author Michael Duergner |
| 36 |
*/ |
| 37 |
|
| 38 |
/** |
| 39 |
* Manages the object class "sambaGroupMapping" for groups. |
| 40 |
* |
| 41 |
* @package modules |
| 42 |
*/ |
| 43 |
class sambaGroupMapping extends baseModule { |
| 44 |
|
| 45 |
/** |
| 46 |
* These attributes will be ignored by default if a new account is copied from an existing one. |
| 47 |
*/ |
| 48 |
private const ATTRIBUTES_TO_IGNORE_ON_COPY = ['sambaSID', 'gidNumber', 'displayName']; |
| 49 |
|
| 50 |
// Variables |
| 51 |
/** @var array<string, int> $rids Array of well known RIDs */ |
| 52 |
private array $rids; |
| 53 |
/** @var array<string, int> $sambaGroupTypes array of sambaGroupTypes */ |
| 54 |
private array $sambaGroupTypes; |
| 55 |
/** @var samba3domain[] $cachedDomainList cache for domain list */ |
| 56 |
private ?array $cachedDomainList = null; |
| 57 |
/** @var array<string, string>|null $sidCache cache for existing Samba entries (array(dn => cn)) */ |
| 58 |
private ?array $sidCache = null; |
| 59 |
|
| 60 |
/** |
| 61 |
* Creates a new module for Samba 3 groups. |
| 62 |
* |
| 63 |
* @param string $scope account type |
| 64 |
*/ |
| 65 |
public function __construct($scope) { |
| 66 |
// load error messages |
| 67 |
$this->rids = [ |
| 68 |
_('Domain admins') => 512, |
| 69 |
_('Domain users') => 513, |
| 70 |
_('Domain guests') => 514, |
| 71 |
_('Domain computers') => 515, |
| 72 |
_('Domain controllers') => 516, |
| 73 |
_('Domain certificate admins') => 517, |
| 74 |
_('Domain schema admins') => 518, |
| 75 |
_('Domain enterprise admins') => 519, |
| 76 |
_('Domain policy admins') => 520]; |
| 77 |
|
| 78 |
$this->sambaGroupTypes = [ |
| 79 |
_('Domain group') => 2, |
| 80 |
_('Local group') => 4, |
| 81 |
_('Builtin group') => 5, |
| 82 |
]; |
| 83 |
// call parent constructor |
| 84 |
parent::__construct($scope); |
| 85 |
$this->autoAddObjectClasses = false; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* {@inheritDoc} |
| 90 |
*/ |
| 91 |
public function can_manage(): bool { |
| 92 |
return $this->get_scope() === 'group'; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* {@inheritDoc} |
| 97 |
*/ |
| 98 |
public function get_metaData(): array { |
| 99 |
$return = []; |
| 100 |
// icon |
| 101 |
$return['icon'] = 'samba.svg'; |
| 102 |
// alias name |
| 103 |
$return["alias"] = _('Samba 3'); |
| 104 |
// module dependencies |
| 105 |
$return['dependencies'] = ['depends' => [['posixGroup', 'rfc2307bisPosixGroup']], 'conflicts' => []]; |
| 106 |
// managed object classes |
| 107 |
$return['objectClasses'] = ['sambaGroupMapping']; |
| 108 |
// managed attributes |
| 109 |
$return['attributes'] = ['gidNumber', 'sambaSID', 'sambaGroupType', 'displayName', 'sambaSIDList', 'description']; |
| 110 |
// available PDF fields |
| 111 |
$return['PDF_fields'] = [ |
| 112 |
'gidNumber' => _('GID number'), |
| 113 |
'sambaSID' => _('Windows group'), |
| 114 |
'displayName' => _('Display name'), |
| 115 |
'sambaGroupType' => _('Samba group type'), |
| 116 |
'description' => _('Description'), |
| 117 |
'sambaSIDList' => _('Local members'), |
| 118 |
]; |
| 119 |
// upload fields |
| 120 |
// search existing Samba 3 domains |
| 121 |
if (isLoggedIn()) { |
| 122 |
$return['upload_columns'] = [ |
| 123 |
[ |
| 124 |
'name' => 'sambaGroupMapping_name', |
| 125 |
'description' => _('Samba display name'), |
| 126 |
'help' => 'displayName', |
| 127 |
'example' => _('Domain administrators') |
| 128 |
], |
| 129 |
[ |
| 130 |
'name' => 'sambaGroupMapping_rid', |
| 131 |
'description' => _('Samba RID number'), |
| 132 |
'help' => 'rid', |
| 133 |
'example' => _('Domain admins') |
| 134 |
], |
| 135 |
[ |
| 136 |
'name' => 'sambaGroupMapping_groupType', |
| 137 |
'description' => _('Samba group type'), |
| 138 |
'help' => 'type', |
| 139 |
'values' => implode(", ", array_keys($this->sambaGroupTypes) + $this->sambaGroupTypes), |
| 140 |
'example' => '2' |
| 141 |
] |
| 142 |
]; |
| 143 |
$return['upload_preDepends'] = ['posixGroup', 'rfc2307bisPosixGroup']; |
| 144 |
} |
| 145 |
// help Entries |
| 146 |
$return['help'] = [ |
| 147 |
'displayName' => [ |
| 148 |
"Headline" => _("Display name"), 'attr' => 'displayName', |
| 149 |
"Text" => _("This is the group name which will be shown in Windows.") |
| 150 |
], |
| 151 |
'sambaSID' => [ |
| 152 |
"Headline" => _("Windows group name"), 'attr' => 'sambaSID', |
| 153 |
"Text" => _("If you want to use a well known RID you can select a well known group.") |
| 154 |
], |
| 155 |
'rid' => [ |
| 156 |
"Headline" => _("Samba RID number"), |
| 157 |
"Text" => _("This is the relative ID (similar to UID on Unix) for Windows accounts. If you leave this empty LAM will calculate the RID from the UID. This can be either a number or the name of a special group:") . ' ' . implode(", ", array_keys($this->rids)) |
| 158 |
], |
| 159 |
'sambaDomainName' => [ |
| 160 |
"Headline" => _("Domain"), |
| 161 |
"Text" => _("Windows-Domain name of group.") . ' ' . _("Can be left empty.") |
| 162 |
], |
| 163 |
'type' => [ |
| 164 |
"Headline" => _("Samba group type"), 'attr' => 'sambaGroupType', |
| 165 |
"Text" => _("Windows group type.") |
| 166 |
], |
| 167 |
'sambaSIDList' => [ |
| 168 |
"Headline" => _('Local members'), 'attr' => 'sambaSIDList', |
| 169 |
"Text" => _("Use this to specify other groups or accounts from other domains as group members.") |
| 170 |
], |
| 171 |
'filter' => [ |
| 172 |
"Headline" => _("Filter"), |
| 173 |
"Text" => _("Here you can enter a filter value. Only entries which contain the filter text will be shown.") |
| 174 |
. ' ' . _('Possible wildcards are: "*" = any character, "^" = line start, "$" = line end') |
| 175 |
], |
| 176 |
]; |
| 177 |
return $return; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* {@inheritDoc} |
| 182 |
*/ |
| 183 |
public function loadAttributesFromAccountCopy(array $ldapAttributes, array $attributesToIgnore = []): void { |
| 184 |
parent::loadAttributesFromAccountCopy($ldapAttributes, array_merge(baseModule::ATTRIBUTES_TO_IGNORE_ON_COPY_DEFAULT, self::ATTRIBUTES_TO_IGNORE_ON_COPY)); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Gets the GID number from the Unix group module. |
| 189 |
* |
| 190 |
* @return string|null GID number |
| 191 |
*/ |
| 192 |
private function getGID() { |
| 193 |
$modules = ['posixGroup', 'rfc2307bisPosixGroup']; |
| 194 |
for ($i = 0; $i < count($modules); $i++) { |
| 195 |
if ($this->getAccountContainer()->getAccountModule($modules[$i]) != null) { |
| 196 |
$attrs = $this->getAccountContainer()->getAccountModule($modules[$i])->getAttributes(); |
| 197 |
if (isset($attrs['gidNumber'][0])) { |
| 198 |
return $attrs['gidNumber'][0]; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
return null; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Gets the cn from the Unix group module. |
| 207 |
* |
| 208 |
* @return string|null cn attribute |
| 209 |
*/ |
| 210 |
private function getCn(): ?string { |
| 211 |
$modules = ['posixGroup', 'groupOfNames', 'groupOfUniqueNames']; |
| 212 |
for ($i = 0; $i < count($modules); $i++) { |
| 213 |
if ($this->getAccountContainer()->getAccountModule($modules[$i]) != null) { |
| 214 |
$attrs = $this->getAccountContainer()->getAccountModule($modules[$i])->getAttributes(); |
| 215 |
if (isset($attrs['cn'][0])) { |
| 216 |
return $attrs['cn'][0]; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
return null; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* {@inheritDoc} |
| 225 |
*/ |
| 226 |
public function get_uploadColumns(array $selectedModules, ConfiguredType $type): array { |
| 227 |
$return = parent::get_uploadColumns($selectedModules, $type); |
| 228 |
$domains = $this->getDomains(); |
| 229 |
$domainNames = []; |
| 230 |
for ($i = 0; $i < count($domains); $i++) { |
| 231 |
$domainNames[] = $domains[$i]->name; |
| 232 |
} |
| 233 |
$return[] = [ |
| 234 |
'name' => 'sambaGroupMapping_domain', |
| 235 |
'description' => _('Samba domain name'), |
| 236 |
'help' => 'sambaDomainName', |
| 237 |
'example' => $domainNames[0], |
| 238 |
'values' => implode(", ", $domainNames), |
| 239 |
'required' => true |
| 240 |
]; |
| 241 |
return $return; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* {@inheritDoc} |
| 246 |
*/ |
| 247 |
public function build_uploadAccounts(array $rawAccounts, array $ids, array &$partialAccounts, array $selectedModules, ConfiguredType &$type): array { |
| 248 |
// search existing Samba 3 domains |
| 249 |
$domains = $this->getDomains(); |
| 250 |
$nameToSID = []; |
| 251 |
// get domain SIDs |
| 252 |
for ($i = 0; $i < count($domains); $i++) { |
| 253 |
$nameToSID[$domains[$i]->name] = $domains[$i]->SID; |
| 254 |
} |
| 255 |
// get domain RID bases |
| 256 |
$nameToRIDBase = []; |
| 257 |
for ($i = 0; $i < count($domains); $i++) { |
| 258 |
$nameToRIDBase[$domains[$i]->name] = $domains[$i]->RIDbase; |
| 259 |
} |
| 260 |
$errors = []; |
| 261 |
for ($i = 0; $i < count($rawAccounts); $i++) { |
| 262 |
// group type |
| 263 |
if ($rawAccounts[$i][$ids['sambaGroupMapping_groupType']] != "") { |
| 264 |
if (in_array($rawAccounts[$i][$ids['sambaGroupMapping_groupType']], $this->sambaGroupTypes)) { // number given |
| 265 |
$partialAccounts[$i]['sambaGroupType'] = $rawAccounts[$i][$ids['sambaGroupMapping_groupType']]; |
| 266 |
} |
| 267 |
elseif (in_array($rawAccounts[$i][$ids['sambaGroupMapping_groupType']], array_keys($this->sambaGroupTypes))) { // description given |
| 268 |
$partialAccounts[$i]['sambaGroupType'] = $this->sambaGroupTypes[$rawAccounts[$i][$ids['sambaGroupMapping_groupType']]]; |
| 269 |
} |
| 270 |
else { // invalid type |
| 271 |
$errMsg = $this->messages['groupType'][0]; |
| 272 |
$errMsg[] = [$i, implode(", ", array_keys($this->sambaGroupTypes) + $this->sambaGroupTypes)]; |
| 273 |
$errors[] = $errMsg; |
| 274 |
} |
| 275 |
} |
| 276 |
else { |
| 277 |
$partialAccounts[$i]['sambaGroupType'] = "2"; // 2 is the default (domain group) |
| 278 |
} |
| 279 |
if (!in_array("sambaGroupMapping", $partialAccounts[$i]['objectClass'])) { |
| 280 |
$partialAccounts[$i]['objectClass'][] = "sambaGroupMapping"; |
| 281 |
} |
| 282 |
// SID |
| 283 |
$domSID = $nameToSID[$rawAccounts[$i][$ids['sambaGroupMapping_domain']]]; |
| 284 |
if (!isset($domSID)) { |
| 285 |
$errMsg = $this->messages['sambaSID'][1]; |
| 286 |
$errMsg[] = [$rawAccounts[$i][$ids['sambaGroupMapping_domain']], $i]; |
| 287 |
$errors[] = $errMsg; |
| 288 |
} |
| 289 |
else { |
| 290 |
// RID |
| 291 |
$rid = $rawAccounts[$i][$ids['sambaGroupMapping_rid']]; |
| 292 |
if (isset($this->rids[$rid])) { |
| 293 |
$rid = $this->rids[$rid]; |
| 294 |
} |
| 295 |
// check if RID has to be calculated |
| 296 |
if ($rid == "") { |
| 297 |
$ridBase = $nameToRIDBase[$rawAccounts[$i][$ids['sambaGroupMapping_domain']]]; |
| 298 |
$partialAccounts[$i]['sambaSID'] = $domSID . "-" . (($partialAccounts[$i]['gidNumber'] * 2) + $ridBase + 1); |
| 299 |
} |
| 300 |
elseif (get_preg($rid, 'digit')) { |
| 301 |
$partialAccounts[$i]['sambaSID'] = $domSID . "-" . $rid; |
| 302 |
} |
| 303 |
} |
| 304 |
// display name (UTF-8, no regex check needed) |
| 305 |
if ($rawAccounts[$i][$ids['sambaGroupMapping_name']] == "") { |
| 306 |
$partialAccounts[$i]['displayName'] = $partialAccounts[$i]['cn']; |
| 307 |
} |
| 308 |
else { |
| 309 |
$partialAccounts[$i]['displayName'] = $rawAccounts[$i][$ids['sambaGroupMapping_name']]; |
| 310 |
} |
| 311 |
} |
| 312 |
return $errors; |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* {@inheritDoc} |
| 317 |
*/ |
| 318 |
public function display_html_attributes(): htmlElement|array { |
| 319 |
if (isset($_POST['addObjectClass'])) { |
| 320 |
$this->attributes['objectClass'][] = 'sambaGroupMapping'; |
| 321 |
} |
| 322 |
$return = new htmlResponsiveRow(); |
| 323 |
if (in_array('sambaGroupMapping', $this->attributes['objectClass'])) { |
| 324 |
$sambaDomains = $this->getDomains(); |
| 325 |
if (count($sambaDomains) === 0) { |
| 326 |
$return->add(new htmlStatusMessage("ERROR", _('No Samba 3 domains found in LDAP! Please create one first.'))); |
| 327 |
return $return; |
| 328 |
} |
| 329 |
// Get Domain-SID from group SID |
| 330 |
if (isset($this->attributes['sambaSID'][0])) { |
| 331 |
$domainSID = substr($this->attributes['sambaSID'][0], 0, strrpos($this->attributes['sambaSID'][0], "-")); |
| 332 |
} |
| 333 |
$SID = ''; |
| 334 |
for ($i = 0; $i < count($sambaDomains); $i++) { |
| 335 |
// List with all valid domains |
| 336 |
$sambaDomainNames[] = $sambaDomains[$i]->name; |
| 337 |
if (isset($domainSID) && ($domainSID == $sambaDomains[$i]->SID)) { |
| 338 |
$SID = $sambaDomains[$i]->SID; |
| 339 |
$sel_domain = $sambaDomains[$i]->name; |
| 340 |
} |
| 341 |
} |
| 342 |
// display name |
| 343 |
$displayName = ''; |
| 344 |
if (isset($this->attributes['displayName'][0])) { |
| 345 |
$displayName = $this->attributes['displayName'][0]; |
| 346 |
} |
| 347 |
$displayNameInput = new htmlResponsiveInputField(_('Display name'), 'displayName', $displayName, 'displayName'); |
| 348 |
$displayNameInput->setFieldMaxLength(50); |
| 349 |
$return->add($displayNameInput); |
| 350 |
// Windows group |
| 351 |
$options = [$this->getCn()]; |
| 352 |
$selected = [$this->getCn()]; |
| 353 |
$names = array_keys($this->rids); |
| 354 |
for ($i = 0; $i < count($names); $i++) { |
| 355 |
if (isset($this->attributes['sambaSID'][0]) && ($this->attributes['sambaSID'][0] == $SID . "-" . $this->rids[$names[$i]])) { |
| 356 |
$selected = [$names[$i]]; |
| 357 |
} |
| 358 |
$options[] = $names[$i]; |
| 359 |
} |
| 360 |
$return->add(new htmlResponsiveSelect('sambaSID', $options, $selected, _('Windows group'), 'sambaSID')); |
| 361 |
// group type |
| 362 |
$names = array_keys($this->sambaGroupTypes); |
| 363 |
$selected = [_('Domain group')]; |
| 364 |
for ($i = 0; $i < count($names); $i++) { |
| 365 |
if (!isset($this->attributes['sambaGroupType'][0])) { |
| 366 |
break; |
| 367 |
} |
| 368 |
if ($this->attributes['sambaGroupType'][0] == $this->sambaGroupTypes[$names[$i]]) { |
| 369 |
$selected = [$names[$i]]; |
| 370 |
} |
| 371 |
} |
| 372 |
$return->add(new htmlResponsiveSelect('sambaGroupType', $names, $selected, _('Group type'), 'type')); |
| 373 |
// domain |
| 374 |
$selectedDomain = []; |
| 375 |
if (isset($sel_domain)) { |
| 376 |
$selectedDomain = [$sel_domain]; |
| 377 |
} |
| 378 |
$return->add(new htmlResponsiveSelect('sambaDomainName', $sambaDomainNames, $selectedDomain, _('Domain'), 'sambaDomainName')); |
| 379 |
// local group members |
| 380 |
$memberLabel = new htmlOutputText(_('Local members')); |
| 381 |
$return->addLabel($memberLabel); |
| 382 |
$addMemberButton = new htmlAccountPageButton(static::class, 'members', 'open', 'add.svg', true); |
| 383 |
$addMemberButton->setTitle(_('Add')); |
| 384 |
$addMemberButton->alignment = htmlElement::ALIGN_TOP; |
| 385 |
$memberHelp = new htmlHelpLink('sambaSIDList'); |
| 386 |
if (!empty($this->attributes['sambaSIDList'][0])) { |
| 387 |
$this->loadSIDCache(); |
| 388 |
$memberTable = new htmlTable(); |
| 389 |
$memberTable->alignment = htmlElement::ALIGN_TOP; |
| 390 |
for ($i = 0; $i < count($this->attributes['sambaSIDList']); $i++) { |
| 391 |
$member = $this->attributes['sambaSIDList'][$i]; |
| 392 |
if (isset($this->sidCache[$member])) { |
| 393 |
$member = $this->sidCache[$member]; |
| 394 |
} |
| 395 |
$memberTable->addElement(new htmlOutputText($member)); |
| 396 |
$delButton = new htmlButton('sambaSIDListDel_' . $i, 'del.svg', true); |
| 397 |
$delButton->setTitle(_('Delete')); |
| 398 |
$memberTable->addElement($delButton); |
| 399 |
if ($i === (count($this->attributes['sambaSIDList']) - 1)) { |
| 400 |
$memberTable->addElement($addMemberButton); |
| 401 |
$memberTable->addElement($memberHelp); |
| 402 |
} |
| 403 |
$memberTable->addNewLine(); |
| 404 |
} |
| 405 |
$return->addField($memberTable); |
| 406 |
} |
| 407 |
else { |
| 408 |
$memberGroup = new htmlGroup(); |
| 409 |
$memberGroup->addElement($addMemberButton); |
| 410 |
$memberGroup->addElement($memberHelp); |
| 411 |
$return->addField($memberGroup); |
| 412 |
} |
| 413 |
|
| 414 |
$return->addVerticalSpacer('2rem'); |
| 415 |
|
| 416 |
$remButton = new htmlButton('remObjectClass', _('Remove Samba 3 extension')); |
| 417 |
$remButton->setCSSClasses(['lam-danger']); |
| 418 |
$return->add($remButton, 12, 12, 12, 'text-center'); |
| 419 |
} |
| 420 |
else { |
| 421 |
$return->add(new htmlButton('addObjectClass', _('Add Samba 3 extension'))); |
| 422 |
} |
| 423 |
return $return; |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* This function will create the meta HTML code to show a page to add members. |
| 428 |
* |
| 429 |
* @return htmlElement HTML meta data |
| 430 |
*/ |
| 431 |
public function display_html_members(): htmlElement { |
| 432 |
$return = new htmlResponsiveRow(); |
| 433 |
$typeManager = new TypeManager(); |
| 434 |
// show list of possible new members |
| 435 |
if ((isset($_POST['form_subpage_' . static::class . '_members_select']) || isset($_POST['setFilter'])) && isset($_POST['type'])) { |
| 436 |
$type = $typeManager->getConfiguredType($_POST['type']); |
| 437 |
$this->loadSIDCache(); |
| 438 |
$options = []; |
| 439 |
$filter = get_ldap_filter($type->getId()); |
| 440 |
$entries = searchLDAP($type->getSuffix(), $filter, ['dn', 'cn', 'uid', 'sambaSID']); |
| 441 |
$entryCount = count($entries); |
| 442 |
for ($i = 0; $i < $entryCount; $i++) { |
| 443 |
// require SID |
| 444 |
if (empty($entries[$i]['sambasid'][0])) { |
| 445 |
continue; |
| 446 |
} |
| 447 |
$sid = $entries[$i]['sambasid'][0]; |
| 448 |
// get label |
| 449 |
$label = empty($this->sidCache[$sid]) ? $sid : $this->sidCache[$sid]; |
| 450 |
if (empty($this->attributes['sambaSIDList'][0]) || !in_array($sid, $this->attributes['sambaSIDList'])) { |
| 451 |
$options[$label] = $sid; |
| 452 |
} |
| 453 |
} |
| 454 |
$size = 20; |
| 455 |
if (count($options) < 20) { |
| 456 |
$size = count($options); |
| 457 |
} |
| 458 |
$membersSelect = new htmlSelect('members', $options, [], $size); |
| 459 |
$membersSelect->setHasDescriptiveElements(true); |
| 460 |
$membersSelect->setMultiSelect(true); |
| 461 |
$membersSelect->setTransformSingleSelect(false); |
| 462 |
$return->add($membersSelect); |
| 463 |
$return->addVerticalSpacer('0.5rem'); |
| 464 |
$filterGroup = new htmlGroup(); |
| 465 |
$filterInput = new htmlInputField('newFilter'); |
| 466 |
$filterInput->filterSelectBox('members'); |
| 467 |
$filterGroup->addElement(new htmlOutputText(_('Filter'))); |
| 468 |
$filterGroup->addElement($filterInput); |
| 469 |
$filterGroup->addElement(new htmlHiddenInput('type', $type->getId())); |
| 470 |
$return->add($filterGroup); |
| 471 |
$return->addVerticalSpacer('2rem'); |
| 472 |
$return->addLabel(new htmlAccountPageButton(static::class, 'attributes', 'addMembers', _('Add'))); |
| 473 |
$return->addField(new htmlAccountPageButton(static::class, 'attributes', 'cancel', _('Cancel'))); |
| 474 |
return $return; |
| 475 |
} |
| 476 |
$types = $typeManager->getConfiguredTypesForScopes(['user', 'group', 'host']); |
| 477 |
$options = []; |
| 478 |
$optionsSelected = []; |
| 479 |
foreach ($types as $type) { |
| 480 |
$options[$type->getAlias()] = $type->getId(); |
| 481 |
if (empty($optionsSelected) && ($type->getScope() == 'group')) { |
| 482 |
$optionsSelected[] = $type->getId(); |
| 483 |
} |
| 484 |
} |
| 485 |
$return->addLabel(new htmlOutputText(_('Add entries of this type:'))); |
| 486 |
$typeSelect = new htmlSelect('type', $options, $optionsSelected); |
| 487 |
$typeSelect->setHasDescriptiveElements(true); |
| 488 |
$return->addField($typeSelect); |
| 489 |
$return->addLabel(new htmlOutputText(' ', false)); |
| 490 |
$return->addField(new htmlAccountPageButton(static::class, 'members', 'select', _('Ok'))); |
| 491 |
$return->addVerticalSpacer('2rem'); |
| 492 |
$return->add(new htmlAccountPageButton(static::class, 'attributes', 'membersBack', _('Back')), 12, 12, 12, 'text-center'); |
| 493 |
return $return; |
| 494 |
} |
| 495 |
|
| 496 |
/** |
| 497 |
* Processes user input of the members page. |
| 498 |
* It checks if all input values are correct and updates the associated LDAP attributes. |
| 499 |
* |
| 500 |
* @return string[][] list of info/error messages |
| 501 |
*/ |
| 502 |
public function process_members(): array { |
| 503 |
$return = []; |
| 504 |
if (isset($_POST['form_subpage_' . static::class . '_attributes_addMembers']) && isset($_POST['members'])) { |
| 505 |
for ($i = 0; $i < count($_POST['members']); $i++) { |
| 506 |
$this->attributes['sambaSIDList'][] = $_POST['members'][$i]; |
| 507 |
} |
| 508 |
} |
| 509 |
return $return; |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* {@inheritDoc} |
| 514 |
*/ |
| 515 |
public function get_pdfEntries(array $pdfKeys, string $typeId): array { |
| 516 |
$return = []; |
| 517 |
$this->addSimplePDFField($return, 'gidNumber', _('GID number')); |
| 518 |
$this->addSimplePDFField($return, 'sambaSID', _('Windows group')); |
| 519 |
$this->addSimplePDFField($return, 'displayName', _('Display name')); |
| 520 |
$this->addSimplePDFField($return, 'sambaGroupType', _('Samba group type')); |
| 521 |
$this->addSimplePDFField($return, 'description', _('Description')); |
| 522 |
// local members |
| 523 |
if (!empty($this->attributes['sambaSIDList'][0])) { |
| 524 |
$this->loadSIDCache(); |
| 525 |
$members = []; |
| 526 |
foreach ($this->attributes['sambaSIDList'] as $member) { |
| 527 |
$members[] = empty($this->sidCache[$member]) ? $member : $this->sidCache[$member]; |
| 528 |
} |
| 529 |
$this->addPDFKeyValue($return, 'sambaSIDList', _('Local members'), implode(', ', $members)); |
| 530 |
} |
| 531 |
return $return; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
/** |
| 536 |
* {@inheritDoc} |
| 537 |
*/ |
| 538 |
public function get_profileOptions(string $typeId): ?htmlResponsiveRow { |
| 539 |
$return = new htmlResponsiveRow(); |
| 540 |
// get list of domains |
| 541 |
$sambaDomains = $this->getDomains(); |
| 542 |
$sambaDomainNames = []; |
| 543 |
for ($i = 0; $i < count($sambaDomains); $i++) { |
| 544 |
// extract names |
| 545 |
$sambaDomainNames[] = $sambaDomains[$i]->name; |
| 546 |
} |
| 547 |
// domain |
| 548 |
$return->add(new htmlResponsiveSelect('sambaGroupMapping_sambaDomainName', $sambaDomainNames, [], _('Domain'), 'sambaDomainName')); |
| 549 |
return $return; |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
/** |
| 554 |
* {@inheritDoc} |
| 555 |
*/ |
| 556 |
public function load_profile(array $profile): void { |
| 557 |
if (isset($profile['sambaGroupMapping_sambaDomainName'][0])) { |
| 558 |
// get list of domains |
| 559 |
$sambaDomains = $this->getDomains(); |
| 560 |
for ($i = 0; $i < count($sambaDomains); $i++) { |
| 561 |
if ($sambaDomains[$i]->name == $profile['sambaGroupMapping_sambaDomainName'][0]) { |
| 562 |
$this->attributes['sambaSID'][0] = $sambaDomains[$i]->SID . "-0"; |
| 563 |
break; |
| 564 |
} |
| 565 |
} |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
/** |
| 570 |
* {@inheritDoc} |
| 571 |
*/ |
| 572 |
protected function load_Messages(): void { |
| 573 |
$this->messages['sambaSID'][0] = ['ERROR', _('There can be only one group of this type.')]; // third parameter must be set dynamically |
| 574 |
$this->messages['sambaSID'][1] = ['ERROR', _("Account %s:") . " (sambaGroupMapping_domain): " . _("LAM was unable to find a Samba 3 domain with this name!")]; // third parameter must be set dynamically |
| 575 |
$this->messages['groupType'][0] = ['ERROR', _("Account %s:") . " (sambaGroupMapping_type): " . _("This is not a valid Samba 3 group type!"), _("Possible values") . ": %s"]; |
| 576 |
$this->messages['primaryGroup'][0] = ['ERROR', _('There are still users who have this group as their primary group.')]; |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
/** |
| 581 |
* {@inheritDoc} |
| 582 |
*/ |
| 583 |
public function module_ready(): bool { |
| 584 |
return ($this->getGID() !== null) && ($this->getGID() !== ''); |
| 585 |
} |
| 586 |
|
| 587 |
/** |
| 588 |
* {@inheritDoc} |
| 589 |
*/ |
| 590 |
public function module_complete(): bool { |
| 591 |
if (!in_array('sambaGroupMapping', $this->attributes['objectClass'])) { |
| 592 |
return true; |
| 593 |
} |
| 594 |
return isset($this->attributes['sambaSID']) && ($this->attributes['sambaSID'] != ''); |
| 595 |
} |
| 596 |
|
| 597 |
/** |
| 598 |
* {@inheritDoc} |
| 599 |
*/ |
| 600 |
public function process_attributes(): array { |
| 601 |
// remove extension |
| 602 |
if (isset($_POST['remObjectClass'])) { |
| 603 |
// check for users that have this group as primary group |
| 604 |
$result = searchLDAPByAttribute('sambaPrimaryGroupSID', $this->attributes['sambaSID'][0], 'sambaSamAccount', ['dn'], ['user', 'host']); |
| 605 |
if (count($result) > 0) { |
| 606 |
return [$this->messages['primaryGroup'][0]]; |
| 607 |
} |
| 608 |
// remove attributes |
| 609 |
$this->attributes['objectClass'] = array_delete(['sambaGroupMapping'], $this->attributes['objectClass']); |
| 610 |
$attrKeys = array_keys($this->attributes); |
| 611 |
for ($k = 0; $k < count($attrKeys); $k++) { |
| 612 |
if (strpos($attrKeys[$k], 'samba') > -1) { |
| 613 |
unset($this->attributes[$attrKeys[$k]]); |
| 614 |
} |
| 615 |
} |
| 616 |
if (isset($this->attributes['displayName'])) { |
| 617 |
unset($this->attributes['displayName']); |
| 618 |
} |
| 619 |
return []; |
| 620 |
} |
| 621 |
if (!in_array('sambaGroupMapping', $this->attributes['objectClass'])) { |
| 622 |
return []; |
| 623 |
} |
| 624 |
$errors = []; |
| 625 |
$sambaDomains = $this->getDomains(); |
| 626 |
if (count($sambaDomains) === 0) { |
| 627 |
return []; |
| 628 |
} |
| 629 |
// Save attributes |
| 630 |
$this->attributes['displayName'][0] = $_POST['displayName']; |
| 631 |
$this->attributes['sambaGroupType'][0] = $this->sambaGroupTypes[$_POST['sambaGroupType']]; |
| 632 |
|
| 633 |
// Get Domain SID from name |
| 634 |
$SID = ''; |
| 635 |
$RIDbase = 1000; |
| 636 |
for ($i = 0; $i < count($sambaDomains); $i++) { |
| 637 |
if (!isset($_POST['sambaDomainName'])) { |
| 638 |
break; |
| 639 |
} |
| 640 |
if ($_POST['sambaDomainName'] == $sambaDomains[$i]->name) { |
| 641 |
$SID = $sambaDomains[$i]->SID; |
| 642 |
$RIDbase = $sambaDomains[$i]->RIDbase; |
| 643 |
} |
| 644 |
} |
| 645 |
// Load attributes |
| 646 |
$rids = array_keys($this->rids); |
| 647 |
$wrid = false; |
| 648 |
for ($i = 0; $i < count($rids); $i++) { |
| 649 |
if ($_POST['sambaSID'] == $rids[$i]) { |
| 650 |
$wrid = true; |
| 651 |
// Get Domain SID |
| 652 |
$this->attributes['sambaSID'][0] = $SID . "-" . $this->rids[$rids[$i]]; |
| 653 |
// Do a check if special group is unique |
| 654 |
if ($this->getAccountContainer()->isNewAccount) { |
| 655 |
$result = searchLDAPByAttribute('sambaSID', $SID . "-" . $this->rids[$rids[$i]], 'sambaGroupMapping', ['sambaSID'], ['group']); |
| 656 |
if (count($result) > 0) { |
| 657 |
$message = $this->messages['sambaSID'][0]; |
| 658 |
$message[] = $rids[$i]; |
| 659 |
$errors[] = $message; |
| 660 |
} |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
if (!$wrid) { |
| 665 |
$this->attributes['sambaSID'][0] = $SID . "-" . (((int) $this->getGID()) * 2 + $RIDbase + 1); |
| 666 |
} |
| 667 |
// delete local members |
| 668 |
foreach (array_keys($_POST) as $key) { |
| 669 |
if (str_starts_with($key, 'sambaSIDListDel_')) { |
| 670 |
$index = substr($key, strlen('sambaSIDListDel_')); |
| 671 |
unset($this->attributes['sambaSIDList'][$index]); |
| 672 |
$this->attributes['sambaSIDList'] = array_values($this->attributes['sambaSIDList']); |
| 673 |
break; |
| 674 |
} |
| 675 |
} |
| 676 |
// Return error-messages |
| 677 |
return $errors; |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
/** |
| 682 |
* {@inheritDoc} |
| 683 |
*/ |
| 684 |
public function save_attributes(): array { |
| 685 |
if (!in_array('sambaGroupMapping', $this->attributes['objectClass']) && !in_array('sambaGroupMapping', $this->orig['objectClass'])) { |
| 686 |
return []; |
| 687 |
} |
| 688 |
return $this->getAccountContainer()->save_module_attributes($this->attributes, $this->orig); |
| 689 |
} |
| 690 |
|
| 691 |
/** |
| 692 |
* Returns a list of existing Samba 3 domains. |
| 693 |
* |
| 694 |
* @return samba3domain[] list of samba3domain objects |
| 695 |
*/ |
| 696 |
private function getDomains(): array { |
| 697 |
if ($this->cachedDomainList !== null) { |
| 698 |
return $this->cachedDomainList; |
| 699 |
} |
| 700 |
$this->cachedDomainList = search_domains(); |
| 701 |
return $this->cachedDomainList; |
| 702 |
} |
| 703 |
|
| 704 |
/** |
| 705 |
* Loads the list of Samba accounts into the cache. |
| 706 |
*/ |
| 707 |
private function loadSIDCache(): void { |
| 708 |
if ($this->sidCache !== null) { |
| 709 |
return; |
| 710 |
} |
| 711 |
$results = searchLDAPByFilter('(|(objectClass=sambaSamAccount)(objectClass=sambaGroupMapping))', ['cn', 'uid', 'sambaSID'], ['user', 'group', 'host']); |
| 712 |
$this->sidCache = []; |
| 713 |
foreach ($results as $result) { |
| 714 |
// require SID |
| 715 |
if (empty($result['sambasid'][0])) { |
| 716 |
continue; |
| 717 |
} |
| 718 |
// get label |
| 719 |
if (isset($result['cn'][0])) { |
| 720 |
$label = $result['cn'][0]; |
| 721 |
} |
| 722 |
elseif (isset($result['uid'][0])) { |
| 723 |
$label = $result['uid'][0]; |
| 724 |
} |
| 725 |
else { |
| 726 |
$label = $result['sambasid'][0]; |
| 727 |
} |
| 728 |
$this->sidCache[$result['sambasid'][0]] = $label; |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
/** |
| 733 |
* @inheritDoc |
| 734 |
*/ |
| 735 |
public function getListAttributeDescriptions(ConfiguredType $type): array { |
| 736 |
return [ |
| 737 |
'gidnumber' => _('GID number'), |
| 738 |
'sambasid' => _('Windows group'), |
| 739 |
'displayname' => _('Display name'), |
| 740 |
'sambagrouptype' => _('Samba group type'), |
| 741 |
'description' => _('Description'), |
| 742 |
'sambasidlist' => _('Local members'), |
| 743 |
]; |
| 744 |
} |
| 745 |
|
| 746 |
} |