2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-01-12 13:02:26 +01:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-03-01 02:20:03 +01:00
|
|
|
import { Brackets } from 'typeorm';
|
2023-01-12 13:02:26 +01:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { RoleAssignmentsRepository, RolesRepository } from '@/models/_.js';
|
2023-01-12 13:02:26 +01:00
|
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
import type { MiUser } from '@/models/User.js';
|
|
|
|
import type { MiRole } from '@/models/Role.js';
|
2023-01-12 13:02:26 +01:00
|
|
|
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
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class RoleEntityService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.rolesRepository)
|
|
|
|
private rolesRepository: RolesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.roleAssignmentsRepository)
|
|
|
|
private roleAssignmentsRepository: RoleAssignmentsRepository,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async pack(
|
2023-08-16 10:51:28 +02:00
|
|
|
src: MiRole['id'] | MiRole,
|
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2023-01-12 13:02:26 +01:00
|
|
|
) {
|
|
|
|
const role = typeof src === 'object' ? src : await this.rolesRepository.findOneByOrFail({ id: src });
|
|
|
|
|
2023-03-01 02:20:03 +01:00
|
|
|
const assignedCount = await this.roleAssignmentsRepository.createQueryBuilder('assign')
|
|
|
|
.where('assign.roleId = :roleId', { roleId: role.id })
|
|
|
|
.andWhere(new Brackets(qb => { qb
|
2023-03-01 04:02:37 +01:00
|
|
|
.where('assign.expiresAt IS NULL')
|
2023-03-01 02:20:03 +01:00
|
|
|
.orWhere('assign.expiresAt > :now', { now: new Date() });
|
|
|
|
}))
|
|
|
|
.getCount();
|
2023-01-12 13:02:26 +01:00
|
|
|
|
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-02-05 02:37:03 +01:00
|
|
|
iconUrl: role.iconUrl,
|
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,
|
2023-04-20 13:02:50 +02:00
|
|
|
isExplorable: role.isExplorable,
|
2023-02-05 02:37:03 +01:00
|
|
|
asBadge: role.asBadge,
|
2023-01-12 13:02:26 +01:00
|
|
|
canEditMembersByModerator: role.canEditMembersByModerator,
|
2023-03-12 08:38:08 +01:00
|
|
|
displayOrder: role.displayOrder,
|
2023-01-15 12:52:53 +01:00
|
|
|
policies: policies,
|
2023-03-01 02:20:03 +01:00
|
|
|
usersCount: assignedCount,
|
2023-01-12 13:02:26 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public packMany(
|
|
|
|
roles: any[],
|
2023-08-16 10:51:28 +02:00
|
|
|
me: { id: MiUser['id'] },
|
2023-01-12 13:02:26 +01:00
|
|
|
) {
|
2023-02-22 06:43:18 +01:00
|
|
|
return Promise.all(roles.map(x => this.pack(x, me)));
|
2023-01-12 13:02:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|