Debian Codegraph @

templates/tests/schemaTest.php

raw
1 <?php
2 namespace LAM\TOOLS\TESTS;
3 use \htmlResponsiveRow;
4 use \htmlTitle;
5 use \htmlStatusMessage;
6 use \htmlSubTitle;
7 use \htmlOutputText;
8 use \htmlImage;
9 use LAM\SCHEMA\ObjectClass;
10 use function \LAM\SCHEMA\get_schema_objectclasses;
11 use function \LAM\SCHEMA\get_cached_schema;
12 /*
13
14 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
15 Copyright (C) 2007 - 2026 Roland Gruber
16
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
21
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30
31 */
32
33 /**
34 * Tests the lamdaemon script.
35 *
36 * @author Roland Gruber
37 * @package tools
38 */
39
40 /** security functions */
41 include_once(__DIR__ . "/../../lib/security.inc");
42 /** access to configuration options */
43 include_once(__DIR__ . "/../../lib/config.inc");
44 /** account modules */
45 include_once(__DIR__ . "/../../lib/modules.inc");
46 /** LDAP schema */
47 include_once(__DIR__ . "/../../lib/schema.inc");
48
49 // start session
50 startSecureSession();
51 enforceUserIsLoggedIn();
52
53 // die if no write access
54 if (!checkIfWriteAccessIsAllowed()) {
55 die();
56 }
57
58 checkIfToolIsActive('toolTests');
59
60 setlanguage();
61
62 include __DIR__ . '/../../lib/adminHeader.inc';
63 echo "<div class=\"smallPaddingContent\">\n";
64
65 $container = new htmlResponsiveRow();
66
67 $container->add(new htmlTitle(_("Schema test")), 12);
68
69 get_schema_objectclasses();
70 $classes = get_cached_schema('objectclasses');
71
72 if (!is_array($classes)) {
73 $container->add(new htmlStatusMessage('ERROR', _('Unable to retrieve schema!'), _('You do not have the required access rights or the LDAP schema is not published by your server.')), 12);
74 }
75 else {
76 // loop for active account types
77 $typeManager = new \LAM\TYPES\TypeManager();
78 $types = $typeManager->getConfiguredTypes();
79 foreach ($types as $type) {
80 $moduleNames = $_SESSION['config']->get_AccountModules($type->getId());
81 $container->add(new htmlSubTitle($type->getAlias()), 12);
82 foreach ($moduleNames as $moduleName) {
83 $error = checkSchemaForModule($moduleName, $type->getScope(), $type->getId());
84 $message = _("No problems found.");
85 $icon = '../../graphics/pass.svg';
86 if ($error != null) {
87 $icon = '../../graphics/del.svg';
88 $message = $error;
89 }
90 // module name
91 $aliasName = getModuleAlias($moduleName, $type->getScope()) ?? '';
92 $container->add(new htmlOutputText($aliasName), 10, 3);
93 // icon
94 $container->add(new htmlImage($icon), 2);
95 // text
96 $container->add(new htmlOutputText($message), 12, 7);
97 $container->addVerticalSpacer('0.5rem');
98 }
99 }
100 }
101
102 parseHtml(null, $container, [], true, 'user');
103
104 echo "</div>\n";
105 include __DIR__ . '/../../lib/adminFooter.inc';
106
107 /**
108 * Checks if the object classes and attributes for this module are available.
109 *
110 * @param string $name module name
111 * @param string $scope type (user, group, ...)
112 * @param string $typeId type id
113 * @return string error message or null
114 */
115 function checkSchemaForModule($name, $scope, $typeId): ?string {
116 $module = new $name($scope);
117 $classes = $module->getManagedObjectClasses($typeId);
118 $attrs = $module->getManagedAttributes($typeId);
119 $aliases = array_flip($module->getLDAPAliases($typeId));
120 if (count($classes) === 0) {
121 return null;
122 }
123 $schemaClasses = get_cached_schema('objectclasses');
124 $schemaAttrs = [];
125 // check if object classes are supported
126 foreach ($classes as $objectClass) {
127 if (!isset($schemaClasses[strtolower($objectClass)]) || !($schemaClasses[strtolower($objectClass)] instanceof ObjectClass)) {
128 return sprintf(_("The object class %s is not supported by your LDAP server."), $objectClass);
129 }
130 // get attribute names
131 $schemaAttrs = array_merge($schemaAttrs, getRecursiveAttributesFromObjectClass($schemaClasses[strtolower($objectClass)]));
132 }
133 // check if attributes are supported
134 foreach ($attrs as $attributeName) {
135 if (str_starts_with($attributeName, 'INFO.')) {
136 continue;
137 }
138 if (!in_array_ignore_case($attributeName, $schemaAttrs) && !in_array_ignore_case(str_replace(';binary', '', $attributeName), $schemaAttrs)) {
139 if (isset($aliases[$attributeName]) && in_array_ignore_case($aliases[$attributeName], $schemaAttrs)) {
140 continue;
141 }
142 return sprintf(_("The attribute %s is not supported for the object class %s by your LDAP server."), $attributeName, implode("/", $classes));
143 }
144 }
145 return null;
146 }
147
148 /**
149 * Returns the names of all attributes which are managed by the given object class and its parents.
150 *
151 * @param ObjectClass $oClass object class
152 * @return string[] list of attribute names
153 */
154 function getRecursiveAttributesFromObjectClass($oClass): array {
155 $attrs = [];
156 $attrs = array_merge($attrs, $oClass->getMustAttrNames());
157 $attrs = array_merge($attrs, $oClass->getMayAttrNames());
158 $subClassNames = $oClass->getSupClasses();
159 foreach ($subClassNames as $subClassName) {
160 $schemaClasses = get_cached_schema('objectclasses');
161 $subClass = $schemaClasses[strtolower($subClassName)];
162 if ($subClass instanceof ObjectClass) {
163 $attrs = array_merge($attrs, getRecursiveAttributesFromObjectClass($subClass));
164 }
165 }
166 return $attrs;
167 }