mirror of https://github.com/veypi/OneAuth.git
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.
77 lines
2.4 KiB
Go
77 lines
2.4 KiB
Go
package models
|
|
|
|
import (
|
|
"github.com/veypi/utils/logv"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const AUSTATUS_OK = "ok"
|
|
const AUSTATUS_NO = "no"
|
|
const AUSTATUS_APPLYING = "applying"
|
|
const AUSTATUS_REJECT = "reject"
|
|
|
|
type App struct {
|
|
BaseModel
|
|
Name string `json:"name" methods:"post,*patch,*list" parse:"json"`
|
|
Icon string `json:"icon" methods:"post,*patch" parse:"json"`
|
|
Des string `json:"des" methods:"*post,*patch" parse:"json"`
|
|
Typ string `json:"typ" gorm:"default:public" methods:"post,*patch" parse:"json"`
|
|
Status string `json:"status" gorm:"default:ok" methods:"post,*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:InitRoleID;references:ID"`
|
|
InitUrl string `json:"init_url"`
|
|
UserCount uint `json:"user_count"`
|
|
Key string `json:"-"`
|
|
}
|
|
|
|
type AppUser struct {
|
|
BaseModel
|
|
AppID string `json:"app_id" methods:"get,list,post,patch,delete" parse:"path"`
|
|
App *App `json:"app" gorm:"foreignKey:AppID;references:ID"`
|
|
|
|
UserID string `json:"user_id" methods:"get,*list,post" parse:"json"`
|
|
User *User `json:"user" gorm:"foreignKey:UserID;references:ID"`
|
|
|
|
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.Debug().Where("app_id = ? AND 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 == AUSTATUS_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 == AUSTATUS_OK {
|
|
return m.onOk(tx)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type Resource struct {
|
|
BaseDate
|
|
AppID string `json:"app_id" methods:"get,list,post,patch,delete" parse:"path"`
|
|
App *App `json:"-" gorm:"foreignKey:AppID;references:ID"`
|
|
Name string `json:"name" gorm:"primaryKey" methods:"post,delete" parse:"json"`
|
|
Des string `json:"des" methods:"post" parse:"json"`
|
|
}
|