Skip to content

Commit eba939a

Browse files
committed
feat: 多用户权限验证
1 parent 52c3a2e commit eba939a

File tree

6 files changed

+30
-1
lines changed

6 files changed

+30
-1
lines changed

api/passport/controller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ func register(c *gin.Context) {
1818
return
1919
}
2020

21+
rq.Level = 0 //防止逃逸
22+
2123
if id, err := user.Create(rq); err == nil {
2224
c.Set("Message", "注册成功")
2325
c.Set("Payload", gin.H{"Id": id})
@@ -75,6 +77,7 @@ func updateInfo(c *gin.Context) {
7577
}
7678

7779
rq.Id = c.GetUint("UserId")
80+
rq.Level = 0 //防止逃逸
7881

7982
if err := user.Update(rq); err == nil {
8083
c.Set("Message", "修改成功")

api/user/router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func Router(api *gin.RouterGroup) {
1313
// 需授权接口
1414

1515
rg.Use(midware.AuthGuard())
16+
rg.Use(midware.AdminGuard())
1617

1718
{
1819
rg.GET("/user", list)

module/dborm/table.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ type User struct {
110110
AppId string `gorm:"uniqueIndex"`
111111
Username string `gorm:"uniqueIndex"`
112112
Password string `json:"-"`
113+
Level uint `gorm:"default:5"`
113114
Description string `gorm:"default:什么也没有"`
114115
Sessions []Session
115116
Vendors []Vendor

module/dborm/user/model.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
type CreateParam struct {
1212
Username string `binding:"required"`
1313
Password string `binding:"required"`
14+
Level uint
1415
}
1516

1617
func Create(post *CreateParam) (uint, error) {
@@ -35,8 +36,9 @@ func Create(post *CreateParam) (uint, error) {
3536

3637
type UpdateParam struct {
3738
Id uint
38-
Description string
3939
Password string
40+
Description string
41+
Level uint
4042
}
4143

4244
func Update(post *UpdateParam) error {

module/midware/auth.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/gin-gonic/gin"
55

66
"tdp-cloud/module/dborm/session"
7+
"tdp-cloud/module/dborm/user"
78
)
89

910
func AuthGuard() gin.HandlerFunc {
@@ -25,3 +26,23 @@ func AuthGuard() gin.HandlerFunc {
2526
}
2627

2728
}
29+
30+
func AdminGuard() gin.HandlerFunc {
31+
32+
return func(c *gin.Context) {
33+
34+
rq := &user.FetchParam{
35+
Id: c.GetUint("UserId"),
36+
}
37+
38+
user, err := user.Fetch(rq)
39+
40+
if err != nil || user.Level != 1 {
41+
c.Set("Error", "无权限")
42+
c.Abort()
43+
return
44+
}
45+
46+
}
47+
48+
}

module/migrator/v100001.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func v100001AddUser() error {
2323
_, err := user.Create(&user.CreateParam{
2424
Username: "admin",
2525
Password: "123456",
26+
Level: 1,
2627
})
2728

2829
return err

0 commit comments

Comments
 (0)