Skip to content
This repository was archived by the owner on Aug 1, 2021. It is now read-only.

Commit 63a27ae

Browse files
committed
identity resource almost there
1 parent 2f0cc9e commit 63a27ae

51 files changed

Lines changed: 5701 additions & 257 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Backend/Jp.Application/AutoMapper/ViewModelToDomainMappingProfile.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Jp.Application.ViewModels;
44
using Jp.Application.ViewModels.ClientsViewModels;
55
using Jp.Domain.Commands.Client;
6+
using Jp.Domain.Commands.IdentityResource;
67
using Jp.Domain.Commands.User;
78
using Jp.Domain.Commands.UserManagement;
89

@@ -48,6 +49,14 @@ public ViewModelToDomainMappingProfile()
4849
CreateMap<CopyClientViewModel, CopyClientCommand>().ConstructUsing(c => new CopyClientCommand(c.ClientId));
4950
CreateMap<SaveClientViewModel, SaveClientCommand>().ConstructUsing(c => new SaveClientCommand(c.ClientId, c.ClientName, c.ClientUri, c.LogoUri, c.Description, c.ClientType));
5051

52+
/*
53+
* Identity Resource commands
54+
*/
55+
CreateMap<IdentityResource, RegisterIdentityResourceCommand>().ConstructUsing(c => new RegisterIdentityResourceCommand(c));
56+
CreateMap<IdentityResource, UpdateIdentityResourceCommand>().ConstructUsing(c => new UpdateIdentityResourceCommand(c));
57+
CreateMap<IdentityResource, RemoveIdentityResourceCommand>().ConstructUsing(c => new RemoveIdentityResourceCommand(c.Name));
58+
59+
5160
}
5261
}
5362
}

src/Backend/Jp.Application/Interfaces/IIdentityResourceAppService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
using System.Threading.Tasks;
55
using IdentityServer4.Models;
66
using Jp.Application.ViewModels;
7+
using Jp.Application.ViewModels.IdentityResourceViewModels;
78

89
namespace Jp.Application.Interfaces
910
{
1011
public interface IIdentityResourceAppService: IDisposable
1112
{
1213
Task<IEnumerable<IdentityResource>> GetIdentityResources();
14+
Task<IdentityResource> GetDetails(string name);
15+
Task Save(IdentityResource model);
16+
Task Update(IdentityResource model);
17+
Task Remove(RemoveIdentityResourceViewModel model);
1318
}
1419
}

src/Backend/Jp.Application/Jp.Application.csproj

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,4 @@
1414
<ProjectReference Include="..\Jp.Domain\Jp.Domain.csproj" />
1515
</ItemGroup>
1616

17-
<ItemGroup>
18-
<Reference Include="IdentityServer4">
19-
<HintPath>C:\Users\Bruno\.nuget\packages\identityserver4\2.1.1\lib\netstandard2.0\IdentityServer4.dll</HintPath>
20-
</Reference>
21-
</ItemGroup>
22-
23-
<ItemGroup>
24-
<Folder Include="ViewModels\IdentityResourceViewModels\" />
25-
</ItemGroup>
26-
2717
</Project>

src/Backend/Jp.Application/Services/IdentityResourceAppService.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using IdentityServer4.Models;
88
using Jp.Application.Interfaces;
99
using Jp.Application.ViewModels;
10+
using Jp.Application.ViewModels.IdentityResourceViewModels;
11+
using Jp.Domain.Commands.IdentityResource;
1012
using Jp.Domain.Core.Bus;
1113
using Jp.Domain.Interfaces;
1214

@@ -36,6 +38,31 @@ public Task<IEnumerable<IdentityResource>> GetIdentityResources()
3638
var resultado = _identityResourceRepository.GetAll().Select(s => s.ToModel()).ToList();
3739
return Task.FromResult<IEnumerable<IdentityResource>>(resultado);
3840
}
41+
42+
public async Task<IdentityResource> GetDetails(string name)
43+
{
44+
var irs = await _identityResourceRepository.GetByName(name);
45+
return irs.ToModel();
46+
}
47+
48+
public Task Save(IdentityResource model)
49+
{
50+
var command = _mapper.Map<RegisterIdentityResourceCommand>(model);
51+
return Bus.SendCommand(command);
52+
}
53+
54+
public Task Update(IdentityResource model)
55+
{
56+
var command = _mapper.Map<UpdateIdentityResourceCommand>(model);
57+
return Bus.SendCommand(command);
58+
}
59+
60+
public Task Remove(RemoveIdentityResourceViewModel model)
61+
{
62+
var command = _mapper.Map<RemoveIdentityResourceCommand>(model);
63+
return Bus.SendCommand(command);
64+
}
65+
3966
public void Dispose()
4067
{
4168
GC.SuppressFinalize(this);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace Jp.Application.ViewModels.IdentityResourceViewModels
4+
{
5+
public class RemoveIdentityResourceViewModel
6+
{
7+
[Required]
8+
public string Name { get; set; }
9+
}
10+
}

src/Backend/Jp.Domain/CommandHandlers/ClientCommandHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public async Task Handle(RemoveClientCommand request, CancellationToken cancella
5656
return; ;
5757
}
5858

59-
var savedClient = await _clientRepository.GetClient(request.Client.ClientId);
59+
var savedClient = await _clientRepository.GetByClientId(request.Client.ClientId);
6060
if (savedClient == null)
6161
{
6262
await Bus.RaiseEvent(new DomainNotification("1", "Client not found"));
@@ -104,7 +104,7 @@ public async Task Handle(RemoveSecretCommand request, CancellationToken cancella
104104
return;
105105
}
106106

107-
var savedClient = await _clientRepository.GetClient(request.ClientId);
107+
var savedClient = await _clientRepository.GetByClientId(request.ClientId);
108108
if (savedClient == null)
109109
{
110110
await Bus.RaiseEvent(new DomainNotification("1", "Client not found"));
Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
using System;
2-
using System.Threading;
3-
using System.Threading.Tasks;
1+
using IdentityServer4.EntityFramework.Mappers;
42
using Jp.Domain.Commands.IdentityResource;
53
using Jp.Domain.Core.Bus;
64
using Jp.Domain.Core.Notifications;
75
using Jp.Domain.Events.IdentityResource;
86
using Jp.Domain.Interfaces;
9-
using Jp.Domain.Models;
107
using MediatR;
8+
using System.Threading;
9+
using System.Threading.Tasks;
1110

1211
namespace Jp.Domain.CommandHandlers
1312
{
1413
public class IdentityResourceCommandHandler : CommandHandler,
15-
IRequestHandler<RegisterIdentityResourceCommand>
14+
IRequestHandler<RegisterIdentityResourceCommand>,
15+
IRequestHandler<UpdateIdentityResourceCommand>,
16+
IRequestHandler<RemoveIdentityResourceCommand>
1617
{
1718
private readonly IIdentityResourceRepository _identityResourceRepository;
1819

@@ -26,23 +27,79 @@ public IdentityResourceCommandHandler(
2627
}
2728

2829

29-
public Task Handle(RegisterIdentityResourceCommand request, CancellationToken cancellationToken)
30+
public async Task Handle(RegisterIdentityResourceCommand request, CancellationToken cancellationToken)
3031
{
3132
if (!request.IsValid())
3233
{
3334
NotifyValidationErrors(request);
34-
return Task.CompletedTask;
35+
return;
36+
}
37+
38+
var savedClient = await _identityResourceRepository.GetByName(request.Resource.Name);
39+
if (savedClient != null)
40+
{
41+
await Bus.RaiseEvent(new DomainNotification("1", "Resource already exists"));
42+
return;
3543
}
3644

37-
// Businness logic here
38-
39-
//if (Commit())
40-
//{
41-
// Bus.RaiseEvent(new IdentityResourceRegisteredEvent(IdentityResource.Id));
42-
//}
45+
var irs = request.Resource.ToEntity();
46+
47+
_identityResourceRepository.Add(irs);
48+
49+
if (Commit())
50+
{
51+
await Bus.RaiseEvent(new IdentityResourceRegisteredEvent(irs.Name));
52+
}
4353

44-
return Task.CompletedTask;
4554
}
4655

56+
public async Task Handle(UpdateIdentityResourceCommand request, CancellationToken cancellationToken)
57+
{
58+
if (!request.IsValid())
59+
{
60+
NotifyValidationErrors(request);
61+
return;
62+
}
63+
64+
var savedClient = await _identityResourceRepository.GetByName(request.Resource.Name);
65+
if (savedClient == null)
66+
{
67+
await Bus.RaiseEvent(new DomainNotification("1", "Resource not found"));
68+
return;
69+
}
70+
71+
var irs = request.Resource.ToEntity();
72+
irs.Id = savedClient.Id;
73+
await _identityResourceRepository.UpdateWithChildrens(irs);
74+
75+
if (Commit())
76+
{
77+
await Bus.RaiseEvent(new IdentityResourceUpdatedEvent(request.Resource));
78+
}
79+
}
80+
81+
82+
public async Task Handle(RemoveIdentityResourceCommand request, CancellationToken cancellationToken)
83+
{
84+
if (!request.IsValid())
85+
{
86+
NotifyValidationErrors(request);
87+
return;
88+
}
89+
90+
var savedClient = await _identityResourceRepository.GetByName(request.Resource.Name);
91+
if (savedClient == null)
92+
{
93+
await Bus.RaiseEvent(new DomainNotification("1", "Resource not found"));
94+
return;
95+
}
96+
97+
_identityResourceRepository.Remove(savedClient.Id);
98+
99+
if (Commit())
100+
{
101+
await Bus.RaiseEvent(new IdentityResourceUpdatedEvent(request.Resource));
102+
}
103+
}
47104
}
48105
}

src/Backend/Jp.Domain/CommandHandlers/IdentityResourcesCommandHandler.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/Backend/Jp.Domain/Commands/IdentityResource/IdentityResourceCommand.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ namespace Jp.Domain.Commands.IdentityResource
55
{
66
public abstract class IdentityResourceCommand : Command
77
{
8-
9-
8+
public IdentityServer4.Models.IdentityResource Resource { get; set; }
109

1110
}
1211
}

src/Backend/Jp.Domain/Commands/IdentityResource/RegisterIdentityResourceCommand.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ namespace Jp.Domain.Commands.IdentityResource
44
{
55
public class RegisterIdentityResourceCommand : IdentityResourceCommand
66
{
7-
public RegisterIdentityResourceCommand()
7+
8+
public RegisterIdentityResourceCommand(IdentityServer4.Models.IdentityResource resource)
89
{
9-
10+
Resource = resource;
1011
}
1112

1213
public override bool IsValid()

0 commit comments

Comments
 (0)