From f86dfdf2d9f3d96e88ff334a56e2115a21a26e7c Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Thu, 21 Sep 2023 16:15:53 +0300 Subject: [PATCH 1/3] Adding custom permissions_by_term field --- CHANGELOG.md | 1 + ...s2forms_permissions_by_term.links.menu.yml | 5 ++ .../os2forms_permissions_by_term.module | 35 +++++++++ .../os2forms_permissions_by_term.routing.yml | 7 ++ .../src/Form/SettingsForm.php | 77 +++++++++++++++++++ 5 files changed, 125 insertions(+) create mode 100644 modules/os2forms_permissions_by_term/os2forms_permissions_by_term.links.menu.yml create mode 100644 modules/os2forms_permissions_by_term/os2forms_permissions_by_term.routing.yml create mode 100644 modules/os2forms_permissions_by_term/src/Form/SettingsForm.php diff --git a/CHANGELOG.md b/CHANGELOG.md index ca989385..670ce429 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] - [OS-58] New company address fields +- Custom permissions by term field ## [3.10.0] 2023-08-23 diff --git a/modules/os2forms_permissions_by_term/os2forms_permissions_by_term.links.menu.yml b/modules/os2forms_permissions_by_term/os2forms_permissions_by_term.links.menu.yml new file mode 100644 index 00000000..07842015 --- /dev/null +++ b/modules/os2forms_permissions_by_term/os2forms_permissions_by_term.links.menu.yml @@ -0,0 +1,5 @@ +os2web_permissions_by_term.settings: + title: OS2Forms permissions by term + description: Settings for OS2Forms permissions by term. + route_name: os2web_permissions_by_term.settings_form + parent: system.admin_config_system diff --git a/modules/os2forms_permissions_by_term/os2forms_permissions_by_term.module b/modules/os2forms_permissions_by_term/os2forms_permissions_by_term.module index 79185ac9..12ea7298 100644 --- a/modules/os2forms_permissions_by_term/os2forms_permissions_by_term.module +++ b/modules/os2forms_permissions_by_term/os2forms_permissions_by_term.module @@ -9,6 +9,7 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; use Drupal\node\NodeInterface; +use Drupal\os2forms_permissions_by_term\Form\SettingsForm; use Drupal\views\Plugin\views\query\QueryPluginBase; use Drupal\views\ViewExecutable; use Drupal\webform\WebformInterface; @@ -138,3 +139,37 @@ function os2forms_permissions_by_term_options_list_alter(array &$options, array function os2forms_permissions_by_term_views_query_alter(ViewExecutable $view, QueryPluginBase $query) { \Drupal::service('os2forms_permissions_by_term.maestro_template_helper')->viewsQueryAlter($view, $query); } + +/** + * Implements hook_user_update(). + */ +function os2forms_permissions_by_term_user_update($account) { + $config = \Drupal::config(SettingsForm::$configName); + + if ($customField = $config->get('os2web_permissions_by_term_custom_field')) { + if ($account->get($customField)) { + $accessTerms = $account->get($customField)->referencedEntities(); + + /** @var \Drupal\permissions_by_term\Service\AccessStorage $access_storage */ + $access_storage = \Drupal::service('permissions_by_term.access_storage'); + + // First, we delete existing values from the db. + $access_storage->deleteAllTermPermissionsByUserId($account->id()); + + if (!empty($accessTerms)) { + // For term permissions use user preferred language. + $langcode = $account->getPreferredLangcode(); + + // Second, we insert updated values. + foreach ($accessTerms as $accessTerm) { + $access_storage->addTermPermissionsByUserIds([$account->id()], $accessTerm->id(), $langcode); + } + } + + // Rebuild permissions for nodes if needed. + if (!\Drupal::config('permissions_by_term.settings')->get('disable_node_access_records')) { + node_access_rebuild(TRUE); + } + } + } +} diff --git a/modules/os2forms_permissions_by_term/os2forms_permissions_by_term.routing.yml b/modules/os2forms_permissions_by_term/os2forms_permissions_by_term.routing.yml new file mode 100644 index 00000000..497cbfe5 --- /dev/null +++ b/modules/os2forms_permissions_by_term/os2forms_permissions_by_term.routing.yml @@ -0,0 +1,7 @@ +os2web_permissions_by_term.settings_form: + path: '/admin/config/system/os2forms_permissions_by_term' + defaults: + _title: 'OS2Forms Permissions by terms' + _form: 'Drupal\os2forms_permissions_by_term\Form\SettingsForm' + requirements: + _permission: 'administer site configuration' diff --git a/modules/os2forms_permissions_by_term/src/Form/SettingsForm.php b/modules/os2forms_permissions_by_term/src/Form/SettingsForm.php new file mode 100644 index 00000000..9ba8c596 --- /dev/null +++ b/modules/os2forms_permissions_by_term/src/Form/SettingsForm.php @@ -0,0 +1,77 @@ + t('None')]; + + $userFields = \Drupal::service('entity_field.manager')->getFieldDefinitions('user', 'user'); + + /** @var \Drupal\field\Entity\FieldConfig $field */ + foreach ($userFields as $field_key => $field) { + // If fieldType is entity_reference, we only support taxonomy terms. + if ($field->getType() == 'entity_reference' && $field->getSetting('target_type') == 'taxonomy_term') { + $options[$field_key] = $field_key; + } + } + + $form['os2web_permissions_by_term_custom_field'] = [ + '#type' => 'select', + '#options' => $options, + '#title' => $this->t('Permissions by term custom field'), + '#description' => $this->t('The value of this custom field is mapped to Permission by term real field on hook_user_update().'), + '#default_value' => $this->config(SettingsForm::$configName) + ->get('os2web_permissions_by_term_custom_field'), + ]; + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $values = $form_state->getValues(); + + $config = $this->config(SettingsForm::$configName); + foreach ($values as $key => $value) { + $config->set($key, $value); + } + $config->save(); + + parent::submitForm($form, $form_state); + } + +} From 291518d557bd752aa02c172245ec7eb7985ee891 Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Thu, 21 Sep 2023 16:24:01 +0300 Subject: [PATCH 2/3] Fixing phpcs --- .../src/Form/SettingsForm.php | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/modules/os2forms_permissions_by_term/src/Form/SettingsForm.php b/modules/os2forms_permissions_by_term/src/Form/SettingsForm.php index 9ba8c596..ddf6b6ee 100644 --- a/modules/os2forms_permissions_by_term/src/Form/SettingsForm.php +++ b/modules/os2forms_permissions_by_term/src/Form/SettingsForm.php @@ -2,11 +2,16 @@ namespace Drupal\os2forms_permissions_by_term\Form; +use Drupal\Core\Entity\EntityFieldManagerInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Session\AccountInterface; +use Drupal\webform\WebformTokenManagerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Configure os2web_permissions_by_term settings for this site. + * Configure os2forms_permissions_by_term settings for this site. */ class SettingsForm extends ConfigFormBase { @@ -17,6 +22,32 @@ class SettingsForm extends ConfigFormBase { */ public static $configName = 'os2web_permissions_by_term.settings'; + /** + * Entity field manager. + * + * @var \Drupal\Core\Entity\EntityFieldManagerInterface + */ + private $entityFieldTypeManager; + + /** + * Constructs an SettingsForm object. + * + * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager + * Entity field manager. + */ + public function __construct(EntityFieldManagerInterface $entity_field_manager) { + $this->entityFieldTypeManager = $entity_field_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity_field.manager'), + ); + } + /** * {@inheritdoc} */ @@ -35,9 +66,9 @@ protected function getEditableConfigNames() { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { - $options = [0 => t('None')]; + $options = [0 => $this->t('None')]; - $userFields = \Drupal::service('entity_field.manager')->getFieldDefinitions('user', 'user'); + $userFields = $this->entityFieldTypeManager->getFieldDefinitions('user', 'user'); /** @var \Drupal\field\Entity\FieldConfig $field */ foreach ($userFields as $field_key => $field) { From 008e958396e88752638426cae6ad840222ab8cfc Mon Sep 17 00:00:00 2001 From: Stanislav Kutasevits Date: Thu, 21 Sep 2023 16:26:32 +0300 Subject: [PATCH 3/3] Removing unused use statement --- modules/os2forms_permissions_by_term/src/Form/SettingsForm.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/modules/os2forms_permissions_by_term/src/Form/SettingsForm.php b/modules/os2forms_permissions_by_term/src/Form/SettingsForm.php index ddf6b6ee..ef748f84 100644 --- a/modules/os2forms_permissions_by_term/src/Form/SettingsForm.php +++ b/modules/os2forms_permissions_by_term/src/Form/SettingsForm.php @@ -3,11 +3,8 @@ namespace Drupal\os2forms_permissions_by_term\Form; use Drupal\Core\Entity\EntityFieldManagerInterface; -use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Session\AccountInterface; -use Drupal\webform\WebformTokenManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /**