Map Email Domains to Roles #1543

Closed
opened 2026-02-05 01:11:41 +03:00 by OVERLORD · 2 comments
Owner

Originally created by @homotechsual on GitHub (Feb 18, 2020).

Describe the feature you'd like
It would be nice to map email addresses to roles - if a user registers/signs in with an email address at the domain example.org add them to the ExampleOrg role. We're using the AzureAD auth provider without a Tenant ID (multi-tenant) so our customers are logging in with their own Office 365/Azure AD credentials.

Describe the benefits this feature would bring to BookStack users
In tandem with the email verification workflow this would allow BookStack to store customer specific documentation items with some access control (grant the role access to those chapters/books) and have the role auto assigned.

Additional context
This looks to be similar to the LDAP_USERS_TO_GROUPS setup and could probably reuse some of that logic - assuming the feature is acceptable we wouldn't be opposed to trying for a PR.

Originally created by @homotechsual on GitHub (Feb 18, 2020). **Describe the feature you'd like** It would be nice to map email addresses to roles - if a user registers/signs in with an email address at the domain `example.org` add them to the `ExampleOrg` role. We're using the AzureAD auth provider without a Tenant ID (multi-tenant) so our customers are logging in with their own Office 365/Azure AD credentials. **Describe the benefits this feature would bring to BookStack users** In tandem with the email verification workflow this would allow BookStack to store customer specific documentation items with some access control (grant the role access to those chapters/books) and have the role auto assigned. **Additional context** This looks to be similar to the `LDAP_USERS_TO_GROUPS` setup and could probably reuse some of that logic - assuming the feature is acceptable we wouldn't be opposed to trying for a PR.
Author
Owner

@ssddanbrown commented on GitHub (May 26, 2021):

Hi @MikeyMJCO,
Sorry for the late response.
With the addition of the logical theme system (Included as of v21.04) you could now somewhat easily implement such logic without too much hassle and without altering core files.

The "Getting started" will show how to get this setup. A functions.php file along the lines of the following would achieve this:

<?php

use BookStack\Auth\User;
use BookStack\Theming\ThemeEvents;
use BookStack\Facades\Theme;

$domainRoleMapping = [
    'admin.com' => [1],
    'gmail.com' => [1, 2, 3],
    'danb.me' => [3],
    'default' => [3],
];

Theme::listen(ThemeEvents::AUTH_LOGIN, function ($authSystem, User $user) use ($domainRoleMapping) {
    $splitEmail = explode('@', $user->email);
    $roleIdsToMap = $domainRoleMapping[end($splitEmail)] ?? $domainRoleMapping['default'];
    $user->roles()->sync($roleIdsToMap);
});

Let me know if that works for you.

@ssddanbrown commented on GitHub (May 26, 2021): Hi @MikeyMJCO, Sorry for the late response. With the addition of [the logical theme system](https://github.com/BookStackApp/BookStack/blob/master/dev/docs/logical-theme-system.md) (Included as of v21.04) you could now somewhat easily implement such logic without too much hassle and without altering core files. The "[Getting started](https://github.com/BookStackApp/BookStack/blob/master/dev/docs/logical-theme-system.md#getting-started)" will show how to get this setup. A `functions.php` file along the lines of the following would achieve this: ```php <?php use BookStack\Auth\User; use BookStack\Theming\ThemeEvents; use BookStack\Facades\Theme; $domainRoleMapping = [ 'admin.com' => [1], 'gmail.com' => [1, 2, 3], 'danb.me' => [3], 'default' => [3], ]; Theme::listen(ThemeEvents::AUTH_LOGIN, function ($authSystem, User $user) use ($domainRoleMapping) { $splitEmail = explode('@', $user->email); $roleIdsToMap = $domainRoleMapping[end($splitEmail)] ?? $domainRoleMapping['default']; $user->roles()->sync($roleIdsToMap); }); ``` Let me know if that works for you.
Author
Owner

@ssddanbrown commented on GitHub (Sep 19, 2021):

Since there's been no follow-up I'm going to close this.

@ssddanbrown commented on GitHub (Sep 19, 2021): Since there's been no follow-up I'm going to close this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/BookStack#1543