A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
feat(usercenter): add user login API agent
LinkinStarsb5ef7912d134
diff --git a/internal/base/middleware/user_center_plugin_auth.go b/internal/base/middleware/user_center_plugin_auth.goindex c6a190a4..bf19c319 100644--- a/internal/base/middleware/user_center_plugin_auth.go+++ b/internal/base/middleware/user_center_plugin_auth.go@@ -8,9 +8,13 @@ import ("github.com/segmentfault/pacman/errors")-// BanAPIWhenUserCenterEnabled ban api when user center enabled-func BanAPIWhenUserCenterEnabled(ctx *gin.Context) {- if plugin.UserCenterEnabled() {+// BanAPIForUserCenter ban api for user center+func BanAPIForUserCenter(ctx *gin.Context) {+ uc, ok := plugin.GetUserCenter()+ if !ok {+ return+ }+ if !uc.Description().EnabledOriginalUserSystem {handler.HandleResponse(ctx, errors.Forbidden(reason.ForbiddenError), nil)ctx.Abort()returndiff --git a/internal/controller/connector_controller.go b/internal/controller/connector_controller.goindex 160c0f48..bb6c37ad 100644--- a/internal/controller/connector_controller.go+++ b/internal/controller/connector_controller.go@@ -130,7 +130,7 @@ func (cc *ConnectorController) ConnectorRedirect(connector plugin.Connector) (fnreturn}if len(resp.AccessToken) > 0 {- ctx.Redirect(http.StatusFound, fmt.Sprintf("%s/users/oauth?access_token=%s",+ ctx.Redirect(http.StatusFound, fmt.Sprintf("%s/users/auth-landing?access_token=%s",siteGeneral.SiteUrl, resp.AccessToken))} else {ctx.Redirect(http.StatusFound, fmt.Sprintf("%s/users/confirm-email?binding_key=%s",diff --git a/internal/controller/plugin_user_center_controller.go b/internal/controller/plugin_user_center_controller.goindex 231cdecd..23a8df57 100644--- a/internal/controller/plugin_user_center_controller.go+++ b/internal/controller/plugin_user_center_controller.go@@ -63,6 +63,7 @@ func (uc *UserCenterController) UserCenterAgent(ctx *gin.Context) {resp.AgentInfo.Icon = info.Iconresp.AgentInfo.Url = info.Urlresp.AgentInfo.ControlCenterItems = make([]*schema.ControlCenter, 0)+ resp.AgentInfo.EnabledOriginalUserSystem = info.EnabledOriginalUserSystemitems := uc.ControlCenterItems()for _, item := range items {resp.AgentInfo.ControlCenterItems = append(resp.AgentInfo.ControlCenterItems, &schema.ControlCenter{@@ -141,7 +142,7 @@ func (uc *UserCenterController) UserCenterLoginCallback(ctx *gin.Context) {return}userCenter.AfterLogin(userInfo.ExternalID, resp.AccessToken)- ctx.Redirect(http.StatusFound, fmt.Sprintf("%s/users/oauth?access_token=%s",+ ctx.Redirect(http.StatusFound, fmt.Sprintf("%s/users/auth-landing?access_token=%s",siteGeneral.SiteUrl, resp.AccessToken))}@@ -176,7 +177,7 @@ func (uc *UserCenterController) UserCenterSignUpCallback(ctx *gin.Context) {return}userCenter.AfterLogin(userInfo.ExternalID, resp.AccessToken)- ctx.Redirect(http.StatusFound, fmt.Sprintf("%s/users/oauth?access_token=%s",+ ctx.Redirect(http.StatusFound, fmt.Sprintf("%s/users/auth-landing?access_token=%s",siteGeneral.SiteUrl, resp.AccessToken))}diff --git a/internal/router/answer_api_router.go b/internal/router/answer_api_router.goindex f15a6897..1592e70e 100644--- a/internal/router/answer_api_router.go+++ b/internal/router/answer_api_router.go@@ -102,8 +102,8 @@ func (a *AnswerAPIRouter) RegisterMustUnAuthAnswerAPIRouter(r *gin.RouterGroup)// userr.GET("/user/info", a.userController.GetUserInfoByUserID)- routerGroup := r.Group("", middleware.BanAPIWhenUserCenterEnabled)- r.POST("/user/login/email", a.userController.UserEmailLogin)+ routerGroup := r.Group("", middleware.BanAPIForUserCenter)+ routerGroup.POST("/user/login/email", a.userController.UserEmailLogin)routerGroup.POST("/user/register/email", a.userController.UserRegisterByEmail)routerGroup.GET("/user/register/captcha", a.userController.UserRegisterCaptcha)routerGroup.POST("/user/email/verification", a.userController.UserVerifyEmail)@@ -117,8 +117,8 @@ func (a *AnswerAPIRouter) RegisterMustUnAuthAnswerAPIRouter(r *gin.RouterGroup)func (a *AnswerAPIRouter) RegisterUnAuthAnswerAPIRouter(r *gin.RouterGroup) {// userr.GET("/user/logout", a.userController.UserLogout)- r.POST("/user/email/change/code", middleware.BanAPIWhenUserCenterEnabled, a.userController.UserChangeEmailSendCode)- r.POST("/user/email/verification/send", middleware.BanAPIWhenUserCenterEnabled, a.userController.UserVerifyEmailSend)+ 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)@@ -205,7 +205,7 @@ func (a *AnswerAPIRouter) RegisterAnswerAPIRouter(r *gin.RouterGroup) {r.DELETE("/answer", a.answerController.RemoveAnswer)// user- r.PUT("/user/password", middleware.BanAPIWhenUserCenterEnabled, a.userController.UserModifyPassWord)+ r.PUT("/user/password", middleware.BanAPIForUserCenter, a.userController.UserModifyPassWord)r.PUT("/user/info", a.userController.UserUpdateInfo)r.PUT("/user/interface", a.userController.UserUpdateInterface)r.POST("/user/notice/set", a.userController.UserNoticeSet)@@ -247,8 +247,8 @@ func (a *AnswerAPIRouter) RegisterAnswerAdminAPIRouter(r *gin.RouterGroup) {r.GET("/users/page", a.adminUserController.GetUserPage)r.PUT("/user/status", a.adminUserController.UpdateUserStatus)r.PUT("/user/role", a.adminUserController.UpdateUserRole)- r.POST("/user", middleware.BanAPIWhenUserCenterEnabled, a.adminUserController.AddUser)- r.PUT("/user/password", middleware.BanAPIWhenUserCenterEnabled, a.adminUserController.UpdateUserPassword)+ r.POST("/user", middleware.BanAPIForUserCenter, a.adminUserController.AddUser)+ r.PUT("/user/password", middleware.BanAPIForUserCenter, a.adminUserController.UpdateUserPassword)// reasonr.GET("/reasons", a.reasonController.Reasons)diff --git a/internal/schema/plugin_user_center.go b/internal/schema/plugin_user_center.goindex afa4727c..a9d0e0d3 100644--- a/internal/schema/plugin_user_center.go+++ b/internal/schema/plugin_user_center.go@@ -6,12 +6,13 @@ type UserCenterAgentResp struct {}type AgentInfo struct {- Name string `json:"name"`- Icon string `json:"icon"`- Url string `json:"url"`- LoginRedirectURL string `json:"login_redirect_url"`- SignUpRedirectURL string `json:"sign_up_redirect_url"`- ControlCenterItems []*ControlCenter `json:"control_center"`+ Name string `json:"name"`+ Icon string `json:"icon"`+ Url string `json:"url"`+ LoginRedirectURL string `json:"login_redirect_url"`+ SignUpRedirectURL string `json:"sign_up_redirect_url"`+ ControlCenterItems []*ControlCenter `json:"control_center"`+ EnabledOriginalUserSystem bool `json:"enabled_original_user_system"`}type ControlCenter struct {diff --git a/internal/schema/user_external_login_schema.go b/internal/schema/user_external_login_schema.goindex 0474f58a..0998aea7 100644--- a/internal/schema/user_external_login_schema.go+++ b/internal/schema/user_external_login_schema.go@@ -68,7 +68,8 @@ type UserCenterUserSettingsResp struct {}type UserCenterAdminFunctionAgentResp struct {- UserStatusAgentEnabled bool `json:"user_status_agent_enabled"`+ UserStatusAgentEnabled bool `json:"user_status_agent_enabled"`+ UserPasswordAgentEnabled bool `json:"user_password_agent_enabled"`}type UserSettingAgent struct {diff --git a/internal/service/user_external_login/user_center_login_service.go b/internal/service/user_external_login/user_center_login_service.goindex 0d3ccf43..6bf50fae 100644--- a/internal/service/user_external_login/user_center_login_service.go+++ b/internal/service/user_external_login/user_center_login_service.go@@ -203,7 +203,11 @@ func (us *UserCenterLoginService) UserCenterAdminFunctionAgent(ctx context.Contereturn}desc := userCenter.Description()+ // If user status agent is enabled, admin can not update user status in answer.resp.UserStatusAgentEnabled = desc.UserStatusAgentEnabled+ // If original user system is enabled, admin can update user password in answer.+ // So user password agent is disabled.+ resp.UserPasswordAgentEnabled = !desc.EnabledOriginalUserSystemreturn resp, nil}diff --git a/plugin/user_center.go b/plugin/user_center.goindex 45501b24..6c963e24 100644--- a/plugin/user_center.go+++ b/plugin/user_center.go@@ -25,14 +25,15 @@ type UserCenter interface {}type UserCenterDesc struct {- Name string `json:"name"`- Icon string `json:"icon"`- Url string `json:"url"`- LoginRedirectURL string `json:"login_redirect_url"`- SignUpRedirectURL string `json:"sign_up_redirect_url"`- RankAgentEnabled bool `json:"rank_agent_enabled"`- UserStatusAgentEnabled bool `json:"user_status_agent_enabled"`- MustAuthEmailEnabled bool `json:"must_auth_email_enabled"`+ Name string `json:"name"`+ Icon string `json:"icon"`+ Url string `json:"url"`+ LoginRedirectURL string `json:"login_redirect_url"`+ SignUpRedirectURL string `json:"sign_up_redirect_url"`+ RankAgentEnabled bool `json:"rank_agent_enabled"`+ UserStatusAgentEnabled bool `json:"user_status_agent_enabled"`+ MustAuthEmailEnabled bool `json:"must_auth_email_enabled"`+ EnabledOriginalUserSystem bool `json:"enabled_original_user_system"`}type UserStatus int