Skip to content

Commit

Permalink
feat!: allow disable user (close #3241)
Browse files Browse the repository at this point in the history
From this commit, the guest user will be disabled by default
  • Loading branch information
xhofe committed Feb 4, 2023
1 parent 7bf8071 commit 3d0065b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/bootstrap/data/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func initUser() {
Role: model.GUEST,
BasePath: "/",
Permission: 0,
Disabled: true,
}
if err := db.CreateUser(guest); err != nil {
panic(err)
Expand Down
1 change: 1 addition & 0 deletions internal/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type User struct {
Password string `json:"password"` // password
BasePath string `json:"base_path"` // base path
Role int `json:"role"` // user's role
Disabled bool `json:"disabled"`
// Determine permissions by bit
// 0: can see hidden files
// 1: can access without password
Expand Down
4 changes: 4 additions & 0 deletions server/handles/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func UpdateUser(c *gin.Context) {
if req.OtpSecret == "" {
req.OtpSecret = user.OtpSecret
}
if req.Disabled && req.IsAdmin() {
common.ErrorStrResp(c, "admin user can not be disabled", 400)
return
}
if err := op.UpdateUser(&req); err != nil {
common.ErrorResp(c, err, 500)
} else {
Expand Down
10 changes: 10 additions & 0 deletions server/middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func Auth(c *gin.Context) {
c.Abort()
return
}
if guest.Disabled {
common.ErrorStrResp(c, "Guest user is disabled, login please", 401)
c.Abort()
return
}
c.Set("user", guest)
log.Debugf("use empty token: %+v", guest)
c.Next()
Expand All @@ -50,6 +55,11 @@ func Auth(c *gin.Context) {
c.Abort()
return
}
if user.Disabled {
common.ErrorStrResp(c, "Current user is disabled, replace please", 401)
c.Abort()
return
}
c.Set("user", user)
log.Debugf("use login token: %+v", user)
c.Next()
Expand Down

0 comments on commit 3d0065b

Please sign in to comment.