diff --git a/conf/config.yaml b/conf/config.yaml index 01a733b..4e67e3d 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -1,6 +1,7 @@ lang: "zh-CN" app: web-client: 1 # 1:启用 0:禁用 + register: false #是否开启注册 gin: api-addr: "0.0.0.0:21114" mode: "release" #release,debug,test diff --git a/config/config.go b/config/config.go index 86d5d4b..085809e 100644 --- a/config/config.go +++ b/config/config.go @@ -15,7 +15,8 @@ const ( ) type App struct { - WebClient int `mapstructure:"web-client"` + WebClient int `mapstructure:"web-client"` + Register bool `mapstructure:"register"` } type Config struct { diff --git a/http/controller/admin/login.go b/http/controller/admin/login.go index 3d6ea12..ffc5ad9 100644 --- a/http/controller/admin/login.go +++ b/http/controller/admin/login.go @@ -103,7 +103,10 @@ func (ct *Login) LoginOptions(c *gin.Context) { for _, v := range res.Oauths { ops = append(ops, v.Op) } - response.Success(c, ops) + response.Success(c, gin.H{ + "ops": ops, + "register": global.Config.App.Register, + }) } // OidcAuth diff --git a/http/controller/admin/user.go b/http/controller/admin/user.go index 9f7415c..61eca66 100644 --- a/http/controller/admin/user.go +++ b/http/controller/admin/user.go @@ -5,6 +5,7 @@ import ( "Gwen/http/request/admin" "Gwen/http/response" adResp "Gwen/http/response/admin" + "Gwen/model" "Gwen/service" "github.com/gin-gonic/gin" "gorm.io/gorm" @@ -323,3 +324,40 @@ func (ct *User) GroupUsers(c *gin.Context) { } response.Success(c, data) } + +// Register +func (ct *User) Register(c *gin.Context) { + if !global.Config.App.Register { + response.Fail(c, 101, response.TranslateMsg(c, "RegisterClosed")) + return + } + f := &admin.RegisterForm{} + if err := c.ShouldBindJSON(f); err != nil { + response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error()) + return + } + errList := global.Validator.ValidStruct(c, f) + if len(errList) > 0 { + response.Fail(c, 101, errList[0]) + return + } + u := service.AllService.UserService.Register(f.Username, f.Password) + if u == nil || u.Id == 0 { + response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")) + return + } + // 注册成功后自动登录 + ut := service.AllService.UserService.Login(u, &model.LoginLog{ + UserId: u.Id, + Client: model.LoginLogClientWebAdmin, + Uuid: "", + Ip: c.ClientIP(), + Type: model.LoginLogTypeAccount, + }) + response.Success(c, &adResp.LoginPayload{ + Token: ut.Token, + Username: u.Username, + RouteNames: service.AllService.UserService.RouteNames(u), + Nickname: u.Nickname, + }) +} diff --git a/http/request/admin/user.go b/http/request/admin/user.go index b1b1719..e29133c 100644 --- a/http/request/admin/user.go +++ b/http/request/admin/user.go @@ -59,3 +59,9 @@ type GroupUsersQuery struct { IsMy int `json:"is_my"` UserId uint `json:"user_id"` } + +type RegisterForm struct { + Username string `json:"username" validate:"required,gte=4,lte=10"` + Password string `json:"password" validate:"required,gte=4,lte=20"` + ConfirmPassword string `json:"confirm_password" validate:"required,gte=4,lte=20"` +} diff --git a/http/router/admin.go b/http/router/admin.go index 2026cc6..89d76cf 100644 --- a/http/router/admin.go +++ b/http/router/admin.go @@ -17,7 +17,7 @@ func Init(g *gin.Engine) { adg := g.Group("/api/admin") LoginBind(adg) - + adg.POST("/user/register", (&admin.User{}).Register) adg.Use(middleware.AdminAuth()) //FileBind(adg) UserBind(adg) diff --git a/model/loginLog.go b/model/loginLog.go index 8405c2f..080e1eb 100644 --- a/model/loginLog.go +++ b/model/loginLog.go @@ -12,6 +12,12 @@ type LoginLog struct { TimeModel } +const ( + LoginLogClientWebAdmin = "webadmin" + LoginLogClientWeb = "webclient" + LoginLogClientApp = "app" +) + const ( LoginLogTypeAccount = "account" LoginLogTypeOauth = "oauth" diff --git a/resources/i18n/en.toml b/resources/i18n/en.toml index 6a66250..24e339a 100644 --- a/resources/i18n/en.toml +++ b/resources/i18n/en.toml @@ -119,3 +119,7 @@ other = "Default Group" description = "Share group" one = "Share Group" other = "Share Group" +[RegisterClosed] +description = "Register closed." +one = "Register closed." +other = "Register closed." diff --git a/resources/i18n/ko.toml b/resources/i18n/ko.toml index 2c8eeda..393fc49 100644 --- a/resources/i18n/ko.toml +++ b/resources/i18n/ko.toml @@ -121,3 +121,8 @@ other = "기본 그룹" description = "Share group." one = "공유 그룹" other = "공유 그룹" + +[RegisterClosed] +description = "Register closed." +one = "가입이 종료되었습니다." +other = "가입이 종료되었습니다." \ No newline at end of file diff --git a/resources/i18n/ru.toml b/resources/i18n/ru.toml index fc0b430..885401a 100644 --- a/resources/i18n/ru.toml +++ b/resources/i18n/ru.toml @@ -127,3 +127,8 @@ other = "Группа по умолчанию" description = "Share group." one = "Общая группа" other = "Общая группа" + +[RegisterClosed] +description = "Register closed." +one = "Регистрация закрыта." +other = "Регистрация закрыта." \ No newline at end of file diff --git a/resources/i18n/zh_CN.toml b/resources/i18n/zh_CN.toml index 46b84e5..1e2f0ad 100644 --- a/resources/i18n/zh_CN.toml +++ b/resources/i18n/zh_CN.toml @@ -121,3 +121,7 @@ other = "默认组" description = "Share group." one = "共享组" other = "共享组" +[RegisterClosed] +description = "Register closed." +one = "注册已关闭。" +other = "注册已关闭。" \ No newline at end of file diff --git a/service/user.go b/service/user.go index 59461bb..e586bfc 100644 --- a/service/user.go +++ b/service/user.go @@ -323,3 +323,13 @@ func (us *UserService) FindLatestUserIdFromLoginLogByUuid(uuid string) uint { global.DB.Where("uuid = ?", uuid).Order("id desc").First(llog) return llog.UserId } + +func (us *UserService) Register(username string, password string) *model.User { + u := &model.User{ + Username: username, + Password: us.EncryptPassword(password), + GroupId: 1, + } + global.DB.Create(u) + return u +}