Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#871: Updated OS2Forms Nemid authenticated check #90

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
36 changes: 26 additions & 10 deletions modules/os2forms_nemid/os2forms_nemid.module
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,37 @@ 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 <a href="@logout">log out</a> 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 be considered authenticated as company AND person.
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 && !$authProviderPlugin->isAuthenticatedPerson()) {
_os2forms_nemid_add_authentication_warning_and_disable_form($form, $authProviderService->getLogoutUrl()->toString());
}
}
}
}

/**
* 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 <a href="@logout">log out</a> and sign in with a different account', [
'@logout' => $logoutUrl,
]));
foreach (Element::children($form['actions']) as $key) {
$form['actions'][$key]['#disabled'] = TRUE;
}
}

/**
* Implements hook_preprocess().
*/
Expand Down
Loading