Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit the number of users on an instance #30236

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions custom/conf/app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,11 @@ LEVEL = Info
;USER_DELETE_WITH_COMMENTS_MAX_TIME = 0
;; Valid site url schemes for user profiles
;VALID_SITE_URL_SCHEMES=http,https
;; The maximum number of users allowed to exist before creation is disabled.
;; This is not a security prevention measure.
;; Note, Admins will also be unable to create new users via the dashboard when the limit has been reached
;; `-1` means no limit
;MAX_USER_CREATE_LIMIT=-1


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
1 change: 1 addition & 0 deletions docs/content/administration/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ And the following unique queues:
The user's email will be replaced with a concatenation of the user name in lower case, "@" and NO_REPLY_ADDRESS.
- `USER_DELETE_WITH_COMMENTS_MAX_TIME`: **0** Minimum amount of time a user must exist before comments are kept when the user is deleted.
- `VALID_SITE_URL_SCHEMES`: **http, https**: Valid site url schemes for user profiles
- `MAX_USER_CREATE_LIMIT`: **-1**: The maximum number of users allowed to exist before creation is disabled. Note: This also prevents admins from creating new users. `-1` means no limit.

### Service - Explore (`service.explore`)

Expand Down
14 changes: 14 additions & 0 deletions models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,11 @@ func createUser(ctx context.Context, u *User, createdByAdmin bool, overwriteDefa
return err
}

// Check the number of users already in the system, and if there are too many forbid any new ones.
if HitCreationLimit(ctx) {
return fmt.Errorf("The system has exceeded the user creation limit")
}

// set system defaults
u.KeepEmailPrivate = setting.Service.DefaultKeepEmailPrivate
u.Visibility = setting.Service.DefaultUserVisibilityMode
Expand Down Expand Up @@ -729,6 +734,15 @@ func createUser(ctx context.Context, u *User, createdByAdmin bool, overwriteDefa
return committer.Commit()
}

func HitCreationLimit(ctx context.Context) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NoMoreUsersAllowed?

// don't bother calling DB if limit not set
if setting.Service.MaxUserCreationLimit < 0 ||
int64(setting.Service.MaxUserCreationLimit) < CountUsers(ctx, &CountUserFilter{}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be a concurrent limit? Deactivated users should not be counted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, doing this means we also need to keep the user count in mind when re-activating a user, and the CountUsers must ignore deactivated users.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this feature should be similar to "you have 10 user licenses". Usually if someone leaves, you just deactivate the account and get 1 license back. This would not be possible with the current code.

return false
}
return true
}

// IsLastAdminUser check whether user is the last admin
func IsLastAdminUser(ctx context.Context, user *User) bool {
if user.IsAdmin && CountUsers(ctx, &CountUserFilter{IsAdmin: optional.Some(true)}) <= 1 {
Expand Down
3 changes: 3 additions & 0 deletions modules/setting/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ var Service = struct {
DefaultOrgMemberVisible bool
UserDeleteWithCommentsMaxTime time.Duration
ValidSiteURLSchemes []string
MaxUserCreationLimit int

// OpenID settings
EnableOpenIDSignIn bool
Expand Down Expand Up @@ -234,6 +235,8 @@ func loadServiceFrom(rootCfg ConfigProvider) {
}
Service.ValidSiteURLSchemes = schemes

Service.MaxUserCreationLimit = sec.Key("MAX_USER_CREATE_LIMIT").MustInt(-1)

mustMapSetting(rootCfg, "service.explore", &Service.Explore)

loadOpenIDSetting(rootCfg)
Expand Down
7 changes: 7 additions & 0 deletions routers/web/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,13 @@ func ActivatePost(ctx *context.Context) {
return
}

// Check the number of users already in the system, and if there are too many forbid any new ones.
if user_model.HitCreationLimit(ctx) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user is created auto-active there is no limit?

ctx.Flash.Error("The system has exceeded the user creation limit", true)
renderActivationChangeEmail(ctx)
return
}

if code == "" {
newEmail := strings.TrimSpace(ctx.FormString("change_email"))
if ctx.Doer != nil && newEmail != "" && !strings.EqualFold(ctx.Doer.Email, newEmail) {
Expand Down