mirror of https://gitee.com/answerdev/answer.git
feat(usercenter): add user login API agent
This commit is contained in:
parent
9e0999ba55
commit
b5ef7912d1
|
@ -8,9 +8,13 @@ import (
|
||||||
"github.com/segmentfault/pacman/errors"
|
"github.com/segmentfault/pacman/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BanAPIWhenUserCenterEnabled ban api when user center enabled
|
// BanAPIForUserCenter ban api for user center
|
||||||
func BanAPIWhenUserCenterEnabled(ctx *gin.Context) {
|
func BanAPIForUserCenter(ctx *gin.Context) {
|
||||||
if plugin.UserCenterEnabled() {
|
uc, ok := plugin.GetUserCenter()
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !uc.Description().EnabledOriginalUserSystem {
|
||||||
handler.HandleResponse(ctx, errors.Forbidden(reason.ForbiddenError), nil)
|
handler.HandleResponse(ctx, errors.Forbidden(reason.ForbiddenError), nil)
|
||||||
ctx.Abort()
|
ctx.Abort()
|
||||||
return
|
return
|
||||||
|
|
|
@ -130,7 +130,7 @@ func (cc *ConnectorController) ConnectorRedirect(connector plugin.Connector) (fn
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(resp.AccessToken) > 0 {
|
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))
|
siteGeneral.SiteUrl, resp.AccessToken))
|
||||||
} else {
|
} else {
|
||||||
ctx.Redirect(http.StatusFound, fmt.Sprintf("%s/users/confirm-email?binding_key=%s",
|
ctx.Redirect(http.StatusFound, fmt.Sprintf("%s/users/confirm-email?binding_key=%s",
|
||||||
|
|
|
@ -63,6 +63,7 @@ func (uc *UserCenterController) UserCenterAgent(ctx *gin.Context) {
|
||||||
resp.AgentInfo.Icon = info.Icon
|
resp.AgentInfo.Icon = info.Icon
|
||||||
resp.AgentInfo.Url = info.Url
|
resp.AgentInfo.Url = info.Url
|
||||||
resp.AgentInfo.ControlCenterItems = make([]*schema.ControlCenter, 0)
|
resp.AgentInfo.ControlCenterItems = make([]*schema.ControlCenter, 0)
|
||||||
|
resp.AgentInfo.EnabledOriginalUserSystem = info.EnabledOriginalUserSystem
|
||||||
items := uc.ControlCenterItems()
|
items := uc.ControlCenterItems()
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
resp.AgentInfo.ControlCenterItems = append(resp.AgentInfo.ControlCenterItems, &schema.ControlCenter{
|
resp.AgentInfo.ControlCenterItems = append(resp.AgentInfo.ControlCenterItems, &schema.ControlCenter{
|
||||||
|
@ -141,7 +142,7 @@ func (uc *UserCenterController) UserCenterLoginCallback(ctx *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userCenter.AfterLogin(userInfo.ExternalID, resp.AccessToken)
|
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))
|
siteGeneral.SiteUrl, resp.AccessToken))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,7 +177,7 @@ func (uc *UserCenterController) UserCenterSignUpCallback(ctx *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userCenter.AfterLogin(userInfo.ExternalID, resp.AccessToken)
|
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))
|
siteGeneral.SiteUrl, resp.AccessToken))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -102,8 +102,8 @@ func (a *AnswerAPIRouter) RegisterMustUnAuthAnswerAPIRouter(r *gin.RouterGroup)
|
||||||
|
|
||||||
// user
|
// user
|
||||||
r.GET("/user/info", a.userController.GetUserInfoByUserID)
|
r.GET("/user/info", a.userController.GetUserInfoByUserID)
|
||||||
routerGroup := r.Group("", middleware.BanAPIWhenUserCenterEnabled)
|
routerGroup := r.Group("", middleware.BanAPIForUserCenter)
|
||||||
r.POST("/user/login/email", a.userController.UserEmailLogin)
|
routerGroup.POST("/user/login/email", a.userController.UserEmailLogin)
|
||||||
routerGroup.POST("/user/register/email", a.userController.UserRegisterByEmail)
|
routerGroup.POST("/user/register/email", a.userController.UserRegisterByEmail)
|
||||||
routerGroup.GET("/user/register/captcha", a.userController.UserRegisterCaptcha)
|
routerGroup.GET("/user/register/captcha", a.userController.UserRegisterCaptcha)
|
||||||
routerGroup.POST("/user/email/verification", a.userController.UserVerifyEmail)
|
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) {
|
func (a *AnswerAPIRouter) RegisterUnAuthAnswerAPIRouter(r *gin.RouterGroup) {
|
||||||
// user
|
// user
|
||||||
r.GET("/user/logout", a.userController.UserLogout)
|
r.GET("/user/logout", a.userController.UserLogout)
|
||||||
r.POST("/user/email/change/code", middleware.BanAPIWhenUserCenterEnabled, a.userController.UserChangeEmailSendCode)
|
r.POST("/user/email/change/code", middleware.BanAPIForUserCenter, a.userController.UserChangeEmailSendCode)
|
||||||
r.POST("/user/email/verification/send", middleware.BanAPIWhenUserCenterEnabled, a.userController.UserVerifyEmailSend)
|
r.POST("/user/email/verification/send", middleware.BanAPIForUserCenter, a.userController.UserVerifyEmailSend)
|
||||||
r.GET("/personal/user/info", a.userController.GetOtherUserInfoByUsername)
|
r.GET("/personal/user/info", a.userController.GetOtherUserInfoByUsername)
|
||||||
r.GET("/user/ranking", a.userController.UserRanking)
|
r.GET("/user/ranking", a.userController.UserRanking)
|
||||||
|
|
||||||
|
@ -205,7 +205,7 @@ func (a *AnswerAPIRouter) RegisterAnswerAPIRouter(r *gin.RouterGroup) {
|
||||||
r.DELETE("/answer", a.answerController.RemoveAnswer)
|
r.DELETE("/answer", a.answerController.RemoveAnswer)
|
||||||
|
|
||||||
// user
|
// 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/info", a.userController.UserUpdateInfo)
|
||||||
r.PUT("/user/interface", a.userController.UserUpdateInterface)
|
r.PUT("/user/interface", a.userController.UserUpdateInterface)
|
||||||
r.POST("/user/notice/set", a.userController.UserNoticeSet)
|
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.GET("/users/page", a.adminUserController.GetUserPage)
|
||||||
r.PUT("/user/status", a.adminUserController.UpdateUserStatus)
|
r.PUT("/user/status", a.adminUserController.UpdateUserStatus)
|
||||||
r.PUT("/user/role", a.adminUserController.UpdateUserRole)
|
r.PUT("/user/role", a.adminUserController.UpdateUserRole)
|
||||||
r.POST("/user", middleware.BanAPIWhenUserCenterEnabled, a.adminUserController.AddUser)
|
r.POST("/user", middleware.BanAPIForUserCenter, a.adminUserController.AddUser)
|
||||||
r.PUT("/user/password", middleware.BanAPIWhenUserCenterEnabled, a.adminUserController.UpdateUserPassword)
|
r.PUT("/user/password", middleware.BanAPIForUserCenter, a.adminUserController.UpdateUserPassword)
|
||||||
|
|
||||||
// reason
|
// reason
|
||||||
r.GET("/reasons", a.reasonController.Reasons)
|
r.GET("/reasons", a.reasonController.Reasons)
|
||||||
|
|
|
@ -6,12 +6,13 @@ type UserCenterAgentResp struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type AgentInfo struct {
|
type AgentInfo struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Icon string `json:"icon"`
|
Icon string `json:"icon"`
|
||||||
Url string `json:"url"`
|
Url string `json:"url"`
|
||||||
LoginRedirectURL string `json:"login_redirect_url"`
|
LoginRedirectURL string `json:"login_redirect_url"`
|
||||||
SignUpRedirectURL string `json:"sign_up_redirect_url"`
|
SignUpRedirectURL string `json:"sign_up_redirect_url"`
|
||||||
ControlCenterItems []*ControlCenter `json:"control_center"`
|
ControlCenterItems []*ControlCenter `json:"control_center"`
|
||||||
|
EnabledOriginalUserSystem bool `json:"enabled_original_user_system"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ControlCenter struct {
|
type ControlCenter struct {
|
||||||
|
|
|
@ -68,7 +68,8 @@ type UserCenterUserSettingsResp struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserCenterAdminFunctionAgentResp 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 {
|
type UserSettingAgent struct {
|
||||||
|
|
|
@ -203,7 +203,11 @@ func (us *UserCenterLoginService) UserCenterAdminFunctionAgent(ctx context.Conte
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
desc := userCenter.Description()
|
desc := userCenter.Description()
|
||||||
|
// If user status agent is enabled, admin can not update user status in answer.
|
||||||
resp.UserStatusAgentEnabled = desc.UserStatusAgentEnabled
|
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.EnabledOriginalUserSystem
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,14 +25,15 @@ type UserCenter interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserCenterDesc struct {
|
type UserCenterDesc struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Icon string `json:"icon"`
|
Icon string `json:"icon"`
|
||||||
Url string `json:"url"`
|
Url string `json:"url"`
|
||||||
LoginRedirectURL string `json:"login_redirect_url"`
|
LoginRedirectURL string `json:"login_redirect_url"`
|
||||||
SignUpRedirectURL string `json:"sign_up_redirect_url"`
|
SignUpRedirectURL string `json:"sign_up_redirect_url"`
|
||||||
RankAgentEnabled bool `json:"rank_agent_enabled"`
|
RankAgentEnabled bool `json:"rank_agent_enabled"`
|
||||||
UserStatusAgentEnabled bool `json:"user_status_agent_enabled"`
|
UserStatusAgentEnabled bool `json:"user_status_agent_enabled"`
|
||||||
MustAuthEmailEnabled bool `json:"must_auth_email_enabled"`
|
MustAuthEmailEnabled bool `json:"must_auth_email_enabled"`
|
||||||
|
EnabledOriginalUserSystem bool `json:"enabled_original_user_system"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserStatus int
|
type UserStatus int
|
||||||
|
|
Loading…
Reference in New Issue