mirror of https://github.com/veypi/OneAuth.git
feat: token gen
parent
14931a51e5
commit
3948d51728
@ -1,31 +1,70 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/veypi/utils/logv"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
BaseModel
|
BaseModel
|
||||||
Name string `json:"name" methods:"get,post,*patch,*list" parse:"json"`
|
Name string `json:"name" methods:"get,post,*patch,*list" parse:"json"`
|
||||||
Icon string `json:"icon" methods:"post,*patch" parse:"json"`
|
Icon string `json:"icon" methods:"post,*patch" parse:"json"`
|
||||||
Des string `json:"des" 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"`
|
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"`
|
InitRoleID *string `json:"init_role_id" gorm:"index;type:varchar(32);default: null" methods:"*patch" parse:"json"`
|
||||||
InitRole *Role `json:"init_role" gorm:"foreignKey:ID;references:InitRoleID"`
|
InitRole *Role `json:"init_role" gorm:"foreignKey:InitRoleID;references:ID"`
|
||||||
InitUrl string `json:"init_url"`
|
InitUrl string `json:"init_url"`
|
||||||
UserCount uint `json:"user_count"`
|
UserCount uint `json:"user_count"`
|
||||||
Key string `json:"-"`
|
Key string `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AppUser struct {
|
type AppUser struct {
|
||||||
BaseModel
|
BaseModel
|
||||||
AppID string `json:"app_id" methods:"get,*list,post,*patch" parse:"path"`
|
AppID string `json:"app_id" methods:"get,*list,post" parse:"path"`
|
||||||
App *App `json:"app"`
|
App *App `json:"-" gorm:"foreignKey:AppID;references:ID"`
|
||||||
UserID string `json:"user_id" methods:"get,*list,post,*patch" parse:"path"`
|
|
||||||
User *User `json:"user"`
|
UserID string `json:"user_id" methods:"get,*list,post" parse:"path"`
|
||||||
|
User *User `json:"-" gorm:"foreignKey:UserID;references:ID"`
|
||||||
|
|
||||||
Status string `json:"status" methods:"post,*patch,*list" parse:"json"`
|
Status string `json:"status" methods:"post,*patch,*list" parse:"json"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *AppUser) onOk(tx *gorm.DB) (err error) {
|
||||||
|
app := &App{}
|
||||||
|
logv.AssertError(tx.Where("id = ?", m.AppID).First(app).Error)
|
||||||
|
if app.InitRoleID != nil {
|
||||||
|
urList := make([]*UserRole, 0, 2)
|
||||||
|
logv.AssertError(tx.Where("app_id = ? && user_id = ?", m.AppID, m.UserID).Find(&urList).Error)
|
||||||
|
if len(urList) == 0 {
|
||||||
|
return tx.Create(&UserRole{
|
||||||
|
AppID: m.AppID,
|
||||||
|
UserID: m.UserID,
|
||||||
|
RoleID: *app.InitRoleID,
|
||||||
|
Status: "ok",
|
||||||
|
}).Error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *AppUser) AfterCreate(tx *gorm.DB) error {
|
||||||
|
if m.Status == "ok" {
|
||||||
|
logv.AssertError(m.onOk(tx))
|
||||||
|
}
|
||||||
|
return tx.Model(&App{}).Where("id = ?", m.AppID).Update("user_count", gorm.Expr("user_count + ?", 1)).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *AppUser) AfterUpdate(tx *gorm.DB) error {
|
||||||
|
if m.Status == "ok" {
|
||||||
|
return m.onOk(tx)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type Resource struct {
|
type Resource struct {
|
||||||
BaseDate
|
BaseDate
|
||||||
AppID string `json:"app_id" gorm:"primaryKey;type:varchar(32)" methods:"post,list,delete" parse:"json"`
|
AppID string `json:"app_id" gorm:"primaryKey;type:varchar(32)" methods:"post,list,delete" parse:"json"`
|
||||||
App *App `json:"app" gorm:"foreignKey:ID;references:AppID"`
|
App *App `json:"-" gorm:"foreignKey:AppID;references:ID"`
|
||||||
Name string `json:"name" gorm:"primaryKey" methods:"post,delete" parse:"json"`
|
Name string `json:"name" gorm:"primaryKey" methods:"post,delete" parse:"json"`
|
||||||
Des string `json:"des" methods:"post" parse:"json"`
|
Des string `json:"des" methods:"post" parse:"json"`
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue