Skip to content

Commit 3d0065b

Browse files
committed
feat!: allow disable user (close #3241)
From this commit, the guest user will be disabled by default
1 parent 7bf8071 commit 3d0065b

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

internal/bootstrap/data/user.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func initUser() {
4848
Role: model.GUEST,
4949
BasePath: "/",
5050
Permission: 0,
51+
Disabled: true,
5152
}
5253
if err := db.CreateUser(guest); err != nil {
5354
panic(err)

internal/model/user.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type User struct {
1818
Password string `json:"password"` // password
1919
BasePath string `json:"base_path"` // base path
2020
Role int `json:"role"` // user's role
21+
Disabled bool `json:"disabled"`
2122
// Determine permissions by bit
2223
// 0: can see hidden files
2324
// 1: can access without password

server/handles/user.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ func UpdateUser(c *gin.Context) {
6767
if req.OtpSecret == "" {
6868
req.OtpSecret = user.OtpSecret
6969
}
70+
if req.Disabled && req.IsAdmin() {
71+
common.ErrorStrResp(c, "admin user can not be disabled", 400)
72+
return
73+
}
7074
if err := op.UpdateUser(&req); err != nil {
7175
common.ErrorResp(c, err, 500)
7276
} else {

server/middlewares/auth.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ func Auth(c *gin.Context) {
3333
c.Abort()
3434
return
3535
}
36+
if guest.Disabled {
37+
common.ErrorStrResp(c, "Guest user is disabled, login please", 401)
38+
c.Abort()
39+
return
40+
}
3641
c.Set("user", guest)
3742
log.Debugf("use empty token: %+v", guest)
3843
c.Next()
@@ -50,6 +55,11 @@ func Auth(c *gin.Context) {
5055
c.Abort()
5156
return
5257
}
58+
if user.Disabled {
59+
common.ErrorStrResp(c, "Current user is disabled, replace please", 401)
60+
c.Abort()
61+
return
62+
}
5363
c.Set("user", user)
5464
log.Debugf("use login token: %+v", user)
5565
c.Next()

0 commit comments

Comments
 (0)