# Allow automatic group membership based on subdomain

**URL:** https://meta.discourse.org/t/allow-automatic-group-membership-based-on-subdomain/213615
**Category:** Feature
**Created:** [January 1, 2022, 11:33pm UTC](https://meta.discourse.org/t/allow-automatic-group-membership-based-on-subdomain/213615 "2022-01-01T23:33:33Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Jonathan5](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jonathan5/32/197134_2.png) [@Jonathan5](https://meta.discourse.org/u/Jonathan5)
#### Post date: [January 1, 2022, 11:33pm UTC](https://meta.discourse.org/t/allow-automatic-group-membership-based-on-subdomain/213615/1 "2022-01-01T23:33:33Z")

</div>

Continuing the discussion from [Automatically add users to group based on part of email domain](https://meta.discourse.org/t/automatically-add-users-to-group-based-on-part-of-email-domain/211166/3):

# Proposal

At the minute the group membership setting “Users who register with an email domain that exactly matches one in this list will be automatically added to this group” – where the admin wants to match `example.com` – only matches second-level domains like `email@example.com`.

I propose that this be changed also to match subdomains, e.g. `email@sub.example.com`.

# Reason/benefits

1. It would not be feasible for me manually to type in the subdomains for two government-related domains in particular: each has hundreds of subdomains, which are constantly changing, and the full list is not publicly available.

2. It would provide consistency with `email_domains_whitelist`, as noted by @sam in [Automatic registration domain wild card - #3 by sam](https://meta.discourse.org/t/automatic-registration-domain-wild-card/146933/3)

# Proposed solutions

This is possible by a minor amendment to `pattern = "@(#{domains.gsub('.', '\.')})$"` at [discourse/discourse/blob/20de49c8722f8e50e93732702a8d06570376edcd/app/models/group.rb#L1017](https://github.com/discourse/discourse/blob/20de49c8722f8e50e93732702a8d06570376edcd/app/models/group.rb#L1017)

1. Changing this to `pattern = "@.*(#{domains.gsub('.', '\.')})$"` worked for me when I tested it on my forum.

2. I imagine that `pattern = "@(.+\\.)?(#{domains.gsub('.', '\.')})$"` (based on `email_domains_whitelist`) would also work but I only noticed that later and haven’t tested it. I don’t understand why it’s not `(.+\.)?` but maybe the `\\` is a Ruby thing.

An alternative (if you were worried about users who currently _intend_ to exclude subdomains from automatic membership) would be to allow wildcards, e.g. to type `*.domain.com` for automatic group membership.

# Thanks

Thanks for considering this request.

---

<div class="post-metadata">

### Author: ![Jonathan5](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jonathan5/32/197134_2.png) [@Jonathan5](https://meta.discourse.org/u/Jonathan5)
#### Post date: [August 3, 2022, 9:48am UTC](https://meta.discourse.org/t/allow-automatic-group-membership-based-on-subdomain/213615/2 "2022-08-03T09:48:24Z")

</div>

Just for ease of reference, and perhaps to make this feature request seem more popular than its mere four likes, here are two other times that this feature has been requested. The first was four years ago, and the second just last month.

> [@Adding new user to group automatically with wildcard subdomain mail address](https://meta.discourse.org/t/adding-new-user-to-group-automatically-with-wildcard-subdomain-mail-address/95304):
>
> Hi, I want to assing new users to group automatically according to their email address. I am already use that feature. It works for [username@domain.edu](mailto:username@domain.edu). But it not works for [username@abc.domain.edu](mailto:username@abc.domain.edu) We have not spesific subdomain, so i want to use all email subdomains with that feature. How i can setup for all subdomains?

> [@Bulk adding auto approve email domains](https://meta.discourse.org/t/bulk-adding-auto-approve-email-domains/233932/6):
>
> That is exactly the behavior I was seeking.
