mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-02 22:52:21 +01:00
76 lines
1.9 KiB
TypeScript
76 lines
1.9 KiB
TypeScript
import { isNil, isEmpty } from 'lodash';
|
|
import { User } from '../models/user';
|
|
import { supportedNetworks, SupportedSocialNetwork } from '../models/social-network';
|
|
|
|
export default class UserLib {
|
|
private user: User;
|
|
|
|
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;
|
|
|
|
if (this.user.role === 'manager') {
|
|
return (this.user.id !== customer.id);
|
|
}
|
|
|
|
return false;
|
|
};
|
|
|
|
/**
|
|
* Filter social networks from the user's profile
|
|
*/
|
|
getUserSocialNetworks = (): { name: string, url: string }[] => {
|
|
if (!this.isUser()) {
|
|
return supportedNetworks.map(network => {
|
|
return { name: network, url: '' };
|
|
});
|
|
}
|
|
|
|
const userNetworks = [];
|
|
for (const [name, url] of Object.entries(this.user.profile_attributes)) {
|
|
supportedNetworks.includes(name as SupportedSocialNetwork) && userNetworks.push({ name, url });
|
|
}
|
|
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));
|
|
};
|
|
}
|