You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/oa/models/init.go

93 lines
2.5 KiB
Go

//
// Copyright (C) 2024 veypi <i@veypi.com>
// 2024-09-20 16:10:16
// Distributed under terms of the MIT license.
//
package models
import (
"oa/cfg"
2 months ago
"strings"
"time"
2 months ago
"github.com/google/uuid"
"github.com/veypi/utils/logv"
"gorm.io/gorm"
)
type BaseModel struct {
// ID uint `json:"id" gorm:"primaryKey" methods:"get,patch,delete" parse:"path"`
2 months ago
ID string `json:"id" gorm:"primaryKey;type:varchar(32)" methods:"get,patch,delete" parse:"path"`
BaseDate
}
2 months ago
func (m *BaseModel) BeforeCreate(tx *gorm.DB) error {
if m.ID == "" {
m.ID = strings.ReplaceAll(uuid.New().String(), "-", "")
}
return nil
}
type BaseDate struct {
CreatedAt time.Time `json:"created_at" methods:"*list" parse:"query"`
UpdatedAt time.Time `json:"updated_at" methods:"*list" parse:"query"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
func init() {
2 months ago
cfg.CmdDB.SubCommand("init", "init db data").Command = InitDBData
cfg.ObjList = append(cfg.ObjList, &AppUser{})
cfg.ObjList = append(cfg.ObjList, &Resource{})
cfg.ObjList = append(cfg.ObjList, &Access{})
cfg.ObjList = append(cfg.ObjList, &Role{})
cfg.ObjList = append(cfg.ObjList, &User{})
cfg.ObjList = append(cfg.ObjList, &UserRole{})
2 months ago
cfg.ObjList = append(cfg.ObjList, &Token{})
cfg.ObjList = append(cfg.ObjList, &App{})
}
2 months ago
func InitDBData() error {
app := &App{}
app.ID = cfg.Config.ID
app.Name = "OA"
app.Icon = "/favicon.ico"
app.Key = cfg.Config.Key
2 months ago
logv.AssertError(cfg.DB().Where("id = ?", app.ID).Attrs(app).FirstOrCreate(app).Error)
initRole := map[string]map[string]uint{
"user": {"admin": 5, "normal": 1},
"app": {"admin": 5, "normal": 2},
"fs": {"admin": 5, "normal": 5},
2 months ago
}
adminID := ""
for r, roles := range initRole {
logv.AssertError(cfg.DB().Where("app_id = ? AND name = ?", app.ID, r).FirstOrCreate(&Resource{
AppID: app.ID,
Name: r,
}).Error)
for rName, l := range roles {
role := &Role{}
logv.AssertError(cfg.DB().Where("app_id = ? AND name = ?", app.ID, rName).Attrs(&Role{
BaseModel: BaseModel{
ID: strings.ReplaceAll(uuid.New().String(), "-", ""),
},
AppID: app.ID,
Name: rName,
}).FirstOrCreate(role).Error)
logv.AssertError(cfg.DB().Where("app_id = ? AND role_id = ? AND name = ?", app.ID, role.ID, r).FirstOrCreate(&Access{
AppID: app.ID,
RoleID: &role.ID,
Name: r,
Level: l,
}).Error)
if rName == "admin" {
adminID = role.ID
}
}
}
if app.InitRoleID == nil {
logv.AssertError(cfg.DB().Model(app).Update("init_role_id", adminID).Error)
}
return nil
}