Skip to content

Commit

Permalink
Fixed panic with auth disabled, dont display user tab when auth disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Forceu committed Dec 30, 2024
1 parent 2acf684 commit 366f968
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
33 changes: 16 additions & 17 deletions internal/configuration/database/Database.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func DeleteUser(id int) {
db.DeleteUser(id)
}

// GetSuperAdmin returns the models.User data for the super admin
func GetSuperAdmin() (models.User, bool) {
users := db.GetAllUsers()
for _, user := range users {
Expand All @@ -286,23 +287,11 @@ func GetSuperAdmin() (models.User, bool) {
// EditSuperAdmin changes parameters of the super admin. If no user exists, a new superadmin will be created
// Returns an error if at least one user exists, but no superadmin
func EditSuperAdmin(name, email, password string) error {
users := db.GetAllUsers()
for _, user := range users {
if user.UserLevel == models.UserLevelSuperAdmin {
if name != "" {
user.Name = name
}
if email != "" {
user.Email = email
}
if password != "" {
user.Password = password
}
db.SaveUser(user, false)
return nil
user, ok := GetSuperAdmin()
if !ok {
if len(GetAllUsers()) != 0 {
return errors.New("at least one user exists, but no superadmin found")
}
}
if len(users) == 0 {
newAdmin := models.User{
Name: name,
Email: email,
Expand All @@ -313,5 +302,15 @@ func EditSuperAdmin(name, email, password string) error {
db.SaveUser(newAdmin, true)
return nil
}
return errors.New("at least one user exists, but no superadmin found")
if name != "" {
user.Name = name
}
if email != "" {
user.Email = email
}
if password != "" {
user.Password = password
}
db.SaveUser(user, false)
return nil
}
4 changes: 3 additions & 1 deletion internal/webserver/Webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func showUserAdmin(w http.ResponseWriter, r *http.Request) {
panic(err)
}
view := (&UploadView{}).convertGlobalConfig(ViewUsers, userId)
if !view.ActiveUser.HasPermissionManageUsers() {
if !view.ActiveUser.HasPermissionManageUsers() || configuration.Get().Authentication.Method == models.AuthenticationDisabled {
redirect(w, "admin")
return
}
Expand Down Expand Up @@ -632,6 +632,7 @@ type UploadView struct {
IsDownloadView bool
IsApiView bool
IsLogoutAvailable bool
IsUserTabAvailable bool
EndToEndEncryption bool
IncludeFilename bool
MaxFileSize int
Expand Down Expand Up @@ -743,6 +744,7 @@ func (u *UploadView) convertGlobalConfig(view, userId int) *UploadView {
u.ActiveView = view
u.MaxFileSize = config.MaxFileSizeMB
u.IsLogoutAvailable = authentication.IsLogoutAvailable()
u.IsUserTabAvailable = config.Authentication.Method != models.AuthenticationDisabled
u.EndToEndEncryption = config.Encryption.Level == encryption.EndToEndEncryption
u.MaxParallelUploads = config.MaxParallelUploads
u.ChunkSize = config.ChunkSize
Expand Down
6 changes: 5 additions & 1 deletion internal/webserver/authentication/Authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ func IsAuthenticated(w http.ResponseWriter, r *http.Request) (bool, int) {
return true, userId
}
case models.AuthenticationDisabled:
return true, 0
adminUser, ok := database.GetSuperAdmin()
if !ok {
panic("no super admin found")
}
return true, adminUser.Id
}
return false, -1
}
Expand Down
2 changes: 1 addition & 1 deletion internal/webserver/web/templates/html_header.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
{{ if .ActiveUser.HasPermissionManageLogs }}
<a class="nav-link {{ if eq .ActiveView 1 }}active{{ end }}" href="./logs">Logs</a>
{{ end }}
{{ if .ActiveUser.HasPermissionManageUsers }}
{{ if and .ActiveUser.HasPermissionManageUsers .IsUserTabAvailable }}
<a class="nav-link {{ if eq .ActiveView 3 }}active{{ end }}" href="./users">Users</a>
{{ end }}
<a class="nav-link {{ if eq .ActiveView 2 }}active{{ end }}" href="./apiKeys">API</a>
Expand Down

0 comments on commit 366f968

Please sign in to comment.