Discuss the need to add an access role.

Created on 8 February 2024, 11 months ago
Updated 2 July 2024, 6 months ago

Hi
I see there is access to settings administration.
Is there a need to restrict access for roles who can and cannot use meta tag generation?

Feature request
Status

Needs work

Version

1.0

Component

Code

Created by

🇺🇦Ukraine darkdim Kyiv

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @darkdim
  • Issue was unassigned.
  • First commit to issue fork.
  • Status changed to Needs review 7 months ago
  • 🇮🇳India dev16.addweb

    Looks good @kenyoowen,

    If we need to restrict access by role, we can use the following method. However, using permissions (as shown in #4) is more convenient because it allows us to give access to any user based on their permissions.

    metatag_ai.admin_settings:
      path: '/admin/config/content/metatag-ai'
      defaults:
        _form: '\Drupal\metatag_ai\Form\MetatagAIConfigForm'
        _title: 'Metatag AI Settings'
      requirements:
        _role: 'administrator'
    
    metatag_ai.content_settings:
      path: '/admin/config/content/metatag-ai/content-type'
      defaults:
        _form: '\Drupal\metatag_ai\Form\MetatagAISettingsForm'
        _title: 'Metatag Content Type Settings'
      requirements:
        _role: 'administrator+content_editor'
  • Status changed to Needs work 6 months ago
  • 🇮🇳India ankitv18

    Please check below code: If we check at initial level of having a permission then only it could be useful for any further logic implementation.

    /**
     * Implements hook_form_alter() for node form alter to add the Generator button.
     */
    function metatag_ai_form_alter(&$form, FormStateInterface $form_state, $form_id) {
      if (\Drupal::currentUser()->hasPermission('administer metatag content')) {
         $form_object = $form_state->getFormObject();
        if ($form_object instanceof NodeForm) {
          $content_type = $form_object->getEntity()->getType();
          $selected_content_types = \Drupal::config('metatag_ai.content_settings')->get('metatag_ai.metadata_content_types');
          if (!empty($selected_content_types) && in_array($content_type, $selected_content_types)) {
              $form['actions']['generate_metadata'] = [
                '#type' => 'button',
                '#value' => t('Generate Metatag'),
                '#weight' => 4,
                '#ajax' => [
                  'callback' => 'metatag_ai_generate_submit_form',
                  'event' => 'click',
                ],
              ];
            }
          }
        }
      }
  • 🇵🇭Philippines kenyoOwen

    Hi ankitv18

    Thank you for the review. I tested your code in comment #8 and it works the same as the current change, but I think we stick to the current change since it has better readability and is also more maintainable for me.

    Thanks.

  • 🇮🇳India ankitv18

    @kenyoOwen you have added hasPermission check at low-level of code but if you add that check at starting of the method then it make more more meaningful rather initialising and iterating the lines of code and do nothing at end if user don't have that permission.
    Right now the code block is having Code redundancy and code complexity.

    Rest is the maintainer call on this.

Production build 0.71.5 2024