19 declare(strict_types=1);
53 $this->
ctrl = $DIC->ctrl();
54 $this->
lng = $DIC->language();
55 $this->rbacreview = $DIC->rbac()->review();
56 $this->tpl = $DIC->ui()->mainTemplate();
58 $this->ilErr = $DIC[
'ilErr'];
59 $this->
help = $DIC->help();
60 $this->
http = $DIC->http();
62 $this->ui_factory = $DIC->ui()->factory();
63 $this->ui_renderer = $DIC->ui()->renderer();
64 $this->actor = $DIC->user();
66 $this->pwa_repository = new \ILIAS\Init\PasswordAssitance\Repository\PasswordAssistanceDbRepository(
70 $this->
help->setScreenIdComponent(
'init');
75 $key = $this->
http->wrapper()->query()->retrieve(
78 $this->refinery->kindlyTo()->string(),
80 $this->
http->wrapper()->post()->retrieve(
82 $this->
refinery->byTrying([$this->refinery->kindlyTo()->string(), $this->
refinery->always(
'')])
99 if (!$this->
settings->get(
'setup_ok')) {
100 $this->ilErr->raiseError(
'Setup is not completed. Please run setup routine again.', $this->ilErr->FATAL);
104 if (!$this->
settings->get(
'password_assistance')) {
105 $this->ilErr->raiseError($this->
lng->txt(
'permission_denied'), $this->ilErr->MESSAGE);
108 if ($this->actor->getId() > 0 && !$this->actor->isAnonymous()) {
109 $this->ilErr->raiseError($this->
lng->txt(
'permission_denied'), $this->ilErr->MESSAGE);
112 $this->
lng->loadLanguageModule(
'pwassist');
113 $cmd = $this->
ctrl->getCmd() ??
'';
114 $next_class = $this->
ctrl->getNextClass($this);
116 switch ($next_class) {
118 if ($cmd !==
'' && method_exists($this, $cmd)) {
139 return [
'submitAssignPasswordForm'];
150 private function buildUrl(
string $script, array $query_parameters): string
152 $url = implode(
'/', [
159 http_build_query($query_parameters,
'',
'&')
167 return $this->
refinery->custom()->constraint(
168 static function ($value):
bool {
171 $this->
lng->txt(
'email_not_valid')
177 return $this->
refinery->custom()->transformation(
static function (array $values): array {
178 return array_merge(...$values);
184 return $this->
refinery->custom()->transformation(
static function (array $values): array {
191 return $this->
refinery->custom()->transformation(
static function ($value) {
192 if (is_string($value)) {
193 $value = trim($value);
202 $field_factory = $this->ui_factory->input()->field();
204 return $this->ui_factory
209 $this->
ctrl->getFormAction($this,
'submitAssistanceForm'),
211 $field_factory->section(
213 self::PROP_USERNAME => $field_factory
214 ->text($this->
lng->txt(
'username'))
216 ->withRequired(
true),
217 self::PROP_EMAIL => $field_factory
218 ->text($this->
lng->txt(
'email'))
221 ->withAdditionalTransformation($this->
emailTrafo()),
223 $this->
lng->txt(
'password_assistance'),
228 ->withSubmitLabel($this->
lng->txt(
'submit'))
235 $this->
help->setSubScreenId(
'password_assistance');
238 $tpl->
setVariable(
'TXT_PAGEHEADLINE', $this->
lng->txt(
'password_assistance'));
241 $this->ui_renderer->render($this->ui_factory->symbol()->icon()->custom(
243 $this->
lng->txt(
'password_assistance')
248 'TXT_ENTER_USERNAME_AND_EMAIL',
249 $this->ui_renderer->render(
250 $this->ui_factory->messageBox()->info(
255 $this->
lng->txt(
'pwassist_enter_username_and_email'),
265 $tpl->
setVariable(
'FORM', $this->ui_renderer->render($form ?? $this->getAssistanceForm()));
282 if ($this->
http->request()->getMethod() ===
'POST') {
283 $form = $form->withRequest($this->
http->request());
284 $form_data = $form->getData();
285 $form_valid = $form_data !==
null;
289 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
'form_input_not_valid'));
295 if (
$GLOBALS[
'DIC'][
'ilSetting']->
get(
'auth_mode')) {
296 $defaultAuth =
$GLOBALS[
'DIC'][
'ilSetting']->get(
'auth_mode');
299 $username = $form_data[self::PROP_USERNAME];
300 $email = $form_data[self::PROP_EMAIL];
302 $assistance_callback =
function () use ($defaultAuth, $username, $email):
void {
304 if (!is_numeric($usr_id) || !($usr_id > 0)) {
307 'Could not process password assistance form (reason: no user found) %s / %s',
316 $email_addresses =
array_map(
'strtolower', [$user->getEmail(), $user->getSecondEmail()]);
318 if (!in_array(strtolower($email), $email_addresses,
true)) {
319 if (implode(
'', $email_addresses) ===
'') {
322 'Could not process password assistance form (reason: account without email addresses): %s / %s',
330 'Could not process password assistance form (reason: account email addresses differ from input): %s / %s',
347 'Could not process password assistance form (reason: not permitted for accounts using external authentication sources): %s / %s',
356 'Could not process password assistance form (reason: not permitted for system user or anonymous): %s / %s',
362 $this->sendPasswordAssistanceMail($user);
366 if (($assistance_duration = $this->
settings->get(
'account_assistance_duration')) !==
null) {
367 $duration = $this->
http->durations()->callbackDuration((
int) $assistance_duration);
368 $status =
$duration->stretch($assistance_callback);
370 $status = $assistance_callback();
373 $this->
showMessageForm(sprintf($this->
lng->txt(
'pwassist_mail_sent'), $email), self::PERMANENT_LINK_TARGET_PW);
386 private function sendPasswordAssistanceMail(
ilObjUser $userObj):
void 390 $session = $this->pwa_repository->createSession(
391 $this->pwa_repository->generateHash(),
392 new \ILIAS\Data\ObjectId($userObj->
getId())
399 'lang' => $this->
lng->getLangKey(),
400 'key' => $session->hash()->value()
404 $alternative_pwassist_url = $this->
buildUrl(
408 'lang' => $this->
lng->getLangKey(),
409 'key' => $session->hash()->value()
414 $senderFactory = $DIC->mail()->mime()->senderFactory();
415 $sender = $senderFactory->system();
418 $mm->Subject($this->
lng->txt(
'pwassist_mail_subject'),
true);
426 $this->
lng->txt(
'pwassist_mail_body'),
431 'mailto:' . $DIC->settings()->get(
'admin_email'),
432 $alternative_pwassist_url
441 $field_factory = $this->ui_factory->input()->field();
443 $key = $field_factory
446 ->withDedicatedName(self::PROP_KEY);
447 if ($pwassist_id !==
null) {
448 $key = $key->withValue($pwassist_id);
451 return $this->ui_factory
456 $this->
ctrl->getFormAction($this,
'submitAssignPasswordForm'),
458 $field_factory->section(
460 self::PROP_KEY => $key,
461 self::PROP_USERNAME => $field_factory
462 ->text($this->
lng->txt(
'username'))
464 ->withRequired(
true),
465 self::PROP_PASSWORD => $field_factory
467 $this->
lng->txt(
'password'),
471 ->withRevelation(
true)
472 ->withAdditionalTransformation(
473 $this->
refinery->custom()->constraint(
474 static function (
ILIAS\
Data\Password $value):
bool {
476 trim($value->toString())
480 $problem =
$lng(
'passwd_invalid');
481 $custom_problem =
null;
483 trim($value->toString()),
486 $problem = $custom_problem;
493 ->withAdditionalTransformation(
494 $this->
refinery->custom()->transformation(
495 static function (
ILIAS\
Data\Password $value):
string {
496 return trim($value->toString());
501 $this->
lng->txt(
'password_assistance'),
506 ->withSubmitLabel($this->
lng->txt(
'submit'))
521 private function showAssignPasswordForm(
523 string $pwassist_id =
'' 525 $this->
help->setSubScreenId(
'password_input');
527 if ($pwassist_id ===
'') {
531 $result = $this->pwa_repository->getSessionByHash(
532 new \
ILIAS\Init\PasswordAssitance\ValueObject\PasswordAssistanceHash($pwassist_id)
534 if ($result->isError()) {
535 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
'pwassist_session_expired'));
541 $session = $result->value();
542 if ($session->isExpired($this->clock)) {
543 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
'pwassist_session_expired'));
549 $tpl->
setVariable(
'TXT_PAGEHEADLINE', $this->
lng->txt(
'password_assistance'));
552 $this->ui_renderer->render($this->ui_factory->symbol()->icon()->custom(
554 $this->
lng->txt(
'password_assistance')
559 'TXT_ENTER_USERNAME_AND_NEW_PASSWORD',
560 $this->ui_renderer->render(
561 $this->ui_factory->messageBox()->info($this->
lng->txt(
'pwassist_enter_username_and_new_password'))
565 $tpl->
setVariable(
'FORM', $this->ui_renderer->render($form ?? $this->getAssignPasswordForm($pwassist_id)));
580 private function submitAssignPasswordForm():
void 585 if ($this->
http->request()->getMethod() ===
'POST') {
586 $form = $form->withRequest($this->
http->request());
587 $form_data = $form->getData();
588 $form_valid = $form_data !==
null;
592 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
'form_input_not_valid'));
597 $username = $form_data[self::PROP_USERNAME];
598 $password = $form_data[self::PROP_PASSWORD];
599 $pwassist_id = $form_data[self::PROP_KEY];
601 $result = $this->pwa_repository->getSessionByHash(
602 new \
ILIAS\Init\PasswordAssitance\ValueObject\PasswordAssistanceHash($pwassist_id)
604 if ($result->isError()) {
605 $this->tpl->setOnScreenMessage(
607 str_replace(
"\\n",
'', $this->
lng->txt(
'pwassist_session_expired'))
614 $session = $result->value();
615 if ($session->isExpired($this->clock)) {
616 $this->tpl->setOnScreenMessage(
618 str_replace(
"\\n",
'', $this->
lng->txt(
'pwassist_session_expired'))
624 $is_successful =
true;
631 $is_successful =
false;
636 if ($is_successful && strcasecmp($userObj->
getLogin(), $username) !== 0) {
637 $message = $this->
lng->txt(
'pwassist_login_not_match');
638 $is_successful =
false;
642 if ($is_successful &&
645 $is_successful =
false;
652 if ($is_successful) {
653 $is_successful = $userObj->
resetPassword($password, $password);
654 if (!$is_successful) {
661 if ($is_successful) {
670 if ($is_successful) {
671 $this->pwa_repository->deleteSession($session);
673 $this->ui_renderer->render(
674 $this->ui_factory->messageBox()->info(
675 sprintf($this->
lng->txt(
'pwassist_password_assigned'), $username)
678 self::PERMANENT_LINK_TARGET_PW
681 $this->tpl->setOnScreenMessage(
'failure', str_replace(
"\\n",
'',
$message));
682 $this->showAssignPasswordForm($form, $pwassist_id);
688 $field_factory = $this->ui_factory->input()->field();
690 return $this->ui_factory
695 $this->
ctrl->getFormAction($this,
'submitUsernameAssistanceForm'),
697 $field_factory->section(
699 self::PROP_EMAIL => $field_factory
700 ->text($this->
lng->txt(
'email'))
703 ->withAdditionalTransformation($this->
emailTrafo()),
705 $this->
lng->txt(
'username_assistance'),
710 ->withSubmitLabel($this->
lng->txt(
'submit'))
717 $this->
help->setSubScreenId(
'username_assistance');
720 $tpl->
setVariable(
'TXT_PAGEHEADLINE', $this->
lng->txt(
'password_assistance'));
723 $this->ui_renderer->render($this->ui_factory->symbol()->icon()->custom(
725 $this->
lng->txt(
'password_assistance')
730 'TXT_ENTER_USERNAME_AND_EMAIL',
731 $this->ui_renderer->render(
732 $this->ui_factory->messageBox()->info(
737 $this->
lng->txt(
'pwassist_enter_email'),
747 $tpl->
setVariable(
'FORM', $this->ui_renderer->render($form ?? $this->getUsernameAssistanceForm()));
757 if ($this->
http->request()->getMethod() ===
'POST') {
758 $form = $form->withRequest($this->
http->request());
759 $form_data = $form->getData();
760 $form_valid = $form_data !==
null;
764 $this->tpl->setOnScreenMessage(
'failure', $this->
lng->txt(
'form_input_not_valid'));
769 $email = trim($form_data[self::PROP_EMAIL]);
771 $assistance_callback =
function () use ($email):
void {
774 if (is_array($logins) && count($logins) > 0) {
779 'Could not sent username assistance emails to (reason: no user found): %s',
786 if (($assistance_duration = $this->
settings->get(
'account_assistance_duration')) !==
null) {
787 $duration = $this->
http->durations()->callbackDuration((
int) $assistance_duration);
788 $status =
$duration->stretch($assistance_callback);
790 $status = $assistance_callback();
793 $this->
showMessageForm($this->
lng->txt(
'pwassist_mail_sent_generic'), self::PERMANENT_LINK_TARGET_NAME);
807 'lang' => $this->
lng->getLangKey()
811 $senderFactory = $DIC->mail()->mime()->senderFactory();
812 $sender = $senderFactory->system();
815 $mm->Subject($this->
lng->txt(
'pwassist_mail_subject'),
true);
823 $this->
lng->txt(
'pwassist_username_mail_body'),
824 implode(
",\n", $logins),
828 'mailto:' . $this->
settings->get(
'admin_email'),
839 $tpl->
setVariable(
'TXT_PAGEHEADLINE', $this->
lng->txt(
'password_assistance'));
842 $this->ui_renderer->render($this->ui_factory->symbol()->icon()->custom(
844 $this->
lng->txt(
'password_assistance')
848 $tpl->
setVariable(
'TXT_TEXT', str_replace(
"\\n",
'<br />', $text));
855 $this->tpl->setPermanentLink(
'usr',
null, $context);
buildUrl(string $script, array $query_parameters)
static initStartUpTemplate( $a_tmpl, bool $a_show_back=false, bool $a_show_logout=false)
This method enriches the global template with some user interface elements (language selection...
resetPassword(string $raw, string $raw_retype)
Resets the user password.
static appendUrlParameterString(string $a_url, string $a_par, bool $xml_style=false)
static stripSlashesRecursive($a_data, bool $a_strip_html=true, string $a_allow="")
static getLogger(string $a_component_id)
Get component logger.
Interface Observer Contains several chained tasks and infos about them.
getUnsafeGetCommands()
This method must return a list of unsafe GET commands.
saniziteArrayElementsTrafo()
RefineryFactory $refinery
ILIAS Data Clock ClockInterface $clock
static isPassword(string $a_passwd, ?string &$customError=null)
ILIAS Init PasswordAssitance PasswordAssistanceRepository $pwa_repository
setVariable(string $variable, $value='')
Sets the given variable to the given value.
static printToGlobalTemplate($tpl)
static is_email(string $a_email, ?ilMailRfc822AddressParserFactory $mailAddressParserFactory=null)
This preg-based function checks whether an e-mail address is formally valid.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getAssignPasswordForm(?string $pwassist_id=null)
const PERMANENT_LINK_TARGET_PW
static getUserIdByLogin(string $a_login)
ILIAS UI Factory $ui_factory
static http()
Fetches the global http state from ILIAS.
static getUserLoginsByEmail(string $a_email)
submitUsernameAssistanceForm()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
showMessageForm(string $text, string $permanent_link_context)
static isPasswordValidForUserContext(string $clear_text_password, $user, ?string &$error_language_variable=null)
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
showAssistanceForm(?ILIAS\UI\Component\Input\Container\Form\Form $form=null)
static getPasswordRequirementsInfo()
infotext for ilPasswordInputGUI setInfo()
getUsernameAssistanceForm()
showUsernameAssistanceForm(?ILIAS\UI\Component\Input\Container\Form\Form $form=null)
fillPermanentLink(string $context)
const PERMANENT_LINK_TARGET_NAME
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
ilGlobalTemplateInterface $tpl
sendUsernameAssistanceMail(string $email, array $logins)
static isLocalPasswordEnabledForAuthMode($a_authmode)
Check if local password validation is enabled for a specific auth_mode.
submitAssistanceForm()
If the submitted username and email address matches an entry in the user data table, then ILIAS creates a password assistance session for the user, and sends a password assistance mail to the email address.
getSafePostCommands()
This method must return a list of safe POST commands.
setLastPasswordChangeToNow()
ILIAS UI Renderer $ui_renderer
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...