2023-01-18 17:08:07 +01:00
|
|
|
import { Logger } from '../logger';
|
|
|
|
import { IUser } from '../types/user';
|
|
|
|
import { IUnleashConfig } from '../types/option';
|
|
|
|
import { IAccountStore, IUnleashStores } from '../types/stores';
|
|
|
|
import { AccessService } from './access-service';
|
|
|
|
import { RoleName } from '../types/model';
|
2023-05-23 16:56:34 +02:00
|
|
|
import { IAdminCount } from 'lib/types/stores/account-store';
|
2023-01-18 17:08:07 +01:00
|
|
|
|
|
|
|
interface IUserWithRole extends IUser {
|
|
|
|
rootRole: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class AccountService {
|
|
|
|
private logger: Logger;
|
|
|
|
|
|
|
|
private store: IAccountStore;
|
|
|
|
|
|
|
|
private accessService: AccessService;
|
|
|
|
|
|
|
|
private lastSeenSecrets: Set<string> = new Set<string>();
|
|
|
|
|
|
|
|
constructor(
|
2023-09-27 15:23:05 +02:00
|
|
|
stores: Pick<IUnleashStores, 'accountStore'>,
|
2023-01-18 17:08:07 +01:00
|
|
|
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>,
|
|
|
|
services: {
|
|
|
|
accessService: AccessService;
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
this.logger = getLogger('service/account-service.ts');
|
|
|
|
this.store = stores.accountStore;
|
|
|
|
this.accessService = services.accessService;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAll(): Promise<IUserWithRole[]> {
|
|
|
|
const accounts = await this.store.getAll();
|
2023-11-24 16:06:37 +01:00
|
|
|
const defaultRole = await this.accessService.getPredefinedRole(
|
2023-01-18 17:08:07 +01:00
|
|
|
RoleName.VIEWER,
|
|
|
|
);
|
|
|
|
const userRoles = await this.accessService.getRootRoleForAllUsers();
|
|
|
|
const accountsWithRootRole = accounts.map((u) => {
|
|
|
|
const rootRole = userRoles.find((r) => r.userId === u.id);
|
|
|
|
const roleId = rootRole ? rootRole.roleId : defaultRole.id;
|
|
|
|
return { ...u, rootRole: roleId };
|
|
|
|
});
|
|
|
|
return accountsWithRootRole;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAccountByPersonalAccessToken(secret: string): Promise<IUser> {
|
|
|
|
return this.store.getAccountByPersonalAccessToken(secret);
|
|
|
|
}
|
|
|
|
|
2023-05-23 16:56:34 +02:00
|
|
|
async getAdminCount(): Promise<IAdminCount> {
|
|
|
|
return this.store.getAdminCount();
|
|
|
|
}
|
|
|
|
|
2023-01-18 17:08:07 +01:00
|
|
|
async updateLastSeen(): Promise<void> {
|
|
|
|
if (this.lastSeenSecrets.size > 0) {
|
|
|
|
const toStore = [...this.lastSeenSecrets];
|
|
|
|
this.lastSeenSecrets = new Set<string>();
|
|
|
|
await this.store.markSeenAt(toStore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addPATSeen(secret: string): void {
|
|
|
|
this.lastSeenSecrets.add(secret);
|
|
|
|
}
|
|
|
|
}
|