Skip to content

Commit 531b1f3

Browse files
committed
feat: 新增授予管理员功能
1 parent bcd0be3 commit 531b1f3

File tree

10 files changed

+235
-6
lines changed

10 files changed

+235
-6
lines changed

api/handler/auth.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,33 @@ func IsAdmin(c *gin.Context) {
5858
var err error
5959
var resp *dto.IsAdminResp
6060

61-
tokenStr, _ := token.ExtractToken(c.Request.Header)
62-
c.Set(consts.CtxToken, tokenStr)
61+
c.Set(consts.CtxUserID, token.GetUserID(c))
6362

6463
resp, err = provider.Get().AuthService.IsAdmin(c)
6564
PostProcess(c, nil, resp, err)
6665
}
66+
67+
// GrantAdmin godoc
68+
// @Summary 授予管理员权限
69+
// @Description 授予指定用户管理员权限
70+
// @Tags auth
71+
// @Accept json
72+
// @Produce json
73+
// @Param body body dto.GrantAdminReq true "GrantAdminReq"
74+
// @Success 200 {object} dto.GrantAdminResp
75+
// @Router /api/auth/grant_admin [post]
76+
func GrantAdmin(c *gin.Context) {
77+
var err error
78+
var req dto.GrantAdminReq
79+
var resp *dto.GrantAdminResp
80+
81+
if err = c.ShouldBindJSON(&req); err != nil {
82+
PostProcess(c, &req, nil, err)
83+
return
84+
}
85+
86+
c.Set(consts.CtxUserID, token.GetUserID(c))
87+
88+
resp, err = provider.Get().AuthService.GrantAdmin(c, &req)
89+
PostProcess(c, &req, resp, err)
90+
}

api/router/register.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func SetupRoutes() *gin.Engine {
4444
{
4545
authGroup.POST("/sign_in", handler.SignIn) // 初始化时的登录、授权
4646
authGroup.GET("/is_admin", handler.IsAdmin)
47+
authGroup.POST("/grant_admin", handler.GrantAdmin)
4748
}
4849

4950
// LikeApi

application/dto/auth.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,14 @@ type IsAdminResp struct {
3434
*Resp
3535
IsAdmin bool `json:"isAdmin"`
3636
}
37+
38+
// GrantAdminReq 授予管理员权限的请求体
39+
type GrantAdminReq struct {
40+
UserID string `json:"userId"`
41+
VerifyCode string `json:"verifyCode"`
42+
}
43+
44+
// GrantAdminResp 授予管理员权限的响应体
45+
type GrantAdminResp struct {
46+
*Resp
47+
}

application/service/auth.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var _ IAuthService = (*AuthService)(nil)
3838
type IAuthService interface {
3939
SignIn(ctx context.Context, req *dto.SignInReq) (resp *dto.SignInResp, err error)
4040
IsAdmin(ctx context.Context) (resp *dto.IsAdminResp, err error)
41+
GrantAdmin(ctx context.Context, req *dto.GrantAdminReq) (resp *dto.GrantAdminResp, err error)
4142
}
4243

4344
type AuthService struct {
@@ -150,3 +151,33 @@ func (s *AuthService) IsAdmin(ctx context.Context) (resp *dto.IsAdminResp, err e
150151
Resp: dto.Success(),
151152
}, nil
152153
}
154+
155+
func (s *AuthService) GrantAdmin(ctx context.Context, req *dto.GrantAdminReq) (resp *dto.GrantAdminResp, err error) {
156+
// 鉴权
157+
userId, ok := ctx.Value(consts.CtxUserID).(string)
158+
if !ok || userId == "" {
159+
return nil, errorx.New(errno.ErrUserNotLogin)
160+
}
161+
162+
// 判断用户是否为管理员
163+
admin, err := s.UserRepo.IsAdminByID(ctx, req.UserID)
164+
if err != nil {
165+
return nil, errorx.WrapByCode(err, errno.ErrUserFindFailed,
166+
errorx.KV("key", consts.CtxUserID), errorx.KV("value", req.UserID),
167+
)
168+
}
169+
if admin {
170+
return nil, errorx.New(errno.ErrUserAlreadyAdmin, errorx.KV("id", req.UserID))
171+
}
172+
173+
// 添加管理员
174+
if err = s.UserRepo.Update(ctx, &model.User{ID: req.UserID, Admin: true}); err != nil {
175+
return nil, errorx.WrapByCode(err, errno.ErrUserUpdateFailed,
176+
errorx.KV("key", consts.CtxUserID), errorx.KV("value", req.UserID),
177+
)
178+
}
179+
180+
return &dto.GrantAdminResp{
181+
Resp: dto.Success(),
182+
}, nil
183+
}

docs/docs.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,40 @@ const docTemplate = `{
2929
"host": "{{.Host}}",
3030
"basePath": "{{.BasePath}}",
3131
"paths": {
32+
"/api/auth/grant_admin": {
33+
"post": {
34+
"description": "授予指定用户管理员权限",
35+
"consumes": [
36+
"application/json"
37+
],
38+
"produces": [
39+
"application/json"
40+
],
41+
"tags": [
42+
"auth"
43+
],
44+
"summary": "授予管理员权限",
45+
"parameters": [
46+
{
47+
"description": "GrantAdminReq",
48+
"name": "body",
49+
"in": "body",
50+
"required": true,
51+
"schema": {
52+
"$ref": "#/definitions/dto.GrantAdminReq"
53+
}
54+
}
55+
],
56+
"responses": {
57+
"200": {
58+
"description": "OK",
59+
"schema": {
60+
"$ref": "#/definitions/dto.GrantAdminResp"
61+
}
62+
}
63+
}
64+
}
65+
},
3266
"/api/auth/is_admin": {
3367
"get": {
3468
"description": "判断当前用户是否具有管理员权限",
@@ -1191,6 +1225,28 @@ const docTemplate = `{
11911225
}
11921226
}
11931227
},
1228+
"dto.GrantAdminReq": {
1229+
"type": "object",
1230+
"properties": {
1231+
"userId": {
1232+
"type": "string"
1233+
},
1234+
"verifyCode": {
1235+
"type": "string"
1236+
}
1237+
}
1238+
},
1239+
"dto.GrantAdminResp": {
1240+
"type": "object",
1241+
"properties": {
1242+
"code": {
1243+
"type": "integer"
1244+
},
1245+
"msg": {
1246+
"type": "string"
1247+
}
1248+
}
1249+
},
11941250
"dto.IsAdminResp": {
11951251
"type": "object",
11961252
"properties": {

docs/swagger.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,40 @@
88
},
99
"basePath": "/",
1010
"paths": {
11+
"/api/auth/grant_admin": {
12+
"post": {
13+
"description": "授予指定用户管理员权限",
14+
"consumes": [
15+
"application/json"
16+
],
17+
"produces": [
18+
"application/json"
19+
],
20+
"tags": [
21+
"auth"
22+
],
23+
"summary": "授予管理员权限",
24+
"parameters": [
25+
{
26+
"description": "GrantAdminReq",
27+
"name": "body",
28+
"in": "body",
29+
"required": true,
30+
"schema": {
31+
"$ref": "#/definitions/dto.GrantAdminReq"
32+
}
33+
}
34+
],
35+
"responses": {
36+
"200": {
37+
"description": "OK",
38+
"schema": {
39+
"$ref": "#/definitions/dto.GrantAdminResp"
40+
}
41+
}
42+
}
43+
}
44+
},
1145
"/api/auth/is_admin": {
1246
"get": {
1347
"description": "判断当前用户是否具有管理员权限",
@@ -1170,6 +1204,28 @@
11701204
}
11711205
}
11721206
},
1207+
"dto.GrantAdminReq": {
1208+
"type": "object",
1209+
"properties": {
1210+
"userId": {
1211+
"type": "string"
1212+
},
1213+
"verifyCode": {
1214+
"type": "string"
1215+
}
1216+
}
1217+
},
1218+
"dto.GrantAdminResp": {
1219+
"type": "object",
1220+
"properties": {
1221+
"code": {
1222+
"type": "integer"
1223+
},
1224+
"msg": {
1225+
"type": "string"
1226+
}
1227+
}
1228+
},
11731229
"dto.IsAdminResp": {
11741230
"type": "object",
11751231
"properties": {

docs/swagger.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,20 @@ definitions:
316316
msg:
317317
type: string
318318
type: object
319+
dto.GrantAdminReq:
320+
properties:
321+
userId:
322+
type: string
323+
verifyCode:
324+
type: string
325+
type: object
326+
dto.GrantAdminResp:
327+
properties:
328+
code:
329+
type: integer
330+
msg:
331+
type: string
332+
type: object
319333
dto.IsAdminResp:
320334
properties:
321335
code:
@@ -518,6 +532,28 @@ info:
518532
title: Meowpick Backend API
519533
version: "1.0"
520534
paths:
535+
/api/auth/grant_admin:
536+
post:
537+
consumes:
538+
- application/json
539+
description: 授予指定用户管理员权限
540+
parameters:
541+
- description: GrantAdminReq
542+
in: body
543+
name: body
544+
required: true
545+
schema:
546+
$ref: '#/definitions/dto.GrantAdminReq'
547+
produces:
548+
- application/json
549+
responses:
550+
"200":
551+
description: OK
552+
schema:
553+
$ref: '#/definitions/dto.GrantAdminResp'
554+
summary: 授予管理员权限
555+
tags:
556+
- auth
521557
/api/auth/is_admin:
522558
get:
523559
description: 判断当前用户是否具有管理员权限

infra/config/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ type Config struct {
4545
URL string
4646
DB string
4747
}
48-
Cache cache.CacheConf
49-
Redis *redis.RedisConf
50-
WeApp WeApp
48+
Cache cache.CacheConf
49+
Redis *redis.RedisConf
50+
WeApp WeApp
51+
AdminGrantKey string
5152
}
5253

5354
func NewConfig() (*Config, error) {
@@ -70,4 +71,4 @@ func NewConfig() (*Config, error) {
7071

7172
func GetConfig() *Config {
7273
return config
73-
}
74+
}

provider/wire_gen.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

types/errno/user.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const (
2525
ErrUserNotFound = 100000004
2626
ErrUserInsertFailed = 100000005
2727
ErrUserNotOwner = 100000006
28+
ErrUserAlreadyAdmin = 100000007
29+
ErrUserUpdateFailed = 100000008
2830
)
2931

3032
func init() {
@@ -58,4 +60,14 @@ func init() {
5860
"user not owner: {id}",
5961
code.WithAffectStability(false),
6062
)
63+
code.Register(
64+
ErrUserAlreadyAdmin,
65+
"user already admin: {id}",
66+
code.WithAffectStability(false),
67+
)
68+
code.Register(
69+
ErrUserUpdateFailed,
70+
"failed to update user: {id}",
71+
code.WithAffectStability(false),
72+
)
6173
}

0 commit comments

Comments
 (0)