Skip to content

Commit 366f968

Browse files
committed
Fixed panic with auth disabled, dont display user tab when auth disabled
1 parent 2acf684 commit 366f968

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

internal/configuration/database/Database.go

+16-17
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ func DeleteUser(id int) {
273273
db.DeleteUser(id)
274274
}
275275

276+
// GetSuperAdmin returns the models.User data for the super admin
276277
func GetSuperAdmin() (models.User, bool) {
277278
users := db.GetAllUsers()
278279
for _, user := range users {
@@ -286,23 +287,11 @@ func GetSuperAdmin() (models.User, bool) {
286287
// EditSuperAdmin changes parameters of the super admin. If no user exists, a new superadmin will be created
287288
// Returns an error if at least one user exists, but no superadmin
288289
func EditSuperAdmin(name, email, password string) error {
289-
users := db.GetAllUsers()
290-
for _, user := range users {
291-
if user.UserLevel == models.UserLevelSuperAdmin {
292-
if name != "" {
293-
user.Name = name
294-
}
295-
if email != "" {
296-
user.Email = email
297-
}
298-
if password != "" {
299-
user.Password = password
300-
}
301-
db.SaveUser(user, false)
302-
return nil
290+
user, ok := GetSuperAdmin()
291+
if !ok {
292+
if len(GetAllUsers()) != 0 {
293+
return errors.New("at least one user exists, but no superadmin found")
303294
}
304-
}
305-
if len(users) == 0 {
306295
newAdmin := models.User{
307296
Name: name,
308297
Email: email,
@@ -313,5 +302,15 @@ func EditSuperAdmin(name, email, password string) error {
313302
db.SaveUser(newAdmin, true)
314303
return nil
315304
}
316-
return errors.New("at least one user exists, but no superadmin found")
305+
if name != "" {
306+
user.Name = name
307+
}
308+
if email != "" {
309+
user.Email = email
310+
}
311+
if password != "" {
312+
user.Password = password
313+
}
314+
db.SaveUser(user, false)
315+
return nil
317316
}

internal/webserver/Webserver.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func showUserAdmin(w http.ResponseWriter, r *http.Request) {
322322
panic(err)
323323
}
324324
view := (&UploadView{}).convertGlobalConfig(ViewUsers, userId)
325-
if !view.ActiveUser.HasPermissionManageUsers() {
325+
if !view.ActiveUser.HasPermissionManageUsers() || configuration.Get().Authentication.Method == models.AuthenticationDisabled {
326326
redirect(w, "admin")
327327
return
328328
}
@@ -632,6 +632,7 @@ type UploadView struct {
632632
IsDownloadView bool
633633
IsApiView bool
634634
IsLogoutAvailable bool
635+
IsUserTabAvailable bool
635636
EndToEndEncryption bool
636637
IncludeFilename bool
637638
MaxFileSize int
@@ -743,6 +744,7 @@ func (u *UploadView) convertGlobalConfig(view, userId int) *UploadView {
743744
u.ActiveView = view
744745
u.MaxFileSize = config.MaxFileSizeMB
745746
u.IsLogoutAvailable = authentication.IsLogoutAvailable()
747+
u.IsUserTabAvailable = config.Authentication.Method != models.AuthenticationDisabled
746748
u.EndToEndEncryption = config.Encryption.Level == encryption.EndToEndEncryption
747749
u.MaxParallelUploads = config.MaxParallelUploads
748750
u.ChunkSize = config.ChunkSize

internal/webserver/authentication/Authentication.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ func IsAuthenticated(w http.ResponseWriter, r *http.Request) (bool, int) {
9191
return true, userId
9292
}
9393
case models.AuthenticationDisabled:
94-
return true, 0
94+
adminUser, ok := database.GetSuperAdmin()
95+
if !ok {
96+
panic("no super admin found")
97+
}
98+
return true, adminUser.Id
9599
}
96100
return false, -1
97101
}

internal/webserver/web/templates/html_header.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
{{ if .ActiveUser.HasPermissionManageLogs }}
7575
<a class="nav-link {{ if eq .ActiveView 1 }}active{{ end }}" href="./logs">Logs</a>
7676
{{ end }}
77-
{{ if .ActiveUser.HasPermissionManageUsers }}
77+
{{ if and .ActiveUser.HasPermissionManageUsers .IsUserTabAvailable }}
7878
<a class="nav-link {{ if eq .ActiveView 3 }}active{{ end }}" href="./users">Users</a>
7979
{{ end }}
8080
<a class="nav-link {{ if eq .ActiveView 2 }}active{{ end }}" href="./apiKeys">API</a>

0 commit comments

Comments
 (0)