😎内置角色禁止删除、禁止修改角色编码

This commit is contained in:
zuohuaijun 2025-07-17 11:09:25 +08:00
parent 79aedbcad7
commit 0af8d79a43
7 changed files with 18 additions and 13 deletions

View File

@ -44,8 +44,8 @@ public partial class SysRole : EntityTenant
/// <summary>
/// 是否是内置Y-是N-否)
/// </summary>
[SugarColumn(ColumnDescription = "是否是内置", DefaultValue = "1")]
public YesNoEnum SysFlag { get; set; } = YesNoEnum.Y;
[SugarColumn(ColumnDescription = "是否是内置", DefaultValue = "2")]
public YesNoEnum SysFlag { get; set; } = YesNoEnum.N;
/// <summary>
/// 备注

View File

@ -158,9 +158,9 @@ public enum ErrorCodeEnum
D1018,
/// <summary>
/// 禁止删除系统管理员角色
/// 禁止删除系统内置角色
/// </summary>
[ErrorCodeItemMetadata("禁止删除系统管理员角色")]
[ErrorCodeItemMetadata("禁止删除系统内置角色")]
D1019,
/// <summary>

View File

@ -20,11 +20,11 @@ public class SysRoleSeedData : ISqlSugarEntitySeedData<SysRole>
{
return
[
new SysRole{ Id=1300000000101, Name="系统管理员", DataScope=DataScopeEnum.All, Code="sys_admin", CreateTime=DateTime.Parse("2022-02-10 00:00:00"), Remark="系统管理员", TenantId=SqlSugarConst.DefaultTenantId },
new SysRole{ Id=1300000000102, Name="本部门及以下数据", DataScope=DataScopeEnum.DeptChild, Code="sys_deptChild", CreateTime=DateTime.Parse("2022-02-10 00:00:00"), Remark="本部门及以下数据", TenantId=SqlSugarConst.DefaultTenantId },
new SysRole{ Id=1300000000103, Name="本部门数据", DataScope=DataScopeEnum.Dept, Code="sys_dept", CreateTime=DateTime.Parse("2022-02-10 00:00:00"), Remark="本部门数据", TenantId=SqlSugarConst.DefaultTenantId },
new SysRole{ Id=1300000000104, Name="仅本人数据", DataScope=DataScopeEnum.Self, Code="sys_self", CreateTime=DateTime.Parse("2022-02-10 00:00:00"), Remark="仅本人数据", TenantId=SqlSugarConst.DefaultTenantId },
new SysRole{ Id=1300000000105, Name="自定义数据", DataScope=DataScopeEnum.Define, Code="sys_define", CreateTime=DateTime.Parse("2022-02-10 00:00:00"), Remark="自定义数据", TenantId=SqlSugarConst.DefaultTenantId },
new SysRole{ Id=1300000000101, Name="系统管理员", DataScope=DataScopeEnum.All, Code="sys_admin", SysFlag=YesNoEnum.Y, CreateTime=DateTime.Parse("2022-02-10 00:00:00"), Remark="系统管理员", TenantId=SqlSugarConst.DefaultTenantId },
new SysRole{ Id=1300000000102, Name="本部门及以下数据", DataScope=DataScopeEnum.DeptChild, Code="sys_deptChild", SysFlag = YesNoEnum.Y, CreateTime=DateTime.Parse("2022-02-10 00:00:00"), Remark="本部门及以下数据", TenantId=SqlSugarConst.DefaultTenantId },
new SysRole{ Id=1300000000103, Name="本部门数据", DataScope=DataScopeEnum.Dept, Code="sys_dept", SysFlag=YesNoEnum.Y, CreateTime=DateTime.Parse("2022-02-10 00:00:00"), Remark="本部门数据", TenantId=SqlSugarConst.DefaultTenantId },
new SysRole{ Id=1300000000104, Name="仅本人数据", DataScope=DataScopeEnum.Self, Code="sys_self", SysFlag = YesNoEnum.Y, CreateTime=DateTime.Parse("2022-02-10 00:00:00"), Remark="仅本人数据", TenantId=SqlSugarConst.DefaultTenantId },
new SysRole{ Id=1300000000105, Name="自定义数据", DataScope=DataScopeEnum.Define, Code="sys_define", SysFlag = YesNoEnum.Y, CreateTime=DateTime.Parse("2022-02-10 00:00:00"), Remark="自定义数据", TenantId=SqlSugarConst.DefaultTenantId },
];
}
}

View File

@ -138,9 +138,9 @@ public class SysRoleService : IDynamicApiController, ITransient
[DisplayName("删除角色")]
public async Task DeleteRole(DeleteRoleInput input)
{
// 禁止删除系统管理员角色
// 禁止删除系统内置角色
var sysRole = await _sysRoleRep.GetByIdAsync(input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
if (sysRole.Code == CommonConst.SysAdminRole) throw Oops.Oh(ErrorCodeEnum.D1019);
if(sysRole.SysFlag == YesNoEnum.Y) throw Oops.Oh(ErrorCodeEnum.D1019);
// 若角色有用户则禁止删除
var userIds = await _sysUserRoleService.GetUserIdList(input.Id);

View File

@ -214,7 +214,8 @@ public class SysTenantService : IDynamicApiController, ITransient
TenantId = tenantId,
Name = tenantMark,
Code = CommonConst.SysAdminRole,
DataScope = DataScopeEnum.All,
DataScope = DataScopeEnum.All,
SysFlag = YesNoEnum.Y,
Remark = tenantMark
};
await _sysRoleRep.InsertAsync(newRole);

View File

@ -16,7 +16,7 @@
</el-col>
<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
<el-form-item :label="$t('message.list.roleCode')" prop="code" :rules="[{ required: true, message: $t('message.list.roleCodeRequired'), trigger: 'blur' }]">
<el-input v-model="state.ruleForm.code" :placeholder="$t('message.list.roleCode')" clearable :disabled="state.ruleForm.code == 'sys_admin' && state.ruleForm.id != undefined" />
<el-input v-model="state.ruleForm.code" :placeholder="$t('message.list.roleCode')" clearable :disabled="state.ruleForm.sysFlag == 1 && state.ruleForm.id != undefined" />
</el-form-item>
</el-col>
<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">

View File

@ -44,6 +44,9 @@
<el-tag v-else-if="row.dataScope === 4">{{ $t('message.list.personalData') }}</el-tag>
<el-tag v-else-if="row.dataScope === 5">{{ $t('message.list.customData') }}</el-tag>
</template>
<template #row_sysFlag="{ row }">
<g-sys-dict v-model="row.sysFlag" code="YesNoEnum" />
</template>
<template #row_status="{ row }">
<el-tag v-if="row.status === 1" type="success">{{ $t('message.list.enable') }}</el-tag>
<el-tag v-else type="danger">{{ $t('message.list.disable') }}</el-tag>
@ -136,6 +139,7 @@ const options = useVxeTable<PageRoleOutput>(
{ field: 'name', title: i18n.t('message.list.roleName'), minWidth: 150, showOverflow: 'tooltip' },
{ field: 'code', title: i18n.t('message.list.roleCode'), minWidth: 150, showOverflow: 'tooltip' },
{ field: 'dataScope', title: i18n.t('message.list.dataScope'), minWidth: 150, showOverflow: 'tooltip', slots: { default: 'row_dataScope' } },
{ field: 'sysFlag', title: '是否内置', width: 80, showOverflow: 'tooltip', slots: { default: 'row_sysFlag' } },
{ field: 'tenantName', title: i18n.t('message.list.tenantName'), minWidth: 180, showOverflow: 'tooltip' },
{ field: 'orderNo', title: i18n.t('message.list.orderNo'), width: 80, showOverflow: 'tooltip' },
{ field: 'status', title: i18n.t('message.list.status'), width: 80, showOverflow: 'tooltip', slots: { default: 'row_status' } },