Skip to content
This repository was archived by the owner on May 13, 2023. It is now read-only.

Commit c5070e9

Browse files
committed
Fixed a check whether a user is a manager when requesting employee data
1 parent 9c647ed commit c5070e9

6 files changed

Lines changed: 9 additions & 18 deletions

File tree

API/ShiftPlanning.WebApi/Controllers/EmployeesController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ public IActionResult Get()
176176

177177
var employees = _employeeService.GetEmployees(organization.Id);
178178
if (employees == null) return NotFound();
179-
if(_authManager.IsManager(Request.Headers))
179+
//get claims of the Role type
180+
if(User.IsInRole("Manager"))
180181
{
181182
return Ok(Mapper.Map(employees.OrderBy(e => e.FirstName).ThenBy(e => e.LastName)));
182183
}

API/ShiftPlanning.WebApi/Helpers/Authorization/AuthManager.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System.Collections.Generic;
2-
using System.Linq;
3-
using System.Net.Http.Headers;
42
using Microsoft.AspNetCore.Http;
5-
using Microsoft.Extensions.Primitives;
63
using ShiftPlanning.Model.Models;
74
using ShiftPlanning.WebApi.Exceptions;
85
using ShiftPlanning.WebApi.Repositories;
@@ -46,16 +43,6 @@ public Employee GetEmployeeByHeader(IHeaderDictionary headers)
4643
return _employeeRepository.Read(tokenHash);
4744
}
4845

49-
public bool IsManager(IHeaderDictionary headers)
50-
{
51-
headers.TryGetValue("Authorization", out var token);
52-
if (token.ToString() == null) throw new ObjectNotFoundException("Could not find a manager corresponding to the given 'Authorization' header");
53-
var employee = _employeeRepository.Read(token);
54-
if (employee == null) return false;
55-
if (employee.Role_.Any(r => r.Name == "Manager")) return true;
56-
return false;
57-
}
58-
5946
public IEnumerable<Role> GetRoles(string token)
6047
{
6148
var employee = _employeeRepository.Read(token);

API/ShiftPlanning.WebApi/Helpers/Authorization/IAuthManager.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ public interface IAuthManager
1111
Employee GetEmployeeByHeader(IHeaderDictionary headers);
1212
bool ValidateOrganizationApiKey(string apiKey);
1313
IEnumerable<Role> GetRoles(string token);
14-
bool IsManager(IHeaderDictionary headers);
1514
}
1615
}

API/ShiftPlanning.WebApi/Repositories/EmployeeRepository.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public IEnumerable<Employee> ReadFromOrganization(int organizationId)
6060
{
6161
return _context.Employees
6262
.Where(e => e.Organization.Id == organizationId).OrderBy(x => x.Id)
63-
.Include(x => x.Role_);
63+
.Include(x => x.Role_)
64+
.Include(x => x.CheckIns);
6465
}
6566

6667
public IEnumerable<Employee> ReadFromOrganization(string shortKey)

API/ShiftPlanning.WebApi/Repositories/ScheduleRepository.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public Schedule Read(int id, int organizationId)
4343
{
4444
return _context.Schedules
4545
.Where(x => x.Id == id && x.Organization.Id == organizationId)
46+
.Include(x => x.Shifts)
4647
.Include(x => x.ScheduledShifts)
4748
.ThenInclude(shift => shift.EmployeeAssignments)
4849
.ThenInclude(assignment => assignment.Employee)
@@ -82,7 +83,9 @@ public int Update(Schedule schedule)
8283

8384
public void DeleteScheduledShift(int scheduleId, int scheduledShiftId, int organizationId)
8485
{
85-
var schedule = _context.Schedules.SingleOrDefault(x => x.Id == scheduleId && x.Organization.Id == organizationId);
86+
var schedule = _context.Schedules
87+
.Include(x => x.ScheduledShifts)
88+
.SingleOrDefault(x => x.Id == scheduleId && x.Organization.Id == organizationId);
8689
if (schedule == null) throw new ObjectNotFoundException("Could not find a schedule corresponding to the given id");
8790

8891
var scheduledShift = schedule.ScheduledShifts.SingleOrDefault(x => x.Id == scheduledShiftId);

API/ShiftPlanning.WebApi/Repositories/ShiftRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void Delete(IEnumerable<Shift> shifts)
3737

3838
public void Delete(int id, int organizationId)
3939
{
40-
var shift = _context.Shifts.FirstOrDefault(x => x.Id == id && x.Organization.Id == organizationId);
40+
var shift = _context.Shifts.Include(x => x.CheckIns).FirstOrDefault(x => x.Id == id && x.Organization.Id == organizationId);
4141
if (shift == null) throw new ObjectNotFoundException("Could not find a shift corresponding to the given id");
4242

4343
if(shift.CheckIns.Any()) throw new ForbiddenException("You cannot delete a shift that contains checked in employees");

0 commit comments

Comments
 (0)