refactor: Add telemetry for RBAC (no-changelog) (#8056)

https://linear.app/n8n/issue/PAY-1142
This commit is contained in:
Iván Ovejero
2023-12-19 17:02:52 +01:00
committed by GitHub
parent a895ee87fc
commit 38d1336fa7
5 changed files with 60 additions and 1 deletions

View File

@@ -12,4 +12,23 @@ export class RoleRepository extends Repository<Role> {
async findRole(scope: RoleScopes, name: RoleNames) {
return this.findOne({ where: { scope, name } });
}
/**
* Counts the number of users in each role, e.g. `{ admin: 2, member: 6, owner: 1 }`
*/
async countUsersByRole() {
type Row = { role_name: string; count: number };
const rows: Row[] = await this.createQueryBuilder('role')
.select('role.name')
.addSelect('COUNT(user.id)', 'count')
.innerJoin('user', 'user', 'role.id = user.globalRoleId')
.groupBy('role.name')
.getRawMany();
return rows.reduce<Record<string, number>>((acc, item) => {
acc[item.role_name] = item.count;
return acc;
}, {});
}
}