How to set a `User cap` for a particular domain in Gitlab

Viewed 73

Original question:

I want to limit the number of users from a particular domain that can register into my Gitlab instance. I noticed that I could set a "user cap", but it wasn't specific to a domain.

For example:

I want to limit the number of users registered from these domains. 20 users from testdomain1.com and 30 users from testdomain2.com are allowed to sign up. So, if there are already 20 users registered sucessfully from testdomain1.com, new user from testdomain1.com will not be allowed to sign up.

What should I do for it?

2021.11.18 Edited:

I added a validate to the User model:

# gitlab/app/models/user.rb

class User < ApplicationRecord
  # ...
  validate :email_domain, :ensure_user_email_count

  # ...
  def email_domain
    email_domain = /\@.*?$/.match(email)[0]
    email_domain
  end

  def ensure_user_email_count
    # select count(*) from users where email like '%@test.com';
    if User.where("email LIKE ?", "%#{email_domain}" ).count >= 30
      errors.add(email_domain, _('already has 30 registered email.'))
    end
  end

end

This validate can set "user cap = 30" for each domain but it's still not able to set a "User cap" for a particular domain.

Since the related issue post did not get any response yet. I'm tring to implement it by myself. And it seems like that I need to extend the UI of the Admin Settings page and add some related tables to database to set different "user cap" for different email domain.

1 Answers

The GitLab user cap seems to be per GitLab instance.

So if both your domains are reference the same GitLab instance, you would have only one user cap possible.

But if each of your domain redirects to one autonomous GitLab instance (per domain), then you should be able to set user cap per domain.

The OP Ann Lin has created the issue 345557 to follow that feature request.

TRhe OP reports:

A particular table is needed to store the caps.
But I don’t have enough time now to modify the UI so I found a simple way to do this:

The Allowed domains for sign-ups which called domain_allowlist in database is a text:

gitlabhq_production=# \d application_settings
...
domain_allowlist         | text         |           |          | 
...
gitlabhq_production=# select domain_allowlist from >application_settings;
domain_allowlist  
-------------------
---              +
- testdomain1.com+
- testdomain2.com+

(1 row)

I can modify the testdomain1.com to testdomain1.com#30 to store the user cap and use Regex to get the number 30.

I will modify the UI and add the database table later. And I’ll create a pull request on Gitlab when I’m done.

Related