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

Reliable selection of admin user #22509

Merged
merged 2 commits into from
Jan 18, 2023
Merged
Changes from 1 commit
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: 4 additions & 1 deletion models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,10 @@ func GetUserByOpenID(uri string) (*User, error) {
// GetAdminUser returns the first administrator
func GetAdminUser() (*User, error) {
var admin User
has, err := db.GetEngine(db.DefaultContext).Where("is_admin=?", true).Get(&admin)
has, err := db.GetEngine(db.DefaultContext).
Where("is_admin=?", true).
OrderBy("id asc"). // Reliably get the admin with the lowest ID.
drsybren marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Is there a possibility that the user with ID = 1 is not an admin user?
Because if that is not the case, we could shortcut to Where("id=?", 1).


EDIT:
Thinking of it, that certainly fails when the initial admin deleted his account.

Copy link
Member

Choose a reason for hiding this comment

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

And also if it's skipped on the install page, it could certainly be that no one makes an admin account until later.

drsybren marked this conversation as resolved.
Show resolved Hide resolved
Get(&admin)
if err != nil {
return nil, err
} else if !has {
Expand Down