66 lines
1.9 KiB
Go
66 lines
1.9 KiB
Go
package model
|
|
|
|
import (
|
|
"SciencesServer/app/common/model"
|
|
"SciencesServer/serve/orm"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type SysAuth struct {
|
|
*model.SysAuth
|
|
}
|
|
|
|
// SysAuthScene 信息
|
|
type SysAuthScene struct {
|
|
model.Model
|
|
ParentID uint64 `json:"parent_id"`
|
|
Kind model.SysAuthKind `json:"kind"`
|
|
Name string `json:"name"`
|
|
SceneID uint64 `json:"scene_id"`
|
|
}
|
|
|
|
func (m *SysAuth) FilterAuth() string {
|
|
return "/" + strings.ReplaceAll(m.Auth, ":", "/")
|
|
}
|
|
|
|
// TenantAuth 租户权限
|
|
func (m *SysAuth) TenantAuth(tenantID uint64) ([]*SysAuthScene, error) {
|
|
out := make([]*SysAuthScene, 0)
|
|
|
|
db := orm.GetDB().Table(m.TableName()+" AS a").
|
|
Select("a.id", "a.parent_id", "a.kind", "a.name", "t_a.id AS scene_id").
|
|
Joins(fmt.Sprintf("LEFT JOIN %s AS t_a ON t_a.auth_id = a.id AND t_a.tenant_id = %d AND t_a.is_deleted = %d",
|
|
model.NewSysTenantAuth().TableName(), tenantID, model.DeleteStatusForNot)).
|
|
Where("a.is_deleted = ?", model.DeleteStatusForNot)
|
|
|
|
if err := db.Scan(&out).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// RoleAuth 角色权限
|
|
func (m *SysAuth) RoleAuth(tenantID, roleID uint64) ([]*SysAuthScene, error) {
|
|
out := make([]*SysAuthScene, 0)
|
|
|
|
db := orm.GetDB().Table(m.TableName()+" AS a").
|
|
Select("a.id", "a.parent_id", "a.kind", "a.name", "r_a.id AS scene_id").
|
|
Joins(fmt.Sprintf("LEFT JOIN %s AS r_a ON a.id = r_a.auth_id AND r_a.role_id = %d AND r_a.is_deleted = %d",
|
|
model.NewSysRoleAuth().TableName(), roleID, model.DeleteStatusForNot)).
|
|
Where("a.is_deleted = ?", model.DeleteStatusForNot)
|
|
|
|
if tenantID > 0 {
|
|
db = db.Joins(fmt.Sprintf("RIGHT JOIN %s AS t_a ON a.id = t_a.auth_id AND t_a.tenant_id = %d AND t_a.is_deleted = %d",
|
|
model.NewSysTenantAuth().TableName(), tenantID, model.DeleteStatusForNot))
|
|
}
|
|
if err := db.Scan(&out).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func NewSysAuth() *SysAuth {
|
|
return &SysAuth{SysAuth: model.NewSysAuth()}
|
|
}
|