Skip to content

Commit

Permalink
Auth: Fix type issue and streamline login/logout page code (next step…
Browse files Browse the repository at this point in the history
  • Loading branch information
mjansenDatabay committed Sep 18, 2024
1 parent b303809 commit 4f67c74
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class LoginPageLanguagesOverviewTable implements UI\Component\Table\DataRetrieva
private const DEACTIVATE = 'deactivate';
private const EDIT = 'edit';

protected ServerRequestInterface $request;
protected Data\Factory $data_factory;
private ServerRequestInterface $request;
private Data\Factory $data_factory;
/**
* @var list<array<string, mixed>>|null
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@
use ILIAS\UI;
use ilArrayUtil;
use Psr\Http\Message\ServerRequestInterface;
use ilAuthLoginPageEditorSettings;
use ilLanguage;
use ilCtrl;
use ilCtrlInterface;
use ilAuthLogoutPageEditorSettings;

class LogoutPageLanguagesOverviewTable implements UI\Component\Table\DataRetrieval
{
private const ACTIVATE = 'activate';
private const DEACTIVATE = 'deactivate';
private const EDIT = 'edit';


private ServerRequestInterface $request;
private Data\Factory $data_factory;
/**
Expand All @@ -39,7 +43,7 @@ class LogoutPageLanguagesOverviewTable implements UI\Component\Table\DataRetriev
private ?array $records = null;

public function __construct(
private readonly ilCtrl $ctrl,
private readonly ilCtrlInterface $ctrl,
private readonly ilLanguage $lng,
\ILIAS\HTTP\Services $http,
private readonly \ILIAS\UI\Factory $ui_factory,
Expand Down Expand Up @@ -106,19 +110,19 @@ protected function getActions(): array
);

return [
'edit' => $this->ui_factory->table()->action()->single(
self::EDIT => $this->ui_factory->table()->action()->single(
$this->lng->txt('edit'),
$overview_url_builder->withParameter($overview_action_parameter, 'edit'),
$overview_url_builder->withParameter($overview_action_parameter, self::EDIT),
$overview_row_id
),
'activate' => $this->ui_factory->table()->action()->standard(
self::ACTIVATE => $this->ui_factory->table()->action()->standard(
$this->lng->txt('page_design_activate'),
$overview_url_builder->withParameter($overview_action_parameter, 'activate'),
$overview_url_builder->withParameter($overview_action_parameter, self::ACTIVATE),
$overview_row_id
),
'deactivate' => $this->ui_factory->table()->action()->standard(
self::DEACTIVATE => $this->ui_factory->table()->action()->standard(
$this->lng->txt('page_design_deactivate'),
$overview_url_builder->withParameter($overview_action_parameter, 'deactivate'),
$overview_url_builder->withParameter($overview_action_parameter, self::DEACTIVATE),
$overview_row_id
)
];
Expand Down Expand Up @@ -179,7 +183,7 @@ public function getRows(

foreach ($records as $record) {
$row_id = (string) $record['key'];
$deactivate_action = (bool) $record['status'] == true ? 'activate' : 'deactivate';
$deactivate_action = (bool) $record['status'] === true ? self::ACTIVATE : self::DEACTIVATE;
yield $row_builder->buildDataRow($row_id, $record)->withDisabledAction($deactivate_action);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function executeCommand(): void
$this->ctrl->getLinkTarget($this, 'show')
);

if (strtolower((string) $this->redirect_source) !== strtolower(ilInternalLinkGUI::class)) {
if (strtolower($this->redirect_source ?? '') !== strtolower(ilInternalLinkGUI::class)) {
$this->forwardToPageObject();
}
break;
Expand Down Expand Up @@ -184,7 +184,8 @@ private function handleLoginPageActions(): void
break;

case 'edit':
$this->ctrl->setParameter($this, 'loginpage_languages_key', current($keys));
$this->ctrl->setParameter($this, 'loginpage_languages_key', (string) current($keys));
$this->ctrl->setParameter($this, 'key', ilLanguage::lookupId((string) current($keys)));
$this->ctrl->redirectByClass(ilLoginPageGUI::class, 'edit');

// no break
Expand All @@ -208,7 +209,7 @@ private function getLangKeysToUpdate(): array

$lang_keys = $this->lng->getInstalledLanguages();

if (current($keys) !== 'ALL_OBJECTS') {
if ((string) current($keys) !== 'ALL_OBJECTS') {
$lang_keys = array_intersect($keys, $lang_keys);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,15 @@ class ilAuthLogoutPageEditorGUI
private ilLanguage $lng;
private ilGlobalTemplateInterface $tpl;
private ilTabsGUI $tabs;
private ilRbacSystem $rbacsystem;
private ilSetting $setting;
private ilErrorHandling $ilErr;
private ?ilPropertyFormGUI $form;
private \ILIAS\HTTP\Services $http;
private \ILIAS\Refinery\Factory $refinery;
private \ILIAS\UI\Factory $ui_factory;
private \ILIAS\UI\Renderer $ui_renderer;
private int $ref_id;
private ilAuthLogoutPageEditorSettings $settings;
private ?ilSetting $logout_settings = null;
private \ILIAS\Style\Content\Object\ObjectFacade $content_style_domain;
private ?string $redirect_source = null;
private ?int $key = null;
private GUIService $content_style_gui;
private int $ref_id;

public function __construct(int $a_ref_id)
{
Expand All @@ -53,9 +47,6 @@ public function __construct(int $a_ref_id)
$this->ctrl = $DIC->ctrl();
$this->tpl = $DIC->ui()->mainTemplate();
$this->tabs = $DIC->tabs();
$this->rbacsystem = $DIC->rbac()->system();
$this->setting = $DIC->settings();
$this->ilErr = $DIC['ilErr'];

$this->http = $DIC->http();
$this->refinery = $DIC->refinery();
Expand All @@ -65,9 +56,8 @@ public function __construct(int $a_ref_id)
$this->lng = $DIC['lng'];

$this->lng->loadLanguageModule('auth');
$this->ref_id = $a_ref_id;

$this->settings = ilAuthLogoutPageEditorSettings::getInstance();
$this->ref_id = $a_ref_id;

$content_style = $DIC->contentStyle();
$this->content_style_domain = $content_style
Expand All @@ -90,19 +80,9 @@ public function __construct(int $a_ref_id)
}
}

public function getSettings(): ilAuthLogoutPageEditorSettings
{
return $this->settings;
}

public function getRefId(): int
{
return $this->ref_id;
}

public function executeCommand(): void
{
switch (strtolower($this->ctrl->getNextClass($this))) {
switch (strtolower($this->ctrl->getNextClass($this) ?? '')) {
case strtolower(ilLogoutPageGUI::class):
$this->tabs->clearTargets();
$this->tabs->setBackTarget(
Expand Down Expand Up @@ -134,7 +114,13 @@ private function forwardToPageObject(): void
])
);

$this->key = ilLanguage::lookupId(current($keys));
$this->key = ilLanguage::lookupId((string) current($keys));

if ($this->key === 0) {
$this->tpl->setOnScreenMessage('failure', $this->lng->txt('language_does_not_exist'), true);
$this->show();
return;
}

$this->ctrl->saveParameter($this, 'key');

Expand All @@ -155,7 +141,6 @@ private function forwardToPageObject(): void
$page_gui = new ilLogoutPageGUI($this->key);

$page_gui->setTemplateTargetVar('ADM_CONTENT');

$page_gui->setStyleId($this->content_style_domain->getEffectiveStyleId());
$page_gui->setTemplateOutput(false);

Expand Down Expand Up @@ -199,19 +184,18 @@ private function handleLogoutPageActions(): void
break;

case 'edit':
$this->ctrl->setParameter($this, 'logoutpage_languages_key', current($keys));
$this->ctrl->setParameter($this, 'key', ilLanguage::lookupId(current($keys)));
$this->ctrl->setParameter($this, 'logoutpage_languages_key', (string) current($keys));
$this->ctrl->setParameter($this, 'key', ilLanguage::lookupId((string) current($keys)));
$this->ctrl->redirectByClass(ilLogoutPageGUI::class, 'edit');
break;

// no break
default:
$this->ctrl->redirect($this, 'show');
break;
}
}

/**
* @return array<string>
* @return list<string>
*/
private function getLangKeysToUpdate(): array
{
Expand All @@ -225,7 +209,7 @@ private function getLangKeysToUpdate(): array

$lang_keys = $this->lng->getInstalledLanguages();

if (!((string) current($keys) == 'ALL_OBJECTS')) {
if ((string) current($keys) !== 'ALL_OBJECTS') {
$lang_keys = array_intersect($keys, $lang_keys);
}

Expand Down Expand Up @@ -274,110 +258,4 @@ private function showIliasEditor(): void

$this->tpl->setContent($this->ui_renderer->render($tbl->getComponent()));
}

private function saveLogoutInfo(): void
{
if (!$this->rbacsystem->checkAccess('write', $this->getRefId())) {
$this->ilErr->raiseError($this->lng->txt('permission_denied'), $this->ilErr->MESSAGE);
}

$this->initLogoutForm();
if ($this->form->checkInput()) {
$this->logout_settings = new ilSetting('logout_settings');
foreach ($this->lng->getInstalledLanguages() as $lang_key) {
$settingKey = 'logout_message_' . $lang_key;
if ($this->form->getInput($settingKey)) {
$this->logout_settings->set($settingKey, $this->form->getInput($settingKey));
}
}

if ($this->form->getInput('default_auth_mode')) {
$this->setting->set('default_auth_mode', $this->form->getInput('default_auth_mode'));
}

$this->tpl->setOnScreenMessage('success', $this->lng->txt('logout_information_settings_saved'), true);
}

$this->ctrl->redirect($this, 'show');
}

private function initLogoutForm(): void
{
$this->form = new ilPropertyFormGUI();
$this->form->setFormAction($this->ctrl->getFormAction($this, 'saveLogoutInfo'));
$this->form->setTableWidth('80%');
$this->form->setTitle($this->lng->txt('logout_information'));

$this->form->addCommandButton('saveLogoutInfo', $this->lng->txt('save'));

if (!is_object($this->logout_settings)) {
$this->logout_settings = new ilSetting('logout_settings');
}

$logout_settings = $this->logout_settings->getAll();
$languages = $this->lng->getInstalledLanguages();
$def_language = $this->lng->getDefaultLanguage();

foreach ($this->setDefLangFirst($def_language, $languages) as $lang_key) {
$add = '';

if ($lang_key === $def_language) {
$add = ' (' . $this->lng->txt('default') . ')';
}

$textarea = new ilTextAreaInputGUI(
$this->lng->txt('meta_l_' . $lang_key) . $add,
'logout_message_' . $lang_key
);

$textarea->setRows(10);
$msg_logout_lang = 'logout_message_' . $lang_key;

if (isset($logout_settings[$msg_logout_lang])) {
$textarea->setValue($logout_settings[$msg_logout_lang]);
}

$this->form->addItem($textarea);

unset($logout_settings['logout_message_' . $lang_key]);
}

foreach ($logout_settings as $key => $message) {
$lang_key = substr($key, strrpos($key, '_') + 1, strlen($key) - strrpos($key, '_'));

$textarea = new ilTextAreaInputGUI(
$this->lng->txt('meta_l_' . $lang_key),
'logout_message_' . $lang_key
);

$textarea->setRows(10);
$textarea->setValue($message);

if (!in_array($lang_key, $languages, true)) {
$textarea->setAlert($this->lng->txt('not_installed'));
}

$this->form->addItem($textarea);
}
}

/**
* @param array<string> $a_languages
* @return array<string>
*/
private function setDefLangFirst(string $a_def_language, array $a_languages): array
{
$languages = [];
if ($a_def_language !== '') {
$languages[] = $a_def_language;

foreach ($a_languages as $val) {
if (!in_array($val, $languages, true)) {
$languages[] = $val;
}
}
}

return $languages;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function getInstance(): self
return self::$instance ?? (self::$instance = new self());
}

protected function getStorage(): ilSetting
private function getStorage(): ilSetting
{
return $this->storage;
}
Expand Down

0 comments on commit 4f67c74

Please sign in to comment.