Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed 403 error when area admin tries to get applications #1197 #1204

Merged
merged 3 commits into from
Aug 25, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class ApplicationServiceTests
private Mock<ICurrentUserService> currentUserServiceMock;
private Mock<IMinistryAdminService> ministryAdminServiceMock;
private Mock<IRegionAdminService> regionAdminServiceMock;
private Mock<IAreaAdminService> areaAdminServiceMock;
private Mock<ICodeficatorService> codeficatorServiceMock;

private Mock<IOptions<ApplicationsConstraintsConfig>> applicationsConstraintsConfig;
Expand All @@ -59,6 +60,7 @@ public void SetUp()
currentUserServiceMock = new Mock<ICurrentUserService>();
ministryAdminServiceMock = new Mock<IMinistryAdminService>();
regionAdminServiceMock = new Mock<IRegionAdminService>();
areaAdminServiceMock = new Mock<IAreaAdminService>();
codeficatorServiceMock = new Mock<ICodeficatorService>();

logger = new Mock<ILogger<ApplicationService>>();
Expand All @@ -85,6 +87,7 @@ public void SetUp()
currentUserServiceMock.Object,
ministryAdminServiceMock.Object,
regionAdminServiceMock.Object,
areaAdminServiceMock.Object,
codeficatorServiceMock.Object);
}

Expand Down Expand Up @@ -184,6 +187,47 @@ public async Task GetApplications_WhenRegionAdminCalled_ShouldReturnApplications
Assert.That(result.Entities.FirstOrDefault().Workshop.InstitutionId, Is.EqualTo(institutionId));
}

[Test]
public async Task GetApplications_WhenAreaAdminCalled_ShouldReturnApplications()
{
// Arrange
var institutionId = new Guid("b929a4cd-ee3d-4bad-b2f0-d40aedf656c4");
long catottgId = 31737;
var applications = WithApplicationsList();
SetupGetAllByInstitutionId(applications);

currentUserServiceMock.Setup(c => c.IsAdmin()).Returns(true);
currentUserServiceMock.Setup(c => c.IsMinistryAdmin()).Returns(false);
currentUserServiceMock.Setup(c => c.IsRegionAdmin()).Returns(false);
currentUserServiceMock.Setup(c => c.IsAreaAdmin()).Returns(true);
areaAdminServiceMock
.Setup(m => m.GetByUserId(It.IsAny<string>()))
.Returns(Task.FromResult<AreaAdminDto>(new AreaAdminDto()
{
InstitutionId = institutionId,
CATOTTGId = catottgId,
}));

currentUserServiceMock.Setup(c => c.IsAreaAdmin()).Returns(true);
areaAdminServiceMock
.Setup(m => m.GetByUserId(It.IsAny<string>()))
.Returns(Task.FromResult<AreaAdminDto>(new AreaAdminDto()
{
InstitutionId = institutionId,
}));

codeficatorServiceMock
.Setup(x => x.GetAllChildrenIdsByParentIdAsync(It.IsAny<long>()))
.Returns(Task.FromResult((IEnumerable<long>)new List<long> { catottgId }));

// Act
var result = await service.GetAll(new ApplicationFilter());

// Assert
Assert.That(result.Entities.Count, Is.EqualTo(1));
Assert.That(result.Entities.FirstOrDefault().Workshop.InstitutionId, Is.EqualTo(institutionId));
}

[Test]
public async Task GetApplicationById_WhenIdIsValid_ShouldReturnApplication()
{
Expand Down
28 changes: 27 additions & 1 deletion OutOfSchool/OutOfSchool.WebApi/Services/ApplicationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ApplicationService : IApplicationService, INotificationReciever
private readonly ICurrentUserService currentUserService;
private readonly IMinistryAdminService ministryAdminService;
private readonly IRegionAdminService regionAdminService;
private readonly IAreaAdminService areaAdminService;
private readonly ICodeficatorService codeficatorService;

private readonly string errorNullWorkshopMessage = "Operation failed. Workshop in Application dto is null";
Expand All @@ -52,6 +53,7 @@ public class ApplicationService : IApplicationService, INotificationReciever
/// <param name="currentUserService">Service for managing current user rights.</param>
/// <param name="ministryAdminService">Service for managing ministry admin rigths.</param>
/// <param name="regionAdminService">Service for managing region admin rigths.</param>
/// <param name="areaAdminService">Service for managing area admin rigths.</param>
/// <param name="codeficatorService">Codeficator service.</param>
public ApplicationService(
IApplicationRepository repository,
Expand All @@ -66,6 +68,7 @@ public ApplicationService(
ICurrentUserService currentUserService,
IMinistryAdminService ministryAdminService,
IRegionAdminService regionAdminService,
IAreaAdminService areaAdminService,
ICodeficatorService codeficatorService)
{
applicationRepository = repository ?? throw new ArgumentNullException(nameof(repository));
Expand All @@ -83,6 +86,7 @@ public ApplicationService(
this.currentUserService = currentUserService ?? throw new ArgumentNullException(nameof(currentUserService));
this.ministryAdminService = ministryAdminService ?? throw new ArgumentNullException(nameof(ministryAdminService));
this.regionAdminService = regionAdminService ?? throw new ArgumentNullException(nameof(regionAdminService));
this.areaAdminService = areaAdminService ?? throw new ArgumentNullException(nameof(areaAdminService));
this.codeficatorService = codeficatorService ?? throw new ArgumentNullException(nameof(codeficatorService));
}

Expand Down Expand Up @@ -139,6 +143,28 @@ public async Task<SearchResult<ApplicationDto>> GetAll(ApplicationFilter filter)
}
}

if (currentUserService.IsAreaAdmin())
{
var areaAdmin = await areaAdminService.GetByUserId(currentUserService.UserId);
predicate = predicate
.And(p => p.Workshop.InstitutionHierarchy.InstitutionId == areaAdmin.InstitutionId);

var subSettlementsIds = await codeficatorService
.GetAllChildrenIdsByParentIdAsync(areaAdmin.CATOTTGId).ConfigureAwait(false);

if (subSettlementsIds.Any())
{
var tempPredicate = PredicateBuilder.False<Application>();

foreach (var item in subSettlementsIds)
{
tempPredicate = tempPredicate.Or(x => x.Workshop.Provider.LegalAddress.CATOTTGId == item);
}

predicate = predicate.And(tempPredicate);
}
}

var sortPredicate = SortExpressionBuild(filter);

var totalAmount = await applicationRepository.Count(where: predicate).ConfigureAwait(false);
Expand Down Expand Up @@ -915,4 +941,4 @@ private async Task<int> GetAmountOfApprovedApplications(Guid workshopId)
{
return await applicationRepository.Count(x => x.WorkshopId == workshopId && x.Status == ApplicationStatus.Approved);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public bool IsDeputyOrProviderAdmin() =>
IsInRole(Role.Provider) &&
(IsInSubRole(Subrole.ProviderDeputy) || IsInSubRole(Subrole.ProviderAdmin));

public bool IsAdmin() => IsInRole(Role.TechAdmin) || IsInRole(Role.MinistryAdmin) || IsInRole(Role.RegionAdmin);
public bool IsAdmin() => IsInRole(Role.TechAdmin) || IsInRole(Role.MinistryAdmin) || IsInRole(Role.RegionAdmin) || IsInRole(Role.AreaAdmin);

public bool IsTechAdmin() => IsInRole(Role.TechAdmin);

Expand Down
Loading