forked from redhat-developer/s2i-dotnetcore
-
Notifications
You must be signed in to change notification settings - Fork 19
CVCS - VSBC amalgamation Migration Endpoints #631
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
Open
Cweiner402
wants to merge
1
commit into
bcgov:master
Choose a base branch
from
Cweiner402:feature/datamigrationtoolset
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
Server/SchoolBusAPI/Authorization/MigrationAuthorizationAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| namespace SchoolBusAPI.Authorization | ||
| { | ||
| /// <summary> | ||
| /// Protects migration endpoints: allow if X-Migration-Api-Key matches config, or JWT user has required permissions. | ||
| /// </summary> | ||
| public class MigrationAuthorizationAttribute : TypeFilterAttribute | ||
| { | ||
| public MigrationAuthorizationAttribute(params string[] permissions) : base(typeof(MigrationAuthorizationFilter)) | ||
| { | ||
| Arguments = new object[] { new PermissionRequirement(permissions) }; | ||
| } | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
Server/SchoolBusAPI/Authorization/MigrationAuthorizationFilter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| using Microsoft.AspNetCore.Authorization; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.AspNetCore.Mvc.Filters; | ||
| using Microsoft.Extensions.Configuration; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace SchoolBusAPI.Authorization | ||
| { | ||
| /// <summary> | ||
| /// Allows migration endpoints via API key (no user in legacy DB) or JWT user with required permissions. | ||
| /// </summary> | ||
| public class MigrationAuthorizationFilter : IAsyncAuthorizationFilter | ||
| { | ||
| public const string MigrationApiKeyHeaderName = "X-Migration-Api-Key"; | ||
| public const string ConfigKey = "Migration:ApiKey"; | ||
|
|
||
| private readonly IConfiguration _configuration; | ||
| private readonly IAuthorizationService _authService; | ||
| private readonly PermissionRequirement _requiredPermissions; | ||
|
|
||
| public MigrationAuthorizationFilter( | ||
| IConfiguration configuration, | ||
| IAuthorizationService authService, | ||
| PermissionRequirement requiredPermissions) | ||
| { | ||
| _configuration = configuration; | ||
| _authService = authService; | ||
| _requiredPermissions = requiredPermissions; | ||
| } | ||
|
|
||
| public async Task OnAuthorizationAsync(AuthorizationFilterContext context) | ||
| { | ||
| var apiKeyFromConfig = _configuration[ConfigKey]?.Trim(); | ||
| var apiKeyFromRequest = context.HttpContext.Request.Headers[MigrationApiKeyHeaderName].FirstOrDefault()?.Trim(); | ||
|
|
||
| if (!string.IsNullOrEmpty(apiKeyFromConfig) && | ||
| !string.IsNullOrEmpty(apiKeyFromRequest) && | ||
| apiKeyFromConfig.Equals(apiKeyFromRequest, System.StringComparison.Ordinal)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var result = await _authService.AuthorizeAsync( | ||
| context.HttpContext.User, | ||
| context.ActionDescriptor.DisplayName, | ||
| _requiredPermissions); | ||
|
|
||
| if (!result.Succeeded) | ||
| { | ||
| var problem = new ValidationProblemDetails() | ||
| { | ||
| Type = "https://sb.bc.gov.ca/exception", | ||
| Title = "Access denied", | ||
| Status = StatusCodes.Status401Unauthorized, | ||
| Detail = "Valid X-Migration-Api-Key header or JWT with permission required.", | ||
| Instance = context.HttpContext.Request.Path | ||
| }; | ||
| problem.Extensions.Add("traceId", context.HttpContext.TraceIdentifier); | ||
| context.Result = new UnauthorizedObjectResult(problem); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /* | ||
| * Migration export API for moving data to the new system. | ||
| * Consumed by Hangfire jobs in the new app via HTTP. Supports pagination for large sets. | ||
| * Access: valid X-Migration-Api-Key header (when Migration:ApiKey is set) OR JWT user with required permission. | ||
| */ | ||
|
|
||
| using Microsoft.AspNetCore.Authorization; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using SchoolBusAPI.Authorization; | ||
| using SchoolBusAPI.Services; | ||
|
|
||
| namespace SchoolBusAPI.Controllers | ||
| { | ||
| /// <summary> | ||
| /// Endpoints that return full entity data for migration to the new system | ||
| /// </summary> | ||
| [ApiVersion("1.0")] | ||
| [ApiController] | ||
| [AllowAnonymous] | ||
| public class MigrationController : ControllerBase | ||
| { | ||
| private readonly IMigrationService _migrationService; | ||
|
|
||
| public MigrationController(IMigrationService migrationService) | ||
| { | ||
| _migrationService = migrationService; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Full inspection records with SchoolBus and Inspector. Use skip/take for batching. | ||
| /// </summary> | ||
| /// <param name="skip">Number of records to skip (optional)</param> | ||
| /// <param name="take">Max records to return (optional)</param> | ||
| [HttpGet] | ||
| [Route("/api/migration/inspections")] | ||
| [MigrationAuthorization(Permissions.SchoolBusRead)] | ||
| public IActionResult GetInspections([FromQuery] int? skip, [FromQuery] int? take) | ||
| { | ||
| return _migrationService.GetInspections(skip, take); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Full school bus records with Notes, Attachments, History, CCWData, and related entities. Use skip/take for batching. | ||
| /// </summary> | ||
| [HttpGet] | ||
| [Route("/api/migration/schoolbuses")] | ||
| [MigrationAuthorization(Permissions.SchoolBusRead)] | ||
| public IActionResult GetSchoolBuses([FromQuery] int? skip, [FromQuery] int? take) | ||
| { | ||
| return _migrationService.GetSchoolBuses(skip, take); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Full school bus owner records with Contacts, Notes, Attachments, History. Use skip/take for batching. | ||
| /// </summary> | ||
| [HttpGet] | ||
| [Route("/api/migration/schoolbusowners")] | ||
| [MigrationAuthorization(Permissions.OwnerRead)] | ||
| public IActionResult GetSchoolBusOwners([FromQuery] int? skip, [FromQuery] int? take) | ||
| { | ||
| return _migrationService.GetSchoolBusOwners(skip, take); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// All school districts (reference data, typically small set) | ||
| /// </summary> | ||
| [HttpGet] | ||
| [Route("/api/migration/schooldistricts")] | ||
| [MigrationAuthorization(Permissions.CodeRead)] | ||
| public IActionResult GetSchoolDistricts() | ||
| { | ||
| return _migrationService.GetSchoolDistricts(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// All service areas with District (reference data) | ||
| /// </summary> | ||
| [HttpGet] | ||
| [Route("/api/migration/serviceareas")] | ||
| [MigrationAuthorization(Permissions.CodeRead)] | ||
| public IActionResult GetServiceAreas() | ||
| { | ||
| return _migrationService.GetServiceAreas(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Full contact records with SchoolBusOwner. Use skip/take for batching. | ||
| /// </summary> | ||
| [HttpGet] | ||
| [Route("/api/migration/contacts")] | ||
| [MigrationAuthorization(Permissions.OwnerRead)] | ||
| public IActionResult GetContacts([FromQuery] int? skip, [FromQuery] int? take) | ||
| { | ||
| return _migrationService.GetContacts(skip, take); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Migration export endpoints for moving data to new system. | ||
| * Used by Hangfire in the new app to pull full entity sets. | ||
| */ | ||
|
|
||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| namespace SchoolBusAPI.Services | ||
| { | ||
| /// <summary> | ||
| /// Service that returns full entity data for migration export | ||
| /// </summary> | ||
| public interface IMigrationService | ||
| { | ||
| /// <summary>All inspections with SchoolBus and Inspector</summary> | ||
| IActionResult GetInspections(int? skip, int? take); | ||
|
|
||
| /// <summary>All school buses with Notes, Attachments, History, CCWData, related entities</summary> | ||
| IActionResult GetSchoolBuses(int? skip, int? take); | ||
|
|
||
| /// <summary>All school bus owners with Contacts, Notes, Attachments, History</summary> | ||
| IActionResult GetSchoolBusOwners(int? skip, int? take); | ||
|
|
||
| /// <summary>All school districts</summary> | ||
| IActionResult GetSchoolDistricts(); | ||
|
|
||
| /// <summary>All service areas with District</summary> | ||
| IActionResult GetServiceAreas(); | ||
|
|
||
| /// <summary>All contacts with SchoolBusOwner</summary> | ||
| IActionResult GetContacts(int? skip, int? take); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* | ||
| * Migration export implementation. Read-only, AsNoTracking for bulk export. | ||
| */ | ||
|
|
||
| using System.Linq; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using SchoolBusAPI.Models; | ||
|
|
||
| namespace SchoolBusAPI.Services | ||
| { | ||
| /// <summary> | ||
| /// Exports full entity sets for migration; read-only, no tracking | ||
| /// </summary> | ||
| public class MigrationService : IMigrationService | ||
| { | ||
| private readonly DbAppContext _context; | ||
|
|
||
| public MigrationService(DbAppContext context) | ||
| { | ||
| _context = context; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public IActionResult GetInspections(int? skip, int? take) | ||
| { | ||
| var query = _context.Inspections | ||
| .AsNoTracking() | ||
| .Include(x => x.SchoolBus) | ||
| .Include(x => x.Inspector) | ||
| .OrderBy(x => x.Id); | ||
|
|
||
| var list = ApplyPagination(query, skip, take).ToList(); | ||
| return new ObjectResult(list); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public IActionResult GetSchoolBuses(int? skip, int? take) | ||
| { | ||
| var query = _context.SchoolBuss | ||
| .AsNoTracking() | ||
| .Include(x => x.HomeTerminalCity) | ||
| .Include(x => x.SchoolDistrict) | ||
| .Include(x => x.SchoolBusOwner).ThenInclude(o => o.PrimaryContact) | ||
| .Include(x => x.District).ThenInclude(d => d.Region) | ||
| .Include(x => x.Inspector) | ||
| .Include(x => x.CCWData) | ||
| .Include(x => x.Notes) | ||
| .Include(x => x.Attachments) | ||
| .Include(x => x.History) | ||
| .Include(x => x.CCWNotifications) | ||
| .OrderBy(x => x.Id); | ||
|
|
||
| var list = ApplyPagination(query, skip, take).ToList(); | ||
| return new ObjectResult(list); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public IActionResult GetSchoolBusOwners(int? skip, int? take) | ||
| { | ||
| var query = _context.SchoolBusOwners | ||
| .AsNoTracking() | ||
| .Include(x => x.PrimaryContact) | ||
| .Include(x => x.District) | ||
| .Include(x => x.Contacts) | ||
| .Include(x => x.Notes) | ||
| .Include(x => x.Attachments) | ||
| .Include(x => x.History) | ||
| .OrderBy(x => x.Id); | ||
|
|
||
| var list = ApplyPagination(query, skip, take).ToList(); | ||
| return new ObjectResult(list); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public IActionResult GetSchoolDistricts() | ||
| { | ||
| var list = _context.SchoolDistricts | ||
| .AsNoTracking() | ||
| .OrderBy(x => x.Id) | ||
| .ToList(); | ||
| return new ObjectResult(list); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public IActionResult GetServiceAreas() | ||
| { | ||
| var list = _context.ServiceAreas | ||
| .AsNoTracking() | ||
| .Include(x => x.District) | ||
| .OrderBy(x => x.Id) | ||
| .ToList(); | ||
| return new ObjectResult(list); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public IActionResult GetContacts(int? skip, int? take) | ||
| { | ||
| var query = _context.Contacts | ||
| .AsNoTracking() | ||
| .Include(x => x.SchoolBusOwner) | ||
| .OrderBy(x => x.Id); | ||
|
|
||
| var list = ApplyPagination(query, skip, take).ToList(); | ||
| return new ObjectResult(list); | ||
| } | ||
|
|
||
| private static IQueryable<T> ApplyPagination<T>(IQueryable<T> query, int? skip, int? take) | ||
| { | ||
| if (skip.HasValue && skip.Value > 0) | ||
| query = query.Skip(skip.Value); | ||
| if (take.HasValue && take.Value > 0) | ||
| query = query.Take(take.Value); | ||
| return query; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For these endpoints, do you need to get users or roles information as well? or any other database data like ccw data and history data?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are handling the user role/info checks in the New World so nothing there.
CCW and History we should be getting as a live feed through ICBCS via APIC's endpoints from ICBC's new REST service.
So I BELIEVE that's everything I have a call at 1pm March 5th to confim nothing was missed but those new endpoints actually drop everything back to New World so it should be accounted for in the response payload already. It'd be a change to New World not SchoolBus app wise.