ec-nest/src/resources/resources.service.ts
liuyun 127707ffde feat(rbac): 批量删除 + 数据权限自定义闭环 + 若干修复
- users/orgs/resources 新增 POST /batch-delete(事务 + 失败汇总 + 用户踢下线)
- 角色数据权限自定义:GET/PUT /roles/:roleCode/orgs(roleOrg 写入路径)
- assignResources 校验 resourceCodes 存在性;角色 :id 语义改 :roleCode
- createTenant 补 createdBy;登录锁定双层阈值(IP+用户名 / 用户名全局)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-11 17:42:05 +08:00

55 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Injectable } from '@nestjs/common';
import { BaseCrudService } from '../common/base-crud.service.js';
import { PrismaService } from '../prisma/prisma.service.js';
import { RedisService } from '../common/redis/redis.service.js';
import { getRequestContext } from '../common/context/request-context.js';
import { CreateResourceDto, UpdateResourceDto } from './dto.js';
@Injectable()
export class ResourcesService extends BaseCrudService {
constructor(
prisma: PrismaService,
private readonly redis: RedisService,
) {
super(prisma, 'resource');
}
async createResource(dto: CreateResourceDto) {
const created = await this.create({ ...dto });
await this.clearPermCache();
return created;
}
async updateResource(id: string, dto: UpdateResourceDto) {
const updated = await this.update(id, { ...dto });
await this.clearPermCache();
return updated;
}
async softRemove(id: string) {
const removed = await super.softRemove(id);
await this.clearPermCache();
return removed;
}
/** 批量删除:逐个软删(回收 code),最后统一清权限缓存。 */
async batchDelete(ids: string[]) {
const failed: string[] = [];
for (const id of ids) {
try {
await super.softRemove(id);
} catch {
failed.push(id);
}
}
await this.clearPermCache();
return { total: ids.length, success: ids.length - failed.length, failed };
}
/** 资源变更(含停用/删除)影响权限码聚合,清该租户全部权限缓存。 */
private async clearPermCache(): Promise<void> {
const ctx = getRequestContext();
if (ctx) await this.redis.delByPrefix(`perm:${ctx.tenantCode}:`);
}
}