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.
update CheckPrivateMode
aichy126581bfc1cbe28
diff --git a/cmd/wire_gen.go b/cmd/wire_gen.goindex 61897164..e482d45a 100644--- a/cmd/wire_gen.go+++ b/cmd/wire_gen.go@@ -223,7 +223,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,shortIDMiddleware := middleware.NewShortIDMiddleware(siteInfoCommonService)templateRenderController := templaterender.NewTemplateRenderController(questionService, userService, tagService, answerService, commentService, siteInfoCommonService, questionRepo)templateController := controller.NewTemplateController(templateRenderController, siteInfoCommonService)- templateRouter := router.NewTemplateRouter(templateController, templateRenderController, siteInfoController)+ templateRouter := router.NewTemplateRouter(templateController, templateRenderController, siteInfoController, authUserMiddleware)connectorController := controller.NewConnectorController(siteInfoCommonService, emailService, userExternalLoginService)userCenterLoginService := user_external_login2.NewUserCenterLoginService(userRepo, userCommon, userExternalLoginRepo, userActiveActivityRepo, siteInfoCommonService)userCenterController := controller.NewUserCenterController(userCenterLoginService, siteInfoCommonService)diff --git a/internal/base/middleware/auth.go b/internal/base/middleware/auth.goindex 64e59dc1..24caa0e3 100644--- a/internal/base/middleware/auth.go+++ b/internal/base/middleware/auth.go@@ -1,6 +1,7 @@package middlewareimport (+ "net/http""strings""github.com/answerdev/answer/internal/schema"@@ -140,6 +141,28 @@ func (am *AuthUserMiddleware) AdminAuth() gin.HandlerFunc {}}+func (am *AuthUserMiddleware) CheckPrivateMode() gin.HandlerFunc {+ return func(ctx *gin.Context) {+ userLoginUrl := "/users/login"+ if ctx.Request.URL.Path == userLoginUrl {+ ctx.Next()+ return+ }+ resp, err := am.siteInfoCommonService.GetSiteLogin(ctx)+ if err != nil {+ ctx.Redirect(http.StatusTemporaryRedirect, userLoginUrl)+ ctx.Abort()+ return+ }+ if resp.LoginRequired {+ ctx.Redirect(http.StatusTemporaryRedirect, userLoginUrl)+ ctx.Abort()+ return+ }+ ctx.Next()+ }+}+// GetLoginUserIDFromContext get user id from contextfunc GetLoginUserIDFromContext(ctx *gin.Context) (userID string) {userInfo := GetUserInfoFromContext(ctx)diff --git a/internal/router/template_router.go b/internal/router/template_router.goindex e90dc7ee..23f1138c 100644--- a/internal/router/template_router.go+++ b/internal/router/template_router.go@@ -1,6 +1,7 @@package routerimport (+ "github.com/answerdev/answer/internal/base/middleware""github.com/answerdev/answer/internal/controller"templaterender "github.com/answerdev/answer/internal/controller/template_render""github.com/answerdev/answer/internal/controller_admin"@@ -11,17 +12,21 @@ type TemplateRouter struct {templateController *controller.TemplateControllertemplateRenderController *templaterender.TemplateRenderControllersiteInfoController *controller_admin.SiteInfoController+ authUserMiddleware *middleware.AuthUserMiddleware}func NewTemplateRouter(templateController *controller.TemplateController,templateRenderController *templaterender.TemplateRenderController,siteInfoController *controller_admin.SiteInfoController,+ authUserMiddleware *middleware.AuthUserMiddleware,+) *TemplateRouter {return &TemplateRouter{templateController: templateController,templateRenderController: templateRenderController,siteInfoController: siteInfoController,+ authUserMiddleware: authUserMiddleware,}}@@ -33,15 +38,17 @@ func (a *TemplateRouter) RegisterTemplateRouter(r *gin.RouterGroup) {r.GET("/robots.txt", a.siteInfoController.GetRobots)r.GET("/custom.css", a.siteInfoController.GetCss)- r.GET("/", a.templateController.Index)-- r.GET("/questions", a.templateController.QuestionList)- r.GET("/questions/:id", a.templateController.QuestionInfo)- r.GET("/questions/:id/:title", a.templateController.QuestionInfo)- r.GET("/questions/:id/:title/:answerid", a.templateController.QuestionInfo)-- r.GET("/tags", a.templateController.TagList)- r.GET("/tags/:tag", a.templateController.TagInfo)- r.GET("/users/:username", a.templateController.UserInfo)r.GET("/404", a.templateController.Page404)++ //todo add middleware+ seo := r.Group("")+ seo.Use(a.authUserMiddleware.CheckPrivateMode())+ seo.GET("/", a.templateController.Index)+ seo.GET("/questions", a.templateController.QuestionList)+ seo.GET("/questions/:id", a.templateController.QuestionInfo)+ seo.GET("/questions/:id/:title", a.templateController.QuestionInfo)+ seo.GET("/questions/:id/:title/:answerid", a.templateController.QuestionInfo)+ seo.GET("/tags", a.templateController.TagList)+ seo.GET("/tags/:tag", a.templateController.TagInfo)+ seo.GET("/users/:username", a.templateController.UserInfo)}