diff --git a/api/api.go b/api/api.go index b931075..888a2e6 100644 --- a/api/api.go +++ b/api/api.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "fmt" "github.com/veypi/OneAuth/api/app" + "github.com/veypi/OneAuth/api/resource" "github.com/veypi/OneAuth/api/role" "github.com/veypi/OneAuth/api/token" "github.com/veypi/OneAuth/api/user" @@ -36,10 +37,13 @@ func Router(r OneBD.Router) { }) user.Router(r.SubRouter("/user")) wx.Router(r.SubRouter("wx")) - appRouter := r.SubRouter("app") - app.Router(appRouter) - role.Router(appRouter.SubRouter("/:uuid/")) - token.Router(appRouter.SubRouter("/:uuid/token")) + + app.Router(r.SubRouter("app")) + appRouter := r.SubRouter("/app/:uuid") + role.Router(appRouter.SubRouter("role")) + resource.Router(appRouter.SubRouter("resource")) + token.Router(appRouter.SubRouter("token")) + r.Set("upload", handleUpload, rfc.MethodPost) //message.Router(r.SubRouter("/message")) diff --git a/api/app/app.go b/api/app/app.go index 38bce0c..47cf429 100644 --- a/api/app/app.go +++ b/api/app/app.go @@ -9,11 +9,14 @@ import ( "github.com/veypi/OneBD" "github.com/veypi/OneBD/rfc" "github.com/veypi/utils" + "github.com/veypi/utils/log" + "gorm.io/gorm" + "reflect" ) var appHandlerP = OneBD.NewHandlerPool(func() OneBD.Handler { h := &appHandler{} - h.Ignore(rfc.MethodGet, rfc.MethodPost) + h.Ignore(rfc.MethodGet) return h }) @@ -36,6 +39,20 @@ func (h *appHandler) Get() (interface{}, error) { if err != nil { return nil, err } + if option == "key" { + if uuid == "" { + return nil, oerr.ApiArgsError + } + if !h.GetAuth(auth.APP, uuid).CanDoAny() { + return nil, oerr.NoAuth + } + key := utils.RandSeq(32) + err = cfg.DB().Model(&models.App{}).Where("UUID = ?", uuid).Update("Key", key).Error + if err != nil { + return nil, err + } + return key, nil + } if !h.GetAuth(auth.APP, uuid).CanRead() { return nil, oerr.NoAuth } @@ -63,9 +80,12 @@ func (h *appHandler) Get() (interface{}, error) { } func (h *appHandler) Post() (interface{}, error) { + if !h.Payload.GetAuth(auth.APP, "").CanCreate() { + return nil, oerr.NoAuth + } data := &struct { - Name string `json:"name"` - UUID string `json:"uuid"` + Name string + Icon string }{} err := h.Meta().ReadJson(data) if err != nil { @@ -74,51 +94,70 @@ func (h *appHandler) Post() (interface{}, error) { if data.Name == "" { return nil, oerr.ApiArgsMissing.AttachStr("name") } - _ = h.ParsePayload(h.Meta()) a := &models.App{ - UUID: data.UUID, - Name: data.Name, - Key: utils.RandSeq(32), - Creator: h.Payload.ID, - } - a.Key = utils.RandSeq(32) - if data.UUID != "" { - err = cfg.DB().Where("uuid = ?", data.UUID).FirstOrCreate(a).Error - } else { - data.UUID = utils.RandSeq(16) - err = cfg.DB().Create(a).Error - } + UUID: utils.RandSeq(16), + Name: data.Name, + Icon: data.Icon, + Creator: h.Payload.ID, + EnableRegister: false, + } + a.UUID = utils.RandSeq(16) + err = cfg.DB().Transaction(func(tx *gorm.DB) error { + e := tx.Create(a).Error + if e != nil { + return e + } + au := &models.AppUser{ + AppUUID: a.UUID, + UserID: h.Payload.ID, + Status: models.AUOK, + } + return tx.Create(au).Error + }) if err != nil { return nil, err } return a, nil } +func Struct2Map(obj interface{}) (data map[string]interface{}) { + data = make(map[string]interface{}) + objT := reflect.TypeOf(obj) + objV := reflect.ValueOf(obj) + var item reflect.Value + var k reflect.StructField + for i := 0; i < objT.NumField(); i++ { + k = objT.Field(i) + item = objV.Field(i) + if !item.IsNil() { + data[k.Name] = item.Interface() + } + } + return +} + func (h *appHandler) Patch() (interface{}, error) { uid := h.Meta().Params("uuid") + if uid == "" || !h.Payload.GetAuth(auth.APP, uid).CanUpdate() { + return nil, oerr.NoAuth + } opts := struct { - Icon string `json:"icon"` - Name string `json:"name"` + Icon *string + Name *string + EnableRegister *bool + Des *string + Host *string + UserRefreshUrl *string }{} if err := h.Meta().ReadJson(&opts); err != nil { return nil, err } - target := models.App{ - UUID: uid, - } - if err := cfg.DB().Where(&target).First(&target).Error; err != nil { - return nil, err - } - if !h.Payload.GetAuth(auth.APP, target.UUID).CanUpdate() { - return nil, oerr.NoAuth - } - if opts.Name != "" { - target.Name = opts.Name - } - if opts.Icon != "" { - target.Icon = opts.Icon + query := Struct2Map(opts) + log.Warn().Msgf("%#v", query) + if len(query) == 0 { + return nil, nil } - if err := cfg.DB().Updates(&target).Error; err != nil { + if err := cfg.DB().Table("Apps").Where("UUID = ?", uid).Updates(query).Error; err != nil { return nil, err } return nil, nil diff --git a/api/app/user.go b/api/app/user.go index b50d36b..c4bfb12 100644 --- a/api/app/user.go +++ b/api/app/user.go @@ -52,24 +52,24 @@ func (h *appUserHandler) Post() (interface{}, error) { } status := models.AUOK target := &models.App{} - err := cfg.DB().Where("uuid = ?", h.uuid).First(target).Error + err := cfg.DB().Where("UUID = ?", h.uuid).First(target).Error if err != nil { return nil, err } - if target.EnableRegister { + if !target.EnableRegister { status = models.AUApply } au, err := app.AddUser(cfg.DB(), h.uuid, uint(id), target.InitRoleID, status) return au, err } -func (h *appUserHandler) Update() (interface{}, error) { +func (h *appUserHandler) Patch() (interface{}, error) { id := h.Meta().ParamsInt("id") if h.uuid == "" || id <= 0 { return nil, oerr.ApiArgsMissing } props := struct { - Status string `json:"status"` + Status string }{} err := h.Meta().ReadJson(&props) if err != nil { @@ -82,7 +82,7 @@ func (h *appUserHandler) Update() (interface{}, error) { UserID: uint(id), AppUUID: h.uuid, } - err = cfg.DB().Where(au).Update("status", props.Status).Error + err = cfg.DB().Model(au).Where(au).Update("Status", props.Status).Error return nil, err } diff --git a/api/resource/resource.go b/api/resource/resource.go new file mode 100644 index 0000000..0f536a2 --- /dev/null +++ b/api/resource/resource.go @@ -0,0 +1,76 @@ +package resource + +import ( + "errors" + "github.com/veypi/OneAuth/cfg" + "github.com/veypi/OneAuth/libs/auth" + "github.com/veypi/OneAuth/libs/base" + "github.com/veypi/OneAuth/libs/oerr" + "github.com/veypi/OneAuth/models" + "github.com/veypi/OneBD" +) + +/** +* @name: resource +* @author: veypi +* @date: 2021-11-18 15:25 +* @description:resource +**/ + +var resP = OneBD.NewHandlerPool(func() OneBD.Handler { + return &resourceHandler{} +}) + +type resourceHandler struct { + base.AppHandler +} + +func (h *resourceHandler) Get() (interface{}, error) { + if !h.GetAuth(auth.Res, h.UUID).CanRead() { + return nil, oerr.NoAuth + } + list := make([]*models.Resource, 0, 10) + err := cfg.DB().Where("AppUUID = ?", h.UUID).Find(&list).Error + return list, err +} + +func (h *resourceHandler) Post() (interface{}, error) { + if !h.GetAuth(auth.Res, h.UUID).CanCreate() { + return nil, oerr.NoAuth + } + props := &struct { + Name string + Des string + }{} + err := h.Meta().ReadJson(props) + if err != nil { + return nil, err + } + res := &models.Resource{ + AppUUID: h.UUID, + Name: props.Name, + Des: props.Des, + } + err = cfg.DB().Create(res).Error + return res, err +} + +func (h *resourceHandler) Delete() (interface{}, error) { + if !h.GetAuth(auth.Res, h.UUID).CanDelete() { + return nil, oerr.NoAuth + } + id := uint(h.Meta().ParamsInt("id")) + if id <= 0 { + return nil, oerr.ApiArgsError + } + list := make([]*models.Auth, 0, 10) + err := cfg.DB().Where("ResourceID = ?", id).Find(&list).Error + if err != nil { + return nil, err + } + if len(list) > 0 { + return nil, errors.New("关联权限未删除") + } + err = cfg.DB().Delete(&models.Resource{}, id).Error + return nil, err +} diff --git a/api/resource/router.go b/api/resource/router.go new file mode 100644 index 0000000..5a5c1ca --- /dev/null +++ b/api/resource/router.go @@ -0,0 +1,17 @@ +package resource + +/** +* @name: router +* @author: veypi +* @date: 2021-11-18 15:24 +* @description:router +**/ +import ( + "github.com/veypi/OneBD" + "github.com/veypi/OneBD/rfc" +) + +func Router(r OneBD.Router) { + r.Set("/", resP, rfc.MethodGet, rfc.MethodPost) + r.Set("/:id", resP, rfc.MethodDelete) +} diff --git a/api/role/auth.go b/api/role/auth.go index cb768c2..fb758c4 100644 --- a/api/role/auth.go +++ b/api/role/auth.go @@ -28,7 +28,7 @@ func (h *authHandler) Get() (interface{}, error) { query := &models.Auth{} var err error if aid > 0 { - err = cfg.DB().Where("id = ?", aid).First(query).Error + err = cfg.DB().Where("ID = ?", aid).First(query).Error return query, err } id, _ := strconv.Atoi(h.Meta().Query("id")) @@ -37,12 +37,12 @@ func (h *authHandler) Get() (interface{}, error) { return nil, oerr.ApiArgsMissing } target := &models.App{} - err = cfg.DB().Where("uuid = ?", uuid).First(target).Error + err = cfg.DB().Where("UUID = ?", uuid).First(target).Error if err != nil { return nil, err } u := &models.User{} - err = cfg.DB().Preload("Roles.Auths").Preload("Auths").Where("id = ?", id).First(u).Error + err = cfg.DB().Preload("Roles.Auths").Preload("Auths").Where("ID = ?", id).First(u).Error if err != nil { return nil, err } diff --git a/api/role/role.go b/api/role/role.go index a0292da..2993ac0 100644 --- a/api/role/role.go +++ b/api/role/role.go @@ -14,28 +14,18 @@ var roleP = OneBD.NewHandlerPool(func() OneBD.Handler { return &roleHandler{} }) -type baseAppHandler struct { - base.ApiHandler - uuid string -} - -func (h *baseAppHandler) Init(m OneBD.Meta) error { - h.uuid = m.Params("uuid") - return h.ApiHandler.Init(m) -} - type roleHandler struct { - baseAppHandler + base.AppHandler } func (h *roleHandler) Get() (interface{}, error) { id := h.Meta().ParamsInt("id") - if !h.GetAuth(auth.Role, h.uuid).CanRead() { + if !h.GetAuth(auth.Role, h.UUID).CanRead() { return nil, oerr.NoAuth } if id > 0 { role := &models.Role{} - role.AppUUID = h.uuid + role.AppUUID = h.UUID role.ID = uint(id) err := cfg.DB().Preload("Auths").Preload("Users").First(role).Error if err != nil { @@ -44,7 +34,7 @@ func (h *roleHandler) Get() (interface{}, error) { return role, nil } roles := make([]*models.Role, 0, 10) - err := cfg.DB().Where("app_uuid = ?", h.uuid).Find(&roles).Error + err := cfg.DB().Where("AppUUID = ?", h.UUID).Find(&roles).Error return roles, err } @@ -69,9 +59,9 @@ func (h *roleHandler) Patch() (interface{}, error) { return nil, oerr.NoAuth } query := &struct { - Name *string `json:"name"` + Name *string // 角色标签 - Tag *string `json:"tag" gorm:"default:''"` + Tag *string `gorm:"default:''"` }{} err := h.Meta().ReadJson(query) if err != nil { @@ -90,13 +80,13 @@ func (h *roleHandler) Patch() (interface{}, error) { return nil, cfg.DB().Transaction(func(tx *gorm.DB) error { var err error if query.Tag != nil && *query.Tag != role.Tag { - err = tx.Model(role).Update("tag", *query.Tag).Error + err = tx.Model(role).Update("Tag", *query.Tag).Error if err != nil { return err } } if query.Name != nil && *query.Name != role.Name { - err = tx.Model(role).Update("name", *query.Name).Error + err = tx.Model(role).Update("Name", *query.Name).Error if err != nil { return err } diff --git a/api/role/roleAuth.go b/api/role/roleAuth.go index 4d6ed2d..c9d3dfc 100644 --- a/api/role/roleAuth.go +++ b/api/role/roleAuth.go @@ -20,10 +20,9 @@ var rap = OneBD.NewHandlerPool(func() OneBD.Handler { }) type roleAuthHandler struct { - base.ApiHandler - id uint - aid uint - uuid string + base.AppHandler + id uint + aid uint } func (h *roleAuthHandler) Init(m OneBD.Meta) error { @@ -36,7 +35,7 @@ func (h *roleAuthHandler) Init(m OneBD.Meta) error { } func (h *roleAuthHandler) Post() (interface{}, error) { - if !h.Payload.GetAuth(auth.Auth, h.uuid).CanCreate() { + if !h.Payload.GetAuth(auth.Auth, h.UUID).CanCreate() { return nil, oerr.NoAuth } return nil, nil diff --git a/api/role/router.go b/api/role/router.go index b9aa0fe..ab42b8f 100644 --- a/api/role/router.go +++ b/api/role/router.go @@ -6,9 +6,9 @@ import ( ) func Router(r OneBD.Router) { - r.Set("/role/", roleP, rfc.MethodGet, rfc.MethodPost) - r.Set("/role/:id", roleP, rfc.MethodGet, rfc.MethodDelete, rfc.MethodPatch) - r.Set("/role/:id/auth/:aid", roleP, rfc.MethodGet) + r.Set("/", roleP, rfc.MethodGet, rfc.MethodPost) + r.Set("/:id", roleP, rfc.MethodGet, rfc.MethodDelete, rfc.MethodPatch) + r.Set("/:id/auth/:aid", roleP, rfc.MethodGet) r.Set("/auth/", authP, rfc.MethodGet) r.Set("/auth/:id", authP, rfc.MethodGet) } diff --git a/api/token/token.go b/api/token/token.go index cab9cb7..4771a41 100644 --- a/api/token/token.go +++ b/api/token/token.go @@ -31,7 +31,7 @@ func (h *tokenHandler) Get() (interface{}, error) { } a := &models.App{} a.UUID = uuid - err := cfg.DB().Where("uuid = ?", uuid).First(a).Error + err := cfg.DB().Where("UUID = ?", uuid).First(a).Error if err != nil { return nil, err } @@ -61,7 +61,7 @@ func (h *tokenHandler) Get() (interface{}, error) { return nil, oerr.NoAuth.AttachStr(string(au.Status)) } u := &models.User{} - err = cfg.DB().Preload("Auths").Preload("Roles.Auths").Where("id = ?", h.Payload.ID).First(u).Error + err = cfg.DB().Preload("Auths").Preload("Roles.Auths").Where("ID = ?", h.Payload.ID).First(u).Error if err != nil { return nil, err } diff --git a/api/user/user.go b/api/user/user.go index 422cc8f..7c99cfe 100644 --- a/api/user/user.go +++ b/api/user/user.go @@ -80,18 +80,19 @@ func (h *handler) Post() (interface{}, error) { if err != nil { return nil, oerr.DBErr.Attach(err) } + log.Warn().Msgf("%v %v", self.EnableRegister, h.GetAuth(auth.User, "").CanCreate()) if !self.EnableRegister && !h.Payload.GetAuth(auth.User, "").CanCreate() { - return nil, oerr.NoAuth.AttachStr("register disabled") + return nil, errors.New("register disabled") } var userdata = struct { - Username string `json:"username"` - Password string `json:"password"` - Nickname string `json:"nickname"` - Phone string `json:"phone"` - Email string `json:"email"` - Domain string `json:"domain"` - Title string `json:"title"` - Position string `json:"position"` + Username string + Password string + Nickname string + Phone string + Email string + Domain string + Title string + Position string }{} if err := h.Meta().ReadJson(&userdata); err != nil { return nil, err @@ -137,13 +138,13 @@ func (h *handler) Post() (interface{}, error) { func (h *handler) Patch() (interface{}, error) { uid := h.Meta().Params("user_id") opts := struct { - Password string `json:"password"` - Icon string `json:"icon"` - Nickname string `json:"nickname"` - Phone string `json:"phone" gorm:"type:varchar(20);unique;default:null" json:",omitempty"` - Email string `json:"email" gorm:"type:varchar(50);unique;default:null" json:",omitempty"` - Status string `json:"status"` - Position string `json:"position"` + Password string + Icon string + Nickname string + Phone string `gorm:"type:varchar(20);unique;default:null" json:",omitempty"` + Email string `gorm:"type:varchar(50);unique;default:null" json:",omitempty"` + Status string + Position string }{} if err := h.Meta().ReadJson(&opts); err != nil { return nil, err @@ -208,7 +209,7 @@ func (h *handler) Head() (interface{}, error) { return nil, oerr.ApiArgsError } h.User = new(models.User) - uidType := h.Meta().Query("uid_type") + uidType := h.Meta().Query("UidType") switch uidType { case "username": h.User.Username = uid diff --git a/api/wx/login.go b/api/wx/login.go index bd1cd82..55edd1f 100644 --- a/api/wx/login.go +++ b/api/wx/login.go @@ -100,9 +100,9 @@ func requestCorpToken(corpid, corpsecret string) (string, error) { "corpsecret": corpsecret, } res := &struct { - Errmsg string `json:"errmsg"` - Errcode *uint `json:"errcode"` - AccessToken string `json:"access_token"` + Errmsg string + Errcode *uint + AccessToken string }{} err := tools.Query(addr, query, res) if err != nil { @@ -120,10 +120,10 @@ func requestCorpToken(corpid, corpsecret string) (string, error) { func getUserID(token, code string) (string, error) { addr := "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo" res := &struct { - Errmsg string `json:"errmsg"` - Errcode *uint `json:"errcode"` - UserId string `json:"UserId"` - DeviceId string `json:"device_id"` + Errmsg string + Errcode *uint + UserId string + DeviceId string }{} query := map[string]string{ "access_token": token, diff --git a/cfg/cfg.go b/cfg/cfg.go index 60fe07c..e358f6f 100644 --- a/cfg/cfg.go +++ b/cfg/cfg.go @@ -6,6 +6,7 @@ import ( "gorm.io/driver/mysql" "gorm.io/driver/sqlite" "gorm.io/gorm" + "gorm.io/gorm/schema" ) var Path = cmd.GetCfgPath("oa", "settings") @@ -63,14 +64,22 @@ func DB() *gorm.DB { } return db } + +var gormCfg = &gorm.Config{ + NamingStrategy: schema.NamingStrategy{ + SingularTable: false, // 使用单数表名,启用该选项后,`User` 表将是`user` + NoLowerCase: true, + }, +} + func ConnectDB() *gorm.DB { var err error conn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local", CFG.DB.User, CFG.DB.Pass, CFG.DB.Addr, CFG.DB.DB) if CFG.DB.Type == "sqlite" { conn = CFG.DB.Addr - db, err = gorm.Open(sqlite.Open(conn), &gorm.Config{}) + db, err = gorm.Open(sqlite.Open(conn), gormCfg) } else { - db, err = gorm.Open(mysql.Open(conn), &gorm.Config{}) + db, err = gorm.Open(mysql.Open(conn), gormCfg) } if err != nil { diff --git a/go.mod b/go.mod index 01fa344..259e468 100644 --- a/go.mod +++ b/go.mod @@ -12,4 +12,4 @@ require ( gorm.io/gorm v1.21.3 ) -//replace github.com/veypi/OneBD v0.4.1 => ../OceanCurrent/OneBD +replace github.com/veypi/OneBD v0.4.1 => ../OceanCurrent/OneBD diff --git a/libs/app/user.go b/libs/app/user.go index 0c29669..eb218a9 100644 --- a/libs/app/user.go +++ b/libs/app/user.go @@ -32,7 +32,7 @@ func AddUser(tx *gorm.DB, uuid string, userID uint, roleID uint, status models.A return nil, err } } - err = tx.Model(&models.App{}).Where("uuid = ?", uuid).Update("user_count", gorm.Expr("user_count + ?", 1)).Error + err = tx.Model(&models.App{}).Where("UUID = ?", uuid).Update("UserCount", gorm.Expr("UserCount + ?", 1)).Error return au, err } return nil, err @@ -50,7 +50,7 @@ func EnableUser(tx *gorm.DB, uuid string, userID uint) error { return err } if au.Status != models.AUOK { - return tx.Where(au).Update("status", models.AUOK).Error + return tx.Where(au).Update("Status", models.AUOK).Error } return nil } @@ -63,5 +63,5 @@ func DisableUser(tx *gorm.DB, uuid string, userID uint) error { AppUUID: uuid, } au.UserID = userID - return tx.Where(au).Update("status", models.AUDisable).Error + return tx.Where(au).Update("Status", models.AUDisable).Error } diff --git a/libs/auth/auth.go b/libs/auth/auth.go index 419fc99..97f0d9f 100644 --- a/libs/auth/auth.go +++ b/libs/auth/auth.go @@ -32,8 +32,8 @@ func BindUserRole(tx *gorm.DB, userID uint, roleID uint) error { ur.UserID = userID err = utils.MultiErr( tx.Where(ur).FirstOrCreate(ur).Error, - tx.Model(&models.Role{}).Where("id = ?", roleID). - Update("user_count", gorm.Expr("user_count + ?", 1)).Error, + tx.Model(&models.Role{}).Where("ID = ?", roleID). + Update("UserCount", gorm.Expr("UserCount + ?", 1)).Error, ) return err } diff --git a/libs/base/app_handler.go b/libs/base/app_handler.go new file mode 100644 index 0000000..095d98d --- /dev/null +++ b/libs/base/app_handler.go @@ -0,0 +1,20 @@ +package base + +import "github.com/veypi/OneBD" + +/** +* @name: app_handler +* @author: veypi +* @date: 2021-11-18 15:27 +* @description:app_handler +**/ + +type AppHandler struct { + ApiHandler + UUID string +} + +func (h *AppHandler) Init(m OneBD.Meta) error { + h.UUID = m.Params("uuid") + return h.ApiHandler.Init(m) +} diff --git a/models/app.go b/models/app.go index 92dac6d..298e5e2 100644 --- a/models/app.go +++ b/models/app.go @@ -3,46 +3,46 @@ package models var AppKeys = map[string]string{} type App struct { - UUID string `json:"uuid" gorm:"primaryKey;size:32"` - CreatedAt JSONTime `json:"created_at"` - UpdatedAt JSONTime `json:"updated_at"` - DeletedAt *JSONTime `json:"deleted_at" sql:"index"` - Name string `json:"name"` - Icon string `json:"icon"` - Des string `json:"des"` - Creator uint `json:"creator"` - UserCount uint `json:"user_count"` - Users []*User `json:"users" gorm:"many2many:app_users;"` + UUID string `gorm:"primaryKey;size:32"` + CreatedAt JSONTime + UpdatedAt JSONTime + DeletedAt *JSONTime `sql:"index"` + Name string + Icon string + Des string + Creator uint + UserCount uint + Users []*User `gorm:"many2many:AppUsers;"` // 初始用户角色 - InitRoleID uint `json:"init_role_id"` - InitRole *Role `json:"init_role"` + InitRoleID uint + InitRole *Role // 是否在首页隐藏 - Hide bool `json:"hide"` - // PubKey string `json:"pub_key"` - // PrivateKey string `json:"private_key"` + Hide bool + // PubKey string + // PrivateKey string // 认证成功跳转链接 - Host string `json:"host"` + Host string // 加解密用户token (key+key2) // 两个key都是请求获取时刷新 // key oa发放给app 双方保存 针对app生成 每个应用有一个 // key2 app发放给oa app保存 oa使用一次销毁 针对当个用户生成 每个用户有一个 // 获取app用户加密秘钥key2 // TODO - UserRefreshUrl string `json:"user_refresh_url"` + UserRefreshUrl string // app 校验用户token时使用 Key string `json:"-"` // 是否允许用户自动加入应用 - EnableRegister bool `json:"enable_register"` + EnableRegister bool // - EnableUserKey bool `json:"enable_user_key"` - UserKeyUrl string `json:"user_key_url"` + EnableUserKey bool + UserKeyUrl string // 允许登录方式 - EnableUser bool `json:"enable_user"` - EnableWx bool `json:"enable_wx"` - EnablePhone bool `json:"enable_phone"` - EnableEmail bool `json:"enable_email"` + EnableUser bool + EnableWx bool + EnablePhone bool + EnableEmail bool - Wx *Wechat `json:"wx" gorm:"-"` + Wx *Wechat `gorm:"-"` } type AUStatus string @@ -56,23 +56,23 @@ const ( type AppUser struct { BaseModel - AppUUID string `json:"app_uuid" gorm:"size:32"` - App *App `json:"app" gorm:"association_foreignkey:UUID"` - UserID uint `json:"user_id"` - User *User `json:"user"` - Status AUStatus `json:"status"` + AppUUID string `gorm:"size:32"` + App *App `gorm:"association_foreignkey:UUID"` + UserID uint + User *User + Status AUStatus } type Wechat struct { BaseModel - AppUUID string `json:"app_uuid" gorm:"size:32"` - App *App `json:"app" gorm:"association_foreignkey:UUID"` + AppUUID string `gorm:"size:32"` + App *App `gorm:"association_foreignkey:UUID"` // 网页授权登录用 - WxID string `json:"wx_id"` - AgentID string `json:"agent_id"` - Url string `json:"url"` + WxID string + AgentID string + Url string // 获取access_token用 - CorpID string `json:"corp_id"` - CorpSecret string `json:"corp_secret"` + CorpID string + CorpSecret string } diff --git a/models/init.go b/models/init.go index 5cf6312..3ebfd49 100644 --- a/models/init.go +++ b/models/init.go @@ -95,8 +95,8 @@ func (jt *JSONTime) SetTime(t time.Time) { } type BaseModel struct { - ID uint `json:"id" gorm:"primaryKey"` - CreatedAt JSONTime `json:"created_at"` - UpdatedAt JSONTime `json:"updated_at"` - DeletedAt *JSONTime `json:"deleted_at" sql:"index"` + ID uint `gorm:"primaryKey"` + CreatedAt JSONTime `` + UpdatedAt JSONTime `` + DeletedAt *JSONTime `sql:"index"` } diff --git a/models/message.go b/models/message.go index 512274c..e294524 100644 --- a/models/message.go +++ b/models/message.go @@ -2,10 +2,10 @@ package models type Message struct { BaseModel - UserID uint `json:"user_id"` - User *User `json:"user"` - Title string `json:"title"` - Redirect string `json:"redirect"` - Content string `json:"content"` - From string `json:"from"` + UserID uint + User *User + Title string + Redirect string + Content string + From string } diff --git a/models/role.go b/models/role.go index 4bb3e6a..26e05c4 100644 --- a/models/role.go +++ b/models/role.go @@ -4,50 +4,48 @@ import "github.com/veypi/OneAuth/oalib" type UserRole struct { BaseModel - UserID uint `json:"user_id"` - RoleID uint `json:"role_id"` + UserID uint + RoleID uint } type Role struct { BaseModel - AppUUID string `json:"app_uuid" gorm:"size:32"` - App *App `json:"app" gorm:"association_foreignkey:UUID"` - Name string `json:"name"` + AppUUID string `gorm:"size:32"` + App *App `gorm:"association_foreignkey:UUID"` + Name string // 角色标签 - Tag string `json:"tag" gorm:"default:''"` - Users []*User `json:"users" gorm:"many2many:user_roles;"` + Tag string `gorm:"default:''"` + Users []*User `gorm:"many2many:UserRoles;"` // 具体权限 - Auths []*Auth `json:"auths" gorm:"foreignkey:RoleID;references:ID"` - UserCount uint `json:"user_count"` + Auths []*Auth `gorm:"foreignkey:RoleID;references:ID"` + UserCount uint } // Auth 资源权限 type Auth struct { BaseModel // 该权限作用的应用 - AppUUID string `json:"app_uuid" gorm:"size:32"` - App *App `json:"app" gorm:"association_foreignkey:UUID"` + AppUUID string `gorm:"size:32"` + App *App `gorm:"association_foreignkey:UUID"` // 权限绑定只能绑定一个 - RoleID *uint `json:"role_id" gorm:""` - Role *Role `json:"role"` - UserID *uint `json:"user_id"` - User *User `json:"user"` + RoleID *uint `gorm:""` + Role *Role + UserID *uint + User *User // 资源id - ResourceID uint `json:"resource_id" gorm:"not null"` - Resource *Resource `json:"resource"` + ResourceID uint `gorm:"not null"` + Resource *Resource // resource_name 用于其他系统方便区分权限的名字 - RID string `json:"rid" gorm:""` + RID string `gorm:""` // 具体某个资源的id - RUID string `json:"ruid"` - Level oalib.AuthLevel `json:"level"` + RUID string + Level oalib.AuthLevel } type Resource struct { BaseModel - AppUUID string `json:"app_uuid" gorm:"size:32"` - App *App `json:"app" gorm:"association_foreignkey:UUID"` - Name string `json:"name"` - // 权限标签 - Tag string `json:"tag"` - Des string `json:"des"` + AppUUID string ` gorm:"size:32"` + App *App `gorm:"association_foreignkey:UUID"` + Name string + Des string } diff --git a/models/user.go b/models/user.go index ab2f9d1..89a4048 100644 --- a/models/user.go +++ b/models/user.go @@ -9,20 +9,20 @@ import ( // User db user model type User struct { BaseModel - Username string `json:"username" gorm:"type:varchar(100);unique;not null"` - Nickname string `json:"nickname" gorm:"type:varchar(100)" json:",omitempty"` - Phone string `json:"phone" gorm:"type:varchar(20);unique;default:null" json:",omitempty"` - Email string `json:"email" gorm:"type:varchar(50);unique;default:null" json:",omitempty"` + Username string `gorm:"type:varchar(100);unique;not null"` + Nickname string `gorm:"type:varchar(100)"` + Phone string `gorm:"type:varchar(20);unique;default:null"` + Email string `gorm:"type:varchar(50);unique;default:null"` CheckCode string `gorm:"type:varchar(64);not null" json:"-"` RealCode string `gorm:"type:varchar(32);not null" json:"-"` - Position string `json:"position"` + Position string // disabled 禁用 - Status string `json:"status"` + Status string - Icon string `json:"icon"` - Roles []*Role `json:"roles" gorm:"many2many:user_roles;"` - Apps []*App `json:"apps" gorm:"many2many:app_users;"` - Auths []*Auth `json:"auths" gorm:"foreignkey:UserID;references:ID"` + Icon string + Roles []*Role `gorm:"many2many:UserRoles;"` + Apps []*App `gorm:"many2many:AppUsers;"` + Auths []*Auth `gorm:"foreignkey:UserID;references:ID"` } func (u *User) String() string { @@ -30,7 +30,7 @@ func (u *User) String() string { } func (u *User) LoadAuths(tx *gorm.DB) error { - return tx.Where("id = ?", u.ID).Preload("Auths").Preload("Roles.Auths").First(u).Error + return tx.Where("ID = ?", u.ID).Preload("Auths").Preload("Roles.Auths").First(u).Error } func (u *User) GetAuths() []*Auth { diff --git a/oaf/index.html b/oaf/index.html index c53ce12..88f0481 100644 --- a/oaf/index.html +++ b/oaf/index.html @@ -4,7 +4,7 @@ - Vite App + OA
diff --git a/oaf/src/App.vue b/oaf/src/App.vue index 5f26469..25329cb 100644 --- a/oaf/src/App.vue +++ b/oaf/src/App.vue @@ -4,7 +4,8 @@ @@ -14,7 +15,6 @@ // This starter template is using Vue 3 diff --git a/oaf/src/components/siderframe.vue b/oaf/src/components/siderframe.vue index 6764b8c..60a31c7 100644 --- a/oaf/src/components/siderframe.vue +++ b/oaf/src/components/siderframe.vue @@ -7,14 +7,43 @@ show-trigger="bar" content-style="padding: 4px;" bordered - default-collapsed + :default-collapsed="true" :native-scrollbar="false" - style="height: calc(100vh - 108px)" + :style="{'height': $store.state.height}" > - - + + + + + + + + + +
+ +
@@ -22,6 +51,40 @@