@@ -26,6 +26,7 @@ import (
2626 "github.com/apache/answer/internal/schema"
2727 "github.com/apache/answer/internal/service/role"
2828 "github.com/apache/answer/internal/service/siteinfo_common"
29+ usercommon "github.com/apache/answer/internal/service/user_common"
2930 "github.com/apache/answer/ui"
3031 "github.com/gin-gonic/gin"
3132
@@ -44,15 +45,22 @@ var ctxUUIDKey = "ctxUuidKey"
4445type AuthUserMiddleware struct {
4546 authService * auth.AuthService
4647 siteInfoCommonService siteinfo_common.SiteInfoCommonService
48+ userRepo usercommon.UserRepo
49+ userRoleService * role.UserRoleRelService
4750}
4851
4952// NewAuthUserMiddleware new auth user middleware
5053func NewAuthUserMiddleware (
5154 authService * auth.AuthService ,
52- siteInfoCommonService siteinfo_common.SiteInfoCommonService ) * AuthUserMiddleware {
55+ siteInfoCommonService siteinfo_common.SiteInfoCommonService ,
56+ userRepo usercommon.UserRepo ,
57+ userRoleService * role.UserRoleRelService ,
58+ ) * AuthUserMiddleware {
5359 return & AuthUserMiddleware {
5460 authService : authService ,
5561 siteInfoCommonService : siteInfoCommonService ,
62+ userRepo : userRepo ,
63+ userRoleService : userRoleService ,
5664 }
5765}
5866
@@ -132,6 +140,29 @@ func (am *AuthUserMiddleware) MustAuthWithoutAccountAvailable() gin.HandlerFunc
132140 }
133141}
134142
143+ // validateUserStatus checks email and user status, writes the error response and aborts if invalid.
144+ // Returns true if the user is valid, false if the request was aborted.
145+ func (am * AuthUserMiddleware ) validateUserStatus (ctx * gin.Context , userInfo * entity.UserCacheInfo ) bool {
146+ if userInfo .EmailStatus != entity .EmailStatusAvailable {
147+ handler .HandleResponse (ctx , errors .Forbidden (reason .EmailNeedToBeVerified ),
148+ & schema.ForbiddenResp {Type : schema .ForbiddenReasonTypeInactive })
149+ ctx .Abort ()
150+ return false
151+ }
152+ if userInfo .UserStatus == entity .UserStatusSuspended {
153+ handler .HandleResponse (ctx , errors .Forbidden (reason .UserSuspended ),
154+ & schema.ForbiddenResp {Type : schema .ForbiddenReasonTypeUserSuspended })
155+ ctx .Abort ()
156+ return false
157+ }
158+ if userInfo .UserStatus == entity .UserStatusDeleted {
159+ handler .HandleResponse (ctx , errors .Unauthorized (reason .UnauthorizedError ), nil )
160+ ctx .Abort ()
161+ return false
162+ }
163+ return true
164+ }
165+
135166// MustAuthAndAccountAvailable auth user info and check user status, only allow active user access.
136167func (am * AuthUserMiddleware ) MustAuthAndAccountAvailable () gin.HandlerFunc {
137168 return func (ctx * gin.Context ) {
@@ -147,21 +178,7 @@ func (am *AuthUserMiddleware) MustAuthAndAccountAvailable() gin.HandlerFunc {
147178 ctx .Abort ()
148179 return
149180 }
150- if userInfo .EmailStatus != entity .EmailStatusAvailable {
151- handler .HandleResponse (ctx , errors .Forbidden (reason .EmailNeedToBeVerified ),
152- & schema.ForbiddenResp {Type : schema .ForbiddenReasonTypeInactive })
153- ctx .Abort ()
154- return
155- }
156- if userInfo .UserStatus == entity .UserStatusSuspended {
157- handler .HandleResponse (ctx , errors .Forbidden (reason .UserSuspended ),
158- & schema.ForbiddenResp {Type : schema .ForbiddenReasonTypeUserSuspended })
159- ctx .Abort ()
160- return
161- }
162- if userInfo .UserStatus == entity .UserStatusDeleted {
163- handler .HandleResponse (ctx , errors .Unauthorized (reason .UnauthorizedError ), nil )
164- ctx .Abort ()
181+ if ! am .validateUserStatus (ctx , userInfo ) {
165182 return
166183 }
167184 ctx .Set (ctxUUIDKey , userInfo )
0 commit comments