You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@answer.apache.org by li...@apache.org on 2024/01/11 06:28:34 UTC

(incubator-answer) branch fix/1.2.5/user created (now 661a1c22)

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

linkinstar pushed a change to branch fix/1.2.5/user
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git


      at 661a1c22 fix(user): move logout and change email router

This branch includes the following new commits:

     new 661a1c22 fix(user): move logout and change email router

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(incubator-answer) 01/01: fix(user): move logout and change email router

Posted by li...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

linkinstar pushed a commit to branch fix/1.2.5/user
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git

commit 661a1c225bec9b064ea408a4a14ea9e3d331c914
Author: LinkinStars <li...@foxmail.com>
AuthorDate: Thu Jan 11 14:28:25 2024 +0800

    fix(user): move logout and change email router
---
 internal/base/middleware/auth.go     | 41 ++++++++++++++++++++++++++++++++----
 internal/base/server/http.go         |  7 +++++-
 internal/router/answer_api_router.go |  9 +++++---
 3 files changed, 49 insertions(+), 8 deletions(-)

diff --git a/internal/base/middleware/auth.go b/internal/base/middleware/auth.go
index 0fdc2c42..44fd20a2 100644
--- a/internal/base/middleware/auth.go
+++ b/internal/base/middleware/auth.go
@@ -89,18 +89,51 @@ func (am *AuthUserMiddleware) EjectUserBySiteInfo() gin.HandlerFunc {
 			return
 		}
 
-		_, isLogin := ctx.Get(ctxUUIDKey)
-		if !isLogin {
+		// If site in private mode, user must login.
+		userInfo := GetUserInfoFromContext(ctx)
+		if userInfo == nil {
 			handler.HandleResponse(ctx, errors.Unauthorized(reason.UnauthorizedError), nil)
 			ctx.Abort()
 			return
 		}
+		// If user is not active, eject user.
+		if userInfo.EmailStatus != entity.EmailStatusAvailable {
+			handler.HandleResponse(ctx, errors.Forbidden(reason.EmailNeedToBeVerified),
+				&schema.ForbiddenResp{Type: schema.ForbiddenReasonTypeInactive})
+			ctx.Abort()
+			return
+		}
+		ctx.Next()
+	}
+}
+
+// MustAuthWithoutAccountAvailable auth user info, any login user can access though user is not active.
+func (am *AuthUserMiddleware) MustAuthWithoutAccountAvailable() gin.HandlerFunc {
+	return func(ctx *gin.Context) {
+		token := ExtractToken(ctx)
+		if len(token) == 0 {
+			handler.HandleResponse(ctx, errors.Unauthorized(reason.UnauthorizedError), nil)
+			ctx.Abort()
+			return
+		}
+		userInfo, err := am.authService.GetUserCacheInfo(ctx, token)
+		if err != nil || userInfo == nil {
+			handler.HandleResponse(ctx, errors.Unauthorized(reason.UnauthorizedError), nil)
+			ctx.Abort()
+			return
+		}
+		if userInfo.UserStatus == entity.UserStatusDeleted {
+			handler.HandleResponse(ctx, errors.Unauthorized(reason.UnauthorizedError), nil)
+			ctx.Abort()
+			return
+		}
+		ctx.Set(ctxUUIDKey, userInfo)
 		ctx.Next()
 	}
 }
 
-// MustAuth auth user info. If the user does not log in, an unauthenticated error is displayed
-func (am *AuthUserMiddleware) MustAuth() gin.HandlerFunc {
+// MustAuthAndAccountAvailable auth user info and check user status, only allow active user access.
+func (am *AuthUserMiddleware) MustAuthAndAccountAvailable() gin.HandlerFunc {
 	return func(ctx *gin.Context) {
 		token := ExtractToken(ctx)
 		if len(token) == 0 {
diff --git a/internal/base/server/http.go b/internal/base/server/http.go
index 6e0b6727..daaf90fb 100644
--- a/internal/base/server/http.go
+++ b/internal/base/server/http.go
@@ -74,9 +74,14 @@ func NewHTTPServer(debug bool,
 	unAuthV1.Use(authUserMiddleware.Auth(), authUserMiddleware.EjectUserBySiteInfo())
 	answerRouter.RegisterUnAuthAnswerAPIRouter(unAuthV1)
 
+	// register api that must be authenticated but no need to check account status
+	authWithoutStatusV1 := r.Group("/answer/api/v1")
+	authWithoutStatusV1.Use(authUserMiddleware.MustAuthWithoutAccountAvailable())
+	answerRouter.RegisterAuthUserWithAnyStatusAnswerAPIRouter(authWithoutStatusV1)
+
 	// register api that must be authenticated
 	authV1 := r.Group("/answer/api/v1")
-	authV1.Use(authUserMiddleware.MustAuth())
+	authV1.Use(authUserMiddleware.MustAuthAndAccountAvailable())
 	answerRouter.RegisterAnswerAPIRouter(authV1)
 
 	adminauthV1 := r.Group("/answer/admin/api")
diff --git a/internal/router/answer_api_router.go b/internal/router/answer_api_router.go
index 0a6d7b83..d5085fb6 100644
--- a/internal/router/answer_api_router.go
+++ b/internal/router/answer_api_router.go
@@ -141,9 +141,6 @@ func (a *AnswerAPIRouter) RegisterMustUnAuthAnswerAPIRouter(authUserMiddleware *
 
 func (a *AnswerAPIRouter) RegisterUnAuthAnswerAPIRouter(r *gin.RouterGroup) {
 	// user
-	r.GET("/user/logout", a.userController.UserLogout)
-	r.POST("/user/email/change/code", middleware.BanAPIForUserCenter, a.userController.UserChangeEmailSendCode)
-	r.POST("/user/email/verification/send", middleware.BanAPIForUserCenter, a.userController.UserVerifyEmailSend)
 	r.GET("/personal/user/info", a.userController.GetOtherUserInfoByUsername)
 	r.GET("/user/ranking", a.userController.UserRanking)
 
@@ -183,6 +180,12 @@ func (a *AnswerAPIRouter) RegisterUnAuthAnswerAPIRouter(r *gin.RouterGroup) {
 	r.GET("/personal/rank/page", a.rankController.GetRankPersonalWithPage)
 }
 
+func (a *AnswerAPIRouter) RegisterAuthUserWithAnyStatusAnswerAPIRouter(r *gin.RouterGroup) {
+	r.GET("/user/logout", a.userController.UserLogout)
+	r.POST("/user/email/change/code", middleware.BanAPIForUserCenter, a.userController.UserChangeEmailSendCode)
+	r.POST("/user/email/verification/send", middleware.BanAPIForUserCenter, a.userController.UserVerifyEmailSend)
+}
+
 func (a *AnswerAPIRouter) RegisterAnswerAPIRouter(r *gin.RouterGroup) {
 	// revisions
 	r.GET("/revisions/unreviewed", a.revisionController.GetUnreviewedRevisionList)