Files

66 lines
1.9 KiB
Go
Raw Normal View History

2021-09-28 11:47:19 +08:00
package model
import (
"SciencesServer/app/common/model"
"SciencesServer/serve/orm"
"fmt"
2022-01-07 17:24:39 +08:00
"strings"
2021-09-28 11:47:19 +08:00
)
type SysAuth struct {
*model.SysAuth
}
// SysAuthScene 信息
type SysAuthScene struct {
2022-01-07 17:24:39 +08:00
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, ":", "/")
2021-09-28 11:47:19 +08:00
}
// TenantAuth 租户权限
func (m *SysAuth) TenantAuth(tenantID uint64) ([]*SysAuthScene, error) {
out := make([]*SysAuthScene, 0)
db := orm.GetDB().Table(m.TableName()+" AS a").
2022-01-12 15:05:14 +08:00
Select("a.id", "a.parent_id", "a.kind", "a.name", "t_a.id AS scene_id").
2021-09-28 11:47:19 +08:00
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",
2022-01-07 17:24:39 +08:00
model.NewSysTenantAuth().TableName(), tenantID, model.DeleteStatusForNot)).
2021-09-28 11:47:19 +08:00
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").
2022-01-07 17:24:39 +08:00
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)
2021-09-28 11:47:19 +08:00
2022-01-07 17:24:39 +08:00
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))
}
2021-09-28 11:47:19 +08:00
if err := db.Scan(&out).Error; err != nil {
return nil, err
}
return out, nil
}
func NewSysAuth() *SysAuth {
return &SysAuth{SysAuth: model.NewSysAuth()}
}