From 9f9329950994990691807aad1e3856dff555bb10 Mon Sep 17 00:00:00 2001 From: veypi Date: Mon, 23 Sep 2024 20:07:19 +0800 Subject: [PATCH] auth check --- oa/api/app/app.go | 32 +++------------ oa/api/app/app_user.go | 86 ++++++++++++++++------------------------ oa/api/app/resource.go | 1 + oa/api/role/role.go | 49 ++++++----------------- oa/api/user/user.go | 55 +++++++++---------------- oa/api/user/user_role.go | 54 +++++++------------------ oa/cfg/cfg.go | 1 + oa/errs/errors.go | 33 ++++++++++++--- oa/go.mod | 1 + oa/go.sum | 2 + oa/libs/auth/access.go | 47 ++++++++++++++++++++++ oa/libs/auth/jwt.go | 63 +++++++++++++++++++++++++++++ oa/main.go | 7 ++-- oa/models/app.gen.go | 69 +++++++------------------------- oa/models/app.go | 14 +++---- oa/models/init.go | 2 +- oa/models/role.gen.go | 25 +++--------- oa/models/role.go | 6 +-- oa/models/user.gen.go | 75 +++++++---------------------------- oa/models/user.go | 16 ++++---- 20 files changed, 286 insertions(+), 352 deletions(-) create mode 100644 oa/libs/auth/access.go create mode 100644 oa/libs/auth/jwt.go diff --git a/oa/api/app/app.go b/oa/api/app/app.go index 8e0219b..e486363 100644 --- a/oa/api/app/app.go +++ b/oa/api/app/app.go @@ -14,7 +14,6 @@ func useApp(r rest.Router) { r.Get("/", appList) r.Patch("/:app_id", appPatch) r.Post("/", appPost) - r.Put("/:app_id", appPut) } func appDelete(x *rest.X) (any, error) { opts := &M.AppDelete{} @@ -24,6 +23,7 @@ func appDelete(x *rest.X) (any, error) { } data := &M.App{} + err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error return data, err @@ -36,6 +36,7 @@ func appGet(x *rest.X) (any, error) { } data := &M.App{} + err = cfg.DB().Where("id = ?", opts.ID).First(data).Error return data, err @@ -48,9 +49,10 @@ 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 @@ -64,6 +66,7 @@ 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 @@ -96,6 +99,7 @@ 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 @@ -105,27 +109,3 @@ func appPost(x *rest.X) (any, error) { return data, err } -func appPut(x *rest.X) (any, error) { - opts := &M.AppPut{} - err := x.Parse(opts) - if err != nil { - return nil, err - } - data := &M.App{} - - err = cfg.DB().Where("id = ?", opts.ID).First(data).Error - if err != nil { - return nil, err - } - optsMap := map[string]interface{}{ - "id": opts.ID, - "name": opts.Name, - "icon": opts.Icon, - "des": opts.Des, - "participate": opts.Participate, - "init_role_id": opts.InitRoleID, - } - err = cfg.DB().Model(data).Updates(optsMap).Error - - return data, err -} diff --git a/oa/api/app/app_user.go b/oa/api/app/app_user.go index f6844f2..e5d6a81 100644 --- a/oa/api/app/app_user.go +++ b/oa/api/app/app_user.go @@ -9,38 +9,11 @@ import ( ) func useAppUser(r rest.Router) { - r.Patch("/:app_user_id", appUserPatch) r.Post("/", appUserPost) - r.Put("/:app_user_id", appUserPut) r.Delete("/:app_user_id", appUserDelete) r.Get("/:app_user_id", appUserGet) r.Get("/", appUserList) -} -func appUserPatch(x *rest.X) (any, error) { - opts := &M.AppUserPatch{} - err := x.Parse(opts) - if err != nil { - return nil, err - } - data := &M.AppUser{} - - err = cfg.DB().Where("id = ?", opts.ID).First(data).Error - if err != nil { - return nil, err - } - optsMap := make(map[string]interface{}) - if opts.AppID != nil { - optsMap["app_id"] = opts.AppID - } - if opts.UserID != nil { - optsMap["user_id"] = opts.UserID - } - if opts.Status != nil { - optsMap["status"] = opts.Status - } - err = cfg.DB().Model(data).Updates(optsMap).Error - - return data, err + r.Patch("/:app_user_id", appUserPatch) } func appUserPost(x *rest.X) (any, error) { opts := &M.AppUserPost{} @@ -50,6 +23,7 @@ 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 @@ -58,26 +32,6 @@ func appUserPost(x *rest.X) (any, error) { return data, err } -func appUserPut(x *rest.X) (any, error) { - opts := &M.AppUserPut{} - err := x.Parse(opts) - if err != nil { - return nil, err - } - data := &M.AppUser{} - - err = cfg.DB().Where("id = ?", opts.ID).First(data).Error - if err != nil { - return nil, err - } - optsMap := map[string]interface{}{ - "id": opts.ID, - "status": opts.Status, - } - err = cfg.DB().Model(data).Updates(optsMap).Error - - return data, err -} func appUserDelete(x *rest.X) (any, error) { opts := &M.AppUserDelete{} err := x.Parse(opts) @@ -86,6 +40,7 @@ func appUserDelete(x *rest.X) (any, error) { } data := &M.AppUser{} + err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error return data, err @@ -98,6 +53,7 @@ func appUserGet(x *rest.X) (any, error) { } data := &M.AppUser{} + err = cfg.DB().Where("id = ?", opts.ID).First(data).Error return data, err @@ -110,17 +66,45 @@ 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 return data, err } +func appUserPatch(x *rest.X) (any, error) { + opts := &M.AppUserPatch{} + err := x.Parse(opts) + if err != nil { + return nil, err + } + data := &M.AppUser{} + + + err = cfg.DB().Where("id = ?", opts.ID).First(data).Error + if err != nil { + return nil, err + } + optsMap := make(map[string]interface{}) + if opts.AppID != nil { + optsMap["app_id"] = opts.AppID + } + if opts.UserID != nil { + optsMap["user_id"] = opts.UserID + } + if opts.Status != nil { + optsMap["status"] = opts.Status + } + err = cfg.DB().Model(data).Updates(optsMap).Error + + return data, err +} diff --git a/oa/api/app/resource.go b/oa/api/app/resource.go index 9d6990a..a525442 100644 --- a/oa/api/app/resource.go +++ b/oa/api/app/resource.go @@ -10,6 +10,7 @@ func useResource(r rest.Router) { r.Post("/", resourcePost) r.Delete("/", resourceDelete) r.Get("/", resourceList) + r.Delete("/:resource_id", resourceDelete) } func resourcePost(x *rest.X) (any, error) { opts := &M.ResourcePost{} diff --git a/oa/api/role/role.go b/oa/api/role/role.go index 05cca79..8f56d55 100644 --- a/oa/api/role/role.go +++ b/oa/api/role/role.go @@ -9,46 +9,11 @@ import ( ) func useRole(r rest.Router) { - r.Put("/:role_id", rolePut) - r.Delete("/:role_id", roleDelete) r.Get("/:role_id", roleGet) r.Get("/", roleList) r.Patch("/:role_id", rolePatch) r.Post("/", rolePost) -} -func rolePut(x *rest.X) (any, error) { - opts := &M.RolePut{} - err := x.Parse(opts) - if err != nil { - return nil, err - } - data := &M.Role{} - - err = cfg.DB().Where("id = ?", opts.ID).First(data).Error - if err != nil { - return nil, err - } - optsMap := map[string]interface{}{ - "id": opts.ID, - "name": opts.Name, - "des": opts.Des, - "app_id": opts.AppID, - } - err = cfg.DB().Model(data).Updates(optsMap).Error - - return data, err -} -func roleDelete(x *rest.X) (any, error) { - opts := &M.RoleDelete{} - err := x.Parse(opts) - if err != nil { - return nil, err - } - data := &M.Role{} - - err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error - - return data, err + r.Delete("/:role_id", roleDelete) } func roleGet(x *rest.X) (any, error) { opts := &M.RoleGet{} @@ -120,3 +85,15 @@ func rolePost(x *rest.X) (any, error) { return data, err } +func roleDelete(x *rest.X) (any, error) { + opts := &M.RoleDelete{} + err := x.Parse(opts) + if err != nil { + return nil, err + } + data := &M.Role{} + + err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error + + return data, err +} diff --git a/oa/api/user/user.go b/oa/api/user/user.go index 140881d..6677f97 100644 --- a/oa/api/user/user.go +++ b/oa/api/user/user.go @@ -1,20 +1,21 @@ package user import ( - "github.com/veypi/OneBD/rest" - M "oa/models" "oa/cfg" + "oa/libs/auth" + M "oa/models" "strings" + "github.com/google/uuid" + "github.com/veypi/OneBD/rest" ) func useUser(r rest.Router) { - r.Delete("/:user_id", userDelete) - r.Get("/:user_id", userGet) - r.Get("/", userList) - r.Patch("/:user_id", userPatch) - r.Post("/", userPost) - r.Put("/:user_id", userPut) + r.Delete("/:user_id", auth.Check("user", "user_id", auth.DoDelete), userDelete) + 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) } func userDelete(x *rest.X) (any, error) { opts := &M.UserDelete{} @@ -113,37 +114,19 @@ func userPost(x *rest.X) (any, error) { data.ID = strings.ReplaceAll(uuid.New().String(), "-", "") data.Username = opts.Username - data.Nickname = opts.Nickname - data.Icon = opts.Icon - data.Email = opts.Email - data.Phone = opts.Phone - data.Status = opts.Status - err = cfg.DB().Create(data).Error - - return data, err -} -func userPut(x *rest.X) (any, error) { - opts := &M.UserPut{} - err := x.Parse(opts) - if err != nil { - return nil, err + if opts.Nickname != nil { + data.Nickname = *opts.Nickname } - data := &M.User{} - - err = cfg.DB().Where("id = ?", opts.ID).First(data).Error - if err != nil { - return nil, err + if opts.Icon != nil { + data.Icon = *opts.Icon } - optsMap := map[string]interface{}{ - "id": opts.ID, - "username": opts.Username, - "nickname": opts.Nickname, - "icon": opts.Icon, - "email": opts.Email, - "phone": opts.Phone, - "status": opts.Status, + if opts.Email != nil { + data.Email = *opts.Email } - err = cfg.DB().Model(data).Updates(optsMap).Error + if opts.Phone != nil { + data.Phone = *opts.Phone + } + err = cfg.DB().Create(data).Error return data, err } diff --git a/oa/api/user/user_role.go b/oa/api/user/user_role.go index f16dae3..17a8f8b 100644 --- a/oa/api/user/user_role.go +++ b/oa/api/user/user_role.go @@ -1,20 +1,31 @@ package user import ( + "github.com/google/uuid" "github.com/veypi/OneBD/rest" - M "oa/models" "oa/cfg" + M "oa/models" "strings" - "github.com/google/uuid" ) func useUserRole(r rest.Router) { + r.Delete("/:user_role_id", userRoleDelete) r.Get("/:user_role_id", userRoleGet) r.Get("/", userRoleList) r.Patch("/:user_role_id", userRolePatch) r.Post("/", userRolePost) - r.Put("/:user_role_id", userRolePut) - r.Delete("/:user_role_id", userRoleDelete) +} +func userRoleDelete(x *rest.X) (any, error) { + opts := &M.UserRoleDelete{} + err := x.Parse(opts) + if err != nil { + return nil, err + } + data := &M.UserRole{} + + err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error + + return data, err } func userRoleGet(x *rest.X) (any, error) { opts := &M.UserRoleGet{} @@ -57,9 +68,6 @@ func userRolePatch(x *rest.X) (any, error) { return nil, err } optsMap := make(map[string]interface{}) - if opts.UserID != nil { - optsMap["user_id"] = opts.UserID - } if opts.Status != nil { optsMap["status"] = opts.Status } @@ -83,35 +91,3 @@ func userRolePost(x *rest.X) (any, error) { return data, err } -func userRolePut(x *rest.X) (any, error) { - opts := &M.UserRolePut{} - err := x.Parse(opts) - if err != nil { - return nil, err - } - data := &M.UserRole{} - - err = cfg.DB().Where("id = ?", opts.ID).First(data).Error - if err != nil { - return nil, err - } - optsMap := map[string]interface{}{ - "id": opts.ID, - "status": opts.Status, - } - err = cfg.DB().Model(data).Updates(optsMap).Error - - return data, err -} -func userRoleDelete(x *rest.X) (any, error) { - opts := &M.UserRoleDelete{} - err := x.Parse(opts) - if err != nil { - return nil, err - } - data := &M.UserRole{} - - err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error - - return data, err -} diff --git a/oa/cfg/cfg.go b/oa/cfg/cfg.go index 48cc82a..46b37f7 100644 --- a/oa/cfg/cfg.go +++ b/oa/cfg/cfg.go @@ -16,6 +16,7 @@ import ( type config struct { rest.RestConf DSN string `json:"dsn"` + JWT string `json:"jwt"` } var Config = &config{} diff --git a/oa/errs/errors.go b/oa/errs/errors.go index 985b159..c9a3764 100644 --- a/oa/errs/errors.go +++ b/oa/errs/errors.go @@ -10,8 +10,28 @@ package errs import ( "fmt" "net/http" + + "github.com/veypi/OneBD/rest" ) +func JsonResponse(x *rest.X, data any) error { + x.WriteHeader(http.StatusOK) + return x.JSON(map[string]any{"code": 0, "data": data}) +} + +func JsonErrorResponse(x *rest.X, err error) { + code := 50000 + var msg string + if e := err.(*CodeErr); e != nil { + code = e.Code + msg = e.Msg + } else { + msg = err.Error() + } + x.WriteHeader(code / 100) + x.JSON(map[string]any{"code": code, "err": msg}) +} + type CodeErr struct { Code int Msg string @@ -32,10 +52,11 @@ func New(code int, msg string) *CodeErr { } var ( - AuthFailed = New(401, "auth failed") - AuthExpired = New(401, "auth expired") - AuthInvalid = New(401, "auth invalid") - ArgsInvalid = New(http.StatusBadRequest, "args invalid") - UserNotFound = New(400, "user not found") - UserPwdInvalid = New(400, "password invalid") + ArgsInvalid = New(40001, "args invalid") + AuthNotFound = New(40100, "auth not found") + AuthFailed = New(40101, "auth failed") + AuthExpired = New(40102, "auth expired") + AuthInvalid = New(40103, "auth invalid") + AuthNoPerm = New(40104, "no permission") + NotFound = New(40400, "not found") ) diff --git a/oa/go.mod b/oa/go.mod index 563afc8..7665043 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/golang-jwt/jwt/v5 v5.2.1 github.com/google/uuid v1.6.0 github.com/veypi/OneBD v0.0.0-00010101000000-000000000000 github.com/veypi/utils v0.3.7 diff --git a/oa/go.sum b/oa/go.sum index 96e8e07..58d0f10 100644 --- a/oa/go.sum +++ b/oa/go.sum @@ -3,6 +3,8 @@ github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2 github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= +github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= diff --git a/oa/libs/auth/access.go b/oa/libs/auth/access.go new file mode 100644 index 0000000..9841428 --- /dev/null +++ b/oa/libs/auth/access.go @@ -0,0 +1,47 @@ +// +// access.go +// Copyright (C) 2024 veypi +// 2024-09-23 19:37 +// Distributed under terms of the MIT license. +// + +package auth + +import "github.com/golang-jwt/jwt/v5" + +type AuthLevel uint + +const ( + DoNone = 0 + Do = 1 + DoRead = 1 + DoCreate = 2 + DoUpdate = 3 + DoDelete = 4 + DoAll = 5 +) + +type Access []struct { + Name string `json:"name"` + TID string `json:"tid"` + Level AuthLevel `json:"level"` +} + +func (a *Access) Check(target string, tid string, l AuthLevel) bool { + for _, line := range *a { + if target == line.Name && l >= line.Level { + if line.TID == "" || line.TID == tid { + return true + } + } + } + return false +} + +type Claims struct { + UID string `json:"uid"` + Name string `json:"name"` + Icon string `json:"icon"` + Access Access `json:"access"` + jwt.RegisteredClaims +} diff --git a/oa/libs/auth/jwt.go b/oa/libs/auth/jwt.go new file mode 100644 index 0000000..1a5bd2a --- /dev/null +++ b/oa/libs/auth/jwt.go @@ -0,0 +1,63 @@ +// +// jwt.go +// Copyright (C) 2024 veypi +// 2024-09-23 18:28 +// Distributed under terms of the MIT license. +// + +package auth + +import ( + "context" + "fmt" + "oa/cfg" + "oa/errs" + "strings" + + "github.com/golang-jwt/jwt/v5" + "github.com/veypi/OneBD/rest" +) + +func CheckJWT(x *rest.X) (*Claims, error) { + authHeader := x.Request.Header.Get("Authorization") + if authHeader == "" { + return nil, errs.AuthNotFound + } + // Token is typically in the format "Bearer " + tokenString := strings.TrimPrefix(authHeader, "Bearer ") + if tokenString == authHeader { + return nil, errs.AuthInvalid + } + + // Parse the token + claims := &Claims{} + token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) { + if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { + return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) + } + return []byte(cfg.Config.JWT), nil + }) + + if err != nil || !token.Valid { + return nil, errs.AuthInvalid + } + x.Request = x.Request.WithContext(context.WithValue(x.Request.Context(), "uid", claims.ID)) + return claims, nil +} + +func Check(target string, pid string, l AuthLevel) func(x *rest.X) error { + return func(x *rest.X) error { + claims, err := CheckJWT(x) + if err != nil { + return err + } + tid := "" + if pid != "" { + tid = x.Params.GetStr(pid) + } + if !claims.Access.Check(target, tid, l) { + return errs.AuthFailed + } + return nil + } +} diff --git a/oa/main.go b/oa/main.go index d81904e..6b1ea18 100644 --- a/oa/main.go +++ b/oa/main.go @@ -10,10 +10,10 @@ package main import ( "oa/api" "oa/cfg" + "oa/errs" _ "oa/models" "github.com/veypi/OneBD/rest" - "github.com/veypi/OneBD/rest/middlewares" "github.com/veypi/utils/logv" ) @@ -33,9 +33,8 @@ func runWeb() error { } apiRouter := app.Router().SubRouter("api") api.Use(apiRouter) - - apiRouter.Use(middlewares.JsonResponse) - apiRouter.SetErrFunc(middlewares.JsonErrorResponse) + apiRouter.Use(errs.JsonResponse) + apiRouter.SetErrFunc(errs.JsonErrorResponse) app.Router().Print() return app.Run() } diff --git a/oa/models/app.gen.go b/oa/models/app.gen.go index fb7578e..39451b1 100644 --- a/oa/models/app.gen.go +++ b/oa/models/app.gen.go @@ -1,8 +1,6 @@ package models -import ( - "time" -) +import "time" type AppDelete struct { ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_id"` @@ -18,84 +16,47 @@ type AppList struct { } type AppPatch struct { - ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_id"` - - Name *string `json:"name" parse:"json"` - - Icon *string `json:"icon" parse:"json"` - - Des *string `json:"des" parse:"json"` - + ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_id"` + Name *string `json:"name" parse:"json"` + Icon *string `json:"icon" parse:"json"` + Des *string `json:"des" parse:"json"` Participate *string `json:"participate" gorm:"default:auto" parse:"json"` - - InitRoleID *string `json:"init_role_id" gorm:"index;type:varchar(32)" parse:"json"` + InitRoleID *string `json:"init_role_id" gorm:"index;type:varchar(32)" parse:"json"` } type AppPost struct { - Name string `json:"name" parse:"json"` - - Icon string `json:"icon" parse:"json"` - - Des string `json:"des" parse:"json"` - + Name string `json:"name" parse:"json"` + Icon string `json:"icon" parse:"json"` + Des string `json:"des" parse:"json"` Participate string `json:"participate" gorm:"default:auto" parse:"json"` } -type AppPut struct { - ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_id"` - - Name string `json:"name" parse:"json"` - - Icon string `json:"icon" parse:"json"` - - Des string `json:"des" parse:"json"` - - Participate string `json:"participate" gorm:"default:auto" parse:"json"` - - InitRoleID string `json:"init_role_id" gorm:"index;type:varchar(32)" parse:"json"` -} - type AppUserDelete struct { ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_user_id"` } type AppUserGet struct { - ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_user_id"` - - AppID string `json:"app_id" parse:"path"` - + ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_user_id"` + AppID string `json:"app_id" parse:"path"` UserID string `json:"user_id" parse:"path"` } type AppUserList struct { - AppID *string `json:"app_id" parse:"path"` - + AppID *string `json:"app_id" parse:"path"` UserID *string `json:"user_id" parse:"path"` - Status *string `json:"status" parse:"json"` } type AppUserPatch struct { - ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_user_id"` - - AppID *string `json:"app_id" parse:"path"` - + ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_user_id"` + AppID *string `json:"app_id" parse:"path"` UserID *string `json:"user_id" parse:"path"` - Status *string `json:"status" parse:"json"` } type AppUserPost struct { - AppID string `json:"app_id" parse:"path"` - + AppID string `json:"app_id" parse:"path"` UserID string `json:"user_id" parse:"path"` - - Status string `json:"status" parse:"json"` -} - -type AppUserPut struct { - ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_user_id"` - Status string `json:"status" parse:"json"` } diff --git a/oa/models/app.go b/oa/models/app.go index 978b3e3..f2bac9e 100644 --- a/oa/models/app.go +++ b/oa/models/app.go @@ -1,14 +1,12 @@ package models -import () - type App struct { BaseModel - Name string `json:"name" methods:"get,post,put,*patch,*list" parse:"json"` - Icon string `json:"icon" methods:"post,put,*patch" parse:"json"` - Des string `json:"des" methods:"post,put,*patch" parse:"json"` - Participate string `json:"participate" gorm:"default:auto" methods:"post,put,*patch" parse:"json"` - InitRoleID string `json:"init_role_id" gorm:"index;type:varchar(32)" methods:"put,*patch" parse:"json"` + Name string `json:"name" methods:"get,post,*patch,*list" parse:"json"` + Icon string `json:"icon" methods:"post,*patch" parse:"json"` + Des string `json:"des" methods:"post,*patch" parse:"json"` + Participate string `json:"participate" gorm:"default:auto" methods:"post,*patch" parse:"json"` + InitRoleID string `json:"init_role_id" gorm:"index;type:varchar(32)" methods:"*patch" parse:"json"` InitRole *Role `json:"init_role" gorm:"foreignKey:ID;references:InitRoleID"` InitUrl string `json:"init_url"` UserCount uint `json:"user_count"` @@ -21,7 +19,7 @@ type AppUser struct { App *App `json:"app"` UserID string `json:"user_id" methods:"get,*list,post,*patch" parse:"path"` User *User `json:"user"` - Status string `json:"status" methods:"post,put,*patch,*list" parse:"json"` + Status string `json:"status" methods:"post,*patch,*list" parse:"json"` } type Resource struct { diff --git a/oa/models/init.go b/oa/models/init.go index c95d476..d708ba3 100644 --- a/oa/models/init.go +++ b/oa/models/init.go @@ -14,7 +14,7 @@ import ( type BaseModel struct { // ID uint `json:"id" gorm:"primaryKey" methods:"get,patch,delete" parse:"path"` - ID string `json:"id" gorm:"primaryKey;type:varchar(32)" methods:"get,put,patch,delete" parse:"path"` + ID string `json:"id" gorm:"primaryKey;type:varchar(32)" methods:"get,patch,delete" parse:"path"` BaseDate } diff --git a/oa/models/role.gen.go b/oa/models/role.gen.go index 6e7c789..262ccc1 100644 --- a/oa/models/role.gen.go +++ b/oa/models/role.gen.go @@ -15,29 +15,14 @@ type RoleList struct { } type RolePatch struct { - ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@role_id"` - - Name *string `json:"name" parse:"json"` - - Des *string `json:"des" parse:"json"` - + ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@role_id"` + Name *string `json:"name" parse:"json"` + Des *string `json:"des" parse:"json"` AppID *string `json:"app_id" gorm:"index;type:varchar(32)" parse:"json"` } type RolePost struct { - Name string `json:"name" parse:"json"` - - Des string `json:"des" parse:"json"` - - AppID string `json:"app_id" gorm:"index;type:varchar(32)" parse:"json"` -} - -type RolePut struct { - ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@role_id"` - - Name string `json:"name" parse:"json"` - - Des string `json:"des" parse:"json"` - + Name string `json:"name" parse:"json"` + Des string `json:"des" parse:"json"` AppID string `json:"app_id" gorm:"index;type:varchar(32)" parse:"json"` } diff --git a/oa/models/role.go b/oa/models/role.go index 255ca91..f2b26cc 100644 --- a/oa/models/role.go +++ b/oa/models/role.go @@ -2,9 +2,9 @@ package models type Role struct { BaseModel - Name string `json:"name" methods:"post,put,*patch,*list" parse:"json"` - Des string `json:"des" methods:"post,put,*patch" parse:"json"` - AppID string `json:"app_id" gorm:"index;type:varchar(32)" methods:"post,put,*patch" parse:"json"` + Name string `json:"name" methods:"post,*patch,*list" parse:"json"` + Des string `json:"des" methods:"post,*patch" parse:"json"` + AppID string `json:"app_id" gorm:"index;type:varchar(32)" methods:"post,*patch" parse:"json"` App *App `json:"app" gorm:"foreignKey:ID;references:AppID"` UserCount uint `json:"user_count"` } diff --git a/oa/models/user.gen.go b/oa/models/user.gen.go index e3e1ba7..4298cbb 100644 --- a/oa/models/user.gen.go +++ b/oa/models/user.gen.go @@ -12,67 +12,33 @@ type UserGet struct { type UserList struct { Username *string `json:"username" gorm:"varchar(100);unique;default:not null" parse:"json"` - Nickname *string `json:"nickname" parse:"json"` - - Email *string `json:"email" gorm:"varchar(20);unique;default:null" parse:"json"` - - Phone *string `json:"phone" gorm:"varchar(50);unique;default:null" parse:"json"` - - Status *uint `json:"status" parse:"json"` + Email *string `json:"email" gorm:"varchar(20);unique;default:null" parse:"json"` + Phone *string `json:"phone" gorm:"varchar(50);unique;default:null" parse:"json"` + Status *uint `json:"status" parse:"json"` } type UserPatch struct { - ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@user_id"` - + ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@user_id"` Username *string `json:"username" gorm:"varchar(100);unique;default:not null" parse:"json"` - Nickname *string `json:"nickname" parse:"json"` - - Icon *string `json:"icon" parse:"json"` - - Email *string `json:"email" gorm:"varchar(20);unique;default:null" parse:"json"` - - Phone *string `json:"phone" gorm:"varchar(50);unique;default:null" parse:"json"` - - Status *uint `json:"status" parse:"json"` + Icon *string `json:"icon" parse:"json"` + Email *string `json:"email" gorm:"varchar(20);unique;default:null" parse:"json"` + Phone *string `json:"phone" gorm:"varchar(50);unique;default:null" parse:"json"` + Status *uint `json:"status" parse:"json"` } type UserPost struct { - Username string `json:"username" gorm:"varchar(100);unique;default:not null" parse:"json"` - - Nickname string `json:"nickname" parse:"json"` - - Icon string `json:"icon" parse:"json"` - - Email string `json:"email" gorm:"varchar(20);unique;default:null" parse:"json"` - - Phone string `json:"phone" gorm:"varchar(50);unique;default:null" parse:"json"` - - Status uint `json:"status" parse:"json"` -} - -type UserPut struct { - ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@user_id"` - - Username string `json:"username" gorm:"varchar(100);unique;default:not null" parse:"json"` - - Nickname string `json:"nickname" parse:"json"` - - Icon string `json:"icon" parse:"json"` - - Email string `json:"email" gorm:"varchar(20);unique;default:null" parse:"json"` - - Phone string `json:"phone" gorm:"varchar(50);unique;default:null" parse:"json"` - - Status uint `json:"status" parse:"json"` + Username string `json:"username" gorm:"varchar(100);unique;default:not null" parse:"json"` + Nickname *string `json:"nickname" parse:"json"` + Icon *string `json:"icon" parse:"json"` + Email *string `json:"email" gorm:"varchar(20);unique;default:null" parse:"json"` + Phone *string `json:"phone" gorm:"varchar(50);unique;default:null" parse:"json"` } type UserRoleDelete struct { - ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@user_role_id"` - + ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@user_role_id"` UserID string `json:"user_id" parse:"path"` - RoleID string `json:"role_id" parse:"path"` } @@ -85,23 +51,12 @@ type UserRoleList struct { } type UserRolePatch struct { - ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@user_role_id"` - - UserID *string `json:"user_id" parse:"path"` - + ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@user_role_id"` Status *string `json:"status" parse:"json"` } type UserRolePost struct { UserID string `json:"user_id" parse:"path"` - RoleID string `json:"role_id" parse:"path"` - - Status string `json:"status" parse:"json"` -} - -type UserRolePut struct { - ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@user_role_id"` - Status string `json:"status" parse:"json"` } diff --git a/oa/models/user.go b/oa/models/user.go index ed787f0..243c483 100644 --- a/oa/models/user.go +++ b/oa/models/user.go @@ -2,14 +2,14 @@ package models type User struct { BaseModel - Username string `json:"username" gorm:"varchar(100);unique;default:not null" methods:"post,put,*patch,*list" parse:"json"` - Nickname string `json:"nickname" methods:"post,put,*patch,*list" parse:"json"` - Icon string `json:"icon" methods:"post,put,*patch" parse:"json"` + Username string `json:"username" gorm:"varchar(100);unique;default:not null" methods:"post,*patch,*list" parse:"json"` + Nickname string `json:"nickname" methods:"*post,*patch,*list" parse:"json"` + Icon string `json:"icon" methods:"*post,*patch" parse:"json"` - Email string `json:"email" gorm:"varchar(20);unique;default:null" methods:"post,put,*patch,*list" parse:"json"` - Phone string `json:"phone" gorm:"varchar(50);unique;default:null" methods:"post,put,*patch,*list" parse:"json"` + Email string `json:"email" gorm:"varchar(20);unique;default:null" methods:"*post,*patch,*list" parse:"json"` + Phone string `json:"phone" gorm:"varchar(50);unique;default:null" methods:"*post,*patch,*list" parse:"json"` - Status uint `json:"status" methods:"post,put,*patch,*list" parse:"json"` + Status uint `json:"status" methods:"*patch,*list" parse:"json"` RealCode string `json:"-"` CheckCode string `json:"-"` @@ -17,9 +17,9 @@ type User struct { type UserRole struct { BaseModel - UserID string `json:"user_id" methods:"post,*patch,delete" parse:"path"` + UserID string `json:"user_id" methods:"post,delete" parse:"path"` User *User `json:"user"` RoleID string `json:"role_id" methods:"post,delete" parse:"path"` Role *Role `json:"role"` - Status string `json:"status" methods:"post,put,*patch,*list" parse:"json"` + Status string `json:"status" methods:"post,*patch,*list" parse:"json"` }