| 1 |
<?php |
| 2 |
|
| 3 |
namespace LAM\TYPES; |
| 4 |
/* |
| 5 |
|
| 6 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 7 |
Copyright (C) 2005 - 2026 Roland Gruber |
| 8 |
|
| 9 |
This program is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation; either version 2 of the License, or |
| 12 |
(at your option) any later version. |
| 13 |
|
| 14 |
This program is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
GNU General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License |
| 20 |
along with this program; if not, write to the Free Software |
| 21 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 |
|
| 23 |
*/ |
| 24 |
|
| 25 |
use baseModule; |
| 26 |
use baseType; |
| 27 |
use LAMConfig; |
| 28 |
|
| 29 |
/** |
| 30 |
* This file is the interface to the different account types. |
| 31 |
* |
| 32 |
* @package types |
| 33 |
* @author Roland Gruber |
| 34 |
*/ |
| 35 |
|
| 36 |
/** parent class of account types */ |
| 37 |
include_once(__DIR__ . "/baseType.inc"); |
| 38 |
/** parent class of list views */ |
| 39 |
include_once(__DIR__ . "/lists.inc"); |
| 40 |
/** Used to check if this is a LAM Pro release. */ |
| 41 |
include_once(__DIR__ . "/selfService.inc"); |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* This includes all type definitions. |
| 46 |
*/ |
| 47 |
$typesINC_dirname = substr(__FILE__, 0, strlen(__FILE__) - 10) . "/types"; |
| 48 |
$typesINC_dir = dir($typesINC_dirname); |
| 49 |
// get module names. |
| 50 |
while ($entry = $typesINC_dir->read()) { |
| 51 |
if ((str_ends_with($entry, '.inc')) && is_file($typesINC_dirname . '/' . $entry)) { |
| 52 |
include_once($typesINC_dirname . '/' . $entry); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* Returns a list of available account types. |
| 59 |
* |
| 60 |
* @return string[] list of types |
| 61 |
*/ |
| 62 |
function getTypes(): array { |
| 63 |
$dirname = substr(__FILE__, 0, strlen(__FILE__) - 10) . "/types"; |
| 64 |
$dir = dir($dirname); |
| 65 |
$return = []; |
| 66 |
// get type names. |
| 67 |
while ($entry = $dir->read()) { |
| 68 |
if ((str_ends_with($entry, '.inc')) && is_file($dirname . '/' . $entry)) { |
| 69 |
$entry = substr($entry, 0, strpos($entry, '.')); |
| 70 |
$return[] = $entry; |
| 71 |
} |
| 72 |
} |
| 73 |
$dir->close(); |
| 74 |
return $return; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns the account type for a given type id. |
| 79 |
* |
| 80 |
* @param string $typeId type id (e.g. user_1) |
| 81 |
* @return string scope (e.g. user) |
| 82 |
*/ |
| 83 |
function getScopeFromTypeId(string $typeId): string { |
| 84 |
$parts = explode('_', $typeId); |
| 85 |
return $parts[0]; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Represents a configured account type variant. |
| 90 |
* |
| 91 |
* @package types |
| 92 |
* @author Roland Gruber |
| 93 |
*/ |
| 94 |
class ConfiguredType { |
| 95 |
|
| 96 |
private string $scope; |
| 97 |
|
| 98 |
private string $id; |
| 99 |
|
| 100 |
private ?string $suffix = null; |
| 101 |
|
| 102 |
/** @var ListAttribute[] $attributes list attributes */ |
| 103 |
private ?array $attributes = null; |
| 104 |
|
| 105 |
private ?string $alias = null; |
| 106 |
|
| 107 |
private ?string $additionalLdapFilter = null; |
| 108 |
|
| 109 |
private ?bool $hidden = null; |
| 110 |
|
| 111 |
private ?baseType $baseType = null; |
| 112 |
|
| 113 |
private ?TypeManager $typeManager; |
| 114 |
|
| 115 |
/** |
| 116 |
* Constructor |
| 117 |
* |
| 118 |
* @param TypeManager|null $typeManager type manager |
| 119 |
* @param string $scope account type |
| 120 |
* @param string $id unique ID for this configuration |
| 121 |
*/ |
| 122 |
public function __construct(?TypeManager $typeManager, string $scope, string $id) { |
| 123 |
$this->typeManager = &$typeManager; |
| 124 |
$this->scope = $scope; |
| 125 |
$this->id = $id; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Returns the owning type manager. |
| 130 |
* |
| 131 |
* @return TypeManager type manager |
| 132 |
*/ |
| 133 |
public function getTypeManager(): TypeManager { |
| 134 |
return $this->typeManager; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Returns the account type (e.g. 'user'). |
| 139 |
* |
| 140 |
* @return string account type |
| 141 |
*/ |
| 142 |
public function getScope(): string { |
| 143 |
return $this->scope; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Returns a unique id for this configuration. |
| 148 |
* |
| 149 |
* @return string unique id |
| 150 |
*/ |
| 151 |
public function getId(): string { |
| 152 |
return $this->id; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Returns the LDAP suffix. |
| 157 |
* |
| 158 |
* @return string LDAP suffix |
| 159 |
*/ |
| 160 |
public function getSuffix(): string { |
| 161 |
if ($this->suffix !== null) { |
| 162 |
return $this->suffix; |
| 163 |
} |
| 164 |
$this->suffix = $this->typeManager->getConfig()->get_Suffix($this->id); |
| 165 |
return $this->suffix; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Returns a list of configured attributes. |
| 170 |
* |
| 171 |
* @return ListAttribute[] list of ListAttribute |
| 172 |
*/ |
| 173 |
public function getAttributes(): array { |
| 174 |
if ($this->attributes !== null) { |
| 175 |
return $this->attributes; |
| 176 |
} |
| 177 |
$attributeString = $this->typeManager->getConfig()->get_listAttributes($this->id); |
| 178 |
$attributeSpecs = explode(';', $attributeString); |
| 179 |
$attributes = []; |
| 180 |
foreach ($attributeSpecs as $attributeSpec) { |
| 181 |
$attributes[] = new ListAttribute($attributeSpec); |
| 182 |
} |
| 183 |
$this->attributes = $attributes; |
| 184 |
return $this->attributes; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Returns the alias name. |
| 189 |
* |
| 190 |
* @return string alias name |
| 191 |
*/ |
| 192 |
public function getAlias(): string { |
| 193 |
if ($this->alias !== null) { |
| 194 |
return $this->alias; |
| 195 |
} |
| 196 |
$typeSettings = $this->typeManager->getConfig()->get_typeSettings(); |
| 197 |
if (!empty($typeSettings['customLabel_' . $this->id])) { |
| 198 |
$this->alias = $typeSettings['customLabel_' . $this->id]; |
| 199 |
} |
| 200 |
else { |
| 201 |
$this->alias = $this->getBaseType()->getAlias(); |
| 202 |
} |
| 203 |
return $this->alias; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Returns the additional LDAP filter. |
| 208 |
* |
| 209 |
* @return string LDAP filter |
| 210 |
*/ |
| 211 |
public function getAdditionalLdapFilter(): string { |
| 212 |
if ($this->additionalLdapFilter !== null) { |
| 213 |
return $this->additionalLdapFilter; |
| 214 |
} |
| 215 |
$typeSettings = $this->typeManager->getConfig()->get_typeSettings(); |
| 216 |
$this->additionalLdapFilter = $typeSettings['filter_' . $this->id] ?? ''; |
| 217 |
return $this->additionalLdapFilter; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Returns if this configuration is hidden. |
| 222 |
* |
| 223 |
* @return bool hidden |
| 224 |
*/ |
| 225 |
public function isHidden(): bool { |
| 226 |
if ($this->hidden !== null) { |
| 227 |
return $this->hidden; |
| 228 |
} |
| 229 |
$this->hidden = isAccountTypeHidden($this->id); |
| 230 |
return $this->hidden; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Returns the base type of this configured type. |
| 235 |
* |
| 236 |
* @return baseType base type |
| 237 |
*/ |
| 238 |
public function getBaseType(): baseType { |
| 239 |
if ($this->baseType != null) { |
| 240 |
return $this->baseType; |
| 241 |
} |
| 242 |
$scope = $this->scope; |
| 243 |
$this->baseType = new $scope($this); |
| 244 |
return $this->baseType; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Returns a list of LDAP suffixes for this type. |
| 249 |
* |
| 250 |
* @return string[] sorted list of possible suffixes for this type. |
| 251 |
*/ |
| 252 |
public function getSuffixList(): array { |
| 253 |
$ret = []; |
| 254 |
$filter = $this->getBaseType()->getSuffixFilter(); |
| 255 |
$attrs = ['dn', 'objectClass']; |
| 256 |
$units = searchLDAP($this->getSuffix(), $filter, $attrs); |
| 257 |
foreach ($units as $unit) { |
| 258 |
// Active Directory fix, hide system containers |
| 259 |
$isSystemContainer = in_array('container', $unit['objectclass']) |
| 260 |
&& (preg_match('/.*cn=system,dc=.+/i', $unit['dn']) || preg_match('/.*CN=program data,dc=.+/i', $unit['dn'])); |
| 261 |
$isDnsContainer = preg_match('/.*CN=MicrosoftDNS,CN=System,dc=.+/i', $unit['dn']); |
| 262 |
if ($isSystemContainer || $isDnsContainer) { |
| 263 |
continue; |
| 264 |
} |
| 265 |
$ret[] = $unit['dn']; |
| 266 |
} |
| 267 |
// add root suffix if needed |
| 268 |
$found = false; |
| 269 |
for ($i = 0; $i < count($ret); $i++) { // search suffix case-insensitive |
| 270 |
if (strtolower($this->getSuffix()) === strtolower($ret[$i])) { |
| 271 |
$found = true; |
| 272 |
break; |
| 273 |
} |
| 274 |
} |
| 275 |
if (!$found) { |
| 276 |
$ret[] = $this->getSuffix(); |
| 277 |
} |
| 278 |
usort($ret, compareDN(...)); |
| 279 |
return $ret; |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Returns the names of the active modules for this type. |
| 284 |
* |
| 285 |
* @return class-string<baseModule>[] module names |
| 286 |
*/ |
| 287 |
public function getModules(): array { |
| 288 |
$typeSettings = $this->typeManager->getConfig()->get_typeSettings(); |
| 289 |
if (empty($typeSettings['modules_' . $this->getId()])) { |
| 290 |
return []; |
| 291 |
} |
| 292 |
$modules = $typeSettings['modules_' . $this->getId()]; |
| 293 |
return explode(',', $modules); |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Returns the file name of the type icon. |
| 298 |
* It is 16x16px and located in graphics folder. |
| 299 |
* |
| 300 |
* @return string file name |
| 301 |
*/ |
| 302 |
public function getIcon(): string { |
| 303 |
$baseType = $this->getBaseType(); |
| 304 |
return $baseType->getIcon(); |
| 305 |
} |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* An attribute definition for the account list. |
| 311 |
* |
| 312 |
* @package types |
| 313 |
* @author Roland Gruber |
| 314 |
*/ |
| 315 |
class ListAttribute { |
| 316 |
|
| 317 |
private string $attributeSpec; |
| 318 |
|
| 319 |
/** |
| 320 |
* Constructor. |
| 321 |
* |
| 322 |
* @param string $attributeSpec spec of attribute (e.g. '#uid' or 'uid:User') |
| 323 |
*/ |
| 324 |
public function __construct(string $attributeSpec) { |
| 325 |
$this->attributeSpec = $attributeSpec; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Returns the name of the LDAP attribute. |
| 330 |
* |
| 331 |
* @return string $attributeName name |
| 332 |
*/ |
| 333 |
public function getAttributeName(): string { |
| 334 |
if ($this->isPredefined()) { |
| 335 |
return substr($this->attributeSpec, 1); |
| 336 |
} |
| 337 |
$parts = explode(':', $this->attributeSpec); |
| 338 |
return $parts[0]; |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Returns the display value. |
| 343 |
* |
| 344 |
* @param array<string, string> $predefinedDescriptions predefined descriptions (lower attribute name => label) |
| 345 |
* @return string display value |
| 346 |
*/ |
| 347 |
public function getAlias(array $predefinedDescriptions): string { |
| 348 |
if ($this->isPredefined()) { |
| 349 |
$name = strtolower(substr($this->attributeSpec, 1)); |
| 350 |
return $predefinedDescriptions[$name] ?? $name; |
| 351 |
} |
| 352 |
$parts = explode(':', $this->attributeSpec); |
| 353 |
return $parts[1]; |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Returns if this is a predefined attribute name. |
| 358 |
* |
| 359 |
* @return bool is predefined |
| 360 |
*/ |
| 361 |
private function isPredefined(): bool { |
| 362 |
return str_starts_with($this->attributeSpec, '#'); |
| 363 |
} |
| 364 |
|
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* Provides utility functions to get e.g. configured types. |
| 369 |
* |
| 370 |
* @package types |
| 371 |
* @author Roland Gruber |
| 372 |
*/ |
| 373 |
class TypeManager { |
| 374 |
|
| 375 |
private ?LAMConfig $config; |
| 376 |
|
| 377 |
/** |
| 378 |
* Constructor |
| 379 |
* |
| 380 |
* @param LAMConfig|null $config configuration (uses $_SESSION['config'] by default) |
| 381 |
*/ |
| 382 |
public function __construct(?LAMConfig $config = null) { |
| 383 |
if ($config === null) { |
| 384 |
$config = &$_SESSION['config']; |
| 385 |
} |
| 386 |
$this->config = &$config; |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* Checks if the given type id is valid. |
| 391 |
* |
| 392 |
* @param string $typeId type id |
| 393 |
* @return bool is valid |
| 394 |
*/ |
| 395 |
public static function isValidTypeId(string $typeId): bool { |
| 396 |
return preg_match("/^[a-z0-9_-]+$/i", $typeId); |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Returns the configured type with the given id or null. |
| 401 |
* |
| 402 |
* @param string $typeId type id |
| 403 |
* @return ConfiguredType|null type |
| 404 |
*/ |
| 405 |
public function getConfiguredType(string $typeId): ?ConfiguredType { |
| 406 |
if ($this->config == null) { |
| 407 |
return null; |
| 408 |
} |
| 409 |
$activeTypes = $this->config->get_ActiveTypes(); |
| 410 |
if (in_array($typeId, $activeTypes)) { |
| 411 |
return $this->buildConfiguredType($typeId); |
| 412 |
} |
| 413 |
return null; |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* Returns a list of configured account types. |
| 418 |
* |
| 419 |
* @return ConfiguredType[] list of ConfiguredType |
| 420 |
*/ |
| 421 |
public function getConfiguredTypes(): array { |
| 422 |
$configuredTypes = []; |
| 423 |
$activeTypes = $this->config->get_ActiveTypes(); |
| 424 |
foreach ($activeTypes as $typeId) { |
| 425 |
$type = $this->buildConfiguredType($typeId); |
| 426 |
if ($type === null) { |
| 427 |
continue; |
| 428 |
} |
| 429 |
$configuredTypes[] = $type; |
| 430 |
} |
| 431 |
return $configuredTypes; |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Returns a list of configured types for this scope. |
| 436 |
* |
| 437 |
* @param string $scope scope (e.g. user) |
| 438 |
* @return ConfiguredType[] list of ConfiguredType |
| 439 |
*/ |
| 440 |
public function getConfiguredTypesForScope(string $scope): array { |
| 441 |
$allTypes = $this->getConfiguredTypes(); |
| 442 |
$scopedTypes = []; |
| 443 |
foreach ($allTypes as $type) { |
| 444 |
if ($type->getScope() == $scope) { |
| 445 |
$scopedTypes[] = $type; |
| 446 |
} |
| 447 |
} |
| 448 |
return $scopedTypes; |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* Returns a list of configured types for these scopes. |
| 453 |
* |
| 454 |
* @param string[] $scopes scopes (e.g. user) |
| 455 |
* @return ConfiguredType[] list of ConfiguredType |
| 456 |
*/ |
| 457 |
public function getConfiguredTypesForScopes(array $scopes): array { |
| 458 |
$allTypes = $this->getConfiguredTypes(); |
| 459 |
$scopedTypes = []; |
| 460 |
foreach ($allTypes as $type) { |
| 461 |
if (in_array($type->getScope(), $scopes)) { |
| 462 |
$scopedTypes[] = $type; |
| 463 |
} |
| 464 |
} |
| 465 |
return $scopedTypes; |
| 466 |
} |
| 467 |
|
| 468 |
/** |
| 469 |
* Builds a configured account type. |
| 470 |
* |
| 471 |
* @param string $typeId type id |
| 472 |
* @return ConfiguredType|null type |
| 473 |
*/ |
| 474 |
private function buildConfiguredType(string $typeId): ?ConfiguredType { |
| 475 |
$scope = getScopeFromTypeId($typeId); |
| 476 |
if (!class_exists($scope)) { |
| 477 |
return null; |
| 478 |
} |
| 479 |
return new ConfiguredType($this, $scope, $typeId); |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* Generates a new unique type id for the given scope. |
| 484 |
* |
| 485 |
* @param string $scope account type (e.g. user) |
| 486 |
*/ |
| 487 |
public function generateNewTypeId(string $scope): string { |
| 488 |
$activeTypes = $this->config->get_ActiveTypes(); |
| 489 |
if (!in_array($scope, $activeTypes)) { |
| 490 |
return $scope; |
| 491 |
} |
| 492 |
$counter = 1; |
| 493 |
while (in_array($scope . '_' . $counter, $activeTypes)) { |
| 494 |
$counter++; |
| 495 |
} |
| 496 |
return $scope . '_' . $counter; |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* Returns the associated config object. |
| 501 |
* |
| 502 |
* @return LAMConfig|null config |
| 503 |
*/ |
| 504 |
public function getConfig(): ?LAMConfig { |
| 505 |
return $this->config; |
| 506 |
} |
| 507 |
|
| 508 |
/** |
| 509 |
* Returns if configuration is loaded. |
| 510 |
* |
| 511 |
* @return boolean configured |
| 512 |
*/ |
| 513 |
public function hasConfig(): bool { |
| 514 |
return !empty($this->config); |
| 515 |
} |
| 516 |
|
| 517 |
} |