From 102c945aa0df2b23cc5013a403f225a920b6b882 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Fri, 1 Jan 2021 23:45:26 +0800 Subject: [PATCH] add user-friendly error --- cmd/webserver.go | 2 ++ pkg/api/users.go | 10 ++++++++++ pkg/errs/user.go | 1 + src/locales/en.js | 1 + src/locales/zh_Hans.js | 1 + 5 files changed, 15 insertions(+) diff --git a/cmd/webserver.go b/cmd/webserver.go index 7daba3f2..a315417c 100644 --- a/cmd/webserver.go +++ b/cmd/webserver.go @@ -151,6 +151,8 @@ func startWebServer(c *cli.Context) error { if config.EnableUserRegister { apiRoute.POST("/register.json", bindApi(api.Users.UserRegisterHandler)) + } else { + apiRoute.POST("/register.json", bindApi(api.Users.UserRegistrationNotAllowed)) } apiRoute.GET("/logout.json", bindApi(api.Tokens.TokenRevokeCurrentHandler)) diff --git a/pkg/api/users.go b/pkg/api/users.go index 842cb21c..729a38ee 100644 --- a/pkg/api/users.go +++ b/pkg/api/users.go @@ -9,6 +9,7 @@ import ( "github.com/mayswind/lab/pkg/log" "github.com/mayswind/lab/pkg/models" "github.com/mayswind/lab/pkg/services" + "github.com/mayswind/lab/pkg/settings" ) // UsersApi represents user api @@ -27,6 +28,10 @@ var ( // UserRegisterHandler saves a new user by request parameters func (a *UsersApi) UserRegisterHandler(c *core.Context) (interface{}, *errs.Error) { + if !settings.Container.Current.EnableUserRegister { + return a.UserRegistrationNotAllowed(c) + } + var userRegisterReq models.UserRegisterRequest err := c.ShouldBindJSON(&userRegisterReq) @@ -76,6 +81,11 @@ func (a *UsersApi) UserRegisterHandler(c *core.Context) (interface{}, *errs.Erro return authResp, nil } +// UserRegistrationNotAllowed returns user registration not allowed error +func (a *UsersApi) UserRegistrationNotAllowed(c *core.Context) (interface{}, *errs.Error) { + return nil, errs.ErrUserRegistrationNotAllowed +} + // UserProfileHandler returns user profile of current user func (a *UsersApi) UserProfileHandler(c *core.Context) (interface{}, *errs.Error) { uid := c.GetCurrentUid() diff --git a/pkg/errs/user.go b/pkg/errs/user.go index 94538fcc..000496dd 100644 --- a/pkg/errs/user.go +++ b/pkg/errs/user.go @@ -17,4 +17,5 @@ var ( ErrUserPasswordWrong = NewNormalError(NormalSubcategoryUser, 8, http.StatusBadRequest, "password is wrong") ErrUsernameAlreadyExists = NewNormalError(NormalSubcategoryUser, 9, http.StatusBadRequest, "username already exists") ErrUserEmailAlreadyExists = NewNormalError(NormalSubcategoryUser, 10, http.StatusBadRequest, "email already exists") + ErrUserRegistrationNotAllowed = NewNormalError(NormalSubcategoryUser, 11, http.StatusBadRequest, "user registration not allowed") ) diff --git a/src/locales/en.js b/src/locales/en.js index 83f9adb8..4ba15211 100644 --- a/src/locales/en.js +++ b/src/locales/en.js @@ -302,6 +302,7 @@ export default { 'password is wrong': 'Password is wrong', 'username already exists': 'Username already exists', 'email already exists': 'Email already exists', + 'user registration not allowed': 'User registration is not allowed', 'login name is invalid': 'Login name is invalid', 'login name or password is invalid': 'Login name or password is invalid', 'login name or password is wrong': 'Login name or password is wrong', diff --git a/src/locales/zh_Hans.js b/src/locales/zh_Hans.js index a59a004e..d57b1159 100644 --- a/src/locales/zh_Hans.js +++ b/src/locales/zh_Hans.js @@ -302,6 +302,7 @@ export default { 'password is wrong': '密码错误', 'username already exists': '用户名已经存在', 'email already exists': '邮箱已经存在', + 'user registration not allowed': '不允许用户注册', 'login name is invalid': '登录名无效', 'login name or password is invalid': '登录名或密码无效', 'login name or password is wrong': '登录名或密码错误',