Skip to content

Commit

Permalink
psp-5860 limit lease and license access by region. (#3298)
Browse files Browse the repository at this point in the history
* psp-5860 limit lease and license access by region.

* PR updates.

* lint fixes.

* fix null vs undefined on project display.

* correct null vs undefined at model level.

* removed dead code.
  • Loading branch information
devinleighsmith authored Jul 6, 2023
1 parent 10727ef commit 1089c2b
Show file tree
Hide file tree
Showing 37 changed files with 417 additions and 166 deletions.
33 changes: 32 additions & 1 deletion source/backend/api/Areas/Leases/Controllers/LeaseController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MapsterMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Pims.Api.Policies;
using Pims.Api.Services;
using Pims.Core.Extensions;
using Pims.Core.Json;
using Pims.Dal.Exceptions;
using Pims.Dal.Security;
using Swashbuckle.AspNetCore.Annotations;
Expand All @@ -25,6 +29,7 @@ public class LeaseController : ControllerBase
#region Variables
private readonly ILeaseService _leaseService;
private readonly IMapper _mapper;
private readonly ILogger<LeaseController> _logger;
#endregion

#region Constructors
Expand All @@ -34,11 +39,13 @@ public class LeaseController : ControllerBase
/// </summary>
/// <param name="leaseService"></param>
/// <param name="mapper"></param>
/// <param name="logger"></param>
///
public LeaseController(ILeaseService leaseService, IMapper mapper)
public LeaseController(ILeaseService leaseService, IMapper mapper, ILogger<LeaseController> logger)
{
_mapper = mapper;
_leaseService = leaseService;
_logger = logger;
}
#endregion

Expand All @@ -53,8 +60,16 @@ public LeaseController(ILeaseService leaseService, IMapper mapper)
[Produces("application/json")]
[ProducesResponseType(typeof(IEnumerable<Api.Models.Concepts.LeaseModel>), 200)]
[SwaggerOperation(Tags = new[] { "lease" })]
[TypeFilter(typeof(NullJsonResultFilter))]
public IActionResult GetLease(int id)
{
_logger.LogInformation(
"Request received by Controller: {Controller}, Action: {ControllerAction}, User: {User}, DateTime: {DateTime}",
nameof(LeaseController),
nameof(GetLease),
User.GetUsername(),
DateTime.Now);

var lease = _leaseService.GetById(id);
var mapped = _mapper.Map<Api.Models.Concepts.LeaseModel>(lease);
return new JsonResult(mapped);
Expand All @@ -69,8 +84,16 @@ public IActionResult GetLease(int id)
[Produces("application/json")]
[ProducesResponseType(typeof(IEnumerable<Api.Models.Concepts.LeaseModel>), 200)]
[SwaggerOperation(Tags = new[] { "lease" })]
[TypeFilter(typeof(NullJsonResultFilter))]
public IActionResult AddLease(Api.Models.Concepts.LeaseModel leaseModel, [FromQuery] string[] userOverrideCodes)
{
_logger.LogInformation(
"Request received by Controller: {Controller}, Action: {ControllerAction}, User: {User}, DateTime: {DateTime}",
nameof(LeaseController),
nameof(AddLease),
User.GetUsername(),
DateTime.Now);

var leaseEntity = _mapper.Map<Pims.Dal.Entities.PimsLease>(leaseModel);
var userOverrides = userOverrideCodes.Select(x => UserOverrideCode.Parse(x));
var lease = _leaseService.Add(leaseEntity, userOverrides);
Expand All @@ -87,8 +110,16 @@ public IActionResult AddLease(Api.Models.Concepts.LeaseModel leaseModel, [FromQu
[Produces("application/json")]
[ProducesResponseType(typeof(IEnumerable<Api.Models.Concepts.LeaseModel>), 200)]
[SwaggerOperation(Tags = new[] { "lease" })]
[TypeFilter(typeof(NullJsonResultFilter))]
public IActionResult UpdateLease(Api.Models.Concepts.LeaseModel leaseModel, [FromQuery] string[] userOverrideCodes)
{
_logger.LogInformation(
"Request received by Controller: {Controller}, Action: {ControllerAction}, User: {User}, DateTime: {DateTime}",
nameof(LeaseController),
nameof(UpdateLease),
User.GetUsername(),
DateTime.Now);

var leaseEntity = _mapper.Map<Pims.Dal.Entities.PimsLease>(leaseModel);
var userOverrides = userOverrideCodes.Select(x => UserOverrideCode.Parse(x));
var updatedLease = _leaseService.Update(leaseEntity, userOverrides);
Expand Down
35 changes: 29 additions & 6 deletions source/backend/api/Areas/Leases/Controllers/SearchController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Pims.Api.Areas.Lease.Models.Search;
using Pims.Api.Helpers.Exceptions;
using Pims.Api.Helpers.Extensions;
using Pims.Api.Policies;
using Pims.Api.Services;
using Pims.Core.Extensions;
using Pims.Core.Json;
using Pims.Dal.Entities.Models;
using Pims.Dal.Repositories;
using Pims.Dal.Security;
using Serilog.Core;
using Swashbuckle.AspNetCore.Annotations;

namespace Pims.Api.Areas.Lease.Controllers
Expand All @@ -27,22 +31,25 @@ namespace Pims.Api.Areas.Lease.Controllers
public class SearchController : ControllerBase
{
#region Variables
private readonly ILeaseRepository _leaseRepository;
private readonly ILeaseService _leaseService;
private readonly IMapper _mapper;
private readonly ILogger<LeaseController> _logger;
#endregion

#region Constructors

/// <summary>
/// Creates a new instance of a SearchController(LIS) class, initializes it with the specified arguments.
/// </summary>
/// <param name="leaseRepository"></param>
/// <param name="leaseService"></param>
/// <param name="mapper"></param>
/// <param name="logger"></param>
///
public SearchController(ILeaseRepository leaseRepository, IMapper mapper)
public SearchController(ILeaseService leaseService, IMapper mapper, ILogger<LeaseController> logger)
{
_leaseRepository = leaseRepository;
_leaseService = leaseService;
_mapper = mapper;
_logger = logger;
}
#endregion

Expand All @@ -59,8 +66,16 @@ public SearchController(ILeaseRepository leaseRepository, IMapper mapper)
[ProducesResponseType(typeof(IEnumerable<LeaseModel>), 200)]
[ProducesResponseType(typeof(Api.Models.ErrorResponseModel), 400)]
[SwaggerOperation(Tags = new[] { "lease" })]
[TypeFilter(typeof(NullJsonResultFilter))]
public IActionResult GetLeases()
{
_logger.LogInformation(
"Request received by Controller: {Controller}, Action: {ControllerAction}, User: {User}, DateTime: {DateTime}",
nameof(SearchController),
nameof(GetLeases),
User.GetUsername(),
DateTime.Now);

var uri = new Uri(this.Request.GetDisplayUrl());
var query = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query);
return GetLeases(new LeaseFilterModel(query));
Expand All @@ -77,15 +92,23 @@ public IActionResult GetLeases()
[ProducesResponseType(typeof(IEnumerable<LeaseModel>), 200)]
[ProducesResponseType(typeof(Api.Models.ErrorResponseModel), 400)]
[SwaggerOperation(Tags = new[] { "lease" })]
[TypeFilter(typeof(NullJsonResultFilter))]
public IActionResult GetLeases([FromBody] LeaseFilterModel filter)
{
_logger.LogInformation(
"Request received by Controller: {Controller}, Action: {ControllerAction}, User: {User}, DateTime: {DateTime}",
nameof(SearchController),
nameof(GetLeases),
User.GetUsername(),
DateTime.Now);

filter.ThrowBadRequestIfNull($"The request must include a filter.");
if (!filter.IsValid())
{
throw new BadRequestException("Property filter must contain valid values.");
}

var leases = _leaseRepository.GetPage((LeaseFilter)filter);
var leases = _leaseService.GetPage((LeaseFilter)filter);
return new JsonResult(_mapper.Map<Api.Models.PageModel<LeaseModel>>(leases));
}
#endregion
Expand Down
11 changes: 5 additions & 6 deletions source/backend/api/Areas/Reports/Controllers/LeaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class LeaseController : ControllerBase
{
#region Variables
private readonly ILookupRepository _lookupRepository;
private readonly ILeaseRepository _leaseRepository;
private readonly ILeaseService _leaseService;
private readonly ILeaseReportsService _leaseReportService;
private readonly ILeasePaymentService _leasePaymentService;
private readonly IMapper _mapper;
Expand All @@ -50,15 +50,15 @@ public class LeaseController : ControllerBase
/// Creates a new instance of a ReportController class, initializes it with the specified arguments.
/// </summary>
/// <param name="lookupRepository"></param>
/// <param name="leaseRepository"></param>
/// <param name="leaseService"></param>
/// <param name="leaseReportService"></param>
/// <param name="leasePaymentService"></param>
/// <param name="webHostEnvironment"></param>
/// <param name="mapper"></param>
public LeaseController(ILookupRepository lookupRepository, ILeaseRepository leaseRepository, ILeaseReportsService leaseReportService, ILeasePaymentService leasePaymentService, IWebHostEnvironment webHostEnvironment, IMapper mapper)
public LeaseController(ILookupRepository lookupRepository, ILeaseService leaseService, ILeaseReportsService leaseReportService, ILeasePaymentService leasePaymentService, IWebHostEnvironment webHostEnvironment, IMapper mapper)
{
_lookupRepository = lookupRepository;
_leaseRepository = leaseRepository;
_leaseService = leaseService;
_leaseReportService = leaseReportService;
_leasePaymentService = leasePaymentService;
_mapper = mapper;
Expand Down Expand Up @@ -202,8 +202,7 @@ public IActionResult ExportLeasePayments(int fiscalYearStart)
/// <returns></returns>
public IEnumerable<LeaseModel> GetCrossJoinLeases(Lease.Models.Search.LeaseFilterModel filter, bool all = false)
{
filter.Quantity = all ? _leaseRepository.Count() : filter.Quantity;
var page = _leaseRepository.GetPage((LeaseFilter)filter);
var page = _leaseService.GetPage((LeaseFilter)filter, all);
var allLeases = page.Items.SelectMany(l => l.PimsLeaseTerms.DefaultIfEmpty(), (lease, term) => (lease, term))
.SelectMany(lt => lt.lease.PimsPropertyLeases.DefaultIfEmpty(), (leaseTerm, property) => (leaseTerm.term, leaseTerm.lease, property))
.SelectMany(ltp => ltp.lease.PimsLeaseTenants.DefaultIfEmpty(), (leaseTermProperty, tenant) => (leaseTermProperty.term, leaseTermProperty.lease, leaseTermProperty.property, tenant));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public void Register(TypeAdapterConfig config)
{
config.NewConfig<Entity.PimsPropertyLease, Model.PropertyLeaseModel>()
.Map(dest => dest.Property, src => src.Property)
.Map(dest => dest.PropertyId, src => src.PropertyId)
.Map(dest => dest.Lease.Id, src => src.LeaseId)
.Map(dest => dest.AreaUnitType, src => src.AreaUnitTypeCodeNavigation)
.Map(dest => dest.LeaseArea, src => src.LeaseArea)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class PropertyLeaseModel : BaseAppModel

public long? LeaseId { get; set; }

public long? PropertyId { get; set; }

public PropertyModel Property { get; set; }

public LeaseModel Lease { get; set; }
Expand Down
3 changes: 3 additions & 0 deletions source/backend/api/Services/ILeaseService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using Pims.Dal.Entities;
using Pims.Dal.Entities.Models;
using Pims.Dal.Exceptions;

namespace Pims.Api.Services
Expand All @@ -10,6 +11,8 @@ public interface ILeaseService

PimsLease GetById(long leaseId);

Paged<PimsLease> GetPage(LeaseFilter filter, bool? all = false);

PimsLease Add(PimsLease lease, IEnumerable<UserOverrideCode> userOverrides);

PimsLease Update(PimsLease lease, IEnumerable<UserOverrideCode> userOverrides);
Expand Down
9 changes: 7 additions & 2 deletions source/backend/api/Services/LeaseReportsService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using Pims.Core.Extensions;
using Pims.Dal.Entities;
Expand All @@ -13,18 +14,21 @@ namespace Pims.Api.Services
public class LeaseReportsService : ILeaseReportsService
{
private readonly ILeaseRepository _leaseRepository;
private readonly IUserRepository _userRepository;
private readonly ClaimsPrincipal _user;

public LeaseReportsService(ILeaseRepository leaseRepository, ClaimsPrincipal user)
public LeaseReportsService(ILeaseRepository leaseRepository, IUserRepository userRepository, ClaimsPrincipal user)
{
_leaseRepository = leaseRepository;
_userRepository = userRepository;
_user = user;
}

public IEnumerable<PimsLease> GetAggregatedLeaseReport(int fiscalYearStart)
{
_user.ThrowIfNotAuthorized(Permissions.LeaseView);
DateTime fiscalYearStartDate = fiscalYearStart.ToFiscalYearDate();
var user = _userRepository.GetByKeycloakUserId(this._user.GetUserKey());

// fiscal defined as April 01 to March 31 of following year
return _leaseRepository.GetAllByFilter(
Expand All @@ -34,7 +38,8 @@ public IEnumerable<PimsLease> GetAggregatedLeaseReport(int fiscalYearStart)
StartBeforeDate = fiscalYearStartDate.AddYears(1).AddDays(-1),
NotInStatus = new List<string>() { PimsLeaseStatusTypes.DRAFT, PimsLeaseStatusTypes.DISCARD },
IsReceivable = true,
}, true);
}, user.PimsRegionUsers.Select(u => u.RegionCode).ToHashSet(),
true);
}
}
}
Loading

0 comments on commit 1089c2b

Please sign in to comment.