85 lines
2.2 KiB
TypeScript
85 lines
2.2 KiB
TypeScript
import { KeycloakProfile } from 'keycloak-js';
|
|
import { IIqserUser } from './types/user.response';
|
|
import { IListable } from '../listing';
|
|
import { List } from '../utils';
|
|
|
|
export class IqserUser implements IIqserUser, IListable {
|
|
readonly username: string;
|
|
readonly email?: string;
|
|
readonly firstName?: string;
|
|
readonly lastName?: string;
|
|
readonly name: string;
|
|
readonly searchKey: string;
|
|
readonly active?: boolean;
|
|
|
|
readonly hasAnyRole: boolean;
|
|
|
|
constructor(user: KeycloakProfile | IIqserUser, ...args: unknown[]);
|
|
|
|
constructor(
|
|
user: KeycloakProfile | IIqserUser,
|
|
readonly roles: List,
|
|
readonly userId: string,
|
|
) {
|
|
this.hasAnyRole = this.roles.length > 0;
|
|
this.email = user.email;
|
|
this.username = user.username ?? this.email ?? 'unknown user';
|
|
this.firstName = user.firstName;
|
|
this.lastName = user.lastName;
|
|
this.name = this.firstName && this.lastName ? `${this.firstName} ${this.lastName}` : this.username;
|
|
this.active = (user as IIqserUser).active;
|
|
this.searchKey = `${this.name || '-'}${this.username || '-'}${this.email || ''}`;
|
|
}
|
|
|
|
private _isSpecial?: boolean;
|
|
|
|
/**
|
|
* This getter is required by initials avatar to know if the border should be drawn
|
|
*/
|
|
get isSpecial() {
|
|
if (!this._isExtended) {
|
|
return false;
|
|
}
|
|
|
|
if (this._isSpecial === undefined) {
|
|
throw new Error('Property isSpecial is not implemented');
|
|
}
|
|
|
|
return this._isSpecial;
|
|
}
|
|
|
|
set isSpecial(value: boolean) {
|
|
this._isSpecial = value;
|
|
}
|
|
|
|
get id() {
|
|
return this.userId;
|
|
}
|
|
|
|
/**
|
|
* Checks if this class is used as a base class
|
|
* @private
|
|
*/
|
|
private get _isExtended() {
|
|
return this.constructor !== IqserUser;
|
|
}
|
|
|
|
has(role: string): boolean {
|
|
return this.roles.includes(role);
|
|
}
|
|
|
|
hasAny(roles: List): boolean {
|
|
if (roles?.length > 0) {
|
|
for (const role of roles) {
|
|
if (this.has(role)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|