. */ namespace SP\Modules\Web\Controllers\ConfigLdap; use Exception; use JsonException; use SP\Core\Application; use SP\Core\Events\Event; use SP\Domain\Auth\Ports\LdapCheckService; use SP\Domain\Core\Acl\AclActionsInterface; use SP\Domain\Core\Acl\UnauthorizedPageException; use SP\Domain\Core\Exceptions\CheckException; use SP\Domain\Core\Exceptions\SessionTimeout; use SP\Domain\Core\Exceptions\SPException; use SP\Domain\Http\Dtos\JsonMessage; use SP\Modules\Web\Controllers\SimpleControllerBase; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Mvc\Controller\SimpleControllerHelper; use SP\Mvc\View\TemplateInterface; use function SP\__; use function SP\__u; use function SP\processException; /** * Class CheckImportController */ final class CheckImportController extends SimpleControllerBase { use JsonTrait; use ConfigLdapTrait; public function __construct( Application $application, SimpleControllerHelper $simpleControllerHelper, private readonly LdapCheckService $ldapCheckService, private readonly TemplateInterface $template ) { parent::__construct($application, $simpleControllerHelper); } /** * @return bool * @throws JsonException * @throws SPException */ public function checkImportAction(): bool { try { $ldapParams = $this->getLdapParamsFromRequest($this->request); // Valores para la configuración de LDAP if (!($ldapParams->getServer() || $ldapParams->getSearchBase() || $ldapParams->getBindDn()) ) { return $this->returnJsonResponse(JsonMessage::JSON_ERROR, __u('Missing LDAP parameters')); } $filter = $this->request->analyzeString('ldap_import_filter'); if (empty($filter)) { $data = $this->ldapCheckService->getObjects( $this->request->analyzeBool('ldap_import_groups', false), $ldapParams ); } else { $data = $this->ldapCheckService->getObjectsByFilter($filter, $ldapParams); } $this->template->addTemplate('results', 'itemshow'); $this->template->assign('header', __('Results')); $this->template->assign('results', $data->getResults()); return $this->returnJsonResponseData( ['template' => $this->template->render(), 'items' => $data->getResults()], JsonMessage::JSON_SUCCESS, __u('LDAP connection OK'), [sprintf(__('Objects found: %d'), $data->count())] ); } catch (Exception $e) { processException($e); $this->eventDispatcher->notify('exception', new Event($e)); return $this->returnJsonResponseException($e); } } /** * @return void * @throws SPException * @throws SessionTimeout */ protected function initialize(): void { try { $this->checks(); $this->checkAccess(AclActionsInterface::CONFIG_LDAP); $this->extensionChecker->checkLdap(true); } catch (UnauthorizedPageException|CheckException $e) { $this->eventDispatcher->notify('exception', new Event($e)); $this->returnJsonResponseException($e); } } }