Add option to trigger email when the user modifying the content ONLY has the role "authenticated"

Created on 9 January 2025, about 2 months ago

Problem/Motivation

I work on a website where users with the "Authenticated user" role have been granted permission to create nodes. Administrators expect notifications to be triggered when node creations or updates are performed by users who have only the "Authenticated user" role.

However, the "Authenticated user" role in Drupal is unique because it is automatically assigned to any user who can log in. This includes users with additional roles such as "Administrator" or "Editor". For example, when calling the getRoles() function on an "Editor" user, the returned array will include both roles:

array (
  0 => 'authenticated',
  1 => 'editor',
)

As a result, even if we configure the module to trigger emails based on the "Authenticated user" role, the email will also be triggered for content created or updated by users with additional roles, such as "Administrator" or "Editor".

Proposed resolution

  1. Add a checkbox labeled something like "Trigger email if users have the 'Authenticated user' as their only role".
  2. Ensure this checkbox is visible only when the "Authenticated user" role is selected in the form.
  3. Update the logic in admin_content_notification.common::isCurrentUserRoleAllowedToSendNotification to check the user roles accordingly.

Remaining tasks

  1. Collect opinions on the usefulness of this new feature.
  2. Determine if similar use cases have been implemented or needed in other projects.
  3. Discuss the suggested solution.
  4. Create MR

User interface changes

  1. Add a checkbox labeled something like "Trigger email if users have the 'Authenticated user' as their only role".
  2. Ensure this checkbox is visible only when the "Authenticated user" role is selected in the form.
Feature request
Status

Active

Version

4.1

Component

Code

Created by

🇨🇴Colombia camilo.escobar

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

Merge Requests

Comments & Activities

  • Issue created by @camilo.escobar
  • Hii,

    I’ve successfully implemented the changes requested.

    Now, the system will only trigger email notifications for users who have only the 'Authenticated user' role. This ensures that users with additional roles (like "Administrator" or "Editor") will not trigger notifications, as per your requirements.

    To make this configurable, I’ve added a new checkbox in the settings: "Trigger email if users have the 'Authenticated user' as their only role". This option will only appear when the "Authenticated user" role is selected, allowing administrators to control the behavior more precisely. Also, updated the logic in admin_content_notification.common::isCurrentUserRoleAllowedToSendNotification to check the user roles accordingly.

    Please review the changes and let me know if everything works as expected or if any further adjustments are needed.
    I am attaching a screenshot for the reference.
    Thank you !

  • 🇨🇴Colombia camilo.escobar

    Hi anish.ir,

    Thank you so much for your prompt response and diligent work!

    I’ve reviewed the MR and left a couple of comments for your consideration. Please take a look and let me know your thoughts!

  • Hi camilo.escobar,

    Thank you for your detailed review and feedback on the MR. I appreciate the time you took to analyse the changes and provide valuable suggestions.

    Regarding the Role Selection and Checkbox:
    I’ve implemented the suggested changes to ensure that the "Trigger email if users have the 'Authenticated user' as their only role" checkbox (admin_content_notification_authenticated_user_only) is not effective when the "Authenticated user" role is deselected in the configuration form. This ensures that both configurations work in combination as intended and avoids the problematic scenario you described.

    And for the Log Entry for Non-Configured Roles:
    I’ve considered your comment about logging entries for users with roles that aren’t configured to trigger emails. Since this might not add significant value for admins or developers, I’ve removed the log entries for such cases.

    Please let me know if there are any further adjustments needed. I’m happy to iterate further.

    Thank you again for your guidance!

  • 🇨🇴Colombia camilo.escobar

    Hi anish.ir,

    Thanks for applying the changes! Great job!

  • logic is implemented to ensure that email notifications are only triggered when both the 'Authenticated user' role is selected in the allowed roles configuration and the 'Trigger email if users have the 'Authenticated user' as their only role' checkbox is checked. checks are present to prevent notifications from being sent if the 'Authenticated user' role is unchecked, even if the checkbox was previously selected. Tested various scenarios to confirm expected behaviour.
    So moving it to RTBC.

Production build 0.71.5 2024