Skip to content

Commit 85d7985

Browse files
authored
Merge pull request #15 from xh-polaris/moonlight
feat:普通登录签发token,Alarm,Conversation服务添加鉴权
2 parents e3db727 + ab87ba7 commit 85d7985

File tree

17 files changed

+367
-70
lines changed

17 files changed

+367
-70
lines changed

biz/adaptor/controller/core_api/conversation.go

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

biz/adaptor/controller/core_api/core_api.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package core_api
22

33
import (
44
"context"
5+
56
"github.com/cloudwego/hertz/pkg/app"
67
"github.com/cloudwego/hertz/pkg/protocol/consts"
8+
"github.com/xh-polaris/psych-core-api/biz/adaptor/middleware"
79
"github.com/xh-polaris/psych-core-api/biz/application/dto/core_api"
810
"github.com/xh-polaris/psych-core-api/biz/cst"
911
"github.com/xh-polaris/psych-core-api/pkg/httpx"
1012
"github.com/xh-polaris/psych-core-api/provider"
11-
//"github.com/xh-polaris/psych-idl/kitex_gen/core_api"
1213
)
1314

1415
// ==========================================
@@ -34,6 +35,7 @@ func DashboardGetDataOverview(ctx context.Context, c *app.RequestContext) {
3435
return
3536
}
3637

38+
middleware.StoreToken(ctx, c, &req)
3739
p := provider.Get()
3840
resp, err := p.DashboardService.DashboardGetDataOverview(ctx, &req)
3941
httpx.PostProcess(ctx, c, &req, resp, err)
@@ -58,6 +60,7 @@ func DashboardGetDataTrend(ctx context.Context, c *app.RequestContext) {
5860
return
5961
}
6062

63+
middleware.StoreToken(ctx, c, &req)
6164
p := provider.Get()
6265
resp, err := p.DashboardService.DashboardGetDataTrend(ctx, &req)
6366
httpx.PostProcess(ctx, c, &req, resp, err)
@@ -81,6 +84,7 @@ func DashboardListUnits(ctx context.Context, c *app.RequestContext) {
8184
return
8285
}
8386

87+
middleware.StoreToken(ctx, c, &req)
8488
p := provider.Get()
8589
resp, err := p.DashboardService.DashboardListUnits(ctx, &req)
8690
httpx.PostProcess(ctx, c, &req, resp, err)
@@ -105,6 +109,7 @@ func DashboardGetPsychTrend(ctx context.Context, c *app.RequestContext) {
105109
return
106110
}
107111

112+
middleware.StoreToken(ctx, c, &req)
108113
p := provider.Get()
109114
resp, err := p.DashboardService.DashboardGetPsychTrend(ctx, &req)
110115
httpx.PostProcess(ctx, c, &req, resp, err)
@@ -129,6 +134,7 @@ func DashboardGetAlarmOverview(ctx context.Context, c *app.RequestContext) {
129134
return
130135
}
131136

137+
middleware.StoreToken(ctx, c, &req)
132138
p := provider.Get()
133139
resp, err := p.AlarmService.Overview(ctx, &req)
134140
httpx.PostProcess(ctx, c, &req, resp, err)
@@ -158,6 +164,7 @@ func DashboardListAlarmRecords(ctx context.Context, c *app.RequestContext) {
158164
return
159165
}
160166

167+
middleware.StoreToken(ctx, c, &req)
161168
p := provider.Get()
162169
resp, err := p.AlarmService.ListRecords(ctx, &req)
163170
httpx.PostProcess(ctx, c, &req, resp, err)
@@ -184,6 +191,7 @@ func DashboardListClasses(ctx context.Context, c *app.RequestContext) {
184191
return
185192
}
186193

194+
middleware.StoreToken(ctx, c, &req)
187195
p := provider.Get()
188196
resp, err := p.DashboardService.DashboardListClasses(ctx, &req)
189197
httpx.PostProcess(ctx, c, &req, resp, err)
@@ -213,6 +221,7 @@ func DashboardListUsers(ctx context.Context, c *app.RequestContext) {
213221
return
214222
}
215223

224+
middleware.StoreToken(ctx, c, &req)
216225
p := provider.Get()
217226
resp, err := p.DashboardService.DashboardListUsers(ctx, &req)
218227
httpx.PostProcess(ctx, c, &req, resp, err)
@@ -594,6 +603,7 @@ func DashboardUserConvRecords(ctx context.Context, c *app.RequestContext) {
594603
return
595604
}
596605

606+
middleware.StoreToken(ctx, c, &req)
597607
p := provider.Get()
598608
resp, err := p.DashboardService.DashboardUserConvRecords(ctx, &req)
599609
httpx.PostProcess(ctx, c, &req, resp, err)
@@ -610,6 +620,7 @@ func DashboardUpdateAlarm(ctx context.Context, c *app.RequestContext) {
610620
return
611621
}
612622

623+
middleware.StoreToken(ctx, c, &req)
613624
p := provider.Get()
614625
resp, err := p.AlarmService.UpdateAlarm(ctx, &req)
615626
httpx.PostProcess(ctx, c, &req, resp, err)
@@ -626,6 +637,7 @@ func DashboardGetReport(ctx context.Context, c *app.RequestContext) {
626637
return
627638
}
628639

640+
middleware.StoreToken(ctx, c, &req)
629641
p := provider.Get()
630642
resp, err := p.DashboardService.DashboardGetReport(ctx, &req)
631643
httpx.PostProcess(ctx, c, &req, resp, err)

biz/adaptor/middleware/auth.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package middleware
2+
3+
import (
4+
"context"
5+
6+
"github.com/cloudwego/hertz/pkg/app"
7+
"github.com/xh-polaris/psych-core-api/biz/cst"
8+
"github.com/xh-polaris/psych-core-api/biz/infra/util"
9+
"github.com/xh-polaris/psych-core-api/pkg/errorx"
10+
"github.com/xh-polaris/psych-core-api/pkg/httpx"
11+
"github.com/xh-polaris/psych-core-api/types/errno"
12+
)
13+
14+
func StoreToken(ctx context.Context, c *app.RequestContext, req any) {
15+
authHeader := c.GetHeader("Authorization")
16+
if len(authHeader) == 0 {
17+
httpx.PostProcess(ctx, c, req, nil, errorx.New(errno.ErrUnAuth))
18+
c.Abort()
19+
return
20+
}
21+
22+
// 验证JWT的有效性
23+
_, err := util.ParseJwt(string(authHeader))
24+
if err != nil {
25+
httpx.PostProcess(ctx, c, req, nil, errorx.New(errno.ErrJWTPrase))
26+
c.Abort()
27+
return
28+
}
29+
30+
// 使用context.WithValue传递token
31+
newCtx := context.WithValue(ctx, cst.CtxKeyToken, string(authHeader))
32+
c.Set(cst.CtxKeyToken, newCtx)
33+
c.Next(ctx)
34+
}

biz/application/service/alarm.go

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package service
22

33
import (
44
"context"
5-
"github.com/xh-polaris/psych-core-api/biz/application/dto/core_api"
6-
"github.com/xh-polaris/psych-core-api/biz/infra/util"
75
"sync"
86
"time"
97

8+
"github.com/xh-polaris/psych-core-api/biz/application/dto/core_api"
9+
"github.com/xh-polaris/psych-core-api/biz/infra/util"
10+
1011
"github.com/xh-polaris/psych-core-api/biz/infra/mapper/conversation"
1112
"github.com/xh-polaris/psych-core-api/biz/infra/mapper/report"
1213

@@ -40,6 +41,22 @@ var AlarmServiceSet = wire.NewSet(
4041
)
4142

4243
func (s *AlarmService) Overview(ctx context.Context, req *core_api.DashboardGetAlarmOverviewReq) (resp *core_api.DashboardGetAlarmOverviewResp, err error) {
44+
// 鉴权
45+
userMeta, err := util.ExtraUserMeta(ctx)
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
if req.UnitId != "" {
51+
if !userMeta.HasUnitAdminAuth() || userMeta.UserId != req.UnitId {
52+
return nil, errorx.New(errno.ErrInsufficientAuth)
53+
}
54+
}
55+
if req.UnitId == "" && !userMeta.HasSuperAdminAuth() {
56+
return nil, errorx.New(errno.ErrInsufficientAuth)
57+
}
58+
59+
// 提取unitID
4360
unitOID, err := bson.ObjectIDFromHex(req.UnitId)
4461
if err != nil {
4562
return nil, errorx.New(errno.ErrInvalidParams, errorx.KV("field", "UnitID"), errorx.KV("value", "单位ID"))
@@ -48,7 +65,7 @@ func (s *AlarmService) Overview(ctx context.Context, req *core_api.DashboardGetA
4865
st, err := s.AlarmMapper.AggregateStats(ctx, unitOID, time.Time{}, time.Time{})
4966
if err != nil {
5067
logs.Errorf("aggregate alarm error: %s", errorx.ErrorWithoutStack(err))
51-
return nil, err
68+
return nil, errorx.New(errno.ErrDashboardAlarmUserStat)
5269
}
5370

5471
return &core_api.DashboardGetAlarmOverviewResp{
@@ -60,12 +77,28 @@ func (s *AlarmService) Overview(ctx context.Context, req *core_api.DashboardGetA
6077
ProcessedChange: st.ProcessedChange,
6178
PendingChange: st.PendingChange,
6279
TrackChange: st.TrackChange,
63-
Code: 200,
80+
Code: 0,
6481
Msg: "success",
6582
}, nil
6683
}
6784

6885
func (s *AlarmService) ListRecords(ctx context.Context, req *core_api.DashboardListAlarmRecordsReq) (resp *core_api.DashboardListAlarmRecordsResp, err error) {
86+
// 鉴权
87+
userMeta, err := util.ExtraUserMeta(ctx)
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
if req.UnitId != "" {
93+
if !userMeta.HasUnitAdminAuth() || userMeta.UserId != req.UnitId {
94+
return nil, errorx.New(errno.ErrInsufficientAuth)
95+
}
96+
}
97+
if req.UnitId == "" && !userMeta.HasSuperAdminAuth() {
98+
return nil, errorx.New(errno.ErrInsufficientAuth)
99+
}
100+
101+
// 提取unitID
69102
unitOID, err := bson.ObjectIDFromHex(req.UnitId)
70103
if err != nil {
71104
return nil, errorx.New(errno.ErrInvalidParams, errorx.KV("field", "UnitID"), errorx.KV("value", "单位ID"))
@@ -76,7 +109,7 @@ func (s *AlarmService) ListRecords(ctx context.Context, req *core_api.DashboardL
76109
if total == 0 {
77110
return &core_api.DashboardListAlarmRecordsResp{
78111
Pagination: util.PaginationRes(total, req.PaginationOptions),
79-
Code: 200,
112+
Code: 0,
80113
Msg: "success",
81114
}, nil
82115
}
@@ -94,7 +127,7 @@ func (s *AlarmService) ListRecords(ctx context.Context, req *core_api.DashboardL
94127
return &core_api.DashboardListAlarmRecordsResp{
95128
Records: completeAlarm,
96129
Pagination: util.PaginationRes(total, req.PaginationOptions),
97-
Code: 200,
130+
Code: 0,
98131
Msg: "success",
99132
}, err2
100133
}
@@ -180,6 +213,16 @@ func (s *AlarmService) completeAlarm(ctx context.Context, dbAlarms []*alarm.Alar
180213
}
181214

182215
func (s *AlarmService) UpdateAlarm(ctx context.Context, req *core_api.DashboardUpdateAlarmReq) (resp *core_api.DashboardUpdateAlarmResp, err error) {
216+
// 初步鉴权-需要有UnitAdmin权限
217+
userMeta, err := util.ExtraUserMeta(ctx)
218+
if err != nil {
219+
return nil, err
220+
}
221+
222+
if !userMeta.HasUnitAdminAuth() {
223+
return nil, errorx.New(errno.ErrInsufficientAuth)
224+
}
225+
183226
// 参数校验
184227
if req.Alarm == nil {
185228
return nil, errorx.New(errno.ErrMissingParams, errorx.KV("field", "预警信息"))
@@ -192,6 +235,17 @@ func (s *AlarmService) UpdateAlarm(ctx context.Context, req *core_api.DashboardU
192235
return nil, errorx.New(errno.ErrInvalidParams, errorx.KV("field", "预警ID"))
193236
}
194237

238+
// 二次鉴权:需要在统一unit下
239+
oldAlarm, err := s.AlarmMapper.FindOneById(ctx, alarmId)
240+
// optimize 查不到时考虑直接创建而非报错
241+
if err != nil {
242+
logs.Errorf("find alarm error: %s", errorx.ErrorWithoutStack(err))
243+
return nil, errorx.New(errno.ErrNotFound)
244+
}
245+
if userMeta.UnitId != oldAlarm.UnitID.Hex() {
246+
return nil, errorx.New(errno.ErrInsufficientAuth)
247+
}
248+
195249
// 构建更新字段
196250
update := bson.M{}
197251

@@ -225,13 +279,13 @@ func (s *AlarmService) UpdateAlarm(ctx context.Context, req *core_api.DashboardU
225279
if len(update) > 0 {
226280
if err = s.AlarmMapper.UpdateFields(ctx, alarmId, update); err != nil {
227281
logs.Errorf("update alarm error: %s", errorx.ErrorWithoutStack(err))
228-
return nil, err
282+
return nil, errorx.New(errno.ErrInternalError)
229283
}
230284
}
231285

232286
// 构造返回结果
233287
return &core_api.DashboardUpdateAlarmResp{
234-
Code: 200,
288+
Code: 0,
235289
Msg: "success",
236290
}, nil
237291
}

0 commit comments

Comments
 (0)