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); + } + +}