This is an automated email from the ASF dual-hosted git repository.

linkinstar pushed a commit to branch fix/2.0.1/chat
in repository https://gitbox.apache.org/repos/asf/answer.git


The following commit(s) were added to refs/heads/fix/2.0.1/chat by this push:
     new 869b040e fix(auth): enhance admin user cache management and add status 
checks for email verification and suspension
869b040e is described below

commit 869b040e92d66f344ca9f581592aa8044d399828
Author: LinkinStars <[email protected]>
AuthorDate: Fri Feb 6 20:31:54 2026 +0800

    fix(auth): enhance admin user cache management and add status checks for 
email verification and suspension
---
 internal/base/middleware/auth.go | 15 +++++++++++++++
 internal/service/auth/auth.go    | 34 +++++++++++++++++++++++++++++++++-
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/internal/base/middleware/auth.go b/internal/base/middleware/auth.go
index 57bbaae2..7bfd5e7f 100644
--- a/internal/base/middleware/auth.go
+++ b/internal/base/middleware/auth.go
@@ -184,7 +184,22 @@ func (am *AuthUserMiddleware) AdminAuth() gin.HandlerFunc {
                        return
                }
                if userInfo != nil {
+                       if userInfo.EmailStatus == 
entity.EmailStatusToBeVerified {
+                               _ = 
am.authService.RemoveAdminUserCacheInfo(ctx, token)
+                               handler.HandleResponse(ctx, 
errors.Forbidden(reason.EmailNeedToBeVerified),
+                                       &schema.ForbiddenResp{Type: 
schema.ForbiddenReasonTypeInactive})
+                               ctx.Abort()
+                               return
+                       }
+                       if userInfo.UserStatus == entity.UserStatusSuspended {
+                               _ = 
am.authService.RemoveAdminUserCacheInfo(ctx, token)
+                               handler.HandleResponse(ctx, 
errors.Forbidden(reason.UserSuspended),
+                                       &schema.ForbiddenResp{Type: 
schema.ForbiddenReasonTypeUserSuspended})
+                               ctx.Abort()
+                               return
+                       }
                        if userInfo.UserStatus == entity.UserStatusDeleted {
+                               _ = 
am.authService.RemoveAdminUserCacheInfo(ctx, token)
                                handler.HandleResponse(ctx, 
errors.Unauthorized(reason.UnauthorizedError), nil)
                                ctx.Abort()
                                return
diff --git a/internal/service/auth/auth.go b/internal/service/auth/auth.go
index 8f539bf1..7d275105 100644
--- a/internal/service/auth/auth.go
+++ b/internal/service/auth/auth.go
@@ -145,7 +145,39 @@ func (as *AuthService) RemoveTokensExceptCurrentUser(ctx 
context.Context, userID
 // Admin
 
 func (as *AuthService) GetAdminUserCacheInfo(ctx context.Context, accessToken 
string) (userInfo *entity.UserCacheInfo, err error) {
-       return as.authRepo.GetAdminUserCacheInfo(ctx, accessToken)
+       adminCacheInfo, err := as.authRepo.GetAdminUserCacheInfo(ctx, 
accessToken)
+       if err != nil {
+               return nil, err
+       }
+       if adminCacheInfo == nil {
+               return nil, nil
+       }
+
+       // Keep admin authorization aligned with user-token lifecycle and 
status refresh.
+       refreshedUserCacheInfo, err := as.GetUserCacheInfo(ctx, accessToken)
+       if err != nil {
+               return nil, err
+       }
+       if refreshedUserCacheInfo == nil {
+               if err = as.authRepo.RemoveAdminUserCacheInfo(ctx, 
accessToken); err != nil {
+                       return nil, err
+               }
+               return nil, nil
+       }
+
+       adminCacheInfo.UserStatus = refreshedUserCacheInfo.UserStatus
+       adminCacheInfo.EmailStatus = refreshedUserCacheInfo.EmailStatus
+       if refreshedUserCacheInfo.RoleID > 0 {
+               adminCacheInfo.RoleID = refreshedUserCacheInfo.RoleID
+       }
+       if len(refreshedUserCacheInfo.ExternalID) > 0 {
+               adminCacheInfo.ExternalID = refreshedUserCacheInfo.ExternalID
+       }
+
+       if err = as.authRepo.SetAdminUserCacheInfo(ctx, accessToken, 
adminCacheInfo); err != nil {
+               return nil, err
+       }
+       return adminCacheInfo, nil
 }
 
 func (as *AuthService) SetAdminUserCacheInfo(ctx context.Context, accessToken 
string, userInfo *entity.UserCacheInfo) (err error) {

Reply via email to