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/app.go

77 lines
2.4 KiB
Go

package models
2 months ago
import (
"github.com/veypi/utils/logv"
"gorm.io/gorm"
)
4 weeks ago
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"`
4 weeks ago
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"`
2 months ago
UserID string `json:"user_id" methods:"get,*list,post" parse:"json"`
User *User `json:"user" gorm:"foreignKey:UserID;references:ID"`
2 months ago
2 months ago
Status string `json:"status" methods:"post,*patch,*list" parse:"json"`
}
2 months ago
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)
2 months ago
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 {
4 weeks ago
if m.Status == AUSTATUS_OK {
2 months ago
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 {
4 weeks ago
if m.Status == AUSTATUS_OK {
2 months ago
return m.onOk(tx)
}
return nil
}
type Resource struct {
3 weeks ago
BaseModel
AppID string `json:"app_id" methods:"get,list,post,patch,delete" parse:"path"`
2 months ago
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"`
}