Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type UserAPI struct {
PasswordStrength int
UserChangeNotifier *UserChangeNotifier
Registration bool
LocalAuthEnabled bool
}

// GetUsers returns all the users
Expand Down Expand Up @@ -396,6 +397,11 @@ func (a *UserAPI) DeleteUserByID(ctx *gin.Context) {
// schema:
// $ref: "#/definitions/Error"
func (a *UserAPI) ChangePassword(ctx *gin.Context) {
if !a.LocalAuthEnabled {
ctx.AbortWithError(403, errors.New("local authentication is disabled"))
return
}

pw := model.UserExternalPass{}
if err := ctx.Bind(&pw); err == nil {
if err := password.ValidateNewPassword(pw.Pass); err != nil {
Expand Down
21 changes: 20 additions & 1 deletion api/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *UserSuite) BeforeTest(suiteName, testName string) {
s.notifiedAdd = true
return nil
})
s.a = &UserAPI{DB: s.db, UserChangeNotifier: s.notifier}
s.a = &UserAPI{DB: s.db, UserChangeNotifier: s.notifier, LocalAuthEnabled: true}
}

func (s *UserSuite) AfterTest(suiteName, testName string) {
Expand Down Expand Up @@ -493,6 +493,25 @@ func (s *UserSuite) Test_UpdatePassword_EmptyPassword() {
assert.True(s.T(), password.ComparePassword(user.Pass, []byte("old")))
}

func (s *UserSuite) Test_UpdatePassword_LocalAuthDisabled_Expect403() {
pw, err := password.CreatePassword("old", 5)
require.NoError(s.T(), err)
s.db.CreateUser(&model.User{ID: 1, Name: "jmattheis", Pass: pw})
s.a.LocalAuthEnabled = false

test.WithUser(s.ctx, 1)
s.ctx.Request = httptest.NewRequest("POST", "/user/current/password", strings.NewReader(`{"pass": "new"}`))
s.ctx.Request.Header.Set("Content-Type", "application/json")

s.a.ChangePassword(s.ctx)

assert.Equal(s.T(), 403, s.recorder.Code)
user, err := s.db.GetUserByID(1)
assert.NoError(s.T(), err)
assert.NotNil(s.T(), user)
assert.True(s.T(), password.ComparePassword(user.Pass, []byte("old")))
}

func (s *UserSuite) Test_UpdatePassword_TooLongPassword_Expect400() {
pw, err := password.CreatePassword("old", 5)
require.NoError(s.T(), err)
Expand Down
2 changes: 1 addition & 1 deletion router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func Create(db *database.GormDatabase, vInfo *model.VersionInfo, conf *config.Co
}
sessionHandler := api.SessionAPI{DB: db, NotifyDeleted: streamHandler.NotifyDeletedClient, SecureCookie: conf.Server.SecureCookie, LocalAuthEnabled: conf.LocalAuthEnabled}
userChangeNotifier := new(api.UserChangeNotifier)
userHandler := api.UserAPI{DB: db, PasswordStrength: conf.PassStrength, UserChangeNotifier: userChangeNotifier, Registration: conf.Registration}
userHandler := api.UserAPI{DB: db, PasswordStrength: conf.PassStrength, UserChangeNotifier: userChangeNotifier, Registration: conf.Registration, LocalAuthEnabled: conf.LocalAuthEnabled}

pluginManager, err := plugin.NewManager(db, conf.PluginsDir, g.Group("/plugin/:id/custom/"), streamHandler)
if err != nil {
Expand Down
17 changes: 10 additions & 7 deletions ui/src/layout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import React, {CSSProperties} from 'react';
import {Link} from 'react-router';
import {useMediaQuery} from '@mui/material';
import {ThemeKey} from './theme';
import * as config from '../config';

const themeIcons: Record<ThemeKey, React.ReactElement> = {
dark: <Brightness4 />,
Expand Down Expand Up @@ -195,13 +196,15 @@ const Buttons = ({
<Link className={classes.link} to="/plugins" id="navigate-plugins">
<ResponsiveButton icon={<Apps />} label="plugins" color="inherit" />
</Link>
<ResponsiveButton
icon={<AccountCircle />}
label={name}
onClick={showSettings}
id="changepw"
color="inherit"
/>
{config.get('localAuth') && (
<ResponsiveButton
icon={<AccountCircle />}
label={name}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is the only place where the currently logged in user is displayed. The button shouldn't be removed. Instead, the button should link to a new "settings" page which includes the Theme setting as select box (light,dark,system). and the change password form which should be disabled when localAuth is disabled.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed, the header entry stays and always shows the username. It now links to a new /settings page instead of opening the dialog. The settings page has a theme select (light/dark/system) and the change password form. The form renders disabled when local auth is off; with local auth on it still asks for elevation first, same as the dialog did. SettingsDialog is removed since nothing opens it anymore, and the e2e change-password test goes through the new page now and I left the theme toggle in the header alone; it and the select share the same state.

onClick={showSettings}
id="changepw"
color="inherit"
/>
)}
<ResponsiveButton
icon={<ExitToApp />}
label="Logout"
Expand Down