Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions api/src/controllers/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,18 @@ class AdminController {
place_id = parseInt(place_id);
}
const accessLevel = await this.memberService.getAccessLevel(session.id);
if (accessLevel.includes('admin')) {
const roleId = parseInt(role_id);
const canManageSecurityRoles =
await this.memberService.canManageSecurityRoles(session.id);
const canManageRole =
accessLevel.includes('admin') ||
(canManageSecurityRoles &&
this.memberService.canSecurityManageRole(roleId));
if (canManageRole) {
try {
await this.adminService.fireRole(
parseInt(member_id),
parseInt(role_id),
roleId,
place_id,
);
response.status(200).json({message: 'Role fired successfully'});
Expand All @@ -139,7 +146,7 @@ class AdminController {
response.status(403).json({error: 'Access Denied'});
}
}

public async getDonor(request: Request, response: Response): Promise<string> {
const session = this.memberService.decryptSession(request, response);
if (!session) return;
Expand Down Expand Up @@ -179,12 +186,19 @@ class AdminController {
const session = this.memberService.decryptSession(request, response);
if (!session) return;
const accessLevel = await this.memberService.getAccessLevel(session.id);
if (accessLevel.includes('admin')) {
const {member_id, role_id} = request.body;
const {member_id, role_id} = request.body;
const roleId = parseInt(role_id);
const canManageSecurityRoles =
await this.memberService.canManageSecurityRoles(session.id);
const canManageRole =
accessLevel.includes('admin') ||
(canManageSecurityRoles &&
this.memberService.canSecurityManageRole(roleId));
if (canManageRole) {
try {
await this.adminService.hireRole(
parseInt(member_id),
parseInt(role_id),
roleId,
);
response.status(200).json({message: 'Role hired successfully'});
} catch(e) {
Expand Down
13 changes: 13 additions & 0 deletions api/src/controllers/member.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ class MemberController {
response.status(200).json({ accessLevel });
}

public async canManageSecurityRoles(
request: Request,
response: Response,
): Promise<void> {
const session = this.memberService.decryptSession(request, response);
if (!session) return;

const canManage =
await this.memberService.canManageSecurityRoles(session.id);

response.status(200).json({ canManage });
}

public async getDonorLevel(request: Request, response: Response): Promise<string> {
const session = this.memberService.decryptSession(request, response);
if (!session) return;
Expand Down
3 changes: 3 additions & 0 deletions api/src/routes/member.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ memberRoutes.get('/getrolename', (request, response) =>
memberRoutes.get('/getadminlevel', (request, response) =>
memberController.getAdminLevel(request, response),
);
memberRoutes.get('/can-manage-security-roles', (request, response) =>
memberController.canManageSecurityRoles(request, response),
);
memberRoutes.get('/getdonorlevel', (request, response) =>
memberController.getDonorLevel(request, response),
);
Expand Down
23 changes: 23 additions & 0 deletions api/src/services/member/member.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,36 @@ export class MemberService {
this.roleRepository.roleMap.Admin,
this.roleRepository.roleMap.SecurityCaptain,
this.roleRepository.roleMap.SecurityChief,
this.roleRepository.roleMap.DeputySecurityChief,
this.roleRepository.roleMap.SecurityLieutenant,
this.roleRepository.roleMap.SecurityOfficer,
this.roleRepository.roleMap.SecuritySergeant,
];
return !!roleAssignments.find(assignment => ADMIN_ROLES.includes(assignment.role_id));
}

public async canManageSecurityRoles(memberId: number): Promise<boolean> {
const roleAssignments =
await this.roleAssignmentRepository.getByMemberId(memberId);
const SECURITY_ROLE_MANAGERS = [
this.roleRepository.roleMap.SecurityChief,
this.roleRepository.roleMap.DeputySecurityChief,
];
return !!roleAssignments.find(assignment => SECURITY_ROLE_MANAGERS.includes(assignment.role_id),);
}

public canSecurityManageRole(roleId: number): boolean {
const SECURITY_ROLES = [
this.roleRepository.roleMap.SecurityCaptain,
this.roleRepository.roleMap.SecurityLieutenant,
this.roleRepository.roleMap.SecuritySergeant,
this.roleRepository.roleMap.SecurityOfficer,
this.roleRepository.roleMap.SecurityAdvisor,
this.roleRepository.roleMap.JailGuard,
];
return SECURITY_ROLES.includes(roleId);
}

public async canLeader(memberId: number): Promise<boolean> {
const roleAssignments = await this.roleAssignmentRepository.getByMemberId(memberId);
// Extracted admin roles into a constant for easy management
Expand Down
21 changes: 11 additions & 10 deletions api/src/services/place/place.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,23 +191,24 @@ export class PlaceService {
const SecurityInfo = {};
const securityRoles = [
{ mapName: 'SecurityChief', roleName: 'Security Chief' },
{ mapName: 'DeputySecurityChief', roleName: 'Deputy Security Chief' },
{ mapName: 'SecurityCaptain', roleName: 'Security Captain' },
{ mapName: 'SecurityLieutenant', roleName: 'Security Lieutenant' },
{ mapName: 'SecuritySergeant', roleName: 'Security Sergeant' },
{ mapName: 'SecurityOfficer', roleName: 'Security Officer' },
{ mapName: 'JailGuard', roleName: 'Jail Guard' },
];
try {
await Promise.all(securityRoles.map(async (role) => {
const roleCode = await this.roleRepository.roleMap[role.mapName];
await this.roleAssignmentRepository.getUsernamesByRoleId(roleCode).then(response => {
const users = [];
response.forEach(row => {
users.push(row.username);
});
SecurityInfo[role.roleName] = users;
});
}));
for (const role of securityRoles) {
const roleCode = await this.roleRepository.roleMap[role.mapName];
const response = await this.roleAssignmentRepository.getUsernamesByRoleId(roleCode);
const users = [];

response.forEach(row => {
users.push(row.username);
});
SecurityInfo[role.roleName] = users;
}
} catch (error) {
console.error(error);
}
Expand Down
Loading