auth check

v3
veypi 2 months ago
parent 61d61f28c8
commit 9f93299509

@ -14,7 +14,6 @@ func useApp(r rest.Router) {
r.Get("/", appList) r.Get("/", appList)
r.Patch("/:app_id", appPatch) r.Patch("/:app_id", appPatch)
r.Post("/", appPost) r.Post("/", appPost)
r.Put("/:app_id", appPut)
} }
func appDelete(x *rest.X) (any, error) { func appDelete(x *rest.X) (any, error) {
opts := &M.AppDelete{} opts := &M.AppDelete{}
@ -24,6 +23,7 @@ func appDelete(x *rest.X) (any, error) {
} }
data := &M.App{} data := &M.App{}
err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error
return data, err return data, err
@ -36,6 +36,7 @@ func appGet(x *rest.X) (any, error) {
} }
data := &M.App{} data := &M.App{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
return data, err return data, err
@ -48,6 +49,7 @@ func appList(x *rest.X) (any, error) {
} }
data := make([]*M.App, 0, 10) data := make([]*M.App, 0, 10)
query := cfg.DB() query := cfg.DB()
if opts.Name != nil { if opts.Name != nil {
query = query.Where("name LIKE ?", opts.Name) query = query.Where("name LIKE ?", opts.Name)
@ -64,6 +66,7 @@ func appPatch(x *rest.X) (any, error) {
} }
data := &M.App{} data := &M.App{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
if err != nil { if err != nil {
return nil, err return nil, err
@ -96,6 +99,7 @@ func appPost(x *rest.X) (any, error) {
} }
data := &M.App{} data := &M.App{}
data.ID = strings.ReplaceAll(uuid.New().String(), "-", "") data.ID = strings.ReplaceAll(uuid.New().String(), "-", "")
data.Name = opts.Name data.Name = opts.Name
data.Icon = opts.Icon data.Icon = opts.Icon
@ -105,27 +109,3 @@ func appPost(x *rest.X) (any, error) {
return data, err 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
}

@ -9,38 +9,11 @@ import (
) )
func useAppUser(r rest.Router) { func useAppUser(r rest.Router) {
r.Patch("/:app_user_id", appUserPatch)
r.Post("/", appUserPost) r.Post("/", appUserPost)
r.Put("/:app_user_id", appUserPut)
r.Delete("/:app_user_id", appUserDelete) r.Delete("/:app_user_id", appUserDelete)
r.Get("/:app_user_id", appUserGet) r.Get("/:app_user_id", appUserGet)
r.Get("/", appUserList) r.Get("/", appUserList)
} r.Patch("/:app_user_id", appUserPatch)
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
} }
func appUserPost(x *rest.X) (any, error) { func appUserPost(x *rest.X) (any, error) {
opts := &M.AppUserPost{} opts := &M.AppUserPost{}
@ -50,6 +23,7 @@ func appUserPost(x *rest.X) (any, error) {
} }
data := &M.AppUser{} data := &M.AppUser{}
data.ID = strings.ReplaceAll(uuid.New().String(), "-", "") data.ID = strings.ReplaceAll(uuid.New().String(), "-", "")
data.AppID = opts.AppID data.AppID = opts.AppID
data.UserID = opts.UserID data.UserID = opts.UserID
@ -58,26 +32,6 @@ func appUserPost(x *rest.X) (any, error) {
return data, err 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) { func appUserDelete(x *rest.X) (any, error) {
opts := &M.AppUserDelete{} opts := &M.AppUserDelete{}
err := x.Parse(opts) err := x.Parse(opts)
@ -86,6 +40,7 @@ func appUserDelete(x *rest.X) (any, error) {
} }
data := &M.AppUser{} data := &M.AppUser{}
err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error
return data, err return data, err
@ -98,6 +53,7 @@ func appUserGet(x *rest.X) (any, error) {
} }
data := &M.AppUser{} data := &M.AppUser{}
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
return data, err return data, err
@ -110,6 +66,7 @@ func appUserList(x *rest.X) (any, error) {
} }
data := make([]*M.AppUser, 0, 10) data := make([]*M.AppUser, 0, 10)
query := cfg.DB() query := cfg.DB()
if opts.AppID != nil { if opts.AppID != nil {
query = query.Where("app_id LIKE ?", opts.AppID) query = query.Where("app_id LIKE ?", opts.AppID)
@ -124,3 +81,30 @@ func appUserList(x *rest.X) (any, error) {
return data, err 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
}

@ -10,6 +10,7 @@ func useResource(r rest.Router) {
r.Post("/", resourcePost) r.Post("/", resourcePost)
r.Delete("/", resourceDelete) r.Delete("/", resourceDelete)
r.Get("/", resourceList) r.Get("/", resourceList)
r.Delete("/:resource_id", resourceDelete)
} }
func resourcePost(x *rest.X) (any, error) { func resourcePost(x *rest.X) (any, error) {
opts := &M.ResourcePost{} opts := &M.ResourcePost{}

@ -9,46 +9,11 @@ import (
) )
func useRole(r rest.Router) { func useRole(r rest.Router) {
r.Put("/:role_id", rolePut)
r.Delete("/:role_id", roleDelete)
r.Get("/:role_id", roleGet) r.Get("/:role_id", roleGet)
r.Get("/", roleList) r.Get("/", roleList)
r.Patch("/:role_id", rolePatch) r.Patch("/:role_id", rolePatch)
r.Post("/", rolePost) r.Post("/", rolePost)
} r.Delete("/:role_id", roleDelete)
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
} }
func roleGet(x *rest.X) (any, error) { func roleGet(x *rest.X) (any, error) {
opts := &M.RoleGet{} opts := &M.RoleGet{}
@ -120,3 +85,15 @@ func rolePost(x *rest.X) (any, error) {
return data, err 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
}

@ -1,20 +1,21 @@
package user package user
import ( import (
"github.com/veypi/OneBD/rest"
M "oa/models"
"oa/cfg" "oa/cfg"
"oa/libs/auth"
M "oa/models"
"strings" "strings"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/veypi/OneBD/rest"
) )
func useUser(r rest.Router) { func useUser(r rest.Router) {
r.Delete("/:user_id", userDelete) r.Delete("/:user_id", auth.Check("user", "user_id", auth.DoDelete), userDelete)
r.Get("/:user_id", userGet) r.Get("/:user_id", auth.Check("user", "user_id", auth.DoRead), userGet)
r.Get("/", userList) r.Get("/", auth.Check("user", "", auth.DoRead), userList)
r.Patch("/:user_id", userPatch) r.Patch("/:user_id", auth.Check("user", "user_id", auth.DoUpdate), userPatch)
r.Post("/", userPost) r.Post("/", auth.Check("user", "", auth.DoCreate), userPost)
r.Put("/:user_id", userPut)
} }
func userDelete(x *rest.X) (any, error) { func userDelete(x *rest.X) (any, error) {
opts := &M.UserDelete{} opts := &M.UserDelete{}
@ -113,37 +114,19 @@ func userPost(x *rest.X) (any, error) {
data.ID = strings.ReplaceAll(uuid.New().String(), "-", "") data.ID = strings.ReplaceAll(uuid.New().String(), "-", "")
data.Username = opts.Username data.Username = opts.Username
data.Nickname = opts.Nickname if opts.Nickname != nil {
data.Icon = opts.Icon data.Nickname = *opts.Nickname
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
} }
data := &M.User{} if opts.Icon != nil {
data.Icon = *opts.Icon
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
if err != nil {
return nil, err
} }
optsMap := map[string]interface{}{ if opts.Email != nil {
"id": opts.ID, data.Email = *opts.Email
"username": opts.Username,
"nickname": opts.Nickname,
"icon": opts.Icon,
"email": opts.Email,
"phone": opts.Phone,
"status": opts.Status,
} }
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 return data, err
} }

@ -1,20 +1,31 @@
package user package user
import ( import (
"github.com/google/uuid"
"github.com/veypi/OneBD/rest" "github.com/veypi/OneBD/rest"
M "oa/models"
"oa/cfg" "oa/cfg"
M "oa/models"
"strings" "strings"
"github.com/google/uuid"
) )
func useUserRole(r rest.Router) { func useUserRole(r rest.Router) {
r.Delete("/:user_role_id", userRoleDelete)
r.Get("/:user_role_id", userRoleGet) r.Get("/:user_role_id", userRoleGet)
r.Get("/", userRoleList) r.Get("/", userRoleList)
r.Patch("/:user_role_id", userRolePatch) r.Patch("/:user_role_id", userRolePatch)
r.Post("/", userRolePost) 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) { func userRoleGet(x *rest.X) (any, error) {
opts := &M.UserRoleGet{} opts := &M.UserRoleGet{}
@ -57,9 +68,6 @@ func userRolePatch(x *rest.X) (any, error) {
return nil, err return nil, err
} }
optsMap := make(map[string]interface{}) optsMap := make(map[string]interface{})
if opts.UserID != nil {
optsMap["user_id"] = opts.UserID
}
if opts.Status != nil { if opts.Status != nil {
optsMap["status"] = opts.Status optsMap["status"] = opts.Status
} }
@ -83,35 +91,3 @@ func userRolePost(x *rest.X) (any, error) {
return data, err 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
}

@ -16,6 +16,7 @@ import (
type config struct { type config struct {
rest.RestConf rest.RestConf
DSN string `json:"dsn"` DSN string `json:"dsn"`
JWT string `json:"jwt"`
} }
var Config = &config{} var Config = &config{}

@ -10,8 +10,28 @@ package errs
import ( import (
"fmt" "fmt"
"net/http" "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 { type CodeErr struct {
Code int Code int
Msg string Msg string
@ -32,10 +52,11 @@ func New(code int, msg string) *CodeErr {
} }
var ( var (
AuthFailed = New(401, "auth failed") ArgsInvalid = New(40001, "args invalid")
AuthExpired = New(401, "auth expired") AuthNotFound = New(40100, "auth not found")
AuthInvalid = New(401, "auth invalid") AuthFailed = New(40101, "auth failed")
ArgsInvalid = New(http.StatusBadRequest, "args invalid") AuthExpired = New(40102, "auth expired")
UserNotFound = New(400, "user not found") AuthInvalid = New(40103, "auth invalid")
UserPwdInvalid = New(400, "password invalid") AuthNoPerm = New(40104, "no permission")
NotFound = New(40400, "not found")
) )

@ -7,6 +7,7 @@ replace github.com/veypi/OneBD => ../../../workspace/OneBD/
replace github.com/veypi/utils => ../../../workspace/OceanCurrent/utils/ replace github.com/veypi/utils => ../../../workspace/OceanCurrent/utils/
require ( require (
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0 github.com/google/uuid v1.6.0
github.com/veypi/OneBD v0.0.0-00010101000000-000000000000 github.com/veypi/OneBD v0.0.0-00010101000000-000000000000
github.com/veypi/utils v0.3.7 github.com/veypi/utils v0.3.7

@ -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/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 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= 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 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=

@ -0,0 +1,47 @@
//
// access.go
// Copyright (C) 2024 veypi <i@veypi.com>
// 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
}

@ -0,0 +1,63 @@
//
// jwt.go
// Copyright (C) 2024 veypi <i@veypi.com>
// 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 <token>"
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
}
}

@ -10,10 +10,10 @@ package main
import ( import (
"oa/api" "oa/api"
"oa/cfg" "oa/cfg"
"oa/errs"
_ "oa/models" _ "oa/models"
"github.com/veypi/OneBD/rest" "github.com/veypi/OneBD/rest"
"github.com/veypi/OneBD/rest/middlewares"
"github.com/veypi/utils/logv" "github.com/veypi/utils/logv"
) )
@ -33,9 +33,8 @@ func runWeb() error {
} }
apiRouter := app.Router().SubRouter("api") apiRouter := app.Router().SubRouter("api")
api.Use(apiRouter) api.Use(apiRouter)
apiRouter.Use(errs.JsonResponse)
apiRouter.Use(middlewares.JsonResponse) apiRouter.SetErrFunc(errs.JsonErrorResponse)
apiRouter.SetErrFunc(middlewares.JsonErrorResponse)
app.Router().Print() app.Router().Print()
return app.Run() return app.Run()
} }

@ -1,8 +1,6 @@
package models package models
import ( import "time"
"time"
)
type AppDelete struct { type AppDelete struct {
ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_id"` ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_id"`
@ -19,40 +17,18 @@ type AppList struct {
type AppPatch struct { type AppPatch struct {
ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_id"` ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_id"`
Name *string `json:"name" parse:"json"` Name *string `json:"name" parse:"json"`
Icon *string `json:"icon" parse:"json"` Icon *string `json:"icon" parse:"json"`
Des *string `json:"des" parse:"json"` Des *string `json:"des" parse:"json"`
Participate *string `json:"participate" gorm:"default:auto" 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 { type AppPost struct {
Name string `json:"name" 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"` Icon string `json:"icon" parse:"json"`
Des string `json:"des" parse:"json"` Des string `json:"des" parse:"json"`
Participate string `json:"participate" gorm:"default:auto" 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 { type AppUserDelete struct {
@ -61,41 +37,26 @@ type AppUserDelete struct {
type AppUserGet struct { type AppUserGet struct {
ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_user_id"` ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_user_id"`
AppID string `json:"app_id" parse:"path"` AppID string `json:"app_id" parse:"path"`
UserID string `json:"user_id" parse:"path"` UserID string `json:"user_id" parse:"path"`
} }
type AppUserList struct { type AppUserList struct {
AppID *string `json:"app_id" parse:"path"` AppID *string `json:"app_id" parse:"path"`
UserID *string `json:"user_id" parse:"path"` UserID *string `json:"user_id" parse:"path"`
Status *string `json:"status" parse:"json"` Status *string `json:"status" parse:"json"`
} }
type AppUserPatch struct { type AppUserPatch struct {
ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_user_id"` ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@app_user_id"`
AppID *string `json:"app_id" parse:"path"` AppID *string `json:"app_id" parse:"path"`
UserID *string `json:"user_id" parse:"path"` UserID *string `json:"user_id" parse:"path"`
Status *string `json:"status" parse:"json"` Status *string `json:"status" parse:"json"`
} }
type AppUserPost struct { type AppUserPost struct {
AppID string `json:"app_id" parse:"path"` AppID string `json:"app_id" parse:"path"`
UserID string `json:"user_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"` Status string `json:"status" parse:"json"`
} }

@ -1,14 +1,12 @@
package models package models
import ()
type App struct { type App struct {
BaseModel BaseModel
Name string `json:"name" methods:"get,post,put,*patch,*list" parse:"json"` Name string `json:"name" methods:"get,post,*patch,*list" parse:"json"`
Icon string `json:"icon" methods:"post,put,*patch" parse:"json"` Icon string `json:"icon" methods:"post,*patch" parse:"json"`
Des string `json:"des" methods:"post,put,*patch" parse:"json"` Des string `json:"des" methods:"post,*patch" parse:"json"`
Participate string `json:"participate" gorm:"default:auto" methods:"post,put,*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:"put,*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"` InitRole *Role `json:"init_role" gorm:"foreignKey:ID;references:InitRoleID"`
InitUrl string `json:"init_url"` InitUrl string `json:"init_url"`
UserCount uint `json:"user_count"` UserCount uint `json:"user_count"`
@ -21,7 +19,7 @@ type AppUser struct {
App *App `json:"app"` App *App `json:"app"`
UserID string `json:"user_id" methods:"get,*list,post,*patch" parse:"path"` UserID string `json:"user_id" methods:"get,*list,post,*patch" parse:"path"`
User *User `json:"user"` 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 { type Resource struct {

@ -14,7 +14,7 @@ import (
type BaseModel struct { type BaseModel struct {
// ID uint `json:"id" gorm:"primaryKey" methods:"get,patch,delete" parse:"path"` // 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 BaseDate
} }

@ -16,28 +16,13 @@ type RoleList struct {
type RolePatch struct { type RolePatch struct {
ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@role_id"` ID string `json:"id" gorm:"primaryKey;type:varchar(32)" parse:"path@role_id"`
Name *string `json:"name" parse:"json"` Name *string `json:"name" parse:"json"`
Des *string `json:"des" parse:"json"` Des *string `json:"des" parse:"json"`
AppID *string `json:"app_id" gorm:"index;type:varchar(32)" parse:"json"` AppID *string `json:"app_id" gorm:"index;type:varchar(32)" parse:"json"`
} }
type RolePost struct { type RolePost struct {
Name string `json:"name" 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"`
}
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"` Des string `json:"des" parse:"json"`
AppID string `json:"app_id" gorm:"index;type:varchar(32)" parse:"json"` AppID string `json:"app_id" gorm:"index;type:varchar(32)" parse:"json"`
} }

@ -2,9 +2,9 @@ package models
type Role struct { type Role struct {
BaseModel BaseModel
Name string `json:"name" methods:"post,put,*patch,*list" parse:"json"` Name string `json:"name" methods:"post,*patch,*list" parse:"json"`
Des string `json:"des" methods:"post,put,*patch" parse:"json"` Des string `json:"des" methods:"post,*patch" parse:"json"`
AppID string `json:"app_id" gorm:"index;type:varchar(32)" methods:"post,put,*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"` App *App `json:"app" gorm:"foreignKey:ID;references:AppID"`
UserCount uint `json:"user_count"` UserCount uint `json:"user_count"`
} }

@ -12,67 +12,33 @@ type UserGet struct {
type UserList struct { type UserList struct {
Username *string `json:"username" gorm:"varchar(100);unique;default:not null" parse:"json"` Username *string `json:"username" gorm:"varchar(100);unique;default:not null" parse:"json"`
Nickname *string `json:"nickname" parse:"json"` Nickname *string `json:"nickname" parse:"json"`
Email *string `json:"email" gorm:"varchar(20);unique;default:null" 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"` Phone *string `json:"phone" gorm:"varchar(50);unique;default:null" parse:"json"`
Status *uint `json:"status" parse:"json"` Status *uint `json:"status" parse:"json"`
} }
type UserPatch struct { 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"` Username *string `json:"username" gorm:"varchar(100);unique;default:not null" parse:"json"`
Nickname *string `json:"nickname" parse:"json"` Nickname *string `json:"nickname" parse:"json"`
Icon *string `json:"icon" parse:"json"` Icon *string `json:"icon" parse:"json"`
Email *string `json:"email" gorm:"varchar(20);unique;default:null" 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"` Phone *string `json:"phone" gorm:"varchar(50);unique;default:null" parse:"json"`
Status *uint `json:"status" parse:"json"` Status *uint `json:"status" parse:"json"`
} }
type UserPost struct { type UserPost struct {
Username string `json:"username" gorm:"varchar(100);unique;default:not null" parse:"json"` Username string `json:"username" gorm:"varchar(100);unique;default:not null" parse:"json"`
Nickname *string `json:"nickname" 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"`
Icon string `json:"icon" parse:"json"` Phone *string `json:"phone" gorm:"varchar(50);unique;default:null" 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"`
} }
type UserRoleDelete struct { 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"` UserID string `json:"user_id" parse:"path"`
RoleID string `json:"role_id" parse:"path"` RoleID string `json:"role_id" parse:"path"`
} }
@ -86,22 +52,11 @@ type UserRoleList struct {
type UserRolePatch struct { type UserRolePatch 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"`
Status *string `json:"status" parse:"json"` Status *string `json:"status" parse:"json"`
} }
type UserRolePost struct { type UserRolePost struct {
UserID string `json:"user_id" parse:"path"` UserID string `json:"user_id" parse:"path"`
RoleID string `json:"role_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"` Status string `json:"status" parse:"json"`
} }

@ -2,14 +2,14 @@ package models
type User struct { type User struct {
BaseModel BaseModel
Username string `json:"username" gorm:"varchar(100);unique;default:not null" methods:"post,put,*patch,*list" 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,put,*patch,*list" parse:"json"` Nickname string `json:"nickname" methods:"*post,*patch,*list" parse:"json"`
Icon string `json:"icon" methods:"post,put,*patch" 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"` 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,put,*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:"-"` RealCode string `json:"-"`
CheckCode string `json:"-"` CheckCode string `json:"-"`
@ -17,9 +17,9 @@ type User struct {
type UserRole struct { type UserRole struct {
BaseModel 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"` User *User `json:"user"`
RoleID string `json:"role_id" methods:"post,delete" parse:"path"` RoleID string `json:"role_id" methods:"post,delete" parse:"path"`
Role *Role `json:"role"` 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"`
} }

Loading…
Cancel
Save