2023-01-12 13:02:26 +01:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import type { RoleAssignmentsRepository, RolesRepository } from '@/models/index.js';
|
|
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
|
|
|
import type { Packed } from '@/misc/schema.js';
|
|
|
|
import type { User } from '@/models/entities/User.js';
|
|
|
|
import type { Role } from '@/models/entities/Role.js';
|
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-01-15 12:52:53 +01:00
|
|
|
import { DEFAULT_POLICIES } from '@/core/RoleService.js';
|
2023-01-12 13:02:26 +01:00
|
|
|
import { UserEntityService } from './UserEntityService.js';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class RoleEntityService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.rolesRepository)
|
|
|
|
private rolesRepository: RolesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.roleAssignmentsRepository)
|
|
|
|
private roleAssignmentsRepository: RoleAssignmentsRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async pack(
|
|
|
|
src: Role['id'] | Role,
|
|
|
|
me?: { id: User['id'] } | null | undefined,
|
|
|
|
options?: {
|
|
|
|
detail?: boolean;
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
const opts = Object.assign({
|
|
|
|
detail: true,
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
const role = typeof src === 'object' ? src : await this.rolesRepository.findOneByOrFail({ id: src });
|
|
|
|
|
|
|
|
const assigns = await this.roleAssignmentsRepository.findBy({
|
|
|
|
roleId: role.id,
|
|
|
|
});
|
|
|
|
|
2023-01-15 12:52:53 +01:00
|
|
|
const policies = { ...role.policies };
|
|
|
|
for (const [k, v] of Object.entries(DEFAULT_POLICIES)) {
|
|
|
|
if (policies[k] == null) policies[k] = {
|
2023-01-12 13:02:26 +01:00
|
|
|
useDefault: true,
|
2023-01-15 11:10:39 +01:00
|
|
|
priority: 0,
|
2023-01-12 13:02:26 +01:00
|
|
|
value: v,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return await awaitAll({
|
|
|
|
id: role.id,
|
|
|
|
createdAt: role.createdAt.toISOString(),
|
|
|
|
updatedAt: role.updatedAt.toISOString(),
|
|
|
|
name: role.name,
|
|
|
|
description: role.description,
|
|
|
|
color: role.color,
|
2023-01-13 03:03:54 +01:00
|
|
|
target: role.target,
|
|
|
|
condFormula: role.condFormula,
|
2023-01-12 13:02:26 +01:00
|
|
|
isPublic: role.isPublic,
|
|
|
|
isAdministrator: role.isAdministrator,
|
|
|
|
isModerator: role.isModerator,
|
|
|
|
canEditMembersByModerator: role.canEditMembersByModerator,
|
2023-01-15 12:52:53 +01:00
|
|
|
policies: policies,
|
2023-01-14 03:14:14 +01:00
|
|
|
usersCount: assigns.length,
|
2023-01-12 13:02:26 +01:00
|
|
|
...(opts.detail ? {
|
|
|
|
users: this.userEntityService.packMany(assigns.map(x => x.userId), me),
|
|
|
|
} : {}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public packMany(
|
|
|
|
roles: any[],
|
|
|
|
me: { id: User['id'] },
|
|
|
|
options?: {
|
|
|
|
detail?: boolean;
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
return Promise.all(roles.map(x => this.pack(x, me, options)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|