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

#867: Allow denying address protected citizen from webform #92

Merged
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa
- Adding Lat and Long fetching to DataAddress
- [#84](https://github.com/OS2Forms/os2forms/pull/84)
Added digital post test command.
- [#92](https://github.com/OS2Forms/os2forms/pull/92)
Allow denying address protected citizen from webform.
- [#96](https://github.com/OS2Forms/os2forms/pull/96)
NemLogin autologout pop-up styling.
- [#99](https://github.com/OS2Forms/os2forms/pull/99)
Expand Down
1 change: 1 addition & 0 deletions modules/os2forms_nemid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Besides this module adds a special settings to the Third Party Webform settings:

- Webform type
- Redirect to nemlogin automatically
- Hide form if under address protection

Settings: admin/structure/webform/manage/[webform]/settings

76 changes: 76 additions & 0 deletions modules/os2forms_nemid/os2forms_nemid.module
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use Drupal\migrate\Plugin\MigrateSourceInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\os2forms_nemid\Plugin\WebformElement\NemidElementBase;
use Drupal\os2forms_nemid\Service\FormsHelper;
use Drupal\webform\Utility\WebformFormHelper;
use Drupal\webform\WebformSubmissionInterface;

/**
* Implements hook_form_FORM_ID_alter().
Expand All @@ -21,6 +24,15 @@ function os2forms_nemid_form_webform_os2forms_settings_form_alter(&$form, FormSt
os2forms_nemid_webform_third_party_settings_form_alter($form, $form_state);
}

/**
* Implements hook_ENTITY_TYPE_prepare_form().
*
* Prepare webform.
*/
function os2forms_nemid_webform_submission_prepare_form(WebformSubmissionInterface $webform_submission, string $operation, FormStateInterface $form_state): void {
Drupal::service('os2forms_nemid.forms_helper')->webformSubmissionPrepareForm($webform_submission, $operation, $form_state);
}

/**
* Implements hook_webform_third_party_settings_form_alter().
*/
Expand Down Expand Up @@ -71,12 +83,76 @@ function os2forms_nemid_webform_third_party_settings_form_alter(&$form, FormStat
'#default_value' => !(empty($settings)) ? $settings['nemlogin_auto_redirect'] : FALSE,
'#description' => t('Redirection will happen right after user has is accessing the form, if user is already authenticated via NemID, redirection will not happen.'),
];

$nemloginProtectionSettings = $webform->getThirdPartySetting('os2forms', 'os2forms_nemid_address_protection');

// OS2Forms NemID.
$form['third_party_settings']['os2forms']['os2forms_nemid_address_protection'] = [
'#type' => 'details',
'#title' => t('OS2Forms address protection settings'),
'#open' => TRUE,
];

// Nemlogin auto redirect.
$form['third_party_settings']['os2forms']['os2forms_nemid_address_protection']['nemlogin_hide_form'] = [
'#type' => 'select',
'#options' => [
FormsHelper::WEBFORM_NEM_LOGIN_ADDRESS_PROTECTION_DEFAULT_BEHAVIOUR => t('No'),
FormsHelper::WEBFORM_NEM_LOGIN_ADDRESS_PROTECTION_DISPLAY_ERROR => t('Yes'),
],
'#title' => t('Hide form if user is under address protection'),
'#default_value' => !(empty($nemloginProtectionSettings)) ? $nemloginProtectionSettings['nemlogin_hide_form'] : FormsHelper::WEBFORM_NEM_LOGIN_ADDRESS_PROTECTION_DEFAULT_BEHAVIOUR,
'#description' => t('Hides elements and displays error if nemlogin reveals that citizen is under address protection and an address element is found on the webform'),
];

// Nemlogin address protection.
$form['third_party_settings']['os2forms']['os2forms_nemid_address_protection']['nemlogin_hide_message'] = [
'#title' => t('Access denied message'),
'#type' => 'textarea',
'#default_value' => !(empty($nemloginProtectionSettings)) ? $nemloginProtectionSettings['nemlogin_hide_message'] : '',
'#description' => t('Message shown to user when visiting form'),
'#states' => [
'visible' => [
[':input[name="third_party_settings[os2forms][os2forms_nemid_address_protection][nemlogin_hide_form]"]' => ['value' => FormsHelper::WEBFORM_NEM_LOGIN_ADDRESS_PROTECTION_DISPLAY_ERROR]],
],
'required' => [
[':input[name="third_party_settings[os2forms][os2forms_nemid_address_protection][nemlogin_hide_form]"]' => ['value' => FormsHelper::WEBFORM_NEM_LOGIN_ADDRESS_PROTECTION_DISPLAY_ERROR]],
],
],
];
}

/**
* Implements hook_webform_submission_form_alter().
*/
function os2forms_nemid_webform_submission_form_alter(array &$form, FormStateInterface $form_state, $form_id) {

// Handle address protection.
if ($tempValue = $form_state->getTemporaryValue(FormsHelper::ADDRESS_PROTECTION_STATE)) {
if (FALSE === ($tempValue['access'] ?? TRUE)) {
// Flattening the elements makes it much easier to access nested elements.
$elements = &WebformFormHelper::flattenElements($form['elements']);

$message = $tempValue['message'] ?? t('This form cannot be shown because you have address protection');

$form['os2forms_nemlogin_message'] = [
'#theme' => 'status_messages',
'#message_list' => [
'error' => [$message],
],
];

// Hide all actions ….
$form['actions']['#access'] = FALSE;
// … and elements.
foreach ($elements as &$element) {
$element['#access'] = FALSE;
}
}

return;
}

// Getting webform Nemid settings.
/** @var \Drupal\webform\WebformSubmissionInterface Interface $webformSubmission */
$webformSubmission = $form_state->getFormObject()->getEntity();
Expand Down
2 changes: 1 addition & 1 deletion modules/os2forms_nemid/os2forms_nemid.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ services:
- {name: event_subscriber}
os2forms_nemid.forms_helper:
class: Drupal\os2forms_nemid\Service\FormsHelper
arguments: ['@os2web_nemlogin.auth_provider', '@plugin.manager.os2web_datalookup']
arguments: ['@os2web_nemlogin.auth_provider', '@plugin.manager.os2web_datalookup', '@current_route_match']
90 changes: 89 additions & 1 deletion modules/os2forms_nemid/src/Service/FormsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\os2forms_nemid\Element\NemidCompanyCvrFetchData;
use Drupal\os2forms_nemid\Element\NemidCompanyPNumber;
use Drupal\os2forms_nemid\Element\NemidCprFetchData;
use Drupal\os2web_datalookup\LookupResult\CompanyLookupResult;
use Drupal\os2web_datalookup\LookupResult\CprLookupResult;
use Drupal\os2web_datalookup\Plugin\DataLookupManager;
use Drupal\os2web_nemlogin\Service\AuthProviderService;
use Drupal\webform\WebformSubmissionInterface;

/**
* FormsHelper.
Expand All @@ -20,6 +22,32 @@
* @package Drupal\os2forms_nemid\Service
*/
class FormsHelper {
const ADDRESS_PROTECTION_STATE = 'os2forms_nemlogin_address_protection';

/**
* Defines NemID login address protection display error option.
*/
const WEBFORM_NEM_LOGIN_ADDRESS_PROTECTION_DISPLAY_ERROR = 'os2forms_nemlogin_address_protection_display_error';

/**
* Defines NemID login address protection display default behaviour.
*/
const WEBFORM_NEM_LOGIN_ADDRESS_PROTECTION_DEFAULT_BEHAVIOUR = 'os2forms_nemlogin_address_protection_default_behaviour';

/**
* Defines NemID login address related elements.
*/
private const WEBFORM_NEM_LOGIN_ADDRESS_PROTECTION_ELEMENT_TYPES = [
'os2forms_nemid_address',
'os2forms_nemid_street',
'os2forms_nemid_house_nr',
'os2forms_nemid_floor',
'os2forms_nemid_apartment_nr',
'os2forms_nemid_postal_code',
'os2forms_nemid_city',
'os2forms_nemid_kommunekode',
'os2forms_nemid_coaddress',
];

/**
* Auth provider service.
Expand All @@ -35,17 +63,27 @@ class FormsHelper {
*/
private $dataLookManager;

/**
* The route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
private RouteMatchInterface $routeMatch;

/**
* Constructor.
*
* @param \Drupal\os2web_nemlogin\Service\AuthProviderService $authProviderService
* Auth provider service.
* @param \Drupal\os2web_datalookup\Plugin\DataLookupManager $dataLookPluginManager
* Datalookup plugin manager.
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* Route match service.
*/
public function __construct(AuthProviderService $authProviderService, DataLookupManager $dataLookPluginManager) {
public function __construct(AuthProviderService $authProviderService, DataLookupManager $dataLookPluginManager, RouteMatchInterface $routeMatch) {
$this->authProviderService = $authProviderService;
$this->dataLookManager = $dataLookPluginManager;
$this->routeMatch = $routeMatch;
}

/**
Expand Down Expand Up @@ -307,4 +345,54 @@ protected function getDataFetchTriggerValue($dataFetchValueFieldName, FormStateI
return $value;
}

/**
* Implements hook_ENTITY_TYPE_prepare_form().
*/
public function webformSubmissionPrepareForm(WebformSubmissionInterface $webformSubmission, string $operation, FormStateInterface $formState): void {
// Only perform address protection check when displaying submission form.
$accessCheckRouteNames = [
// Webform attached to a node.
'entity.node.canonical',
// Creating a new submission.
'entity.webform.canonical',
// Editing a submission.
'entity.webform_submission.edit_form',
];

if (!in_array($this->routeMatch->getRouteName(), $accessCheckRouteNames, TRUE)) {
return;
}

// Check if hide address protection is selected.
$hideForm = $webformSubmission->getWebform()->getThirdPartySettings('os2forms')['os2forms_nemid_address_protection']['nemlogin_hide_form'] ?? NULL;

if ($hideForm === self::WEBFORM_NEM_LOGIN_ADDRESS_PROTECTION_DISPLAY_ERROR) {
$cprResult = $this->retrieveCprLookupResult($formState);

if ($cprResult && $cprResult->isNameAddressProtected()) {

// Check if any element violating address
// protection is present in webform.
$elements = $webformSubmission->getWebform()->getElementsDecodedAndFlattened();

foreach ($elements as $element) {

if (in_array($element['#type'], self::WEBFORM_NEM_LOGIN_ADDRESS_PROTECTION_ELEMENT_TYPES)) {

// Violation detected,
// mark form state with temporary key and return.
$message = $webformSubmission->getWebform()->getThirdPartySettings('os2forms')['os2forms_nemid_address_protection']['nemlogin_hide_message'];

$formState->setTemporaryValue(self::ADDRESS_PROTECTION_STATE, [
'access' => FALSE,
'message' => $message,
]);

return;
}
}
}
}
}

}
Loading