feat(user): assign superuser role to first registered user

Add logic to check if registering user is the first one and assign 'superuser' role instead of default 'visitor' role. This ensures proper initial admin setup for new deployments.
This commit is contained in:
2026-04-04 11:53:51 +08:00
parent 671106d3bc
commit f006c2307a

View File

@@ -89,7 +89,21 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) {
return
}
userID, err := AddUser(req.Username, req.Passwd, "visitor")
// Check weather the user is the first user, if it is, give the superuser level
userList, err := DBListUsers()
if err != nil {
SendErrorResponse(w, http.StatusInternalServerError, err.Error())
postLog.Error(fmt.Sprintf("[RegisterHandler] Failed to list users: %v", err))
return
}
newUserType := ""
if len(userList) == 0 {
newUserType = "superuser"
} else {
newUserType = "visitor"
}
userID, err := AddUser(req.Username, req.Passwd, newUserType)
if err != nil {
SendErrorResponse(w, http.StatusInternalServerError, err.Error())
postLog.Error(fmt.Sprintf("[RegisterHandler] Failed to register user \"%s\": %v", req.Username, err))