Skip to content

Commit

Permalink
Merge pull request #493 from bcgov/1.2.5
Browse files Browse the repository at this point in the history
Version 1.2.5
  • Loading branch information
ychung-mot authored Sep 29, 2020
2 parents e654da9 + 5a51490 commit 762ea3c
Show file tree
Hide file tree
Showing 22 changed files with 212 additions and 49 deletions.
31 changes: 22 additions & 9 deletions api/Hmcr.Api/Authentication/HmcrJwtBearerEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,31 @@ private async Task<bool> PopulateCurrentUserFromDb(ClaimsPrincipal principal)
var isApiClient = false;
bool.TryParse(principal.FindFirstValue(HmcrClaimTypes.KcIsApiClient), out isApiClient);

_curentUser.UserName = isApiClient ? principal.FindFirstValue(HmcrClaimTypes.KcApiUsername) : principal.FindFirstValue(HmcrClaimTypes.KcUsername);
var usernames = _curentUser.UserName.Split("@");
//preferred_username token has a form of "{username}@{directory}".
var preferredUsername = isApiClient ? principal.FindFirstValue(HmcrClaimTypes.KcApiUsername) : principal.FindFirstValue(HmcrClaimTypes.KcUsername);
var usernames = preferredUsername.Split("@");
var username = usernames[0].ToUpperInvariant();
var directory = usernames[1].ToUpperInvariant();

var userGuidClaim = directory.ToUpperInvariant() == UserTypeDto.IDIR ? HmcrClaimTypes.KcIdirGuid : HmcrClaimTypes.KcBceidGuid;
var userGuid = new Guid(principal.FindFirstValue(userGuidClaim));

var user = await _userService.GetActiveUserEntityAsync(userGuid);
var email = principal.FindFirstValue(ClaimTypes.Email).ToUpperInvariant();

var user = await _userService.GetActiveUserEntityAsync(userGuid);
if (user == null)
{
_logger.LogWarning($"Access Denied - User[{username}/{userGuid}] does not exist");
return false;
}

//When it's an Api client, we don't want to use the username from the token because it's a hard-code value.
//This is to support the scenario where username has changed for the GUID. Note GUID never changes and unique but username can change.
if (isApiClient)
{
username = user.Username;
email = user.Email;
}

if (directory == "IDIR")
{
_curentUser.UserGuid = userGuid;
Expand All @@ -102,17 +110,22 @@ private async Task<bool> PopulateCurrentUserFromDb(ClaimsPrincipal principal)
_curentUser.UserType = UserTypeDto.BUSINESS;
}

_curentUser.UniversalId = username;
_curentUser.Username = username;
_curentUser.AuthDirName = directory;
_curentUser.Email = email;
_curentUser.UserName = username;
_curentUser.FirstName = user.FirstName;
_curentUser.LastName = user.LastName;
_curentUser.ApiClientId = user.ApiClientId;

if (!isApiClient && user.Username.ToUpperInvariant() != username || user.Email.ToUpperInvariant() != email)
if (isApiClient) //no db update, so everything's done.
{
_logger.LogInformation($"ApiClient Login - {preferredUsername}/{username}");
return true;
}

if (user.Username.ToUpperInvariant() != username || user.Email.ToUpperInvariant() != email) //when the info changed, update db with the latest info from bceid web service
{
_logger.LogWarning($"Username/Email changed from {user.Username}/{user.Email} to {user.Email}/{email}.");
_logger.LogWarning($"Username/Email changed from {user.Username}/{user.Email} to {username}/{email}.");
await _userService.UpdateUserFromBceidAsync(userGuid, username, user.UserType, user.ConcurrencyControlNumber);
}

Expand All @@ -133,7 +146,7 @@ private void AddClaimsFromUserInfo(ClaimsPrincipal principal, UserCurrentDto use
claims.Add(new Claim(HmcrClaimTypes.ServiceAreaNumber, serviceArea.ServiceAreaNumber.ToString()));
}

claims.Add(new Claim(ClaimTypes.Name, _curentUser.UniversalId));
claims.Add(new Claim(ClaimTypes.Name, _curentUser.Username));

principal.AddIdentity(new ClaimsIdentity(claims));
}
Expand Down
2 changes: 1 addition & 1 deletion api/Hmcr.Api/Middlewares/ExceptionMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task InvokeAsync(HttpContext httpContext)
return;

var guid = Guid.NewGuid();
_logger.LogError($"HMCR Exception{guid}: {ex}");
_logger.LogError($"HMCR Exception {guid}: {ex}");
await HandleExceptionAsync(httpContext, guid);
}
}
Expand Down
2 changes: 1 addition & 1 deletion api/Hmcr.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"AllowedHosts": "*",
"Constants": {
"Version": "1.2.4.0",
"Version": "1.2.5.0",
"SwaggerApiUrl": "/swagger/v1/swagger.json"
},
"Serilog": {
Expand Down
4 changes: 2 additions & 2 deletions api/Hmcr.Data/Database/Entities/AppDbContextPartial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ private void PerformAudit()
{
if (entry.Members.Any(m => m.Metadata.Name == AppCreateUserGuid)) //auditable entity
{
entry.Member(AppLastUpdateUserid).CurrentValue = _currentUser.UniversalId;
entry.Member(AppLastUpdateUserid).CurrentValue = _currentUser.Username;
entry.Member(AppLastUpdateUserDirectory).CurrentValue = _currentUser.AuthDirName;
entry.Member(AppLastUpdateUserGuid).CurrentValue = _currentUser.UserGuid;
entry.Member(AppLastUpdateTimestamp).CurrentValue = currentTime;

if (entry.State == EntityState.Added)
{
entry.Member(AppCreateUserid).CurrentValue = _currentUser.UniversalId;
entry.Member(AppCreateUserid).CurrentValue = _currentUser.Username;
entry.Member(AppCreateUserDirectory).CurrentValue = _currentUser.AuthDirName;
entry.Member(AppCreateUserGuid).CurrentValue = _currentUser.UserGuid;
entry.Member(AppCreateTimestamp).CurrentValue = currentTime;
Expand Down
2 changes: 2 additions & 0 deletions api/Hmcr.Domain/CsvHelpers/CsvHelperUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Hmcr.Model.Utils;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;

namespace Hmcr.Domain.CsvHelpers
Expand Down Expand Up @@ -32,6 +33,7 @@ public static void Config(Dictionary<string, List<string>> errors, CsvReader csv
}

csv.Configuration.IgnoreBlankLines = true;
csv.Configuration.ShouldSkipRecord = (record) => record.All(field => field.IsEmpty());
}
}
}
2 changes: 1 addition & 1 deletion api/Hmcr.Domain/Hangfire/EmailJobService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public EmailJobService(IFeebackMessageRepository feedbackRepo, IEmailService ema
public async Task ResendEmails()
{
_user.AuthDirName = UserTypeDto.IDIR;
_user.UniversalId = "hangfire";
_user.Username = "hangfire";
_user.UserGuid = new Guid();

var feedbackMessages = await _feedbackRepo.GetFailedFeedbackMessagesAsync();
Expand Down
31 changes: 26 additions & 5 deletions api/Hmcr.Domain/Hangfire/RockfallReportJobService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,14 +487,35 @@ private string GetValidationEntityName(RockfallReportCsvDto untypedRow)
CsvHelperUtils.Config(errors, csv, false);
csv.Configuration.RegisterClassMap<RockfallReportCsvDtoMap>();

var rows = csv.GetRecords<RockfallReportCsvDto>().ToList();
for (var i = 0; i < rows.Count; i++)
var rows = GetRecords(csv);

return (rows, string.Join(',', csv.Context.HeaderRecord).Replace("\"", ""));
}

private List<RockfallReportCsvDto> GetRecords(CsvReader csv)
{
var rows = new List<RockfallReportCsvDto>();

while (csv.Read())
{
rows[i].RowNum = i + 2;
rows[i].ServiceArea = _serviceArea.ConvertToServiceAreaString(rows[i].ServiceArea);
RockfallReportCsvDto row = null;

try
{
row = csv.GetRecord<RockfallReportCsvDto>();
}
catch (Exception ex)
{
_logger.LogError(ex.ToString());
throw;
}

row.RowNum = csv.Context.Row;
row.ServiceArea = _serviceArea.ConvertToServiceAreaString(row.ServiceArea);
rows.Add(row);
}

return (rows, string.Join(',', csv.Context.HeaderRecord).Replace("\"", ""));
return rows;
}

private (decimal rowNum, List<RockfallReportTyped> rows) ParseRowsTyped(string text, Dictionary<string, List<string>> errors)
Expand Down
2 changes: 1 addition & 1 deletion api/Hmcr.Domain/Hangfire/SubmissionObjectJobService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public SubmissionObjectJobService(ISubmissionObjectRepository submissionRepo,
public async Task RunReportingJob(decimal serviceAreaNumber)
{
_user.AuthDirName = UserTypeDto.IDIR;
_user.UniversalId = "hangfire";
_user.Username = "hangfire";
_user.UserGuid = new Guid();

var submissions = _submissionRepo.GetSubmissionObjecsForBackgroundJob(serviceAreaNumber);
Expand Down
31 changes: 26 additions & 5 deletions api/Hmcr.Domain/Hangfire/WildlifeReportJobService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,35 @@ private string GetValidationEntityName(WildlifeReportCsvDto untypedRow)
CsvHelperUtils.Config(errors, csv, false);
csv.Configuration.RegisterClassMap<WildlifeReportCsvDtoMap>();

var rows = csv.GetRecords<WildlifeReportCsvDto>().ToList();
for (var i = 0; i < rows.Count; i++)
var rows = GetRecords(csv);

return (rows, string.Join(',', csv.Context.HeaderRecord).Replace("\"", ""));
}

private List<WildlifeReportCsvDto> GetRecords(CsvReader csv)
{
var rows = new List<WildlifeReportCsvDto>();

while (csv.Read())
{
rows[i].RowNum = i + 2;
rows[i].ServiceArea = _serviceArea.ConvertToServiceAreaString(rows[i].ServiceArea);
WildlifeReportCsvDto row = null;

try
{
row = csv.GetRecord<WildlifeReportCsvDto>();
}
catch (Exception ex)
{
_logger.LogError(ex.ToString());
throw;
}

row.RowNum = csv.Context.Row;
row.ServiceArea = _serviceArea.ConvertToServiceAreaString(row.ServiceArea);
rows.Add(row);
}

return (rows, string.Join(',', csv.Context.HeaderRecord).Replace("\"", ""));
return rows;
}

private (decimal rowNum, List<WildlifeReportTyped> rows) ParseRowsTyped(string text, Dictionary<string, List<string>> errors)
Expand Down
33 changes: 28 additions & 5 deletions api/Hmcr.Domain/Hangfire/WorkReportJobService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CsvHelper;
using CsvHelper.TypeConversion;
using Hmcr.Data.Database;
using Hmcr.Data.Database.Entities;
using Hmcr.Data.Repositories;
Expand All @@ -19,6 +20,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -648,14 +650,35 @@ private string GetValidationEntityName(WorkReportCsvDto untypedRow, ActivityCode
CsvHelperUtils.Config(errors, csv, false);
csv.Configuration.RegisterClassMap<WorkReportCsvDtoMap>();

var rows = csv.GetRecords<WorkReportCsvDto>().ToList();
for (var i = 0; i < rows.Count; i++)
var rows = GetRecords(csv);

return (rows, string.Join(',', csv.Context.HeaderRecord).Replace("\"", ""));
}

private List<WorkReportCsvDto> GetRecords(CsvReader csv)
{
var rows = new List<WorkReportCsvDto>();

while (csv.Read())
{
rows[i].RowNum = i + 2;
rows[i].ServiceArea = _serviceArea.ConvertToServiceAreaString(rows[i].ServiceArea);
WorkReportCsvDto row = null;

try
{
row = csv.GetRecord<WorkReportCsvDto>();
}
catch (Exception ex)
{
_logger.LogError(ex.ToString());
throw;
}

row.RowNum = csv.Context.Row;
row.ServiceArea = _serviceArea.ConvertToServiceAreaString(row.ServiceArea);
rows.Add(row);
}

return (rows, string.Join(',', csv.Context.HeaderRecord).Replace("\"", ""));
return rows;
}

private (decimal rowNum, List<WorkReportTyped> rows) ParseRowsTyped(string text, Dictionary<string, List<string>> errors)
Expand Down
4 changes: 2 additions & 2 deletions api/Hmcr.Domain/Services/KeyCloakService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ public async Task<KeycloakClientDto> GetUserClientAsync()


var directoryType = _currentUser.UserType == UserTypeDto.INTERNAL ? "idir" : "bceid";
var username = $"{_currentUser.UniversalId.ToLowerInvariant()}@{directoryType}";
var username = $"{_currentUser.Username.ToLowerInvariant()}@{directoryType}";
var email = _currentUser.Email.ToLowerInvariant();
var guidClaimValue = _currentUser.UserGuid.ToString();

var createDto = new KeycloakClientCreateDto(_audience, username, email, $"{directoryType}_userid", guidClaimValue);
createDto.ClientId = $"api.{_currentUser.UserName.ToLowerInvariant()}.{GetUniqueToken(3)}";
createDto.ClientId = $"api.{_currentUser.Username.ToLowerInvariant()}.{GetUniqueToken(3)}";

var payload = JsonSerializer.Serialize(createDto, _jsonOptions);
var response = await _httpClient.PostAsync("clients", new StringContent(payload, Encoding.UTF8, "application/json"));
Expand Down
10 changes: 7 additions & 3 deletions api/Hmcr.Domain/Services/RockfallReportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ protected override async Task<bool> ParseRowsAsync(SubmissionObjectCreateDto sub

var headerValidated = false;
var rows = new List<RockfallRptInitCsvDto>();
var rowNum = 1;

while (csv.Read())
{
Expand All @@ -77,7 +76,6 @@ protected override async Task<bool> ParseRowsAsync(SubmissionObjectCreateDto sub
}
}

row.RowNum = ++rowNum;
row.ServiceArea = serviceArea.ConvertToServiceAreaString(row.ServiceArea);
rows.Add(row);
}
Expand Down Expand Up @@ -109,7 +107,13 @@ protected override async Task<bool> ParseRowsAsync(SubmissionObjectCreateDto sub

if (!serviceAreastrings.Contains(row.ServiceArea))
{
errors.AddItem("ServiceArea", $"The file contains service area which is not {serviceAreastrings[0]}.");
errors.AddItem(Fields.ServiceArea, $"The file contains service area which is not {serviceAreastrings[0]}.");
return false;
}

if (row.McrrIncidentNumber.IsEmpty())
{
errors.AddItem(Fields.McrrIncidentNumber, $"MCRR Incident Number is missing for row [{csv.Context.Row}].");
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions api/Hmcr.Domain/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public async Task<UserBceidAccountDto> GetBceidAccountAsync(string username, str
var (error, account) = await _bceid.GetBceidAccountCachedAsync(null, user.Username, user.UserType, _currentUser.UserGuid, _currentUser.UserType);
if (error.IsNotEmpty())
{
throw new HmcrException($"Unable to retrieve User[{user.Username} ({user.UserType})] from BCeID Service.");
throw new HmcrException($"Unable to retrieve User[{user.Username}]-[{user.UserType}] from BCeID Service.");
}

user.Email = account.Email;
Expand All @@ -102,7 +102,7 @@ public async Task<UserBceidAccountDto> GetBceidAccountAsync(string username, str

if (await _userRepo.DoesUsernameExistAsync(user.Username, user.UserType))
{
errors.AddItem(Fields.Username, $"Username [{user.Username} ({user.UserType})] already exists.");
errors.AddItem(Fields.Username, $"Username [{user.Username}]-[{user.UserType}] already exists.");
}

if (errors.Count > 0)
Expand Down
2 changes: 0 additions & 2 deletions api/Hmcr.Domain/Services/WildlifeReportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ protected override async Task<bool> ParseRowsAsync(SubmissionObjectCreateDto sub

var headerValidated = false;
var rows = new List<WildlifeRptInitCsvDto>();
var rowNum = 1;

while (csv.Read())
{
Expand All @@ -75,7 +74,6 @@ protected override async Task<bool> ParseRowsAsync(SubmissionObjectCreateDto sub
}
}

row.RowNum = ++rowNum;
row.ServiceArea = serviceArea.ConvertToServiceAreaString(row.ServiceArea);
rows.Add(row);
}
Expand Down
10 changes: 7 additions & 3 deletions api/Hmcr.Domain/Services/WorkReportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ protected override async Task<bool> ParseRowsAsync(SubmissionObjectCreateDto sub

var headerValidated = false;
var rows = new List<WorkRptInitCsvDto>();
var rowNum = 1;

while (csv.Read())
{
Expand All @@ -77,7 +76,6 @@ protected override async Task<bool> ParseRowsAsync(SubmissionObjectCreateDto sub
}
}

row.RowNum = ++rowNum;
row.ServiceArea = serviceArea.ConvertToServiceAreaString(row.ServiceArea);
rows.Add(row);
}
Expand Down Expand Up @@ -109,7 +107,13 @@ protected override async Task<bool> ParseRowsAsync(SubmissionObjectCreateDto sub

if (!serviceAreastrings.Contains(row.ServiceArea))
{
errors.AddItem("ServiceArea", $"The file contains service area which is not {serviceAreastrings[0]}.");
errors.AddItem(Fields.ServiceArea, $"The file contains service area which is not {serviceAreastrings[0]}.");
return false;
}

if (row.RecordNumber.IsEmpty())
{
errors.AddItem(Fields.RecordNumber, $"Record Number is missing for row [{csv.Context.Row}].");
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion api/Hmcr.Hangfire/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"AllowedHosts": "*",
"Constants": {
"Version": "1.2.2.0",
"Version": "1.2.5.0",
"SwaggerApiUrl": "/swagger/v1/swagger.json"
},
"Serilog": {
Expand Down
Loading

0 comments on commit 762ea3c

Please sign in to comment.