drupal-8drupal-viewsdrupal-permissions

Is it possible in Drupal 8 to allow registered site users to see each other's email addresses?


I'm developing a Drupal 8 website for a small volunteer organization of trusted members. For those who have a certain role on the website, we'd like to use a View to display all of the registered user information (only to other registered users). I have the View working fine, but the email field is only displayed for admin users.

I checked permissions to no avail, and have been reading through various posts for a couple of hours but none seem to answer the question.

Ideally trying to solve this within the CMS, but happy to do PHP if necessary.

Screenshot of current View output as Admin I'm trying to keep the email field when viewing as a registered, non-admin user.


Solution

  • Finally found a viable patch solution for this issue - thanks to those who wrote it. Patch is just a few lines in two PHP files that creates a new permission options for viewing other users' email addresses.

    Patch is below, from this Drupal post.

    From b0c658e8707f1b851caf700eec9ee4001b6dbbb6 Mon Sep 17 00:00:00 2001
    From: Axel Rutz <axel.rutz@machbarmacher.net>
    Date: Sat, 22 Dec 2018 02:40:28 +0100
    Subject: [PATCH] Issue #2799049 by cilefen, axel.rutz: Add new permission to
     view user email field
    
    ---
     core/modules/user/src/UserAccessControlHandler.php | 3 +++
     core/modules/user/user.permissions.yml             | 2 ++
     2 files changed, 5 insertions(+)
    
    diff --git a/core/modules/user/src/UserAccessControlHandler.php b/core/modules/user/src/UserAccessControlHandler.php
    index 9e04c3ffda..486ee744a0 100644
    --- a/core/modules/user/src/UserAccessControlHandler.php
    +++ b/core/modules/user/src/UserAccessControlHandler.php
    @@ -120,6 +120,9 @@ protected function checkFieldAccess($operation, FieldDefinitionInterface $field_
             // Allow view access to own mail address and other personalization
             // settings.
             if ($operation == 'view') {
    +          if ($field_definition->getName() === 'mail' && $account->hasPermission('view user mail field')) {
    +            return AccessResult::allowed()->cachePerPermissions();
    +          }
               return $is_own_account ? AccessResult::allowed()->cachePerUser() : AccessResult::neutral();
             }
             // Anyone that can edit the user can also edit this field.
    diff --git a/core/modules/user/user.permissions.yml b/core/modules/user/user.permissions.yml
    index a295b1f98f..f21f1deea4 100644
    --- a/core/modules/user/user.permissions.yml
    +++ b/core/modules/user/user.permissions.yml
    @@ -11,6 +11,8 @@ administer users:
       restrict access: true
     access user profiles:
       title: 'View user information'
    +view user mail field:
    +  title: 'View user mail field'
     change own username:
       title: 'Change own username'
     select account cancellation method:
    -- 
    2.17.1