From 22f29f383b3c57f0e0aefd64f7b43a537cd2bc21 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Fri, 1 Mar 2024 14:46:58 +0100 Subject: [PATCH 1/4] #871: Updated OS2Forms Nemid authenticated check --- modules/os2forms_nemid/os2forms_nemid.module | 29 ++++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/modules/os2forms_nemid/os2forms_nemid.module b/modules/os2forms_nemid/os2forms_nemid.module index cd86dc49..2b56c74c 100644 --- a/modules/os2forms_nemid/os2forms_nemid.module +++ b/modules/os2forms_nemid/os2forms_nemid.module @@ -134,21 +134,32 @@ function os2forms_nemid_webform_submission_form_alter(array &$form, FormStateInt // User is authenticated, check if the form type is corresponding to // authentication type. if ($authProviderPlugin->isAuthenticated()) { - if ($authProviderPlugin->isAuthenticatedPerson() && $webform_type !== NemidElementBase::WEBFORM_TYPE_PERSONAL - || $authProviderPlugin->isAuthenticatedCompany() && $webform_type !== NemidElementBase::WEBFORM_TYPE_COMPANY) { - \Drupal::messenger() - ->addWarning(t('Your login type does match the login type required by the webform. Please log out and sign in with a different account', [ - '@logout' => $authProviderService->getLogoutUrl() - ->toString(), - ])); - foreach (Element::children($form['actions']) as $key) { - $form['actions'][$key]['#disabled'] = TRUE; + // Users often login on behalf of company as themselves, i.e. values may contain both a cpr and cvr value, + // and they will therefore be considered authenticated as company AND person. + if ($webform_type === NemidElementBase::WEBFORM_TYPE_COMPANY) { + if (!$authProviderPlugin->isAuthenticatedCompany()) { + addWarningAndDisableForm($form, $authProviderService->getLogoutUrl()->toString()); + } + } elseif ($webform_type === NemidElementBase::WEBFORM_TYPE_PERSONAL) { + if (!$authProviderPlugin->isAuthenticatedPerson()) { + addWarningAndDisableForm($form, $authProviderService->getLogoutUrl()->toString()); } } } } } +function addWarningAndDisableForm(array $form, string $logoutUrl): void +{ + \Drupal::messenger() + ->addWarning(t('Your login type does match the login type required by the webform. Please log out and sign in with a different account', [ + '@logout' => $logoutUrl, + ])); + foreach (Element::children($form['actions']) as $key) { + $form['actions'][$key]['#disabled'] = TRUE; + } +} + /** * Implements hook_preprocess(). */ From 436d3ab9086659b3c39deda154fc32d98cf75cb1 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Fri, 1 Mar 2024 14:50:21 +0100 Subject: [PATCH 2/4] #871: Updated CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d63471c..cc14fc2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] - CprFetchData adding ajax error fix +- Updated `OS2Forms Nemid` authentication check [#90](https://github.com/OS2Forms/os2forms/pull/90) ## [3.14.0] From c2b4226214d425366df669ca620fc873c7ff8d1b Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Fri, 1 Mar 2024 15:07:30 +0100 Subject: [PATCH 3/4] #871: Applied coding standards --- modules/os2forms_nemid/os2forms_nemid.module | 23 ++++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/modules/os2forms_nemid/os2forms_nemid.module b/modules/os2forms_nemid/os2forms_nemid.module index 2b56c74c..be77654f 100644 --- a/modules/os2forms_nemid/os2forms_nemid.module +++ b/modules/os2forms_nemid/os2forms_nemid.module @@ -134,23 +134,32 @@ function os2forms_nemid_webform_submission_form_alter(array &$form, FormStateInt // User is authenticated, check if the form type is corresponding to // authentication type. if ($authProviderPlugin->isAuthenticated()) { - // Users often login on behalf of company as themselves, i.e. values may contain both a cpr and cvr value, - // and they will therefore be considered authenticated as company AND person. + // Users often login on behalf of company as themselves + // i.e. values may contain both a cpr and cvr value, + // and they will be considered authenticated as company AND person. if ($webform_type === NemidElementBase::WEBFORM_TYPE_COMPANY) { if (!$authProviderPlugin->isAuthenticatedCompany()) { - addWarningAndDisableForm($form, $authProviderService->getLogoutUrl()->toString()); + _os2forms_nemid_add_authentication_warning_and_disable_form($form, $authProviderService->getLogoutUrl()->toString()); } - } elseif ($webform_type === NemidElementBase::WEBFORM_TYPE_PERSONAL) { + } + elseif ($webform_type === NemidElementBase::WEBFORM_TYPE_PERSONAL) { if (!$authProviderPlugin->isAuthenticatedPerson()) { - addWarningAndDisableForm($form, $authProviderService->getLogoutUrl()->toString()); + _os2forms_nemid_add_authentication_warning_and_disable_form($form, $authProviderService->getLogoutUrl()->toString()); } } } } } -function addWarningAndDisableForm(array $form, string $logoutUrl): void -{ +/** + * Adds authentication warning and disables actions on form. + * + * @param array $form + * Form array. + * @param string $logoutUrl + * Logout url. + */ +function _os2forms_nemid_add_authentication_warning_and_disable_form(array $form, string $logoutUrl) { \Drupal::messenger() ->addWarning(t('Your login type does match the login type required by the webform. Please log out and sign in with a different account', [ '@logout' => $logoutUrl, From e92f784cac207efbd3b5eac677b63b54ef88fcb3 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Mon, 4 Mar 2024 10:45:07 +0100 Subject: [PATCH 4/4] #871: Cleanup --- modules/os2forms_nemid/os2forms_nemid.module | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/modules/os2forms_nemid/os2forms_nemid.module b/modules/os2forms_nemid/os2forms_nemid.module index be77654f..5b3bda61 100644 --- a/modules/os2forms_nemid/os2forms_nemid.module +++ b/modules/os2forms_nemid/os2forms_nemid.module @@ -137,15 +137,11 @@ function os2forms_nemid_webform_submission_form_alter(array &$form, FormStateInt // Users often login on behalf of company as themselves // i.e. values may contain both a cpr and cvr value, // and they will be considered authenticated as company AND person. - if ($webform_type === NemidElementBase::WEBFORM_TYPE_COMPANY) { - if (!$authProviderPlugin->isAuthenticatedCompany()) { - _os2forms_nemid_add_authentication_warning_and_disable_form($form, $authProviderService->getLogoutUrl()->toString()); - } + if ($webform_type === NemidElementBase::WEBFORM_TYPE_COMPANY && !$authProviderPlugin->isAuthenticatedCompany()) { + _os2forms_nemid_add_authentication_warning_and_disable_form($form, $authProviderService->getLogoutUrl()->toString()); } - elseif ($webform_type === NemidElementBase::WEBFORM_TYPE_PERSONAL) { - if (!$authProviderPlugin->isAuthenticatedPerson()) { - _os2forms_nemid_add_authentication_warning_and_disable_form($form, $authProviderService->getLogoutUrl()->toString()); - } + elseif ($webform_type === NemidElementBase::WEBFORM_TYPE_PERSONAL && !$authProviderPlugin->isAuthenticatedPerson()) { + _os2forms_nemid_add_authentication_warning_and_disable_form($form, $authProviderService->getLogoutUrl()->toString()); } } } @@ -159,7 +155,7 @@ function os2forms_nemid_webform_submission_form_alter(array &$form, FormStateInt * @param string $logoutUrl * Logout url. */ -function _os2forms_nemid_add_authentication_warning_and_disable_form(array $form, string $logoutUrl) { +function _os2forms_nemid_add_authentication_warning_and_disable_form(array &$form, string $logoutUrl) { \Drupal::messenger() ->addWarning(t('Your login type does match the login type required by the webform. Please log out and sign in with a different account', [ '@logout' => $logoutUrl,