I'd like to request new core functionality to allow site administrator the ability to grant users of certain roles access to another users email address.
Currently access to a user email addresses is restricted to Admin accounts only. IE if a users email address is output in a view it will not show for users other than Admins.
Example for a use case is an Intranet, where one user may want to contact another user via an email link.
Proposed resolution
Add a new permission with the machine name 'view user email addresses'.
Release note
A new permission 'view user email addresses' has been added which allows for read-only access to user e-mail addresses.
Comments
Comment #2
cilefen commentedComment #3
stephen ollmanThank you!!
That works very nicely. I hope this patch can make it in to the next release as I believe it has value.
Comment #4
stephen ollmanComment #5
tstoecklerI don't think we can get away with re-using an existing permission, wouldn't this mean a possibly unwanted disclosure on existing sites?
Comment #6
dawehnerGood catch @tstoeckler! Just imagine you have patched drupal.org with this patch. Everyone can access profiles, but noone can and should see the actual registered mail address.
Comment #7
stephen ollmanIf another permission item is required then I'm all for that, as long as it accommodates the requirement.
Comment #8
tstoecklerYes, I think an additional permission should be fine in terms of backwards compatibility.
Comment #10
dpiThis could easily be done in contrib with
hook_entity_field_accessand a new custom permission.I dont think this is necessary in core.
Comment #11
stephen ollmanI'm sure it could easily be done by someone with the appropriate level of knowledge, but what about those users of Drupal that don't have this skill or are not in a position to add custom modules.
Having this permission option available makes sense and something that should be configurable through the UI.
Comment #12
dpiEmploy someone who can. It only has to be done once, as a contrib module. Something like Field Permissions, if it does not already handle base fields.
We dont need a permission in core for everything. People already complain of permission [page] bloat.
Comment #13
stephen ollmanYou are missing the point, not everyone is in a position to add a custom module or employ a developer.
Some organisations have a very strict policy around 'just add a custom module', and some out right don't allow the addition of custom modules without first jumping through some major hoops.
What is required here is a small change to core to accommodate a quite important and relatively simple feature.
I used the example of an intranet. Currently one 'authenticated' user cannot see the email address of another without this patch, or a custom module.
Comment #15
steveoliver commentedClosing this since the related issue is dealing with the underlying problem. With the change in that issue, developers can grant field-level access to User fields via hook_entity_field_access().
Comment #16
ex dj commentedWhen upgrading to 8.6, this patch throws a Composer error.
Comment #17
florianmuellerchThe patch for Drupal 8.6 in case anyone needs it :-)
Comment #18
geek-merlinValid feature request. Crosslinking SO: https://drupal.stackexchange.com/questions/205285/how-do-i-allow-regular...
Comment #19
geek-merlinBoth patches do not add a corresponding permission so NW.
Comment #20
geek-merlinAs @dawehner and @tstoeckler concluded, this needs a new permission.
Simple patch flying in that implements this.
Comment #21
geek-merlinComment #22
runofthemill commentedI just tried the patch from #20 and it worked great! Made the changes to the two files, then went to Permissions to select the new correspnding checkboxes that appeared.
My test was on a View that displays all users. Admins could see every user's email address, but other registered users couldn't and I wanted them to. Once I added the new permission for one user type, the user table on the View immediately showed user email addresses, as promised. I then logged out and back in as another user role to whom I'd not given the permission, and that user still could not see the email addresses.
In brief, it worked as expected with a positive and negative test.
Thanks @axel.rutz.
(I'm not sure how to officially document this though.)
Comment #23
stephen ollman@RunOfTheMill
It good to hear that someone else appreciates and understands the relevance of this role based permission.
I'm still hopeful to see this put into core to help promote Drupal in the 'Intranet' space.
@axel.rutz
Many many thanks for the patch, fingers crossed it's picked up and put into core.
Comment #24
dougvann commented#20 Works for me!
You can add my D8.6.7 Intranet project to the list of successful, & very pleased, benefactors of this patch.
Thanks for leading the charge, Stephen! :-)
Comment #25
philsward commented+1 to this request.
#20 seems to work fine. Would love to see this included sooner than later?
Sure would be nice if this were also added as a "field" so it could be included on the user profile page...
Outside of hardcore security testing, I'd say it's good to go. Let's get this small QoL detail added to core :-)
Comment #28
brooke_heaton commentedThis is why the Drupal 8 adoption curve is horrible.
Comment #29
brooke_heaton commented#20 does not apply on 8.7 for me.
Comment #30
a_mitch commentedUpdated the patch to apply to drupal 8.7
Comment #31
a_mitch commentedFixing an indentation error in the last patch.
Comment #32
martijn de witComment #33
dm_mash commented#31 works on 8.7.1.
Comment #34
aaronbaumanAdd test coverage for #31
2799049-34-TEST-ONLY.patchshould fail2799049-34.patchshould passComment #37
aaronbaumanHelps to post the right patch.
In this patch: the failing test from #34 + the fix from #31
Comment #38
jannakha commentedworks on 8.7.5
Comment #39
cilefen commentedA change notice is probably needed for a change such as this..
Comment #40
switzernI've tested the patch in #2799049-37: Add new permission to view user email field and can confirm that it applies cleanly to 8.7.5.
It provides the expected functionality:
- Add the "view user mail field" permission that is disabled for all roles except administrator by default and can be enabled for any other user role.
- Once this permission is enabled for a user role other than administrator, the user mail field is accessible by that role.
- When the permission is removed for a user role, any user with that role is no longer able to access the user mail field.
Comment #41
yepaThe Change record is published ;)
As a bonus this screenshot for documentation:

Comment #42
luenemannRTBC again per #41
Comment #43
aaronbauman- Use createMock() instead of getMock() in UserAccessControlHandlerTest
- Add authViewer property to the test class
My interdiff generator is choking, so apologies for the old fashioned diff.
Back to Needs Review for good measure.
Comment #44
geek-merlinLooked into the interdiff of #43 and i can confirm this is a trivial replacement of a deprecated method by the undeprecated.
Tests pass. So back to RTBC as of #38 and #41.
Comment #45
wim leersLooks good! 👍
But we're forgetting about the API-First impact here.
\Drupal\Tests\jsonapi\Functional\UserTest::testGetMailFieldOnlyVisibleToOwner()specifically is testing that themailfield is only ever visible to the owner. We need to expand that test coverage: we need to check that when this new permission is granted, that non-owners also can see themailfield.(After that's done, we also need to expand the test coverage in
UserTest::testCollectionFilterAccess(). That may be trickier. Let's start with::testGetMailFieldOnlyVisibleToOwner().)Sorry to kick this back to 😔 I promise I'll provide reviews!
Comment #46
anas_maw commentedReroll the patch to work on the latest version
Comment #47
hussainwebBefore I work on the tests as per #45, I thought I could simplify the patch and update the comments for the new permission. Doing that here.
There is also a repeated check in this if condition. The outer switch block already checks this. So, this can be removed. Once we remove this, there is only one conditiion which means we can collapse this 4 lines into a ternary operator.
Also, I am not sure why the reroll in #46 was necessary. The patch in #43 applied cleanly for me on 8.8.x.
Comment #49
hussainwebAh, I missed the falling switch. Disregard the patch in #47. I am just changing the comment now.
Comment #50
hussainwebAdding one of the tests mentioned by @Wim Leers in #45.
Comment #51
binnythomas commentedI can see the permissions coming up correctly in 8.7 core but on of the test hunks failed. So the logic is working.
Comment #52
binnythomas commentedPatch #46 works for 8.7 branch
Comment #54
broonPatch #50 doesn't cleanly apply to 8.7.8 (one chunk fails to be inserted automatically), but - when applied manually - works like a charm.
Also, I want to chip in, that my company is doing a lot of project websites for European Commission or federal ministries. And they always need some internal area where the partners (often distributed throughout all of Europe) are listed with their contact information.
This is the code that doesn't get inserted automatically when trying to apply the patch:
Comment #55
eblue commentedEdit: I rerolled the patch, but this was my mistake. This reroll isn't relevant.
The patch works for me when applied manually as well.
Comment #56
nord102Patch from #46 applied successfully and works as expected
Comment #57
rensingh99 commentedHi,
I have reviewed the patch #55 and it worked as design.
Below are my updates after applying the patch.
1). it had added the one permission "View user mail field".
2). I have made one role "email test" and one view that is showing an email address.
3). I have given permission("View user mail field") to the role "email test".
4). And the user who has the role "email test" was able to see the email of another user.
So, the patch is working great.
Below is the output screenshot of the test result.
Thanks,
Ren
Comment #58
stephen ollmanAny plans for final integration into core?
It's been great to see this process play out over the past 3 years!!
Comment #59
philsward commented#55 is failing on 8.7.11
Comment #60
alexpottThis can be written as
Also for test clarity I suggest change "authViewer" to "emailViewer" as this really means a viewer with permission to view emails.
I do think the issue so far has no really answered the question "Is this really supposed to be in core?" - there are modules like field_permissions that might be installable and allow you to configured this through the UI. And there's the other issue that incorrectly setting this allows for unintended information exposure. It would be great to see some thinking around this part. Perhaps the permission needs to warn about information disclosure more - I don't know.
Comment #61
hardik_patel_12 commentedPoints covered in patch as suggested in #60.
Comment #62
alexpott@Hardik_Patel_12 it's a great idea to run tests that you change before updating a patch. That way you know whether your changes are good if all you are changing is a test.
In order for the test to work the property name needs to be $emailViewer too...
Comment #63
damienmckennaThe use case is "some sites want extra permission over who can see an account's email address outside of that specific user and user admins". Why does this need to be in core rather than an optional download?
The purpose of a modular system is that addons can handle specific use cases not provided by core, not to pile on every feature into core that any site ever might need. I don't understand why there's pushback on this concept.
We already have the Field Permissions module available to theoretically handle this scenario, IMHO this should be "closed (won't fix)".
Comment #64
jungleAgree with @DamienMcKenna or create a tiny contributed module if applicable, such as user_mail_permission.
But from the perspective of promoting Drupal, I'd vote to "LET'S DO IT", a learner or newcomer may not know the existence of the field_permissions module and use it, and field_permissions is a little heavy, if just for this use case.
Comment #65
damienmckennaI reviewed Field Permissions and it doesn't currently support the mail field because, as the maintainer said:
I suggest folks looking for this put together a small contributed module to inject this permission into the user system to control access to that base field.
Comment #66
alexpott@drumm from the security team made an excellent point in favour of this change
Comment #67
hussainwebI am in favour of this change too. While it might be possible to do this in a contrib module, the scope of the permission just feels right to be in the core. I think sites which expose an API with user data commonly need this.
Comment #68
jungleFixed and refactored UserAccessControlHandlerTest.
Omitted
expects($this->any())which is by default, and updated assertions, such asComment #69
jungle$context_tokens is a required argument of CacheContextsManager::assertValidTokens()
Comment #70
geek-merlinI have some more live use cases where this is needed:
We used this in several sites for small communities where every member sees the list of contact data of all other members. Those communities usually have neither funds nor php folks nor core committers but constitute a important part of Drupal's long tail.
Another use case i encountered is getting user data via json:api into a remote system. Without this permission it is impossible for the api user to have full read access on users without getting further permissions. (Note this issue has Api-First Initiative tag.)
Comment #71
dwwI believe the comment wants to say "should be accessible". ;)
Totally unfamiliar with this test, but on reading the patch, these changes seem unrelated and out of scope. What's going on here?
Same concern on nearly all of the changes to all of the tests in this patch. ;)
Do we want 'restrict access: true' for this one? Kinda on the fence. If we use that flag too much, people will tune it out. OTOH, giving this out too broadly would be info disclosure.
As a compromise (and probably worth doing, regardless) can we rename the new permission to something a bit more scary and self-documenting like "View all user mail fields" or something?
NW for at least point 1. I'd love feedback on points 2 and 3 before RTBC.
Thanks,
-Derek
Comment #72
dwwProbably out of scope, but should we rename this test method given the changes to what it asserts?
Comment #73
jungleThank you, @dww for reviewing.
Instead of continuing working on my previous patch, started from #61 again to avoid unnecessary changes/refactoring which might make review harder.
Changes made:
inaccessibletoaccessibleto address #71.1restrict access: truetouser.permissions.ymladdressed #17.3#17.2 and #18, keep them untouched unless have to. it's about code refactoring.
Comment #74
andrey.troeglazov commentedI have checked patch from #73 on my website, and it works for me.
Permission appears, user with this permission can see email.
Comment #75
dwwRe: #73 - thanks, @jungle!
I'm not sure adding 'restrict access: true' is the right move, but I wanted us to consider it. I think this might be more useful (#71.3):
Comment #77
philsward commentedAgree-to-disagree with the "add this to a contrib" school of thought, I don't understand why this isn't in core.
So if we want to use Drupal as a member or directory site, we should install a bunch of contrib modules to make that happen? All I needed was a way to expose email on a directory list made from a view.
I do understand how this can be a bad permission if it isn't well thought out, but so can "Bypass content access control" which I have had to do in the past because it was flat out easier to grant than trying to track down some xyz contrib module that gave more granular control.
"If" Drupal is now supposed to be some fancy Enterprise OpenSource software, wouldn't it make more sense that more and more "companies" would use it for personnel collection and display? I'm just playing devils advocate here, but I keep hearing how Drupal is going after Enterprise, which means email is a pretty big concern for enterprises to expose if they're using Drupal as an internal tool... Right? So wouldn't core want to cater to the main demographic target? Just sayin...
#73 works great on 8.8.5 BTW. Thanks for the work on pushing this forward.
Comment #78
MatsEx commentedPatch #73 works in 8.9.2. Great thanks to @jungle!
Crazy that this isn't a part of core already.
Comment #79
QuietSpecialist commentedThe permission description says, "Warning: Give to trusted roles only; this permission has security implications."
What security implications are there if the mail field value is visible to anonymous users?
I understand there are privacy issues, but if a user chooses to make their email address public and the email address is obfuscated, what else is there to be concerned about?
Comment #80
stephen ollmanJust catching up on this issue.
What was the final outcome of this?
Will it be included in future releases of D9 or is there still some debate about its inclusion?
Comment #81
bohus ulrychYes, I'm waiting for this too. It would be much appreciated.
Comment #82
lendudeInstead of first bunching in mail with some other options and then doing an
if()on the same key that theswitchuses, wouldn't it be clearer to just give mail its own group in the switch?Yeah, adding 'restrict access' seems like a bit much here, I like the suggestion to make it a little more scary, or maybe add a description? "This permission may have privacy implications."
Comment #83
davo20019 commentedSince we don't have this in core yet, I went ahead and created this small contrib module: https://www.drupal.org/project/view_user_email
Comment #84
neograph734I've taken the feedback from #75 and #82 and merged that into a new patch. I too hope that this can become a part of core.
Changes:
Hoping this addresses all issues.
If people agree on the permission name change, the draft change record will need another update.
Comment #86
neograph734That seems unrelated. Switching to needs review.
Comment #88
Ruchi Joshi commentedPatch#84 is working fine for all the issues mentioned under comment#84. +1 for RTBC.
Screenshot is attached
Steps:
1. Visit /admin/people
2. Create users with moderator and authenticated role.
3. Provide "View user email" permission to moderator.
3. As an admin, create a view with permission "View user email"
4. Also enable email fields on view.
5. Email address of all the users available will be visible to moderator but not to authenticated users.
Comment #89
tanubansal commentedTested #84, working fine on 9.1
This can be moved to RTBC
Comment #90
yepaThe change record has been updated.
Comment #91
neograph734Then please allow me to RTBC my own patch on behalf of Ruchi Joshi and tanubansal who both RTBC'd +1.
Comment #92
jungleLooks irrelevant
Comment #93
neograph734That is the summary of the patch? Won't make it into the code base.
Comment #94
anmolgoyal74 commentedRemoved the unused use statement.
Comment #95
neograph734Not sure how that ended up there, but thanks for spotting it. Considering the minor change and the functioning already confirmed by #88 and #89. I am setting this back to RTBC.
Comment #96
catch'access user mail' seems potentially misleading in that it looks like it will allow you to access their e-mail (which is not impossible with messaging or e-mail digestion modules). Not entirely sure what we could use instead - tagging for usability review.
Comment #97
andypostComment #98
catchBumping this back to CNR until we get some more usability feedback.
Comment #99
off commentedHow about "Last access" info?
Comment #100
philsward commentedI would rather see this get added as-is with a new issue created to discuss renaming the "access user email" nomenclature.
Let that issue live in discussion for the next 5 years so we can all at the very least have the functionality of what this patch proposes.
This issue is already over 4 years old. It's done and works. If no-one has any issues with the misleading code in the next month or two, let's put it to rest before the tests require something different that cause it to fail and put it back to square 1. I'm tired of having to remember to re-patch my sites.
Comment #101
neograph734I did the name change in #84 (by now I wish I had not), but I still somewhat think that this name makes sense for the permission, because it is in line with the rest of core.
As for the usability aspect, I've changed the permission label to 'Access user email addresses'. That should remove all possible confusion and be backwards compatible with everybody using this patch.
Could we please get it in now?
Comment #102
geek-merlin@Neograph734 You may increase chances if you provide an interdiff.
Comment #103
neograph734@geek-merlin, thanks for the feedback. I had thought about it, but an interdiff for one word felt quite useless... So I decided to write in the message content what I changed. (But I can imagine that is easily overlooked.)
To enhance usability, the permission label has been changed to highlight it is about email addresses only.
Patch from #94:
Patch from #101:
As for the system name of the permission; that follows the pattern 'access [entity-type] [field]' used throughout core, for which I still believe it makes sense.
Comment #104
abhijith s commentedApplied patch #101 on 9.2.x.The patch is working fine.
After patch:
New permission shown

Permission checked view:

Permission unchecked view:

RTBC +1
Comment #105
benjifisherWe discussed this issue at #3195727: Drupal Usability Meeting 2021-02-05 and again at #3197014: Drupal Usability Meeting 2021-02-12.
We agreed that the description has to say more. The current text ("all other user accounts") suggests that this permission gives access to some sort of directory where all users are listed with their e-mail addresses. That is not what it does!
We need to make clear that this permission only applies to pages where the e-mail address is (optionally) rendered. Also, it is not limited to "pages": if the JSON:API module is enabled, then the permission affects the fields exposed to the API.
Something like this:
Maybe it would be clearer to describe what happens for users who do not have this permission: something like
Is that an accurate description?
Comment #106
anmolgoyal74 commented@benjifisher Thank you for the review.
I believe the second one looks more appropriate.
Comment #107
neograph734That looks good and accurate for what the permission does. However (English not being my primary language), I'd say 'JSON:API' does not fit nicely in a 'listing of places'.
IMO it would be better to use 'API response' (making it more or less a place again). So then it becomes:
Perhaps a native speaker can help out here?
Would it require a new patch or can this be changed during commit? It is only the description of a permission.
Comment #108
neograph734Implemented usability feedback from #105 and my addition from #107.
As for the change record, it now states the permission is
view user email, but the system name isaccess user mail. I think that could be enhanced. Also the screenshot could do with an update with the new long description?Comment #109
neograph734CR updated.
Comment #110
neograph734Status was changed in #98 because of usability. This has been addressed in the recent patch (only a change in a label and nothing functional).
Back to RTBC.
Comment #112
catchLooked through this again.
I think there is still a small potential for confusion where a user doesn't have this permission, but is able to see user's e-mail address because they have 'edit all users' permission. However it's not the only case where we have permission crossover like this, and requiring both to edit e-mail addresses would be a signficant change with bc implications.
I do agree that this is somewhat of a borderline feature for core given that it's doable from contrib, however there are enough separate use-cases that it seems reasonable to add, as well as support for the security team since it can discourage over-granting permissions.
So.. Committed 84c8b1e and pushed to 9.2.x. Thanks!
Comment #114
catchSorry I thought about this overnight and I think the new permission name is still confusing: 'access user mail' still feels like reading e-mails, not viewing e-mail addresses.
'access user email addresses' might be clearer, even though it's a bit longer?
Reverted for now - an issue summary update would still be useful here too.
Comment #115
james.williamsAgreed, it totally sounds like that! My preference would be to use ‘view user email addresses’ rather than use the word ‘access’ but that would be a totally unnecessary bikeshed diversion :-)
I imagine concise strings are preferred, but I don’t think there will be one any more concise than 4 words.
Comment #116
dwwPer #71.3, I agree the name as originally committed wasn't great. At that point, I suggested "View all user mail fields" but that's not good.
+1 to "access user email addresses", that seems very clear and self-documenting.
Comment #117
catchActually 'view user email addresses' is a lot better, since that's the access $operation, whereas access is a concept that could also mean edit. Updating the issue summary with that.
Comment #118
neograph734When I wrote #84 I did a check and throughout core there are so many 'access *' permissions that for as far I could see only granted view access.
It felt like the best match and a name that is consistent with the rest.
'view user email addresses' is a good name too. Better even, and if the consistency of permission names is less importance than a potential misleading name this feels like a good move.
Comment #119
dwwRe-roll for 'view user email addresses'. Further proof this is the right solution (from the interdiff):
Now the machine name matches the label. Good sign! ;)
Also, fixed the release note snippet to match, so I don't think we need any more summary edits.
Thanks!
-Derek
Comment #121
dww#RandomFail: #3191559: [random test failure] Random test fail in EntityReferenceWidgetTest
Re-queued and back to NR.
Comment #122
longwave+1 to 'view user email addresses', it describes precisely what the permission allows, I don't think we can make it any shorter without losing some meaning or being confusing.
Comment #124
catchThanks for the quick turnaround, glad I thought about this more after committing it now...
Committed 6e137b3 and pushed to 9.2.x. Thanks!
Comment #126
effulgentsia commentedTagging for release note mention. The snippet is already in the issue summary, thanks for that!
Comment #127
effulgentsia commented@catch clarified to me that this should be a highlight, not a release note, because there's no risk of it breaking anything on existing sites.
Comment #128
dydave commentedApplied the patch from #119 to
drupal/core:8.9.14withcomposerwithout any issue.After adding the new permission
view user email addressesto the role's configuration, the email address would display properly.Thanks again for contributing this feature.
Comment #129
hassebasse commentedI've tried the patch from #119 on D9.1.8 and it does not work. I check the box, rebuild pemissions and clear all cache, but to no use. It does not work.
The only way for me to have it working is to check Administer users
Comment #130
piridium commented@hassebasse You could try to upgrade to 9.2. The new permission is in core now: https://www.drupal.org/node/3070293
Comment #131
hassebasse commentedThank you @piridium , I will look into this later.
Comment #132
stephen ollman7 years in the making. Thank you to everyone that contributed to this functionality.
It's great to see it finally make its way into core after suggesting it back in 2016.