Skip to content

Commit

Permalink
uses the SystemUser model directly (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
simen-rekkedal authored May 28, 2024
1 parent f64d722 commit 77e8b34
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

public interface ISystemUserClient
{
Task<SystemUserReal?> GetSpecificSystemUserReal(int partyId, Guid id, CancellationToken cancellationToken = default);
Task<SystemUserReal?> PostNewSystemUserReal(int partyId, SystemUserDescriptor newSystemUserDescriptor, CancellationToken cancellation = default);
Task<SystemUser?> GetSpecificSystemUserReal(int partyId, Guid id, CancellationToken cancellationToken = default);
Task<SystemUser?> PostNewSystemUserReal(int partyId, SystemUserDescriptor newSystemUserDescriptor, CancellationToken cancellation = default);
Task<bool> DeleteSystemUserReal(Guid id, CancellationToken cancellationToken = default);
Task<bool> ChangeSystemUserRealTitle(string newTitle, Guid id, CancellationToken cancellationToken = default);
Task<bool> ChangeSystemUserRealDescription(string newDescr, Guid id, CancellationToken cancellationToken = default);
Task<List<SystemUserReal>> GetSystemUserRealsForChosenUser(int id, CancellationToken cancellationToken = default);
Task<List<SystemUser>> GetSystemUserRealsForChosenUser(int id, CancellationToken cancellationToken = default);
Task<bool> ChangeSystemUserRealProduct(string selectedSystemType, Guid id, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
/// </summary>
public interface ISystemUserService
{
Task<List<SystemUserDTO>> GetAllSystemUserDTOsForChosenUser(int id, CancellationToken cancellationToken = default);
Task<SystemUserDTO?> GetSpecificSystemUserDTO(int partyId, Guid id, CancellationToken cancellationToken = default);
Task<SystemUserReal?> PostNewSystemUserDescriptor(int partyId, SystemUserDescriptor newSystemUserDescriptor, CancellationToken cancellation = default);
Task<List<SystemUser>> GetAllSystemUserDTOsForChosenUser(int id, CancellationToken cancellationToken = default);
Task<SystemUser?> GetSpecificSystemUserDTO(int partyId, Guid id, CancellationToken cancellationToken = default);
Task<SystemUser?> PostNewSystemUserDescriptor(int partyId, SystemUserDescriptor newSystemUserDescriptor, CancellationToken cancellation = default);
Task<bool> DeleteSystemUser(Guid id, CancellationToken cancellationToken = default);
Task<bool> ChangeSystemUserTitle(string newTitle, Guid id, CancellationToken cancellationToken = default);
Task<bool> ChangeSystemUserDescription(string newDescr, Guid id, CancellationToken cancellationToken = default);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;

namespace Altinn.Authentication.UI.Core.SystemUsers;

#nullable enable
/// <summary>
/// The model of the System User response given in the CRUD API in SystemUserController.cs
/// This model will be exchanged between this Authentication component, the PostGress db and the BFF for the Frontend.
/// The BFF will provide a tailored DTO to the Frontend.
/// </summary>
[ExcludeFromCodeCoverage]
public class SystemUser
{
/// <summary>
/// GUID created by the "real" Authentication Component
/// When the Frontend send a request for the creation of a new SystemUser the Id is null
/// </summary>
[JsonPropertyName("Id")]
public string Id { get; set; } = string.Empty;

/// <summary>
/// The Title and Description are strings set by the end-user in the Frontend.
/// In the db this field is required.
/// </summary>
[JsonPropertyName("IntegrationTitle")]
public string IntegrationTitle { get; set; }

/// <summary>
/// For off the shelf systems.
/// Should probably be human readable (instead of a GUID) but unique string without whitespace
/// The "real" Authentication Component should validate that the SystemName is unique
/// Retrieved from the SystemRegister, the full CRUD Api is in a different service
/// In the db this field is required, but if we use this model as a DTO, we allow null
/// </summary>
[JsonPropertyName("ProductName")]
public string ProductName { get; set; } = string.Empty;

/// <summary>
/// The OwnedBy identifies the end-user Organisation, and is fetched from the login Context and
/// user party serivces
/// In the db this field is required, but if we use this model as a DTO, we allow null
/// </summary>
[JsonPropertyName("OwnedByPartyId")]
public string OwnedByPartyId { get; set; } = string.Empty;

/// <summary>
/// Nice to have for debugging and logging.
/// </summary>
[JsonPropertyName("Created")]
public System.DateTime Created { get; set; } = DateTime.UtcNow;

/// <summary>
/// False by default, if a SystemUser is deleted in the API,
/// it is marked as IsDeleted ("tombstoned") rather than actually deleted
/// from the database. This is to avoid complications with cascade delete,
/// and the need to maintain consistent logging, and possible compliance rules.
/// </summary>
[JsonPropertyName("IsDeleted")]
public bool IsDeleted { get; set; } = false;

/// <summary>
/// The name of the Supplier of the Product used in this Integration.
/// In later phases, it will be possible to use non-supplier based Products, in which case the ClientId property should be filled out.
/// </summary>
[JsonPropertyName("SupplierName")]
public string SupplierName { get; set; } = string.Empty;

/// <summary>
/// The organization number for the Supplier of the Product
/// In later phases, it will be possible to use non-supplier based Products, in which case the ClientId property should be filled out.
/// </summary>
[JsonPropertyName("SupplierOrgno")]
public string SupplierOrgNo { get; set; } = string.Empty;

/// <summary>
/// For self-made systems, not delivered in the first Phase of the Project, and therefore not in the DTO
/// In these cases the SupplierName and SupplierOrgNo will be blank
/// </summary>
[JsonPropertyName("ClientId")]
public Guid ClientId { get; set; } = Guid.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ public async Task<bool> DeleteSystemUser(Guid id, CancellationToken cancellation
return await _systemUserClient.DeleteSystemUserReal(id, cancellationToken);
}

public async Task<List<SystemUserDTO>> GetAllSystemUserDTOsForChosenUser(int id, CancellationToken cancellationToken = default)
public async Task<List<SystemUser>> GetAllSystemUserDTOsForChosenUser(int id, CancellationToken cancellationToken = default)
{
var listofReal = await _systemUserClient.GetSystemUserRealsForChosenUser(id, cancellationToken);
List<SystemUserDTO> listofDTOs = new();
foreach(var sur in listofReal) {
var dto = MapFromSystemUserRealToDTO(sur);
if (dto is not null) listofDTOs.Add(dto);
}
return listofDTOs;
var lista = await _systemUserClient.GetSystemUserRealsForChosenUser(id, cancellationToken);
//List<SystemUserDTO> listofDTOs = new();
//foreach(var sur in listofReal) {
// var dto = MapFromSystemUserRealToDTO(sur);
// if (dto is not null) listofDTOs.Add(dto);
//}
return lista;
}

public async Task<SystemUserDTO?> GetSpecificSystemUserDTO(int partyId, Guid id, CancellationToken cancellationToken = default)
{
return MapFromSystemUserRealToDTO(await _systemUserClient.GetSpecificSystemUserReal(partyId ,id, cancellationToken));
public async Task<SystemUser?> GetSpecificSystemUserDTO(int partyId, Guid id, CancellationToken cancellationToken = default)
{
//return MapFromSystemUserRealToDTO(await _systemUserClient.GetSpecificSystemUserReal(partyId ,id, cancellationToken));
return await _systemUserClient.GetSpecificSystemUserReal(partyId, id, cancellationToken);
}

private static SystemUserDTO? MapFromSystemUserRealToDTO(SystemUserReal? systemUserReal)
Expand All @@ -59,7 +60,7 @@ public async Task<List<SystemUserDTO>> GetAllSystemUserDTOsForChosenUser(int id,
return systemUserDTO;
}

public async Task<SystemUserReal?> PostNewSystemUserDescriptor(int partyId, SystemUserDescriptor newSystemUserDescriptor, CancellationToken cancellation = default)
public async Task<SystemUser?> PostNewSystemUserDescriptor(int partyId, SystemUserDescriptor newSystemUserDescriptor, CancellationToken cancellation = default)
{
return await _systemUserClient.PostNewSystemUserReal(partyId, newSystemUserDescriptor, cancellation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public SystemUserClient(
_httpClient = httpClient;
}

public async Task<SystemUserReal?> GetSpecificSystemUserReal(int partyId, Guid id, CancellationToken cancellationToken = default)
public async Task<SystemUser?> GetSpecificSystemUserReal(int partyId, Guid id, CancellationToken cancellationToken = default)
{
string token = JwtTokenUtil.GetTokenFromContext(_httpContextAccessor.HttpContext!, _platformSettings.JwtCookieName!)!;
string endpointUrl = $"systemuser/{partyId}/{id}";
Expand All @@ -56,13 +56,13 @@ public SystemUserClient(

if (response.IsSuccessStatusCode)
{
return JsonSerializer.Deserialize<SystemUserReal>(await response.Content.ReadAsStringAsync(cancellationToken))!;
return JsonSerializer.Deserialize<SystemUser>(await response.Content.ReadAsStringAsync(cancellationToken))!;
}

return null;
}

public async Task<SystemUserReal?> PostNewSystemUserReal(
public async Task<SystemUser?> PostNewSystemUserReal(
int partyId,
SystemUserDescriptor newSystemUserDescriptor,
CancellationToken cancellation = default)
Expand All @@ -83,7 +83,7 @@ public SystemUserClient(

if (response.IsSuccessStatusCode)
{
return JsonSerializer.Deserialize<SystemUserReal>(await response.Content.ReadAsStringAsync(cancellation))!;
return JsonSerializer.Deserialize<SystemUser>(await response.Content.ReadAsStringAsync(cancellation))!;
}

return null;
Expand All @@ -108,25 +108,23 @@ public Task<bool> ChangeSystemUserRealDescription(string newDescr, Guid id, Canc
throw new NotImplementedException();
}

public async Task<List<SystemUserReal>> GetSystemUserRealsForChosenUser(int id, CancellationToken cancellationToken = default)
public async Task<List<SystemUser>> GetSystemUserRealsForChosenUser(int id, CancellationToken cancellationToken = default)
{
string token = JwtTokenUtil.GetTokenFromContext(_httpContextAccessor.HttpContext!, _platformSettings.JwtCookieName!)!;
string endpointUrl = $"systemuser/{id}";

List<SystemUserReal> list = [];
List<SystemUser> list = [];

HttpResponseMessage response = await _httpClient.GetAsync(token, endpointUrl);

if (response.IsSuccessStatusCode)
{
list = JsonSerializer.Deserialize<List<SystemUserReal>>(await response.Content.ReadAsStringAsync(cancellationToken))!;
list = JsonSerializer.Deserialize<List<SystemUser>>(await response.Content.ReadAsStringAsync(cancellationToken))!;
}

return list;
}



public Task<bool> ChangeSystemUserRealProduct(string selectedSystemType, Guid id, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ private static List<SystemUserReal> MockTestHelper()
return systemUserList;
}

private List<SystemUser> MockTestHelperNew()
{
return [];
}

private readonly HttpClient _httpClient;
private static List<SystemUserReal> _systemUserList = [];
private static List<SystemUser> _systemUserList = [];

private static SystemUserReal MapDescriptorToSystemUserReal(SystemUserDescriptor sysdescr)
{
Expand All @@ -71,27 +76,33 @@ private static SystemUserReal MapDescriptorToSystemUserReal(SystemUserDescriptor
public SystemUserClientMock(HttpClient httpClient)
{
_httpClient = httpClient;
_systemUserList = MockTestHelper();
_systemUserList = MockTestHelperNew();
}

public async Task<SystemUserReal?> GetSpecificSystemUserReal(int partyId, Guid id, CancellationToken cancellationToken = default)
public async Task<SystemUser?> GetSpecificSystemUserReal(int partyId, Guid id, CancellationToken cancellationToken = default)
{
await Task.Delay(50);
return _systemUserList.Find(i => i.Id == id.ToString());
}

public async Task<SystemUserReal> PostNewSystemUserReal(int partyId, SystemUserDescriptor newSystemUserDescriptor, CancellationToken cancellation = default)
public async Task<SystemUser> PostNewSystemUserReal(int partyId, SystemUserDescriptor newSystemUserDescriptor, CancellationToken cancellation = default)
{
await Task.Delay(50);
var sysreal = MapDescriptorToSystemUserReal(newSystemUserDescriptor);
_systemUserList.Add(sysreal);
return sysreal;
//var sysreal = MapDescriptorToSystemUserReal(newSystemUserDescriptor);
SystemUser newSystemUser = MapDescriptorToSystemUser(newSystemUserDescriptor);
_systemUserList.Add(newSystemUser);
return newSystemUser;
}

private SystemUser MapDescriptorToSystemUser(SystemUserDescriptor newSystemUserDescriptor)
{
throw new NotImplementedException();
}

public async Task<bool> DeleteSystemUserReal(Guid id, CancellationToken cancellationToken = default)
{
await Task.Delay(50);
SystemUserReal? toDelete = _systemUserList.Find(i => i.Id == id.ToString());
SystemUser? toDelete = _systemUserList.Find(i => i.Id == id.ToString());
if (toDelete is null) return false;
_systemUserList.Remove(toDelete);
return true;
Expand All @@ -109,7 +120,7 @@ public async Task<bool> ChangeSystemUserRealDescription(string newDescr, Guid id
throw new NotImplementedException();
}

public async Task<List<SystemUserReal>> GetSystemUserRealsForChosenUser(int id, CancellationToken cancellationToken = default)
public async Task<List<SystemUser>> GetSystemUserRealsForChosenUser(int id, CancellationToken cancellationToken = default)
{
await Task.Delay(50);
return _systemUserList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public SystemUserController(ISystemUserService systemUserService, IHttpContextAc
public async Task<ActionResult> GetSystemUserListForLoggedInUser(CancellationToken cancellationToken = default)
{
var (partyId, actionResult) = ResolvePartyId();

if (partyId == 0) return actionResult;

var list = await _systemUserService.GetAllSystemUserDTOsForChosenUser(partyId, cancellationToken);
Expand All @@ -61,7 +62,7 @@ public async Task<ActionResult> GetSystemUserDetailsById(Guid guid, Cancellation
var (partyId, actionResult) = ResolvePartyId();
if (partyId == 0) return actionResult;

SystemUserDTO? details = await _systemUserService.GetSpecificSystemUserDTO(partyId, guid, cancellationToken);
SystemUser? details = await _systemUserService.GetSpecificSystemUserDTO(partyId, guid, cancellationToken);

return Ok(details);
}
Expand Down Expand Up @@ -117,24 +118,24 @@ public async Task<ActionResult> UploadCertificate([FromForm] IFormFile file, [Fr
public async Task<ActionResult> Post([FromBody] SystemUserDescriptor newSystemUserDescriptor, CancellationToken cancellationToken = default)
{
int partyId = AuthenticationHelper.GetUsersPartyId( _httpContextAccessor.HttpContext!);
if (partyId.ToString() != newSystemUserDescriptor.OwnedByPartyId) return BadRequest();

newSystemUserDescriptor.OwnedByPartyId = partyId.ToString();
var usr = await _systemUserService.PostNewSystemUserDescriptor(partyId, newSystemUserDescriptor, cancellationToken);
if (usr is not null)
{
SystemUserDTO dto = new()
{
Id = usr.Id,
IntegrationTitle = usr.Title,
ClientId = usr.ClientId,
Created = usr.Created,
Description = usr.Description,
OwnedByPartyId = usr.OwnedByPartyId,
ProductName = usr.SystemType,
ServiceOwner = "",
SupplierName = "",
SupplierOrgno = ""
};
//SystemUserDTO dto = new()
//{
// Id = usr.Id,
// IntegrationTitle = usr.Title,
// ClientId = usr.ClientId,
// Created = usr.Created,
// Description = usr.Description,
// OwnedByPartyId = usr.OwnedByPartyId,
// ProductName = usr.SystemType,
// ServiceOwner = "",
// SupplierName = "",
// SupplierOrgno = ""
//};
return Ok(usr);
}

Expand Down

0 comments on commit 77e8b34

Please sign in to comment.