Skip to content

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(login): add must login middle

LinkinStarc42b3e1c3afc
diff --git a/cmd/answer/wire_gen.go b/cmd/answer/wire_gen.go
index 64dbd4be..f6aaec4f 100644
--- a/cmd/answer/wire_gen.go
+++ b/cmd/answer/wire_gen.go
@@ -204,7 +204,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
answerAPIRouter := router.NewAnswerAPIRouter(langController, userController, commentController, reportController, voteController, tagController, followController, collectionController, questionController, answerController, searchController, revisionController, rankController, controller_backyardReportController, userBackyardController, reasonController, themeController, siteInfoController, siteinfoController, notificationController, dashboardController, uploadController, activityController, roleController)
swaggerRouter := router.NewSwaggerRouter(swaggerConf)
uiRouter := router.NewUIRouter(siteinfoController)
- authUserMiddleware := middleware.NewAuthUserMiddleware(authService)
+ authUserMiddleware := middleware.NewAuthUserMiddleware(authService, siteInfoCommonService)
avatarMiddleware := middleware.NewAvatarMiddleware(serviceConf, uploaderService)
templateRenderController := templaterender.NewTemplateRenderController(questionService, userService, tagService, answerService, commentService)
templateController := controller.NewTemplateController(templateRenderController, siteInfoCommonService)
diff --git a/docs/docs.go b/docs/docs.go
index 78e53c0a..ef144a70 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -105,7 +105,7 @@ const docTemplate = `{
"in": "body",
"required": true,
"schema": {
- "$ref": "#/definitions/entity.AdminSetAnswerStatusRequest"
+ "$ref": "#/definitions/schema.AdminSetAnswerStatusRequest"
}
}
],
@@ -822,6 +822,148 @@ const docTemplate = `{
}
}
},
+ "/answer/admin/api/siteinfo/login": {
+ "get": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "get site info login config",
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "admin"
+ ],
+ "summary": "get site info login config",
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "allOf": [
+ {
+ "$ref": "#/definitions/handler.RespBody"
+ },
+ {
+ "type": "object",
+ "properties": {
+ "data": {
+ "$ref": "#/definitions/schema.SiteLoginResp"
+ }
+ }
+ }
+ ]
+ }
+ }
+ }
+ },
+ "put": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "update site login",
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "admin"
+ ],
+ "summary": "update site login",
+ "parameters": [
+ {
+ "description": "login info",
+ "name": "data",
+ "in": "body",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/schema.SiteLoginReq"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/handler.RespBody"
+ }
+ }
+ }
+ }
+ },
+ "/answer/admin/api/siteinfo/seo": {
+ "get": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "get site seo information",
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "admin"
+ ],
+ "summary": "get site seo information",
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "allOf": [
+ {
+ "$ref": "#/definitions/handler.RespBody"
+ },
+ {
+ "type": "object",
+ "properties": {
+ "data": {
+ "$ref": "#/definitions/schema.SiteSeoResp"
+ }
+ }
+ }
+ ]
+ }
+ }
+ }
+ },
+ "put": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "update site seo information",
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "admin"
+ ],
+ "summary": "update site seo information",
+ "parameters": [
+ {
+ "description": "seo",
+ "name": "data",
+ "in": "body",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/schema.SiteSeoReq"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/handler.RespBody"
+ }
+ }
+ }
+ }
+ },
"/answer/admin/api/siteinfo/write": {
"get": {
"security": [
@@ -918,6 +1060,84 @@ const docTemplate = `{
}
}
},
+ "/answer/admin/api/user": {
+ "post": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "add user",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "admin"
+ ],
+ "summary": "add user",
+ "parameters": [
+ {
+ "description": "user",
+ "name": "data",
+ "in": "body",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/schema.AddUserReq"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/handler.RespBody"
+ }
+ }
+ }
+ }
+ },
+ "/answer/admin/api/user/password": {
+ "put": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "update user password",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "admin"
+ ],
+ "summary": "update user password",
+ "parameters": [
+ {
+ "description": "user",
+ "name": "data",
+ "in": "body",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/schema.UpdateUserPasswordReq"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/handler.RespBody"
+ }
+ }
+ }
+ }
+ },
"/answer/admin/api/user/role": {
"put": {
"security": [
@@ -2746,6 +2966,45 @@ const docTemplate = `{
}
}
},
+ "/answer/api/v1/question/reopen": {
+ "put": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "reopen question",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "api-question"
+ ],
+ "summary": "reopen question",
+ "parameters": [
+ {
+ "description": "question",
+ "name": "data",
+ "in": "body",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/schema.ReopenQuestionReq"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/handler.RespBody"
+ }
+ }
+ }
+ }
+ },
"/answer/api/v1/question/search": {
"post": {
"description": "SearchQuestionList",
@@ -3346,7 +3605,7 @@ const docTemplate = `{
"type": "object",
"properties": {
"data": {
- "$ref": "#/definitions/schema.SiteGeneralResp"
+ "$ref": "#/definitions/schema.SiteInfoResp"
}
}
}
@@ -4672,20 +4931,29 @@ const docTemplate = `{
}
}
}
+ },
+ "/robots.txt": {
+ "get": {
+ "description": "get site robots information",
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "site"
+ ],
+ "summary": "get site robots information",
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "type": "string"
+ }
+ }
+ }
+ }
}
},
"definitions": {
- "entity.AdminSetAnswerStatusRequest": {
- "type": "object",
- "properties": {
- "answer_id": {
- "type": "string"
- },
- "status": {
- "type": "string"
- }
- }
- },
"handler.RespBody": {
"type": "object",
"properties": {
@@ -4812,6 +5080,9 @@ const docTemplate = `{
"display_name": {
"type": "string"
},
+ "main_tag_slug_name": {
+ "type": "string"
+ },
"object_type": {
"type": "string"
},
@@ -4847,6 +5118,9 @@ const docTemplate = `{
"created_at": {
"type": "integer"
},
+ "id": {
+ "type": "string"
+ },
"object_id": {
"type": "string"
},
@@ -4934,6 +5208,40 @@ const docTemplate = `{
}
}
},
+ "schema.AddUserReq": {
+ "type": "object",
+ "required": [
+ "display_name",
+ "email",
+ "password"
+ ],
+ "properties": {
+ "display_name": {
+ "type": "string",
+ "maxLength": 30
+ },
+ "email": {
+ "type": "string",
+ "maxLength": 500
+ },
+ "password": {
+ "type": "string",
+ "maxLength": 32,
+ "minLength": 8
+ }
+ }
+ },
+ "schema.AdminSetAnswerStatusRequest": {
+ "type": "object",
+ "properties": {
+ "answer_id": {
+ "type": "string"
+ },
+ "status": {
+ "type": "string"
+ }
+ }
+ },
"schema.AdminSetQuestionStatusRequest": {
"type": "object",
"properties": {
@@ -4969,7 +5277,6 @@ const docTemplate = `{
"type": "string"
},
"question_id": {
- "description": "question_id",
"type": "string"
}
}
@@ -6163,6 +6470,14 @@ const docTemplate = `{
}
}
},
+ "schema.ReopenQuestionReq": {
+ "type": "object",
+ "properties": {
+ "question_id": {
+ "type": "string"
+ }
+ }
+ },
"schema.ReportHandleReq": {
"type": "object",
"required": [
@@ -6328,6 +6643,7 @@ const docTemplate = `{
"required": [
"contact_email",
"name",
+ "permalink",
"site_url"
],
"properties": {
@@ -6343,6 +6659,11 @@ const docTemplate = `{
"type": "string",
"maxLength": 128
},
+ "permalink": {
+ "type": "integer",
+ "maximum": 3,
+ "minimum": 0
+ },
"short_description": {
"type": "string",
"maxLength": 255
@@ -6358,6 +6679,7 @@ const docTemplate = `{
"required": [
"contact_email",
"name",
+ "permalink",
"site_url"
],
"properties": {
@@ -6373,6 +6695,11 @@ const docTemplate = `{
"type": "string",
"maxLength": 128
},
+ "permalink": {
+ "type": "integer",
+ "maximum": 3,
+ "minimum": 0
+ },
"short_description": {
"type": "string",
"maxLength": 255
@@ -6383,6 +6710,23 @@ const docTemplate = `{
}
}
},
+ "schema.SiteInfoResp": {
+ "type": "object",
+ "properties": {
+ "branding": {
+ "$ref": "#/definitions/schema.SiteBrandingResp"
+ },
+ "general": {
+ "$ref": "#/definitions/schema.SiteGeneralResp"
+ },
+ "interface": {
+ "$ref": "#/definitions/schema.SiteInterfaceResp"
+ },
+ "login": {
+ "$ref": "#/definitions/schema.SiteLoginResp"
+ }
+ }
+ },
"schema.SiteInterfaceReq": {
"type": "object",
"required": [
@@ -6461,6 +6805,50 @@ const docTemplate = `{
}
}
},
+ "schema.SiteLoginReq": {
+ "type": "object",
+ "properties": {
+ "allow_new_registrations": {
+ "type": "boolean"
+ },
+ "login_required": {
+ "type": "boolean"
+ }
+ }
+ },
+ "schema.SiteLoginResp": {
+ "type": "object",
+ "properties": {
+ "allow_new_registrations": {
+ "type": "boolean"
+ },
+ "login_required": {
+ "type": "boolean"
+ }
+ }
+ },
+ "schema.SiteSeoReq": {
+ "type": "object",
+ "required": [
+ "robots"
+ ],
+ "properties": {
+ "robots": {
+ "type": "string"
+ }
+ }
+ },
+ "schema.SiteSeoResp": {
+ "type": "object",
+ "required": [
+ "robots"
+ ],
+ "properties": {
+ "robots": {
+ "type": "string"
+ }
+ }
+ },
"schema.SiteWriteReq": {
"type": "object",
"properties": {
@@ -6773,6 +7161,23 @@ const docTemplate = `{
}
}
},
+ "schema.UpdateUserPasswordReq": {
+ "type": "object",
+ "required": [
+ "password",
+ "user_id"
+ ],
+ "properties": {
+ "password": {
+ "type": "string",
+ "maxLength": 32,
+ "minLength": 8
+ },
+ "user_id": {
+ "type": "string"
+ }
+ }
+ },
"schema.UpdateUserRoleReq": {
"type": "object",
"required": [
@@ -6824,6 +7229,10 @@ const docTemplate = `{
"description": "display_name",
"type": "string"
},
+ "id": {
+ "description": "user_id",
+ "type": "string"
+ },
"ip_info": {
"description": "ip info",
"type": "string"
diff --git a/docs/swagger.json b/docs/swagger.json
index aaad0374..17932c39 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -93,7 +93,7 @@
"in": "body",
"required": true,
"schema": {
- "$ref": "#/definitions/entity.AdminSetAnswerStatusRequest"
+ "$ref": "#/definitions/schema.AdminSetAnswerStatusRequest"
}
}
],
@@ -810,6 +810,148 @@
}
}
},
+ "/answer/admin/api/siteinfo/login": {
+ "get": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "get site info login config",
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "admin"
+ ],
+ "summary": "get site info login config",
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "allOf": [
+ {
+ "$ref": "#/definitions/handler.RespBody"
+ },
+ {
+ "type": "object",
+ "properties": {
+ "data": {
+ "$ref": "#/definitions/schema.SiteLoginResp"
+ }
+ }
+ }
+ ]
+ }
+ }
+ }
+ },
+ "put": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "update site login",
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "admin"
+ ],
+ "summary": "update site login",
+ "parameters": [
+ {
+ "description": "login info",
+ "name": "data",
+ "in": "body",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/schema.SiteLoginReq"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/handler.RespBody"
+ }
+ }
+ }
+ }
+ },
+ "/answer/admin/api/siteinfo/seo": {
+ "get": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "get site seo information",
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "admin"
+ ],
+ "summary": "get site seo information",
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "allOf": [
+ {
+ "$ref": "#/definitions/handler.RespBody"
+ },
+ {
+ "type": "object",
+ "properties": {
+ "data": {
+ "$ref": "#/definitions/schema.SiteSeoResp"
+ }
+ }
+ }
+ ]
+ }
+ }
+ }
+ },
+ "put": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "update site seo information",
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "admin"
+ ],
+ "summary": "update site seo information",
+ "parameters": [
+ {
+ "description": "seo",
+ "name": "data",
+ "in": "body",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/schema.SiteSeoReq"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/handler.RespBody"
+ }
+ }
+ }
+ }
+ },
"/answer/admin/api/siteinfo/write": {
"get": {
"security": [
@@ -906,6 +1048,84 @@
}
}
},
+ "/answer/admin/api/user": {
+ "post": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "add user",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "admin"
+ ],
+ "summary": "add user",
+ "parameters": [
+ {
+ "description": "user",
+ "name": "data",
+ "in": "body",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/schema.AddUserReq"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/handler.RespBody"
+ }
+ }
+ }
+ }
+ },
+ "/answer/admin/api/user/password": {
+ "put": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "update user password",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "admin"
+ ],
+ "summary": "update user password",
+ "parameters": [
+ {
+ "description": "user",
+ "name": "data",
+ "in": "body",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/schema.UpdateUserPasswordReq"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/handler.RespBody"
+ }
+ }
+ }
+ }
+ },
"/answer/admin/api/user/role": {
"put": {
"security": [
@@ -2734,6 +2954,45 @@
}
}
},
+ "/answer/api/v1/question/reopen": {
+ "put": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "reopen question",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "api-question"
+ ],
+ "summary": "reopen question",
+ "parameters": [
+ {
+ "description": "question",
+ "name": "data",
+ "in": "body",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/schema.ReopenQuestionReq"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/handler.RespBody"
+ }
+ }
+ }
+ }
+ },
"/answer/api/v1/question/search": {
"post": {
"description": "SearchQuestionList",
@@ -3334,7 +3593,7 @@
"type": "object",
"properties": {
"data": {
- "$ref": "#/definitions/schema.SiteGeneralResp"
+ "$ref": "#/definitions/schema.SiteInfoResp"
}
}
}
@@ -4660,20 +4919,29 @@
}
}
}
+ },
+ "/robots.txt": {
+ "get": {
+ "description": "get site robots information",
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "site"
+ ],
+ "summary": "get site robots information",
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "type": "string"
+ }
+ }
+ }
+ }
}
},
"definitions": {
- "entity.AdminSetAnswerStatusRequest": {
- "type": "object",
- "properties": {
- "answer_id": {
- "type": "string"
- },
- "status": {
- "type": "string"
- }
- }
- },
"handler.RespBody": {
"type": "object",
"properties": {
@@ -4800,6 +5068,9 @@
"display_name": {
"type": "string"
},
+ "main_tag_slug_name": {
+ "type": "string"
+ },
"object_type": {
"type": "string"
},
@@ -4835,6 +5106,9 @@
"created_at": {
"type": "integer"
},
+ "id": {
+ "type": "string"
+ },
"object_id": {
"type": "string"
},
@@ -4922,6 +5196,40 @@
}
}
},
+ "schema.AddUserReq": {
+ "type": "object",
+ "required": [
+ "display_name",
+ "email",
+ "password"
+ ],
+ "properties": {
+ "display_name": {
+ "type": "string",
+ "maxLength": 30
+ },
+ "email": {
+ "type": "string",
+ "maxLength": 500
+ },
+ "password": {
+ "type": "string",
+ "maxLength": 32,
+ "minLength": 8
+ }
+ }
+ },
+ "schema.AdminSetAnswerStatusRequest": {
+ "type": "object",
+ "properties": {
+ "answer_id": {
+ "type": "string"
+ },
+ "status": {
+ "type": "string"
+ }
+ }
+ },
"schema.AdminSetQuestionStatusRequest": {
"type": "object",
"properties": {
@@ -4957,7 +5265,6 @@
"type": "string"
},
"question_id": {
- "description": "question_id",
"type": "string"
}
}
@@ -6151,6 +6458,14 @@
}
}
},
+ "schema.ReopenQuestionReq": {
+ "type": "object",
+ "properties": {
+ "question_id": {
+ "type": "string"
+ }
+ }
+ },
"schema.ReportHandleReq": {
"type": "object",
"required": [
@@ -6316,6 +6631,7 @@
"required": [
"contact_email",
"name",
+ "permalink",
"site_url"
],
"properties": {
@@ -6331,6 +6647,11 @@
"type": "string",
"maxLength": 128
},
+ "permalink": {
+ "type": "integer",
+ "maximum": 3,
+ "minimum": 0
+ },
"short_description": {
"type": "string",
"maxLength": 255
@@ -6346,6 +6667,7 @@
"required": [
"contact_email",
"name",
+ "permalink",
"site_url"
],
"properties": {
@@ -6361,6 +6683,11 @@
"type": "string",
"maxLength": 128
},
+ "permalink": {
+ "type": "integer",
+ "maximum": 3,
+ "minimum": 0
+ },
"short_description": {
"type": "string",
"maxLength": 255
@@ -6371,6 +6698,23 @@
}
}
},
+ "schema.SiteInfoResp": {
+ "type": "object",
+ "properties": {
+ "branding": {
+ "$ref": "#/definitions/schema.SiteBrandingResp"
+ },
+ "general": {
+ "$ref": "#/definitions/schema.SiteGeneralResp"
+ },
+ "interface": {
+ "$ref": "#/definitions/schema.SiteInterfaceResp"
+ },
+ "login": {
+ "$ref": "#/definitions/schema.SiteLoginResp"
+ }
+ }
+ },
"schema.SiteInterfaceReq": {
"type": "object",
"required": [
@@ -6449,6 +6793,50 @@
}
}
},
+ "schema.SiteLoginReq": {
+ "type": "object",
+ "properties": {
+ "allow_new_registrations": {
+ "type": "boolean"
+ },
+ "login_required": {
+ "type": "boolean"
+ }
+ }
+ },
+ "schema.SiteLoginResp": {
+ "type": "object",
+ "properties": {
+ "allow_new_registrations": {
+ "type": "boolean"
+ },
+ "login_required": {
+ "type": "boolean"
+ }
+ }
+ },
+ "schema.SiteSeoReq": {
+ "type": "object",
+ "required": [
+ "robots"
+ ],
+ "properties": {
+ "robots": {
+ "type": "string"
+ }
+ }
+ },
+ "schema.SiteSeoResp": {
+ "type": "object",
+ "required": [
+ "robots"
+ ],
+ "properties": {
+ "robots": {
+ "type": "string"
+ }
+ }
+ },
"schema.SiteWriteReq": {
"type": "object",
"properties": {
@@ -6761,6 +7149,23 @@
}
}
},
+ "schema.UpdateUserPasswordReq": {
+ "type": "object",
+ "required": [
+ "password",
+ "user_id"
+ ],
+ "properties": {
+ "password": {
+ "type": "string",
+ "maxLength": 32,
+ "minLength": 8
+ },
+ "user_id": {
+ "type": "string"
+ }
+ }
+ },
"schema.UpdateUserRoleReq": {
"type": "object",
"required": [
@@ -6812,6 +7217,10 @@
"description": "display_name",
"type": "string"
},
+ "id": {
+ "description": "user_id",
+ "type": "string"
+ },
"ip_info": {
"description": "ip info",
"type": "string"
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index 58aec495..7de68006 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -1,11 +1,4 @@
definitions:
- entity.AdminSetAnswerStatusRequest:
- properties:
- answer_id:
- type: string
- status:
- type: string
- type: object
handler.RespBody:
properties:
code:
@@ -95,6 +88,8 @@ definitions:
type: string
display_name:
type: string
+ main_tag_slug_name:
+ type: string
object_type:
type: string
question_id:
@@ -118,6 +113,8 @@ definitions:
type: string
created_at:
type: integer
+ id:
+ type: string
object_id:
type: string
object_type:
@@ -179,6 +176,30 @@ definitions:
- object_id
- report_type
type: object
+ schema.AddUserReq:
+ properties:
+ display_name:
+ maxLength: 30
+ type: string
+ email:
+ maxLength: 500
+ type: string
+ password:
+ maxLength: 32
+ minLength: 8
+ type: string
+ required:
+ - display_name
+ - email
+ - password
+ type: object
+ schema.AdminSetAnswerStatusRequest:
+ properties:
+ answer_id:
+ type: string
+ status:
+ type: string
+ type: object
schema.AdminSetQuestionStatusRequest:
properties:
question_id:
@@ -203,7 +224,6 @@ definitions:
answer_id:
type: string
question_id:
- description: question_id
type: string
type: object
schema.AnswerUpdateReq:
@@ -1067,6 +1087,11 @@ definitions:
required:
- tag_id
type: object
+ schema.ReopenQuestionReq:
+ properties:
+ question_id:
+ type: string
+ type: object
schema.ReportHandleReq:
properties:
flagged_content:
@@ -1189,6 +1214,10 @@ definitions:
name:
maxLength: 128
type: string
+ permalink:
+ maximum: 3
+ minimum: 0
+ type: integer
short_description:
maxLength: 255
type: string
@@ -1198,6 +1227,7 @@ definitions:
required:
- contact_email
- name
+ - permalink
- site_url
type: object
schema.SiteGeneralResp:
@@ -1211,6 +1241,10 @@ definitions:
name:
maxLength: 128
type: string
+ permalink:
+ maximum: 3
+ minimum: 0
+ type: integer
short_description:
maxLength: 255
type: string
@@ -1220,8 +1254,20 @@ definitions:
required:
- contact_email
- name
+ - permalink
- site_url
type: object
+ schema.SiteInfoResp:
+ properties:
+ branding:
+ $ref: '#/definitions/schema.SiteBrandingResp'
+ general:
+ $ref: '#/definitions/schema.SiteGeneralResp'
+ interface:
+ $ref: '#/definitions/schema.SiteInterfaceResp'
+ login:
+ $ref: '#/definitions/schema.SiteLoginResp'
+ type: object
schema.SiteInterfaceReq:
properties:
language:
@@ -1276,6 +1322,34 @@ definitions:
terms_of_service_parsed_text:
type: string
type: object
+ schema.SiteLoginReq:
+ properties:
+ allow_new_registrations:
+ type: boolean
+ login_required:
+ type: boolean
+ type: object
+ schema.SiteLoginResp:
+ properties:
+ allow_new_registrations:
+ type: boolean
+ login_required:
+ type: boolean
+ type: object
+ schema.SiteSeoReq:
+ properties:
+ robots:
+ type: string
+ required:
+ - robots
+ type: object
+ schema.SiteSeoResp:
+ properties:
+ robots:
+ type: string
+ required:
+ - robots
+ type: object
schema.SiteWriteReq:
properties:
recommend_tags:
@@ -1498,6 +1572,18 @@ definitions:
required:
- language
type: object
+ schema.UpdateUserPasswordReq:
+ properties:
+ password:
+ maxLength: 32
+ minLength: 8
+ type: string
+ user_id:
+ type: string
+ required:
+ - password
+ - user_id
+ type: object
schema.UpdateUserRoleReq:
properties:
role_id:
@@ -1535,6 +1621,9 @@ definitions:
display_name:
description: display_name
type: string
+ id:
+ description: user_id
+ type: string
ip_info:
description: ip info
type: string
@@ -1749,7 +1838,7 @@ paths:
name: data
required: true
schema:
- $ref: '#/definitions/entity.AdminSetAnswerStatusRequest'
+ $ref: '#/definitions/schema.AdminSetAnswerStatusRequest'
produces:
- application/json
responses:
@@ -2187,6 +2276,88 @@ paths:
summary: update site legal info
tags:
- admin
+ /answer/admin/api/siteinfo/login:
+ get:
+ description: get site info login config
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ allOf:
+ - $ref: '#/definitions/handler.RespBody'
+ - properties:
+ data:
+ $ref: '#/definitions/schema.SiteLoginResp'
+ type: object
+ security:
+ - ApiKeyAuth: []
+ summary: get site info login config
+ tags:
+ - admin
+ put:
+ description: update site login
+ parameters:
+ - description: login info
+ in: body
+ name: data
+ required: true
+ schema:
+ $ref: '#/definitions/schema.SiteLoginReq'
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ $ref: '#/definitions/handler.RespBody'
+ security:
+ - ApiKeyAuth: []
+ summary: update site login
+ tags:
+ - admin
+ /answer/admin/api/siteinfo/seo:
+ get:
+ description: get site seo information
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ allOf:
+ - $ref: '#/definitions/handler.RespBody'
+ - properties:
+ data:
+ $ref: '#/definitions/schema.SiteSeoResp'
+ type: object
+ security:
+ - ApiKeyAuth: []
+ summary: get site seo information
+ tags:
+ - admin
+ put:
+ description: update site seo information
+ parameters:
+ - description: seo
+ in: body
+ name: data
+ required: true
+ schema:
+ $ref: '#/definitions/schema.SiteSeoReq'
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ $ref: '#/definitions/handler.RespBody'
+ security:
+ - ApiKeyAuth: []
+ summary: update site seo information
+ tags:
+ - admin
/answer/admin/api/siteinfo/write:
get:
description: get site interface
@@ -2243,6 +2414,54 @@ paths:
summary: Get theme options
tags:
- admin
+ /answer/admin/api/user:
+ post:
+ consumes:
+ - application/json
+ description: add user
+ parameters:
+ - description: user
+ in: body
+ name: data
+ required: true
+ schema:
+ $ref: '#/definitions/schema.AddUserReq'
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ $ref: '#/definitions/handler.RespBody'
+ security:
+ - ApiKeyAuth: []
+ summary: add user
+ tags:
+ - admin
+ /answer/admin/api/user/password:
+ put:
+ consumes:
+ - application/json
+ description: update user password
+ parameters:
+ - description: user
+ in: body
+ name: data
+ required: true
+ schema:
+ $ref: '#/definitions/schema.UpdateUserPasswordReq'
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ $ref: '#/definitions/handler.RespBody'
+ security:
+ - ApiKeyAuth: []
+ summary: update user password
+ tags:
+ - admin
/answer/admin/api/user/role:
put:
consumes:
@@ -3354,6 +3573,30 @@ paths:
summary: SearchQuestionList
tags:
- api-question
+ /answer/api/v1/question/reopen:
+ put:
+ consumes:
+ - application/json
+ description: reopen question
+ parameters:
+ - description: question
+ in: body
+ name: data
+ required: true
+ schema:
+ $ref: '#/definitions/schema.ReopenQuestionReq'
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ $ref: '#/definitions/handler.RespBody'
+ security:
+ - ApiKeyAuth: []
+ summary: reopen question
+ tags:
+ - api-question
/answer/api/v1/question/search:
post:
consumes:
@@ -3721,7 +3964,7 @@ paths:
- $ref: '#/definitions/handler.RespBody'
- properties:
data:
- $ref: '#/definitions/schema.SiteGeneralResp'
+ $ref: '#/definitions/schema.SiteInfoResp'
type: object
summary: get site info
tags:
@@ -4528,6 +4771,19 @@ paths:
summary: UserList
tags:
- api-question
+ /robots.txt:
+ get:
+ description: get site robots information
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ type: string
+ summary: get site robots information
+ tags:
+ - site
securityDefinitions:
ApiKeyAuth:
in: header
diff --git a/internal/base/constant/constant.go b/internal/base/constant/constant.go
index 72c4077a..52ae9062 100644
--- a/internal/base/constant/constant.go
+++ b/internal/base/constant/constant.go
@@ -12,6 +12,8 @@ const (
AdminTokenCacheTime = 7 * 24 * time.Hour
AcceptLanguageFlag = "Accept-Language"
UserTokenMappingCacheKey = "answer:user-token:mapping:"
+ SiteInfoCacheKey = "answer:site-info:"
+ SiteInfoCacheTime = 1 * time.Hour
)
const (
diff --git a/internal/base/middleware/auth.go b/internal/base/middleware/auth.go
index e793d3c7..555a2d10 100644
--- a/internal/base/middleware/auth.go
+++ b/internal/base/middleware/auth.go
@@ -4,6 +4,7 @@ import (
"strings"
"github.com/answerdev/answer/internal/schema"
+ "github.com/answerdev/answer/internal/service/siteinfo_common"
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/reason"
@@ -18,13 +19,17 @@ var ctxUUIDKey = "ctxUuidKey"
// AuthUserMiddleware auth user middleware
type AuthUserMiddleware struct {
- authService *auth.AuthService
+ authService *auth.AuthService
+ siteInfoCommonService *siteinfo_common.SiteInfoCommonService
}
// NewAuthUserMiddleware new auth user middleware
-func NewAuthUserMiddleware(authService *auth.AuthService) *AuthUserMiddleware {
+func NewAuthUserMiddleware(
+ authService *auth.AuthService,
+ siteInfoCommonService *siteinfo_common.SiteInfoCommonService) *AuthUserMiddleware {
return &AuthUserMiddleware{
- authService: authService,
+ authService: authService,
+ siteInfoCommonService: siteInfoCommonService,
}
}
@@ -48,6 +53,29 @@ func (am *AuthUserMiddleware) Auth() gin.HandlerFunc {
}
}
+// EjectUserBySiteInfo if admin config the site can access by nologin user, eject user.
+func (am *AuthUserMiddleware) EjectUserBySiteInfo() gin.HandlerFunc {
+ return func(ctx *gin.Context) {
+ mustLogin := false
+ siteInfo, _ := am.siteInfoCommonService.GetSiteLogin(ctx)
+ if siteInfo != nil {
+ mustLogin = siteInfo.LoginRequired
+ }
+ if !mustLogin {
+ ctx.Next()
+ return
+ }
+
+ _, isLogin := ctx.Get(ctxUUIDKey)
+ if !isLogin {
+ handler.HandleResponse(ctx, errors.Unauthorized(reason.UnauthorizedError), nil)
+ ctx.Abort()
+ return
+ }
+ ctx.Next()
+ }
+}
+
// MustAuth auth user info. If the user does not log in, an unauthenticated error is displayed
func (am *AuthUserMiddleware) MustAuth() gin.HandlerFunc {
return func(ctx *gin.Context) {
diff --git a/internal/base/server/http.go b/internal/base/server/http.go
index 17e4eec3..af5c1b42 100644
--- a/internal/base/server/http.go
+++ b/internal/base/server/http.go
@@ -50,9 +50,13 @@ func NewHTTPServer(debug bool,
static.Use(avatarMiddleware.AvatarThumb())
staticRouter.RegisterStaticRouter(static)
+ // The route must be available without logging in
+ mustUnAuthV1 := r.Group("/answer/api/v1")
+ answerRouter.RegisterMustUnAuthAnswerAPIRouter(mustUnAuthV1)
+
// register api that no need to login
unAuthV1 := r.Group("/answer/api/v1")
- unAuthV1.Use(authUserMiddleware.Auth())
+ unAuthV1.Use(authUserMiddleware.Auth(), authUserMiddleware.EjectUserBySiteInfo())
answerRouter.RegisterUnAuthAnswerAPIRouter(unAuthV1)
// register api that must be authenticated
diff --git a/internal/controller/answer_controller.go b/internal/controller/answer_controller.go
index d089d509..d3d1c478 100644
--- a/internal/controller/answer_controller.go
+++ b/internal/controller/answer_controller.go
@@ -268,7 +268,7 @@ func (ac *AnswerController) Adopted(ctx *gin.Context) {
// @Accept json
// @Produce json
// @Security ApiKeyAuth
-// @Param data body entity.AdminSetAnswerStatusRequest true "AdminSetAnswerStatusRequest"
+// @Param data body schema.AdminSetAnswerStatusRequest true "AdminSetAnswerStatusRequest"
// @Router /answer/admin/api/answer/status [put]
// @Success 200 {object} handler.RespBody
func (ac *AnswerController) AdminSetAnswerStatus(ctx *gin.Context) {
diff --git a/internal/controller/siteinfo_controller.go b/internal/controller/siteinfo_controller.go
index 2ec71992..6b6261fd 100644
--- a/internal/controller/siteinfo_controller.go
+++ b/internal/controller/siteinfo_controller.go
@@ -26,7 +26,7 @@ func NewSiteinfoController(siteInfoService *siteinfo_common.SiteInfoCommonServic
// @Description get site info
// @Tags site
// @Produce json
-// @Success 200 {object} handler.RespBody{data=schema.SiteGeneralResp}
+// @Success 200 {object} handler.RespBody{data=schema.SiteInfoResp}
// @Router /answer/api/v1/siteinfo [get]
func (sc *SiteinfoController) GetSiteInfo(ctx *gin.Context) {
var err error
diff --git a/internal/controller_backyard/siteinfo_controller.go b/internal/controller_backyard/siteinfo_controller.go
index d6378741..81ca62cc 100644
--- a/internal/controller_backyard/siteinfo_controller.go
+++ b/internal/controller_backyard/siteinfo_controller.go
@@ -245,7 +245,7 @@ func (sc *SiteInfoController) UpdateSiteLegal(ctx *gin.Context) {
// @Security ApiKeyAuth
// @Tags admin
// @Produce json
-// @Param data body schema.SiteLegalReq true "login info"
+// @Param data body schema.SiteLoginReq true "login info"
// @Success 200 {object} handler.RespBody{}
// @Router /answer/admin/api/siteinfo/login [put]
func (sc *SiteInfoController) UpdateSiteLogin(ctx *gin.Context) {
diff --git a/internal/repo/site_info/siteinfo_repo.go b/internal/repo/site_info/siteinfo_repo.go
index bcb90199..bee8ae50 100644
--- a/internal/repo/site_info/siteinfo_repo.go
+++ b/internal/repo/site_info/siteinfo_repo.go
@@ -2,12 +2,15 @@ package site_info
import (
"context"
+ "encoding/json"
+ "github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/data"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/service/siteinfo_common"
"github.com/segmentfault/pacman/errors"
+ "github.com/segmentfault/pacman/log"
"xorm.io/builder"
)
@@ -23,32 +26,55 @@ func NewSiteInfo(data *data.Data) siteinfo_common.SiteInfoRepo {
// SaveByType save site setting by type
func (sr *siteInfoRepo) SaveByType(ctx context.Context, siteType string, data *entity.SiteInfo) (err error) {
- var (
- old = &entity.SiteInfo{}
- exist bool
- )
- exist, _ = sr.data.DB.Where(builder.Eq{"type": siteType}).Get(old)
+ old := &entity.SiteInfo{}
+ exist, err := sr.data.DB.Where(builder.Eq{"type": siteType}).Get(old)
+ if err != nil {
+ return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
if exist {
_, err = sr.data.DB.ID(old.ID).Update(data)
- if err != nil {
- err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
- }
- return
+ } else {
+ _, err = sr.data.DB.Insert(data)
}
-
- _, err = sr.data.DB.Insert(data)
if err != nil {
- err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
+ sr.setCache(ctx, siteType, data)
return
}
// GetByType get site info by type
func (sr *siteInfoRepo) GetByType(ctx context.Context, siteType string) (siteInfo *entity.SiteInfo, exist bool, err error) {
+ siteInfo = sr.getCache(ctx, siteType)
+ if siteInfo != nil {
+ return siteInfo, true, nil
+ }
siteInfo = &entity.SiteInfo{}
exist, err = sr.data.DB.Where(builder.Eq{"type": siteType}).Get(siteInfo)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
+ if exist {
+ sr.setCache(ctx, siteType, siteInfo)
+ }
return
}
+
+func (sr *siteInfoRepo) getCache(ctx context.Context, siteType string) (siteInfo *entity.SiteInfo) {
+ siteInfo = &entity.SiteInfo{}
+ siteInfoCache, err := sr.data.Cache.GetString(ctx, constant.SiteInfoCacheKey+siteType)
+ if err != nil {
+ return nil
+ }
+ _ = json.Unmarshal([]byte(siteInfoCache), siteInfo)
+ return siteInfo
+}
+
+func (sr *siteInfoRepo) setCache(ctx context.Context, siteType string, siteInfo *entity.SiteInfo) {
+ siteInfoCache, _ := json.Marshal(siteInfo)
+ err := sr.data.Cache.SetString(ctx,
+ constant.SiteInfoCacheKey+siteType, string(siteInfoCache), constant.SiteInfoCacheTime)
+ if err != nil {
+ log.Error(err)
+ }
+}
diff --git a/internal/router/answer_api_router.go b/internal/router/answer_api_router.go
index f98a6439..552ae049 100644
--- a/internal/router/answer_api_router.go
+++ b/internal/router/answer_api_router.go
@@ -87,11 +87,17 @@ func NewAnswerAPIRouter(
}
}
-func (a *AnswerAPIRouter) RegisterUnAuthAnswerAPIRouter(r *gin.RouterGroup) {
+func (a *AnswerAPIRouter) RegisterMustUnAuthAnswerAPIRouter(r *gin.RouterGroup) {
// i18n
r.GET("/language/config", a.langController.GetLangMapping)
r.GET("/language/options", a.langController.GetUserLangOptions)
+ //siteinfo
+ r.GET("/siteinfo", a.siteinfoController.GetSiteInfo)
+ r.GET("/siteinfo/legal", a.siteinfoController.GetSiteLegalInfo)
+}
+
+func (a *AnswerAPIRouter) RegisterUnAuthAnswerAPIRouter(r *gin.RouterGroup) {
// comment
r.GET("/comment/page", a.commentController.GetCommentWithPage)
r.GET("/personal/comment/page", a.commentController.GetCommentPersonalWithPage)
@@ -139,11 +145,6 @@ func (a *AnswerAPIRouter) RegisterUnAuthAnswerAPIRouter(r *gin.RouterGroup) {
//rank
r.GET("/personal/rank/page", a.rankController.GetRankPersonalWithPage)
-
- //siteinfo
- r.GET("/siteinfo", a.siteinfoController.GetSiteInfo)
- r.GET("/siteinfo/legal", a.siteinfoController.GetSiteLegalInfo)
-
}
func (a *AnswerAPIRouter) RegisterAnswerAPIRouter(r *gin.RouterGroup) {