refactoring

This commit is contained in:
Roland Gruber 2025-07-16 19:29:43 +02:00
parent 2adae535e2
commit 68a26ab851
3 changed files with 63 additions and 48 deletions

View file

@ -1748,8 +1748,8 @@ class moduleCache {
/** /**
* Returns a new/cached module with the given name and scope. * Returns a new/cached module with the given name and scope.
* *
* @param String $name module name * @param string $name module name
* @param String $scope module scope (e.g. user) * @param string|null $scope module scope (e.g. user)
* @return null|baseModule module object * @return null|baseModule module object
*/ */
public static function getModule($name, $scope): ?object { public static function getModule($name, $scope): ?object {

View file

@ -3,17 +3,20 @@
namespace LAM\CONFIG; namespace LAM\CONFIG;
use htmlJavaScript; use htmlJavaScript;
use \moduleCache; use LAM\TYPES\TypeManager;
use \htmlSpacer; use LAMConfig;
use \htmlTable; use moduleCache;
use \htmlButton; use htmlSpacer;
use \htmlResponsiveRow; use htmlTable;
use \htmlSubTitle; use htmlButton;
use htmlResponsiveRow;
use htmlSubTitle;
use function LAM\TYPES\getTypes;
/* /*
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2009 - 2024 Roland Gruber Copyright (C) 2009 - 2025 Roland Gruber
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -70,6 +73,9 @@ if (isset($_POST['cancelSettings'])) {
} }
$conf = &$_SESSION['conf_config']; $conf = &$_SESSION['conf_config'];
if (!($conf instanceof LAMConfig)) {
die();
}
$errorsToDisplay = checkInput(); $errorsToDisplay = checkInput();
@ -105,8 +111,6 @@ if ((isset($_POST['saveSettings']) || isset($_POST['editmodules'])
} }
} }
$allTypes = \LAM\TYPES\getTypes();
echo $_SESSION['header']; echo $_SESSION['header'];
printHeaderContents(_("LDAP Account Manager Configuration"), '../..'); printHeaderContents(_("LDAP Account Manager Configuration"), '../..');
echo "</head><body>\n"; echo "</head><body>\n";
@ -130,7 +134,7 @@ printConfigurationPageTabs(ConfigurationPageTab::MODULE_SETTINGS);
// module settings // module settings
$typeManager = new \LAM\TYPES\TypeManager($conf); $typeManager = new TypeManager($conf);
$types = $typeManager->getConfiguredTypes(); $types = $typeManager->getConfiguredTypes();
// get list of scopes of modules // get list of scopes of modules
@ -156,19 +160,22 @@ for ($i = 0; $i < count($modules); $i++) {
continue; continue;
} }
$module = moduleCache::getModule($modules[$i], null); $module = moduleCache::getModule($modules[$i], null);
if ($module === null) {
continue;
}
$iconImage = $module->getIcon(); $iconImage = $module->getIcon();
if (($iconImage != null) && !(str_starts_with($iconImage, 'http')) && !(str_starts_with($iconImage, '/'))) { if (($iconImage != null) && !(str_starts_with($iconImage, 'http')) && !(str_starts_with($iconImage, '/'))) {
$iconImage = '../../graphics/' . $iconImage; $iconImage = '../../graphics/' . $iconImage;
} }
$row = new htmlResponsiveRow(); $row = new htmlResponsiveRow();
$row->add(new htmlSubTitle(getModuleAlias($modules[$i], null), $iconImage, null, true), 12); $row->add(new htmlSubTitle($module->get_alias(), $iconImage, null, true));
if (is_array($options[$modules[$i]])) { if (is_array($options[$modules[$i]])) {
foreach ($options[$modules[$i]] as $option) { foreach ($options[$modules[$i]] as $option) {
$row->add($option, 12); $row->add($option);
} }
} }
else { else {
$row->add($options[$modules[$i]], 12); $row->add($options[$modules[$i]]);
} }
$configTypes = parseHtml($modules[$i], $row, $old_options, false, null); $configTypes = parseHtml($modules[$i], $row, $old_options, false, null);
$_SESSION['conf_types'] = array_merge($configTypes, $_SESSION['conf_types']); $_SESSION['conf_types'] = array_merge($configTypes, $_SESSION['conf_types']);
@ -210,7 +217,10 @@ function checkInput(): array {
return []; return [];
} }
$conf = &$_SESSION['conf_config']; $conf = &$_SESSION['conf_config'];
$typeManager = new \LAM\TYPES\TypeManager($conf); if (!($conf instanceof LAMConfig)) {
return [];
}
$typeManager = new TypeManager($conf);
$types = $typeManager->getConfiguredTypes(); $types = $typeManager->getConfiguredTypes();
// check module options // check module options

View file

@ -1,18 +1,23 @@
<?php <?php
namespace LAM\DELETE; namespace LAM\DELETE;
use \htmlGroup; use accountContainer;
use DateTime;
use htmlGroup;
use htmlJavaScript; use htmlJavaScript;
use \htmlResponsiveRow; use htmlResponsiveRow;
use \htmlButton; use htmlButton;
use \htmlSpacer; use htmlSpacer;
use \htmlHiddenInput; use htmlHiddenInput;
use \htmlOutputText; use htmlOutputText;
use \htmlStatusMessage; use htmlStatusMessage;
use LAM\TYPES\TypeManager;
use moduleCache;
/* /*
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2003 - 2006 Tilo Lutz Copyright (C) 2003 - 2006 Tilo Lutz
Copyright (C) 2007 - 2023 Roland Gruber Copyright (C) 2007 - 2025 Roland Gruber
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -83,7 +88,7 @@ foreach ($_SESSION as $key => $value) {
} }
} }
$typeManager = new \LAM\TYPES\TypeManager(); $typeManager = new TypeManager();
if (isset($_POST['type']) && ($typeManager->getConfiguredType($_POST['type']) === null)) { if (isset($_POST['type']) && ($typeManager->getConfiguredType($_POST['type']) === null)) {
logNewMessage(LOG_ERR, 'Invalid type: ' . $_POST['type']); logNewMessage(LOG_ERR, 'Invalid type: ' . $_POST['type']);
@ -109,42 +114,45 @@ if (isset($_GET['type']) && isset($_SESSION['delete_dn'])) {
$users[] = substr($dn, $start, $end-$start); $users[] = substr($dn, $start, $end-$start);
} }
$sessionKey = $sessionAccountPrefix . (new \DateTime('now', getTimeZone()))->getTimestamp() . generateRandomText(); $sessionKey = $sessionAccountPrefix . (new DateTime('now', getTimeZone()))->getTimestamp() . generateRandomText();
//load account //load account
$_SESSION[$sessionKey] = new \accountContainer($type, $sessionKey); $_SESSION[$sessionKey] = new accountContainer($type, $sessionKey);
// Show HTML Page // Show HTML Page
include __DIR__ . '/../lib/adminHeader.inc'; include __DIR__ . '/../lib/adminHeader.inc';
echo "<div class=\"smallPaddingContent\">"; echo "<div class=\"smallPaddingContent\">";
echo "<br>\n"; echo "<br>\n";
echo "<form action=\"delete.php\" method=\"post\">\n"; echo "<form action=\"delete.php\" method=\"post\">\n";
$container = new htmlResponsiveRow(); $container = new htmlResponsiveRow();
$container->add(new htmlOutputText(_("Do you really want to remove the following accounts?")), 12); $container->add(new htmlOutputText(_("Do you really want to remove the following accounts?")));
$container->addVerticalSpacer('2rem'); $container->addVerticalSpacer('2rem');
$userCount = count($users); $userCount = count($users);
for ($i = 0; $i < $userCount; $i++) { for ($i = 0; $i < $userCount; $i++) {
$container->addLabel(new htmlOutputText(_("Account name:"))); $container->addLabel(new htmlOutputText(_("Account name:")));
$container->addField(new htmlOutputText($users[$i])); $container->addField(new htmlOutputText($users[$i]));
$container->addLabel(new htmlOutputText(_('DN') . ':')); $container->addLabel(new htmlOutputText(_('DN') . ':'));
$container->addField(new htmlOutputText($_SESSION['delete_dn'][$i])); $container->addField(new htmlOutputText((string) $_SESSION['delete_dn'][$i]));
$_SESSION[$sessionKey]->load_account($_SESSION['delete_dn'][$i]); $_SESSION[$sessionKey]->load_account($_SESSION['delete_dn'][$i]);
if (!$_SESSION[$sessionKey]->hasOnlyVirtualChildren()) { if (!$_SESSION[$sessionKey]->hasOnlyVirtualChildren()) {
$childCount = getChildCount($_SESSION['delete_dn'][$i]); $childCount = getChildCount($_SESSION['delete_dn'][$i]);
if ($childCount > 0) { if ($childCount > 0) {
$container->addLabel(new htmlOutputText(_('Number of child entries') . ':')); $container->addLabel(new htmlOutputText(_('Number of child entries') . ':'));
$container->addField(new htmlOutputText($childCount)); $container->addField(new htmlOutputText((string) $childCount));
} }
} }
$container->addVerticalSpacer('0.5rem'); $container->addVerticalSpacer('0.5rem');
} }
addSecurityTokenToMetaHTML($container); addSecurityTokenToMetaHTML($container);
$container->add(new htmlHiddenInput('type', $type->getId()), 12); $container->add(new htmlHiddenInput('type', $type->getId()));
$container->addVerticalSpacer('1rem'); $container->addVerticalSpacer('1rem');
parseHtml(null, $container, [], false, $type->getScope()); parseHtml(null, $container, [], false, $type->getScope());
// Print delete rows from modules // Print delete rows from modules
$modules = $_SESSION['config']->get_AccountModules($type->getId()); $modules = $_SESSION['config']->get_AccountModules($type->getId());
$values = []; $values = [];
foreach ($modules as $module) { foreach ($modules as $module) {
$module = \moduleCache::getModule($module, $type->getScope()); $module = moduleCache::getModule($module, $type->getScope());
if ($module === null) {
continue;
}
parseHtml($module::class, $module->display_html_delete(), $values, true, $type->getScope()); parseHtml($module::class, $module->display_html_delete(), $values, true, $type->getScope());
} }
$buttonContainer = new htmlResponsiveRow(); $buttonContainer = new htmlResponsiveRow();
@ -156,7 +164,7 @@ if (isset($_GET['type']) && isset($_SESSION['delete_dn'])) {
$buttonGroup->addElement(new htmlSpacer('0.5rem', null)); $buttonGroup->addElement(new htmlSpacer('0.5rem', null));
$cancelButton = new htmlButton('cancel', _('Cancel')); $cancelButton = new htmlButton('cancel', _('Cancel'));
$buttonGroup->addElement($cancelButton); $buttonGroup->addElement($cancelButton);
$buttonContainer->add($buttonGroup, 12); $buttonContainer->add($buttonGroup);
$buttonContainer->addVerticalSpacer('1rem'); $buttonContainer->addVerticalSpacer('1rem');
parseHtml(null, $buttonContainer, [], false, $type->getScope()); parseHtml(null, $buttonContainer, [], false, $type->getScope());
echo "</form>\n"; echo "</form>\n";
@ -180,8 +188,8 @@ elseif (isset($_POST['cancelAllOk'])) {
if (isset($_POST['delete'])) { if (isset($_POST['delete'])) {
$typeId = $_POST['type']; $typeId = $_POST['type'];
$type = $typeManager->getConfiguredType($typeId); $type = $typeManager->getConfiguredType($typeId);
if (!checkIfDeleteEntriesIsAllowed($type->getId()) || !checkIfWriteAccessIsAllowed($type->getId())) { if (($type === null) || !checkIfDeleteEntriesIsAllowed($type->getId()) || !checkIfWriteAccessIsAllowed($type->getId())) {
logNewMessage(LOG_ERR, 'User tried to delete entries of forbidden type '. $type->getId()); logNewMessage(LOG_ERR, 'User tried to delete entries of forbidden type '. $typeId);
die(); die();
} }
// Show HTML Page // Show HTML Page
@ -190,15 +198,15 @@ if (isset($_POST['delete'])) {
echo "<div class=\"smallPaddingContent\"><br>\n"; echo "<div class=\"smallPaddingContent\"><br>\n";
$container = new htmlResponsiveRow(); $container = new htmlResponsiveRow();
addSecurityTokenToMetaHTML($container); addSecurityTokenToMetaHTML($container);
$container->add(new htmlHiddenInput('type', $type->getId()), 12); $container->add(new htmlHiddenInput('type', $type->getId()));
$sessionKey = $sessionAccountPrefix . (new \DateTime('now', getTimeZone()))->getTimestamp() . generateRandomText(); $sessionKey = $sessionAccountPrefix . (new DateTime('now', getTimeZone()))->getTimestamp() . generateRandomText();
$_SESSION[$sessionKey] = new \accountContainer($type, $sessionKey); $_SESSION[$sessionKey] = new accountContainer($type, $sessionKey);
// Delete dns // Delete dns
$allOk = true; $allOk = true;
$allErrors = []; $allErrors = [];
foreach ($_SESSION['delete_dn'] as $deleteDN) { foreach ($_SESSION['delete_dn'] as $deleteDN) {
// Set to true if an real error has happened // Set to true if a real error has happened
$stopProcessing = false; $stopProcessing = false;
// First load DN. // First load DN.
$_SESSION[$sessionKey]->load_account($deleteDN); $_SESSION[$sessionKey]->load_account($deleteDN);
@ -313,22 +321,19 @@ if (isset($_POST['delete'])) {
} }
} }
if (!$stopProcessing) { if (!$stopProcessing) {
$container->add(new htmlOutputText(sprintf(_('Deleted DN: %s'), $deleteDN)), 12); $container->add(new htmlOutputText(sprintf(_('Deleted DN: %s'), $deleteDN)));
foreach ($errors as $error) {
$container->add(htmlStatusMessage::fromParamArray($error), 12);
}
} }
else { else {
$container->add(new htmlOutputText(sprintf(_('Error while deleting DN: %s'), $deleteDN)), 12); $container->add(new htmlOutputText(sprintf(_('Error while deleting DN: %s'), $deleteDN)));
foreach ($errors as $error) { }
$container->add(htmlStatusMessage::fromParamArray($error), 12); foreach ($errors as $error) {
} $container->add(htmlStatusMessage::fromParamArray($error));
} }
$allErrors = [...$allErrors, ...$errors]; $allErrors = [...$allErrors, ...$errors];
} }
$container->addVerticalSpacer('2rem'); $container->addVerticalSpacer('2rem');
$buttonName = $allOk ? 'cancelAllOk' : 'cancel'; $buttonName = $allOk ? 'cancelAllOk' : 'cancel';
$container->add(new htmlButton($buttonName, _('Back to list')), 12); $container->add(new htmlButton($buttonName, _('Back to list')));
$container->addVerticalSpacer('1rem'); $container->addVerticalSpacer('1rem');
if ($allOk) { if ($allOk) {
$_SESSION['listRedirectMessages'] = $allErrors; $_SESSION['listRedirectMessages'] = $allErrors;