-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -729,6 +734,15 @@ func createUser(ctx context.Context, u *User, createdByAdmin bool, overwriteDefa | |
return committer.Commit() | ||
} | ||
|
||
func HitCreationLimit(ctx context.Context) bool { | ||
// don't bother calling DB if limit not set | ||
if setting.Service.MaxUserCreationLimit < 0 || | ||
int64(setting.Service.MaxUserCreationLimit) < CountUsers(ctx, &CountUserFilter{}) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NoMoreUsersAllowed
?