57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import * as dotenv from 'dotenv';
|
|
import * as UserService from '../users/users.service';
|
|
|
|
|
|
dotenv.config();
|
|
|
|
/**
|
|
* Checks if the password gives admin privileges (view / create / edit / delete)
|
|
* @param password
|
|
*/
|
|
export const checkAdminPrivileges = async (sessionId: string, sessionKey: string, ip: string) => {
|
|
if(sessionId) {
|
|
let user = await UserService.checkSession(sessionId, sessionKey, ip);
|
|
return user.isActive;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Checks if the password gives member view privileges
|
|
* @param password
|
|
*/
|
|
export const checkMemberPrivileges = async (sessionId: string, sessionKey: string, password: string, ip: string) => {
|
|
if(sessionId) {
|
|
let user = await UserService.checkSession(sessionId, sessionKey, ip);
|
|
return user.isActive;
|
|
}
|
|
|
|
return password == process.env.MEMBER_CREDENTIAL;
|
|
}
|
|
|
|
/**
|
|
* Checks if the password gives management view privileges
|
|
* @param password
|
|
*/
|
|
export const checkManagementPrivileges = async (sessionId: string, sessionKey: string, password: string, ip: string) => {
|
|
if(sessionId) {
|
|
let user = await UserService.checkSession(sessionId, sessionKey, ip);
|
|
return user.isActive;
|
|
}
|
|
|
|
return password == process.env.MANAGEMENT_CREDENTIAL;
|
|
}
|
|
|
|
export const hasAccess = async (calendarName: string, sessionId: string, sessionKey: string, password: string, ip: string) => {
|
|
switch (calendarName) {
|
|
case 'public':
|
|
return true;
|
|
case 'members':
|
|
return await checkMemberPrivileges(sessionId, sessionKey, password, ip);
|
|
case 'management':
|
|
return await checkManagementPrivileges(sessionId, sessionKey, password, ip);
|
|
default:
|
|
return false;
|
|
}
|
|
}
|