sync: 同步 v1.0.4 并脱敏配置与镜像
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// package system
|
||||
// @Link https://github.com/xiujiecn/xiu-admin
|
||||
// @Copyright Copyright (c) 2025 LiXiujie
|
||||
// @Author Lxj <li@xiujie.cn>
|
||||
// @Author Lxj <xxx@example.com>
|
||||
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
||||
package system
|
||||
|
||||
@@ -47,7 +47,7 @@ func (l *sSysRole) Model(ctx context.Context, option ...*handler.Option) *gdb.Mo
|
||||
FilterAuth: false, // 角色表不验证数据权限
|
||||
})
|
||||
}
|
||||
return handler.Model(dao.SysRole.Ctx(ctx), option...)
|
||||
return handler.Model(dao.SysRole.Ctx(ctx).Master(), option...)
|
||||
}
|
||||
|
||||
func (l *sSysRole) ModelQuery(ctx context.Context, option ...*handler.Option) *gdb.Model {
|
||||
@@ -57,13 +57,12 @@ func (l *sSysRole) ModelQuery(ctx context.Context, option ...*handler.Option) *g
|
||||
FilterAuth: false, // 角色表不验证数据权限
|
||||
})
|
||||
}
|
||||
return handler.Model(service.MemoryDB().DB(ctx).Ctx(ctx).Model(dao.SysRole.Table()), option...)
|
||||
return handler.Model(dao.SysRole.Ctx(ctx).Master(), option...)
|
||||
}
|
||||
|
||||
// 获取租户下角色列表
|
||||
func (s *sSysRole) List(ctx context.Context, param *model.SysRoleListParam) (res []*model.SysRoleListModel, total int, err error) {
|
||||
db := s.ModelQuery(ctx)
|
||||
// 注意:ModelQuery使用内存数据库,删除的记录会被物理删除,所以不需要过滤deleted_at
|
||||
if param.RoleName != "" {
|
||||
db = db.WhereLike(dao.SysRole.Columns().RoleName, "%"+param.RoleName+"%")
|
||||
}
|
||||
@@ -109,6 +108,19 @@ func (s *sSysRole) View(ctx context.Context, param *model.SysRoleViewParam) (res
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if res == nil {
|
||||
return nil, gerror.New("角色不存在或已删除")
|
||||
}
|
||||
template, err := rolepolicy.TemplateForRoles(ctx, res.RoleId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res.IsTemplateRole = template != nil
|
||||
defaultRoleCount, err := dao.SysDept.Ctx(ctx).Master().Where("tenant_id", res.TenantId).Where("dept_id", res.DeptId).Where("dept_role", res.RoleId).Count()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res.IsDeptDefaultRole = defaultRoleCount > 0
|
||||
// 获取角色菜单
|
||||
roleMenus, err := s.getRoleMenuRelations(ctx, param.RoleId)
|
||||
if err != nil {
|
||||
@@ -136,7 +148,7 @@ func (s *sSysRole) View(ctx context.Context, param *model.SysRoleViewParam) (res
|
||||
}
|
||||
|
||||
func (s *sSysRole) getRoleMenuRelations(ctx context.Context, id int64) (res []*entity.SysRoleMenu, err error) {
|
||||
err = dao.SysRoleMenu.Ctx(ctx).
|
||||
err = dao.SysRoleMenu.Ctx(ctx).Master().
|
||||
Where(dao.SysRoleMenu.Columns().TenantId, contexts.GetTenantId(ctx)).
|
||||
Where(dao.SysRoleMenu.Columns().RoleId, id).Scan(&res)
|
||||
return
|
||||
@@ -144,7 +156,7 @@ func (s *sSysRole) getRoleMenuRelations(ctx context.Context, id int64) (res []*e
|
||||
|
||||
// 获取角色菜单
|
||||
func (s *sSysRole) GetRoleMenu(ctx context.Context, id int64) (res []*entity.SysMenu, err error) {
|
||||
err = dao.SysRoleMenu.Ctx(ctx).
|
||||
err = dao.SysRoleMenu.Ctx(ctx).Master().
|
||||
Where(dao.SysRoleMenu.Columns().TenantId, contexts.GetTenantId(ctx)).
|
||||
Where(dao.SysRoleMenu.Columns().RoleId, id).Scan(&res)
|
||||
return
|
||||
@@ -152,7 +164,7 @@ func (s *sSysRole) GetRoleMenu(ctx context.Context, id int64) (res []*entity.Sys
|
||||
|
||||
// 获取角色部门
|
||||
func (s *sSysRole) GetRoleDept(ctx context.Context, id int64) (res []*entity.SysDept, err error) {
|
||||
err = dao.SysRoleDept.Ctx(ctx).
|
||||
err = dao.SysRoleDept.Ctx(ctx).Master().
|
||||
Where(dao.SysRoleDept.Columns().TenantId, contexts.GetTenantId(ctx)).
|
||||
Where(dao.SysRoleDept.Columns().RoleId, id).Scan(&res)
|
||||
return
|
||||
@@ -177,7 +189,7 @@ func (s *sSysRole) GetRoleListMenu(ctx context.Context, ids []int64) (res []*ent
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
err = dao.SysRoleMenu.Ctx(ctx).
|
||||
err = dao.SysRoleMenu.Ctx(ctx).Master().
|
||||
Where(dao.SysRoleMenu.Columns().TenantId, contexts.GetTenantId(ctx)).
|
||||
WhereIn(dao.SysRoleMenu.Columns().RoleId, ids).Scan(&res)
|
||||
return
|
||||
@@ -194,8 +206,8 @@ func (s *sSysRole) Add(ctx context.Context, param *model.SysRoleAddParam) (role
|
||||
return nil
|
||||
})
|
||||
if err == nil && role != nil {
|
||||
err = rolesync.EnqueueRole(ctx, role.RoleId, contexts.GetUserId(ctx))
|
||||
event.EventsInstance().Emit(ctx, consts.EventKeyDBSysRoleCreate, role.RoleId)
|
||||
err = rolesync.EnqueueRole(ctx, role.RoleId, contexts.GetUserId(ctx))
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -247,8 +259,8 @@ func (s *sSysRole) Edit(ctx context.Context, param *model.SysRoleEditParam) (rol
|
||||
return nil
|
||||
})
|
||||
if err == nil {
|
||||
err = rolesync.EnqueueRole(ctx, param.RoleId, contexts.GetUserId(ctx))
|
||||
event.EventsInstance().Emit(ctx, consts.EventKeyDBSysRoleUpdate, param.RoleId)
|
||||
err = rolesync.EnqueueRole(ctx, param.RoleId, contexts.GetUserId(ctx))
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -264,7 +276,7 @@ func (s *sSysRole) editRoleAtomic(ctx context.Context, param *model.SysRoleEditP
|
||||
if user == nil {
|
||||
return role, gerror.New("登录已过期")
|
||||
}
|
||||
template, templateErr := rolepolicy.Template(ctx)
|
||||
template, templateErr := rolepolicy.TemplateForRoles(ctx, param.RoleId)
|
||||
if templateErr != nil {
|
||||
return role, templateErr
|
||||
}
|
||||
@@ -299,7 +311,11 @@ func (s *sSysRole) editRoleAtomic(ctx context.Context, param *model.SysRoleEditP
|
||||
|
||||
// 删除角色
|
||||
func (s *sSysRole) Delete(ctx context.Context, param *model.SysRoleDeleteParam) (role *model.SysRoleDeleteModel, err error) {
|
||||
template, templateErr := rolepolicy.Template(ctx)
|
||||
targetIDs := []int64{param.RoleId}
|
||||
if param.RoleId == 0 {
|
||||
targetIDs = param.RoleIds
|
||||
}
|
||||
template, templateErr := rolepolicy.TemplateForRoles(ctx, targetIDs...)
|
||||
if templateErr != nil {
|
||||
return nil, templateErr
|
||||
}
|
||||
@@ -331,19 +347,32 @@ func (s *sSysRole) Delete(ctx context.Context, param *model.SysRoleDeleteParam)
|
||||
}
|
||||
data := do.SysRole{}
|
||||
data.DeletedBy = user.ID
|
||||
data.DeletedAt = gtime.Now()
|
||||
_, err = s.Model(ctx).WhereIn(dao.SysRole.Columns().RoleId, roleIds).Data(data).OmitNil().Update()
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, _ gdb.TX) error {
|
||||
if _, deleteErr := s.Model(ctx).WhereIn(dao.SysRole.Columns().RoleId, roleIds).Data(data).OmitNil().Update(); deleteErr != nil {
|
||||
return deleteErr
|
||||
}
|
||||
if _, deleteErr := s.Model(ctx).WhereIn(dao.SysRole.Columns().RoleId, roleIds).Delete(); deleteErr != nil {
|
||||
return deleteErr
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return role, err
|
||||
}
|
||||
event.EventsInstance().Emit(ctx, consts.EventKeyDBSysRoleDelete, roleIds)
|
||||
clearRoleUsersAccessCache(ctx, roleIds...)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sSysRole) RoleMenu(ctx context.Context, roleId int64, menuIds []int64, dataScopes map[int64]string) (err error) {
|
||||
template, templateErr := rolepolicy.Template(ctx)
|
||||
template, templateErr := rolepolicy.TemplateForRoles(ctx, roleId)
|
||||
if templateErr != nil {
|
||||
return templateErr
|
||||
}
|
||||
if template != nil && template.RoleId == roleId {
|
||||
if !contexts.IsSuperAdmin(ctx) {
|
||||
return gerror.New("仅平台超级管理员可修改代理模板")
|
||||
}
|
||||
for _, scope := range dataScopes {
|
||||
if scope == consts.SysRoleDataScopeAll || scope == consts.SysRoleDataScopeCustom {
|
||||
return gerror.New("模板按钮不允许全部或自定义数据范围")
|
||||
@@ -352,15 +381,11 @@ func (s *sSysRole) RoleMenu(ctx context.Context, roleId int64, menuIds []int64,
|
||||
}
|
||||
tenantId := contexts.GetTenantId(ctx)
|
||||
if !contexts.IsSuperAdmin(ctx) {
|
||||
allowed, err := rolepolicy.RestrictMenus(ctx, tenantId, contexts.GetDeptId(ctx), menuIds)
|
||||
allowed, err := rolepolicy.NormalizeAssignableMenus(ctx, tenantId, contexts.GetDeptId(ctx), contexts.GetUserId(ctx), menuIds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, id := range menuIds {
|
||||
if !slices.Contains(allowed, id) {
|
||||
return gerror.New("角色授权超出机构或模板范围")
|
||||
}
|
||||
}
|
||||
menuIds = allowed
|
||||
for _, scope := range dataScopes {
|
||||
if scope == consts.SysRoleDataScopeAll || scope == consts.SysRoleDataScopeCustom {
|
||||
return gerror.New("不允许授予全部或自定义数据范围")
|
||||
@@ -369,7 +394,7 @@ func (s *sSysRole) RoleMenu(ctx context.Context, roleId int64, menuIds []int64,
|
||||
}
|
||||
// 查询角色菜单
|
||||
roleMenu := make([]*entity.SysRoleMenu, 0)
|
||||
err = dao.SysRoleMenu.Ctx(ctx).Where(dao.SysRoleMenu.Columns().TenantId, tenantId).Where(dao.SysRoleMenu.Columns().RoleId, roleId).Scan(&roleMenu)
|
||||
err = dao.SysRoleMenu.Ctx(ctx).Master().Where(dao.SysRoleMenu.Columns().TenantId, tenantId).Where(dao.SysRoleMenu.Columns().RoleId, roleId).Scan(&roleMenu)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -407,7 +432,7 @@ func (s *sSysRole) RoleMenu(ctx context.Context, roleId int64, menuIds []int64,
|
||||
}
|
||||
}
|
||||
for _, menuId := range updateMenuIds {
|
||||
_, err = dao.SysRoleMenu.Ctx(ctx).
|
||||
_, err = dao.SysRoleMenu.Ctx(ctx).Master().
|
||||
Where(dao.SysRoleMenu.Columns().TenantId, tenantId).
|
||||
Where(dao.SysRoleMenu.Columns().RoleId, roleId).
|
||||
Where(dao.SysRoleMenu.Columns().MenuId, menuId).
|
||||
@@ -443,7 +468,7 @@ func clearRoleUsersAccessCache(ctx context.Context, roleIds ...int64) {
|
||||
return
|
||||
}
|
||||
relations := make([]*entity.SysUserRole, 0)
|
||||
if err := dao.SysUserRole.Ctx(ctx).
|
||||
if err := dao.SysUserRole.Ctx(ctx).Master().
|
||||
Fields(dao.SysUserRole.Columns().UserId).
|
||||
Where(dao.SysUserRole.Columns().TenantId, contexts.GetTenantId(ctx)).
|
||||
WhereIn(dao.SysUserRole.Columns().RoleId, roleIds).
|
||||
@@ -474,7 +499,7 @@ func (s *sSysRole) RoleDept(ctx context.Context, roleId int64, deptIds []int64)
|
||||
tenantId := contexts.GetTenantId(ctx)
|
||||
// 查询角色部门
|
||||
roleDept := make([]*entity.SysRoleDept, 0)
|
||||
err = dao.SysRoleDept.Ctx(ctx).Where(dao.SysRoleDept.Columns().TenantId, tenantId).Where(dao.SysRoleDept.Columns().RoleId, roleId).Scan(&roleDept)
|
||||
err = dao.SysRoleDept.Ctx(ctx).Master().Where(dao.SysRoleDept.Columns().TenantId, tenantId).Where(dao.SysRoleDept.Columns().RoleId, roleId).Scan(&roleDept)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -511,10 +536,13 @@ func (s *sSysRole) RoleDept(ctx context.Context, roleId int64, deptIds []int64)
|
||||
|
||||
// 编辑角色数据权限
|
||||
func (s *sSysRole) EditRoleDataScope(ctx context.Context, model *model.SysRoleDataScopeEditParam) (err error) {
|
||||
template, templateErr := rolepolicy.Template(ctx)
|
||||
template, templateErr := rolepolicy.TemplateForRoles(ctx, model.RoleId)
|
||||
if templateErr != nil {
|
||||
return templateErr
|
||||
}
|
||||
if template != nil && !contexts.IsSuperAdmin(ctx) {
|
||||
return gerror.New("仅平台超级管理员可修改代理模板")
|
||||
}
|
||||
if !contexts.IsSuperAdmin(ctx) || (template != nil && template.RoleId == model.RoleId) {
|
||||
if model.DataScope == consts.SysRoleDataScopeAll || model.DataScope == consts.SysRoleDataScopeCustom || model.DataScope == "" {
|
||||
return gerror.New("不允许全部、自定义或空数据权限")
|
||||
@@ -544,10 +572,8 @@ func (s *sSysRole) EditRoleDataScope(ctx context.Context, model *model.SysRoleDa
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = rolesync.EnqueueRole(ctx, model.RoleId, contexts.GetUserId(ctx)); err != nil {
|
||||
return err
|
||||
}
|
||||
event.EventsInstance().Emit(ctx, consts.EventKeyDBSysRoleUpdate, model.RoleId)
|
||||
err = rolesync.EnqueueRole(ctx, model.RoleId, contexts.GetUserId(ctx))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -558,7 +584,7 @@ func (s *sSysRole) ensureRolesNotInherited(ctx context.Context, roleIds []int64)
|
||||
if contexts.IsSuperAdmin(ctx) {
|
||||
return nil
|
||||
}
|
||||
count, err := g.Model(dao.SysRole.Table()+" r").
|
||||
count, err := g.Model(dao.SysRole.Table()+" r").Ctx(ctx).
|
||||
LeftJoin(dao.SysDept.Table()+" d", "d.dept_id = r.dept_id AND d.tenant_id = r.tenant_id").
|
||||
WhereIn("r.role_id", roleIds).
|
||||
Where("r.deleted_at IS NULL").
|
||||
@@ -581,7 +607,14 @@ func (s *sSysRole) ensureRolesHaveNoInheritedChildren(ctx context.Context, roleI
|
||||
if len(roleIds) == 0 {
|
||||
return nil
|
||||
}
|
||||
count, err := g.Model(dao.SysRole.Table()+" r").
|
||||
configured, err := dao.SysConfig.Ctx(ctx).Master().Where("tenant_id", consts.DefaultSystemTenantCode).Where("config_key", rolepolicy.TemplateConfigKey).Count()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if configured > 0 {
|
||||
return nil
|
||||
}
|
||||
count, err := g.Model(dao.SysRole.Table()+" r").Ctx(ctx).
|
||||
LeftJoin(dao.SysDept.Table()+" child_dept", "child_dept.dept_id = r.dept_id AND child_dept.tenant_id = r.tenant_id").
|
||||
Where("r.created_dept IN (SELECT dept_id FROM sys_role WHERE role_id IN (?) AND is_pass = 1 AND deleted_at IS NULL)", roleIds).
|
||||
Where("r.deleted_at IS NULL").
|
||||
|
||||
Reference in New Issue
Block a user