-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Does to much, splitting it up in three registries. #403
- Loading branch information
Showing
20 changed files
with
223 additions
and
146 deletions.
There are no files selected for viewing
This file contains 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 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
45 changes: 45 additions & 0 deletions
45
src/Digdir.Domain.Dialogporten.Application/Common/IUserNameRegistry.cs
This file contains 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,45 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Digdir.Domain.Dialogporten.Application.Common.Extensions; | ||
using Digdir.Domain.Dialogporten.Application.Externals; | ||
using Digdir.Domain.Dialogporten.Application.Externals.Presentation; | ||
|
||
namespace Digdir.Domain.Dialogporten.Application.Common; | ||
|
||
public interface IUserNameRegistry | ||
{ | ||
bool TryGetCurrentUserPid([NotNullWhen(true)] out string? userPid); | ||
Task<string?> GetCurrentUserName(string personalIdentificationNumber, CancellationToken cancellationToken); | ||
} | ||
|
||
public class UserNameRegistry : IUserNameRegistry | ||
{ | ||
private readonly IUser _user; | ||
private readonly INameRegistry _nameRegistry; | ||
|
||
public UserNameRegistry(IUser user, INameRegistry nameRegistry) | ||
{ | ||
_user = user ?? throw new ArgumentNullException(nameof(user)); | ||
_nameRegistry = nameRegistry ?? throw new ArgumentNullException(nameof(nameRegistry)); | ||
} | ||
|
||
public bool TryGetCurrentUserPid([NotNullWhen(true)] out string? userPid) => _user.TryGetPid(out userPid); | ||
|
||
public async Task<string?> GetCurrentUserName(string personalIdentificationNumber, CancellationToken cancellationToken) => | ||
await _nameRegistry.GetName(personalIdentificationNumber, cancellationToken); | ||
} | ||
|
||
internal sealed class LocalDevelopmentUserNameRegistryDecorator : IUserNameRegistry | ||
{ | ||
private readonly IUserNameRegistry _userNameRegistry; | ||
|
||
public LocalDevelopmentUserNameRegistryDecorator(IUserNameRegistry userNameRegistry) | ||
{ | ||
_userNameRegistry = userNameRegistry ?? throw new ArgumentNullException(nameof(userNameRegistry)); | ||
} | ||
|
||
public bool TryGetCurrentUserPid([NotNullWhen(true)] out string? userPid) => | ||
_userNameRegistry.TryGetCurrentUserPid(out userPid); | ||
|
||
public async Task<string?> GetCurrentUserName(string personalIdentificationNumber, CancellationToken cancellationToken) | ||
=> await Task.FromResult("Local Development User"); | ||
} |
49 changes: 49 additions & 0 deletions
49
src/Digdir.Domain.Dialogporten.Application/Common/IUserOrganizationRegistry.cs
This file contains 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,49 @@ | ||
using Digdir.Domain.Dialogporten.Application.Common.Extensions; | ||
using Digdir.Domain.Dialogporten.Application.Externals; | ||
using Digdir.Domain.Dialogporten.Application.Externals.Presentation; | ||
|
||
namespace Digdir.Domain.Dialogporten.Application.Common; | ||
|
||
public interface IUserOrganizationRegistry | ||
{ | ||
Task<string?> GetCurrentUserOrgShortName(CancellationToken cancellationToken); | ||
} | ||
|
||
public class UserOrganizationRegistry : IUserOrganizationRegistry | ||
{ | ||
private readonly IUser _user; | ||
private readonly IOrganizationRegistry _organizationRegistry; | ||
|
||
public UserOrganizationRegistry(IUser user, IOrganizationRegistry organizationRegistry) | ||
{ | ||
_user = user ?? throw new ArgumentNullException(nameof(user)); | ||
_organizationRegistry = organizationRegistry ?? throw new ArgumentNullException(nameof(organizationRegistry)); | ||
} | ||
|
||
public async Task<string?> GetCurrentUserOrgShortName(CancellationToken cancellationToken) | ||
{ | ||
if (_user.TryGetOrgShortName(out var orgShortName)) | ||
{ | ||
return orgShortName; | ||
} | ||
|
||
if (!_user.TryGetOrgNumber(out var orgNumber)) | ||
{ | ||
return null; | ||
} | ||
|
||
return await _organizationRegistry.GetOrgShortName(orgNumber, cancellationToken); | ||
} | ||
} | ||
|
||
internal sealed class LocalDevelopmentUserOrganizationRegistryDecorator : IUserOrganizationRegistry | ||
{ | ||
private readonly IUserOrganizationRegistry _userOrganizationRegistry; | ||
|
||
public LocalDevelopmentUserOrganizationRegistryDecorator(IUserOrganizationRegistry userOrganizationRegistry) | ||
{ | ||
_userOrganizationRegistry = userOrganizationRegistry ?? throw new ArgumentNullException(nameof(userOrganizationRegistry)); | ||
} | ||
|
||
public Task<string?> GetCurrentUserOrgShortName(CancellationToken cancellationToken) => Task.FromResult("digdir")!; | ||
} |
51 changes: 51 additions & 0 deletions
51
src/Digdir.Domain.Dialogporten.Application/Common/IUserResourceRegistry.cs
This file contains 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,51 @@ | ||
using System.Diagnostics; | ||
using Digdir.Domain.Dialogporten.Application.Common.Extensions; | ||
using Digdir.Domain.Dialogporten.Application.Externals; | ||
using Digdir.Domain.Dialogporten.Application.Externals.Presentation; | ||
|
||
namespace Digdir.Domain.Dialogporten.Application.Common; | ||
|
||
public interface IUserResourceRegistry | ||
{ | ||
Task<bool> CurrentUserIsOwner(string serviceResource, CancellationToken cancellationToken); | ||
Task<IReadOnlyCollection<string>> GetCurrentUserResourceIds(CancellationToken cancellationToken); | ||
} | ||
|
||
public class UserResourceRegistry : IUserResourceRegistry | ||
{ | ||
private readonly IUser _user; | ||
private readonly IResourceRegistry _resourceRegistry; | ||
|
||
public UserResourceRegistry(IUser user, IResourceRegistry resourceRegistry) | ||
{ | ||
_user = user ?? throw new ArgumentNullException(nameof(user)); | ||
_resourceRegistry = resourceRegistry ?? throw new ArgumentNullException(nameof(resourceRegistry)); | ||
} | ||
|
||
public async Task<bool> CurrentUserIsOwner(string serviceResource, CancellationToken cancellationToken) | ||
{ | ||
var resourceIds = await GetCurrentUserResourceIds(cancellationToken); | ||
return resourceIds.Contains(serviceResource); | ||
} | ||
|
||
public Task<IReadOnlyCollection<string>> GetCurrentUserResourceIds(CancellationToken cancellationToken) => | ||
!_user.TryGetOrgNumber(out var orgNumber) | ||
? throw new UnreachableException() | ||
: _resourceRegistry.GetResourceIds(orgNumber, cancellationToken); | ||
} | ||
|
||
internal sealed class LocalDevelopmentUserResourceRegistryDecorator : IUserResourceRegistry | ||
{ | ||
private readonly IUserResourceRegistry _userResourceRegistry; | ||
|
||
public LocalDevelopmentUserResourceRegistryDecorator(IUserResourceRegistry userResourceRegistry) | ||
{ | ||
_userResourceRegistry = userResourceRegistry ?? throw new ArgumentNullException(nameof(userResourceRegistry)); | ||
} | ||
|
||
public Task<bool> CurrentUserIsOwner(string serviceResource, CancellationToken cancellationToken) => | ||
Task.FromResult(true); | ||
|
||
public Task<IReadOnlyCollection<string>> GetCurrentUserResourceIds(CancellationToken cancellationToken) => | ||
_userResourceRegistry.GetCurrentUserResourceIds(cancellationToken); | ||
} |
93 changes: 0 additions & 93 deletions
93
src/Digdir.Domain.Dialogporten.Application/Common/IUserService.cs
This file was deleted.
Oops, something went wrong.
This file contains 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 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.