diff --git a/oa/api/app/app.go b/oa/api/app/app.go index e486363..6bcc6d7 100644 --- a/oa/api/app/app.go +++ b/oa/api/app/app.go @@ -1,11 +1,11 @@ package app import ( + "github.com/google/uuid" "github.com/veypi/OneBD/rest" - M "oa/models" "oa/cfg" + M "oa/models" "strings" - "github.com/google/uuid" ) func useApp(r rest.Router) { @@ -23,7 +23,6 @@ func appDelete(x *rest.X) (any, error) { } data := &M.App{} - err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error return data, err @@ -36,7 +35,6 @@ func appGet(x *rest.X) (any, error) { } data := &M.App{} - err = cfg.DB().Where("id = ?", opts.ID).First(data).Error return data, err @@ -49,10 +47,9 @@ func appList(x *rest.X) (any, error) { } data := make([]*M.App, 0, 10) - query := cfg.DB() if opts.Name != nil { - query = query.Where("name LIKE ?", opts.Name) + query = query.Where("name LIKE ?", opts.Name) } err = query.Find(&data).Error @@ -66,7 +63,6 @@ func appPatch(x *rest.X) (any, error) { } data := &M.App{} - err = cfg.DB().Where("id = ?", opts.ID).First(data).Error if err != nil { return nil, err @@ -99,7 +95,6 @@ func appPost(x *rest.X) (any, error) { } data := &M.App{} - data.ID = strings.ReplaceAll(uuid.New().String(), "-", "") data.Name = opts.Name data.Icon = opts.Icon diff --git a/oa/api/app/app_user.go b/oa/api/app/app_user.go index e5d6a81..eaab0e9 100644 --- a/oa/api/app/app_user.go +++ b/oa/api/app/app_user.go @@ -1,11 +1,11 @@ package app import ( + "github.com/google/uuid" "github.com/veypi/OneBD/rest" - M "oa/models" "oa/cfg" + M "oa/models" "strings" - "github.com/google/uuid" ) func useAppUser(r rest.Router) { @@ -23,7 +23,6 @@ func appUserPost(x *rest.X) (any, error) { } data := &M.AppUser{} - data.ID = strings.ReplaceAll(uuid.New().String(), "-", "") data.AppID = opts.AppID data.UserID = opts.UserID @@ -40,7 +39,6 @@ func appUserDelete(x *rest.X) (any, error) { } data := &M.AppUser{} - err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error return data, err @@ -53,7 +51,6 @@ func appUserGet(x *rest.X) (any, error) { } data := &M.AppUser{} - err = cfg.DB().Where("id = ?", opts.ID).First(data).Error return data, err @@ -66,16 +63,15 @@ func appUserList(x *rest.X) (any, error) { } data := make([]*M.AppUser, 0, 10) - query := cfg.DB() if opts.AppID != nil { - query = query.Where("app_id LIKE ?", opts.AppID) + query = query.Where("app_id LIKE ?", opts.AppID) } if opts.UserID != nil { - query = query.Where("user_id LIKE ?", opts.UserID) + query = query.Where("user_id LIKE ?", opts.UserID) } if opts.Status != nil { - query = query.Where("status LIKE ?", opts.Status) + query = query.Where("status LIKE ?", opts.Status) } err = query.Find(&data).Error @@ -89,7 +85,6 @@ func appUserPatch(x *rest.X) (any, error) { } data := &M.AppUser{} - err = cfg.DB().Where("id = ?", opts.ID).First(data).Error if err != nil { return nil, err diff --git a/oa/api/app/resource.go b/oa/api/app/resource.go index a525442..5d71d01 100644 --- a/oa/api/app/resource.go +++ b/oa/api/app/resource.go @@ -34,8 +34,8 @@ func resourceDelete(x *rest.X) (any, error) { return nil, err } data := &M.Resource{ - AppID: opts.AppID, - Name: opts.Name, + AppID: opts.AppID, + Name: opts.Name, } err = cfg.DB().Delete(data).Error diff --git a/oa/api/init.go b/oa/api/init.go index e845a56..22a591d 100644 --- a/oa/api/init.go +++ b/oa/api/init.go @@ -12,6 +12,7 @@ import ( "oa/api/access" "oa/api/app" "oa/api/role" + "oa/api/token" "oa/api/user" ) @@ -20,4 +21,5 @@ func Use(r rest.Router) { app.Use(r.SubRouter("app")) role.Use(r.SubRouter("role")) user.Use(r.SubRouter("user")) + token.Use(r.SubRouter("token")) } diff --git a/oa/api/role/role.go b/oa/api/role/role.go index 8f56d55..7d49c4f 100644 --- a/oa/api/role/role.go +++ b/oa/api/role/role.go @@ -1,11 +1,11 @@ package role import ( + "github.com/google/uuid" "github.com/veypi/OneBD/rest" - M "oa/models" "oa/cfg" + M "oa/models" "strings" - "github.com/google/uuid" ) func useRole(r rest.Router) { diff --git a/oa/api/token/init.go b/oa/api/token/init.go new file mode 100644 index 0000000..bf5ea48 --- /dev/null +++ b/oa/api/token/init.go @@ -0,0 +1,16 @@ +// +// Copyright (C) 2024 veypi +// 2024-09-24 22:37:12 +// Distributed under terms of the MIT license. +// +// Auto generated by OneBD. DO NOT EDIT + +package token + +import ( + "github.com/veypi/OneBD/rest" +) + +func Use(r rest.Router) { + useToken(r) +} diff --git a/oa/api/token/token.go b/oa/api/token/token.go new file mode 100644 index 0000000..37b6e7e --- /dev/null +++ b/oa/api/token/token.go @@ -0,0 +1,113 @@ +package token + +import ( + "oa/cfg" + M "oa/models" + "strings" + + "github.com/google/uuid" + "github.com/veypi/OneBD/rest" +) + +func useToken(r rest.Router) { + r.Get("/salt/:id", tokenSalt) + r.Post("/", tokenPost) + r.Get("/:token_id", tokenGet) + r.Patch("/:token_id", tokenPatch) + r.Delete("/:token_id", tokenDelete) + r.Get("/", tokenList) +} +func tokenSalt(x *rest.X) (any, error) { + opts := &M.TokenSalt{} + err := x.Parse(opts) + if err != nil { + return nil, err + } + data := &M.User{} + + err = cfg.DB().Where("id = ?", opts.ID).First(data).Error + return data.Salt, err +} +func tokenGet(x *rest.X) (any, error) { + opts := &M.TokenGet{} + err := x.Parse(opts) + if err != nil { + return nil, err + } + data := &M.Token{} + + err = cfg.DB().Where("id = ?", opts.ID).First(data).Error + + return data, err +} +func tokenPatch(x *rest.X) (any, error) { + opts := &M.TokenPatch{} + err := x.Parse(opts) + if err != nil { + return nil, err + } + data := &M.Token{} + + err = cfg.DB().Where("id = ?", opts.ID).First(data).Error + if err != nil { + return nil, err + } + optsMap := make(map[string]interface{}) + if opts.ExpiredAt != nil { + optsMap["expired_at"] = opts.ExpiredAt + } + if opts.OverPerm != nil { + optsMap["over_perm"] = opts.OverPerm + } + err = cfg.DB().Model(data).Updates(optsMap).Error + + return data, err +} +func tokenDelete(x *rest.X) (any, error) { + opts := &M.TokenDelete{} + err := x.Parse(opts) + if err != nil { + return nil, err + } + data := &M.Token{} + + err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error + + return data, err +} +func tokenPost(x *rest.X) (any, error) { + opts := &M.TokenPost{} + err := x.Parse(opts) + if err != nil { + return nil, err + } + data := &M.Token{} + + data.ID = strings.ReplaceAll(uuid.New().String(), "-", "") + data.UserID = opts.UserID + data.AppID = opts.AppID + if opts.ExpiredAt != nil { + data.ExpiredAt = *opts.ExpiredAt + } + if opts.OverPerm != nil { + data.OverPerm = *opts.OverPerm + } + err = cfg.DB().Create(data).Error + + return data, err +} +func tokenList(x *rest.X) (any, error) { + opts := &M.TokenList{} + err := x.Parse(opts) + if err != nil { + return nil, err + } + data := make([]*M.Token, 0, 10) + + query := cfg.DB() + query = query.Where("user_id = ?", opts.UserID) + query = query.Where("app_id = ?", opts.AppID) + err = query.Find(&data).Error + + return data, err +} diff --git a/oa/api/user/user.go b/oa/api/user/user.go index 6677f97..0ffbd14 100644 --- a/oa/api/user/user.go +++ b/oa/api/user/user.go @@ -1,10 +1,14 @@ package user import ( + "fmt" + "math/rand" "oa/cfg" + "oa/errs" "oa/libs/auth" M "oa/models" "strings" + "time" "github.com/google/uuid" "github.com/veypi/OneBD/rest" @@ -15,8 +19,9 @@ func useUser(r rest.Router) { r.Get("/:user_id", auth.Check("user", "user_id", auth.DoRead), userGet) r.Get("/", auth.Check("user", "", auth.DoRead), userList) r.Patch("/:user_id", auth.Check("user", "user_id", auth.DoUpdate), userPatch) - r.Post("/", auth.Check("user", "", auth.DoCreate), userPost) + r.Post("/", userPost) } + func userDelete(x *rest.X) (any, error) { opts := &M.UserDelete{} err := x.Parse(opts) @@ -114,11 +119,18 @@ func userPost(x *rest.X) (any, error) { data.ID = strings.ReplaceAll(uuid.New().String(), "-", "") data.Username = opts.Username + data.Salt = opts.Salt + data.Code = opts.Code + if data.Username == "" || len(data.Salt) != 32 || len(data.Code) != 256 { + return nil, errs.ArgsInvalid + } if opts.Nickname != nil { data.Nickname = *opts.Nickname } if opts.Icon != nil { data.Icon = *opts.Icon + } else { + data.Icon = fmt.Sprintf("/media/icon/%d.jpg", rand.New(rand.NewSource(time.Now().UnixNano())).Intn(230)) } if opts.Email != nil { data.Email = *opts.Email @@ -126,7 +138,10 @@ func userPost(x *rest.X) (any, error) { if opts.Phone != nil { data.Phone = *opts.Phone } + data.Status = 1 err = cfg.DB().Create(data).Error - - return data, err + if err != nil { + return nil, err + } + return data, nil } diff --git a/oa/go.mod b/oa/go.mod index 7665043..ec4a22d 100644 --- a/oa/go.mod +++ b/oa/go.mod @@ -7,6 +7,7 @@ replace github.com/veypi/OneBD => ../../../workspace/OneBD/ replace github.com/veypi/utils => ../../../workspace/OceanCurrent/utils/ require ( + github.com/go-sql-driver/mysql v1.7.0 github.com/golang-jwt/jwt/v5 v5.2.1 github.com/google/uuid v1.6.0 github.com/veypi/OneBD v0.0.0-00010101000000-000000000000 @@ -16,7 +17,6 @@ require ( ) require ( - github.com/go-sql-driver/mysql v1.7.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/rs/zerolog v1.17.2 // indirect