-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(middleware,controller): finish bizctx, user data in ctx, get use…
…r info
- Loading branch information
1 parent
fc395a8
commit 92229f5
Showing
25 changed files
with
386 additions
and
69 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package bizctx | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hitokoto-osc/reviewer/internal/service" | ||
|
||
"github.com/gogf/gf/v2/net/ghttp" | ||
"github.com/hitokoto-osc/reviewer/internal/consts" | ||
"github.com/hitokoto-osc/reviewer/internal/model" | ||
) | ||
|
||
type sBizCtx struct{} | ||
|
||
func init() { | ||
service.RegisterBizCtx(New()) | ||
} | ||
|
||
func New() service.IBizCtx { | ||
return &sBizCtx{} | ||
} | ||
|
||
// Init initializes and injects custom business context object into request context. | ||
func (s *sBizCtx) Init(r *ghttp.Request, customCtx *model.Context) { | ||
r.SetCtxVar(consts.ContextKey, customCtx) | ||
} | ||
|
||
// Get retrieves and returns the user object from context. | ||
// It returns nil if nothing found in given context. | ||
func (s *sBizCtx) Get(ctx context.Context) *model.Context { | ||
value := ctx.Value(consts.ContextKey) | ||
if value == nil { | ||
return nil | ||
} | ||
if localCtx, ok := value.(*model.Context); ok { | ||
return localCtx | ||
} | ||
return nil | ||
} | ||
|
||
// SetUser injects business user object into context. | ||
func (s *sBizCtx) SetUser(ctx context.Context, ctxUser *model.ContextUser) { | ||
s.Get(ctx).User = ctxUser | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/gogf/gf/v2/errors/gerror" | ||
|
||
"github.com/hitokoto-osc/reviewer/internal/model" | ||
|
||
"github.com/gogf/gf/v2/frame/g" | ||
"github.com/gogf/gf/v2/net/ghttp" | ||
"github.com/hitokoto-osc/reviewer/internal/consts" | ||
"github.com/hitokoto-osc/reviewer/internal/service" | ||
) | ||
|
||
// AuthorizationV1 用于 v1 接口校验用户是否登录 | ||
// 尝试顺序 Authorization: Bearer Token -> param -> form -> body -> query -> Router | ||
func (s *sMiddleware) AuthorizationV1(r *ghttp.Request) { | ||
g.Log("AuthorizationV1").Debugf(r.GetCtx(), "AuthorizationV1: %s", r.GetHeader("Authorization")) | ||
authStr := r.GetHeader("Authorization") | ||
if authStr == "" || !strings.HasPrefix(authStr, "Bearer ") { | ||
if v := r.Get("token"); v != nil && !v.IsNil() && strings.HasPrefix(v.String(), "Bearer ") { | ||
authStr = v.String() | ||
} else { | ||
r.Response.Status = http.StatusUnauthorized | ||
return | ||
} | ||
} | ||
token := strings.Trim(strings.TrimPrefix(authStr, "Bearer "), " ") | ||
if len(token) != consts.UserAccessTokenV1Length { | ||
r.Response.Status = http.StatusUnauthorized | ||
return | ||
} | ||
flag, err := service.User().VerifyAPIV1Token(r.GetCtx(), token) | ||
if err != nil { | ||
g.Log().Error(r.GetCtx(), gerror.Wrap(err, "校验用户 Token 时发生错误")) | ||
r.Response.Status = http.StatusInternalServerError | ||
return | ||
} | ||
if !flag { | ||
r.Response.Status = http.StatusUnauthorized | ||
return | ||
} | ||
// 注入用户信息到上下文 | ||
user, err := service.User().GetUserByToken(r.GetCtx(), token) | ||
if err != nil { | ||
g.Log().Error(r.GetCtx(), gerror.Wrap(err, "获取用户信息时发生错误")) | ||
} else if user == nil { | ||
r.Response.Status = http.StatusUnauthorized | ||
return | ||
} | ||
userPoll, err := service.User().GetPollUserByUserID(r.GetCtx(), user.Id) | ||
if err != nil { | ||
g.Log().Error(r.GetCtx(), gerror.Wrap(err, "获取用户投票信息时发生错误")) | ||
} else if userPoll == nil { | ||
g.Log().Error(r.GetCtx(), gerror.New("获取用户投票信息时发生错误,用户投票信息为空")) | ||
r.Response.Status = http.StatusInternalServerError | ||
return | ||
} | ||
userPattern := &model.UserPattern{ | ||
Users: *user, | ||
Poll: *userPoll, | ||
Role: service.User().MustGetRoleByUser(r.GetCtx(), user), | ||
Status: service.User().MustGetUserStatusByUser(r.GetCtx(), user), | ||
} | ||
service.BizCtx().SetUser(r.GetCtx(), userPattern) | ||
r.Middleware.Next() | ||
} | ||
|
||
// AuthorizationAdminV1 用于 v1 接口校验用户是否登录且是否具有管理员权限 | ||
func (s *sMiddleware) AuthorizationAdminV1(r *ghttp.Request) { | ||
r.Middleware.Next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/gogf/gf/v2/net/ghttp" | ||
"github.com/hitokoto-osc/reviewer/internal/model" | ||
"github.com/hitokoto-osc/reviewer/internal/service" | ||
) | ||
|
||
// Ctx 用于注入业务上下文,实现单次请求中的业务数据共享 | ||
func (s *sMiddleware) Ctx(r *ghttp.Request) { | ||
customCtx := &model.Context{} | ||
service.BizCtx().Init(r, customCtx) | ||
r.Middleware.Next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.