feat: complete cmpp platform phases 0-5
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
export interface CreateUserDto {
|
||||
tenantId?: string;
|
||||
username: string;
|
||||
displayName: string;
|
||||
password: string;
|
||||
status?: string;
|
||||
}
|
||||
|
||||
export interface CreateRoleDto {
|
||||
code: string;
|
||||
name: string;
|
||||
scope?: string;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export interface CreatePermissionDto {
|
||||
code: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export interface AssignRoleDto {
|
||||
userId: string;
|
||||
roleId: string;
|
||||
}
|
||||
|
||||
export interface AssignPermissionDto {
|
||||
roleId: string;
|
||||
permissionId: string;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
list(tenantId?: string) {
|
||||
return this.prisma.user.findMany({
|
||||
where: tenantId ? { tenantId } : undefined,
|
||||
include: { roles: { include: { role: true } } },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
take: 100,
|
||||
});
|
||||
}
|
||||
|
||||
findByUsername(username: string) {
|
||||
return this.prisma.user.findUnique({ where: { username } });
|
||||
}
|
||||
|
||||
create(data: CreateUserDto) {
|
||||
return this.prisma.user.create({
|
||||
data: {
|
||||
tenantId: data.tenantId,
|
||||
username: data.username,
|
||||
displayName: data.displayName,
|
||||
passwordHash: hashPassword(data.password),
|
||||
status: data.status ?? 'active',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
listRoles() {
|
||||
return this.prisma.role.findMany({
|
||||
include: { permissions: { include: { permission: true } } },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
take: 100,
|
||||
});
|
||||
}
|
||||
|
||||
createRole(data: CreateRoleDto) {
|
||||
return this.prisma.role.create({
|
||||
data: {
|
||||
code: data.code,
|
||||
name: data.name,
|
||||
scope: data.scope ?? 'platform',
|
||||
description: data.description,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
listPermissions() {
|
||||
return this.prisma.permission.findMany({ orderBy: { createdAt: 'desc' }, take: 200 });
|
||||
}
|
||||
|
||||
createPermission(data: CreatePermissionDto) {
|
||||
return this.prisma.permission.create({ data });
|
||||
}
|
||||
|
||||
assignRole(data: AssignRoleDto) {
|
||||
return this.prisma.userRole.upsert({
|
||||
where: { userId_roleId: { userId: data.userId, roleId: data.roleId } },
|
||||
update: {},
|
||||
create: data,
|
||||
});
|
||||
}
|
||||
|
||||
assignPermission(data: AssignPermissionDto) {
|
||||
return this.prisma.rolePermission.upsert({
|
||||
where: { roleId_permissionId: { roleId: data.roleId, permissionId: data.permissionId } },
|
||||
update: {},
|
||||
create: data,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function hashPassword(password: string) {
|
||||
return createHash('sha256').update(password).digest('hex');
|
||||
}
|
||||
Reference in New Issue
Block a user