Exemplo n.º 1
0
        public async Task Update(UpdateRoleInput input)
        {
            CheckUserDefinedDataPermission(input.DataPermissionType, input.DataPermissionOrgIds);
            using (var locker = await _lockerProvider.CreateLockAsync("UpdateRole"))
            {
                await locker.Lock(async() =>
                {
                    var role = await _roleRepository.GetAsync(input.Id, false);
                    if (role.DataPermissionType == DataPermissionType.UserDefined &&
                        _session.UserId != role.CreatorUserId)
                    {
                        throw new BusinessException("自定义数据权限的角色只允许用户创建者自己修改");
                    }

                    if (input.Identification != role.Identification)
                    {
                        var exsitRole =
                            await _roleRepository.FirstOrDefaultAsync(p => p.Identification == input.Identification,
                                                                      false);
                        if (exsitRole != null)
                        {
                            throw new BusinessException($"系统中已经存在{input.Identification}的角色");
                        }
                    }

                    role = input.MapTo(role);
                    await UnitOfWorkAsync(async(conn, trans) =>
                    {
                        await _roleRepository.UpdateAsync(role, conn, trans);
                        var deleteSql = "DELETE FROM RolePermission WHERE RoleId=@RoleId AND TenantId=@TenantId";
                        await conn.ExecuteAsync(deleteSql, new { RoleId = role.Id, TenantId = _session.TenantId }, trans);
                        await _rolePermissionRepository.DeleteAsync(p => p.RoleId == role.Id, conn, trans);
                        await _roleDataPermissionOrgRelationRepository.DeleteAsync(p => p.RoleId == role.Id, conn,
                                                                                   trans);
                        await _roleOrganizationRepository.DeleteAsync(p => p.RoleId == role.Id, conn, trans);
                        var insertSql =
                            "INSERT INTO RolePermission(PermissionId,RoleId,CreateTime,CreateBy,TenantId) VALUES(@PermissionId,@RoleId,@CreationTime,@CreatorUserId,@TenantId)";
                        var rolePermissions = new List <RolePermission>();
                        foreach (var permissionId in input.PermissionIds)
                        {
                            rolePermissions.Add(new RolePermission
                            {
                                PermissionId  = permissionId,
                                RoleId        = role.Id,
                                CreationTime  = DateTime.Now,
                                CreatorUserId = _session.UserId,
                                TenantId      = _session.TenantId
                            });
                        }
                        await conn.ExecuteAsync(insertSql, rolePermissions, trans);
                        if (!input.IsAllOrg)
                        {
                            foreach (var orgId in input.OrgIds)
                            {
                                var roleOrg = new RoleOrganization()
                                {
                                    RoleId = role.Id, OrgId = orgId
                                };
                                await _roleOrganizationRepository.InsertAsync(roleOrg, conn, trans);
                            }
                        }

                        if (input.DataPermissionType == DataPermissionType.UserDefined)
                        {
                            var insertDataPermissionOrgSql =
                                "INSERT INTO RoleDataPermissionOrgRelation(RoleId,OrgId,CreateTime,CreateBy,TenantId) VALUES(@RoleId,@OrgId,@CreationTime,@CreatorUserId,@TenantId)";
                            var dataPermissionOrgDatas = new List <RoleDataPermissionOrgRelation>();
                            foreach (var orgId in input.DataPermissionOrgIds)
                            {
                                dataPermissionOrgDatas.Add(new RoleDataPermissionOrgRelation()
                                {
                                    RoleId        = role.Id,
                                    OrgId         = orgId,
                                    CreationTime  = DateTime.Now,
                                    CreatorUserId = _session.UserId,
                                    TenantId      = _session.TenantId
                                });
                            }

                            await conn.ExecuteAsync(insertDataPermissionOrgSql, dataPermissionOrgDatas, trans);
                        }

                        await RemoveRoleCheckPemissionCache(role.Id);
                    }, Connection);
                });
            }
        }
Exemplo n.º 2
0
        public async Task <long> Create(CreateRoleInput input, DbConnection conn, DbTransaction trans,
                                        long?tenantId = null)
        {
            using (var locker = await _lockerProvider.CreateLockAsync("CreateRole"))
            {
                return(await locker.Lock(async() =>
                {
                    var exsitRole =
                        await _roleRepository.FirstOrDefaultAsync(p => p.Identification == input.Identification, false);
                    if (exsitRole != null)
                    {
                        throw new BusinessException($"系统中已经存在{input.Identification}的角色");
                    }
                    CheckUserDefinedDataPermission(input.DataPermissionType, input.DataPermissionOrgIds);
                    var role = input.MapTo <Role>();
                    role.TenantId = _session.TenantId;
                    if (tenantId.HasValue)
                    {
                        role.TenantId = tenantId.Value;
                    }

                    var roleId = await _roleRepository.InsertAndGetIdAsync(role, conn, trans);
                    await _rolePermissionRepository.DeleteAsync(p => p.RoleId == role.Id, conn, trans);
                    var insertSql =
                        "INSERT INTO RolePermission(PermissionId,RoleId,CreateTime,CreateBy,TenantId) VALUES(@PermissionId,@RoleId,@CreationTime,@CreatorUserId,@TenantId)";
                    var rolePermissions = new List <RolePermission>();
                    foreach (var permissionId in input.PermissionIds)
                    {
                        rolePermissions.Add(new RolePermission
                        {
                            PermissionId = permissionId,
                            RoleId = roleId,
                            CreationTime = DateTime.Now,
                            CreatorUserId = _session.UserId,
                            TenantId = role.TenantId
                        });
                    }
                    await conn.ExecuteAsync(insertSql, rolePermissions, trans);
                    if (!input.IsAllOrg)
                    {
                        foreach (var orgId in input.OrgIds)
                        {
                            var roleOrg = new RoleOrganization()
                            {
                                RoleId = roleId, OrgId = orgId, TenantId = role.TenantId
                            };
                            await _roleOrganizationRepository.InsertAsync(roleOrg, conn, trans);
                        }
                    }

                    if (input.DataPermissionType == DataPermissionType.UserDefined)
                    {
                        var insertDataPermissionOrgSql =
                            "INSERT INTO RoleDataPermissionOrgRelation(RoleId,OrgId,CreateTime,CreateBy,TenantId) VALUES(@RoleId,@OrgId,@CreationTime,@CreatorUserId,@TenantId)";
                        var dataPermissionOrgDatas = new List <RoleDataPermissionOrgRelation>();
                        foreach (var orgId in input.DataPermissionOrgIds)
                        {
                            dataPermissionOrgDatas.Add(new RoleDataPermissionOrgRelation()
                            {
                                RoleId = roleId,
                                OrgId = orgId,
                                CreationTime = DateTime.Now,
                                CreatorUserId = _session.UserId,
                                TenantId = role.TenantId
                            });
                        }

                        await conn.ExecuteAsync(insertDataPermissionOrgSql, dataPermissionOrgDatas, trans);
                    }

                    return role.Id;
                }));
            }
        }