1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-02 22:52:21 +01:00

76 lines
1.9 KiB
TypeScript
Raw Normal View History

import { isNil, isEmpty } from 'lodash';
import { User } from '../models/user';
import { supportedNetworks, SupportedSocialNetwork } from '../models/social-network';
2021-06-30 15:32:10 +02:00
export default class UserLib {
private readonly user: User;
2021-06-30 15:32:10 +02:00
constructor (user: User) {
this.user = user;
}
/**
* Check if the current user has privileged access for resources concerning the provided customer
*/
isPrivileged = (customer: User): boolean => {
if (this.user?.role === 'admin') return true;
2021-06-30 15:32:10 +02:00
if (this.user?.role === 'manager') {
return (this.user?.id !== customer.id);
2021-06-30 15:32:10 +02:00
}
return false;
2022-03-29 17:21:29 +02:00
};
2022-04-28 17:31:31 +02:00
/**
* Filter social networks from the user's profile
*/
getUserSocialNetworks = (): { name: string, url: string }[] => {
if (!this.isUser()) {
return supportedNetworks.map(network => {
return { name: network, url: '' };
});
}
2022-04-28 17:31:31 +02:00
const userNetworks = [];
for (const [name, url] of Object.entries(this.user.profile_attributes)) {
supportedNetworks.includes(name as SupportedSocialNetwork) && userNetworks.push({ name, url });
2022-04-28 17:31:31 +02:00
}
return userNetworks;
};
/**
* Return the email given by the SSO provider, parsed if needed
* @return {String} E-mail of the current user
*/
ssoEmail = (): string => {
const { email } = this.user;
if (email) {
const duplicate = email.match(/^<([^>]+)>.{20}-duplicate$/);
if (duplicate) {
return duplicate[1];
}
}
return email;
};
/**
* Test if the user's mail is marked as duplicate
*/
hasDuplicate = (): boolean => {
const { email } = this.user;
if (email) {
return !(email.match(/^<([^>]+)>.{20}-duplicate$/) === null);
}
};
/**
* Check if the current user is not empty
*/
private isUser = (): boolean => {
if (isNil(this.user)) return false;
return !(isEmpty(this.user.invoicing_profile_attributes) && isEmpty(this.user.statistic_profile_attributes));
};
2021-06-30 15:32:10 +02:00
}