Add `EnableUserEditing`, to control whether a regular user can change their own details (default `true`)

This commit is contained in:
Deluan 2021-05-02 17:10:56 -04:00
parent 2ff1c79b64
commit 7feda4bea4
6 changed files with 22 additions and 0 deletions

View File

@ -42,6 +42,7 @@ type configOptions struct {
EnableGravatar bool EnableGravatar bool
EnableFavourites bool EnableFavourites bool
EnableStarRating bool EnableStarRating bool
EnableUserEditing bool
DefaultTheme string DefaultTheme string
GATrackingID string GATrackingID string
EnableLogRedacting bool EnableLogRedacting bool
@ -148,6 +149,7 @@ func init() {
viper.SetDefault("enablegravatar", false) viper.SetDefault("enablegravatar", false)
viper.SetDefault("enablefavourites", true) viper.SetDefault("enablefavourites", true)
viper.SetDefault("enablestarrating", true) viper.SetDefault("enablestarrating", true)
viper.SetDefault("enableuserediting", true)
viper.SetDefault("defaulttheme", "Dark") viper.SetDefault("defaulttheme", "Dark")
viper.SetDefault("gatrackingid", "") viper.SetDefault("gatrackingid", "")
viper.SetDefault("enablelogredacting", true) viper.SetDefault("enablelogredacting", true)

View File

@ -4,6 +4,8 @@ import (
"context" "context"
"time" "time"
"github.com/navidrome/navidrome/conf"
. "github.com/Masterminds/squirrel" . "github.com/Masterminds/squirrel"
"github.com/astaxie/beego/orm" "github.com/astaxie/beego/orm"
"github.com/deluan/rest" "github.com/deluan/rest"
@ -145,6 +147,9 @@ func (r *userRepository) Update(entity interface{}, cols ...string) error {
return rest.ErrPermissionDenied return rest.ErrPermissionDenied
} }
if !usr.IsAdmin { if !usr.IsAdmin {
if !conf.Server.EnableUserEditing {
return rest.ErrPermissionDenied
}
u.IsAdmin = false u.IsAdmin = false
u.UserName = usr.UserName u.UserName = usr.UserName
} }

View File

@ -48,6 +48,7 @@ func serveIndex(ds model.DataStore, fs fs.FS) http.HandlerFunc {
"losslessFormats": strings.ToUpper(strings.Join(consts.LosslessFormats, ",")), "losslessFormats": strings.ToUpper(strings.Join(consts.LosslessFormats, ",")),
"devActivityPanel": conf.Server.DevActivityPanel, "devActivityPanel": conf.Server.DevActivityPanel,
"devFastAccessCoverArt": conf.Server.DevFastAccessCoverArt, "devFastAccessCoverArt": conf.Server.DevFastAccessCoverArt,
"enableUserEditing": conf.Server.EnableUserEditing,
} }
j, err := json.Marshal(appConfig) j, err := json.Marshal(appConfig)
if err != nil { if err != nil {

View File

@ -189,6 +189,16 @@ var _ = Describe("serveIndex", func() {
expected := strings.ToUpper(strings.Join(consts.LosslessFormats, ",")) expected := strings.ToUpper(strings.Join(consts.LosslessFormats, ","))
Expect(config).To(HaveKeyWithValue("losslessFormats", expected)) Expect(config).To(HaveKeyWithValue("losslessFormats", expected))
}) })
It("sets the enableUserEditing", func() {
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("enableUserEditing", true))
})
}) })
var appConfigRegex = regexp.MustCompile(`(?m)window.__APP_CONFIG__="([^"]*)`) var appConfigRegex = regexp.MustCompile(`(?m)window.__APP_CONFIG__="([^"]*)`)

View File

@ -17,6 +17,7 @@ const defaultConfig = {
devFastAccessCoverArt: false, devFastAccessCoverArt: false,
enableStarRating: true, enableStarRating: true,
defaultTheme: 'Dark', defaultTheme: 'Dark',
enableUserEditing: true,
} }
let config let config

View File

@ -80,6 +80,9 @@ const CustomUserMenu = ({ onClick, ...rest }) => {
return null return null
} }
if (permissions !== 'admin') { if (permissions !== 'admin') {
if (!config.enableUserEditing) {
return null
}
userResource.icon = PersonIcon userResource.icon = PersonIcon
} else { } else {
userResource.icon = SupervisorAccountIcon userResource.icon = SupervisorAccountIcon