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

Commit 379b6cd

Browse files
committed
role
1 parent 485c399 commit 379b6cd

37 files changed

Lines changed: 3820 additions & 21 deletions

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public DomainToViewModelMappingProfile()
2929
CreateMap<IdentityServer4.EntityFramework.Entities.IdentityResource, IdentityResourceListView>(MemberList.Destination);
3030
CreateMap<IdentityServer4.EntityFramework.Entities.ApiScope, ScopeViewModel>(MemberList.Destination);
3131
CreateMap<Claim, ClaimViewModel>(MemberList.Destination);
32+
CreateMap<Role, RoleViewModel>(MemberList.Destination);
3233
}
3334
}
3435
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public ViewModelToDomainMappingProfile()
4444
CreateMap<RemoveAccountViewModel, RemoveAccountCommand>().ConstructUsing(c => new RemoveAccountCommand(c.Id));
4545
CreateMap<SaveUserClaimViewModel, SaveUserClaimCommand>().ConstructUsing(c => new SaveUserClaimCommand(c.Username, c.Type, c.Value));
4646
CreateMap<RemoveUserClaimViewModel, RemoveUserClaimCommand>().ConstructUsing(c => new RemoveUserClaimCommand(c.Username, c.Type));
47+
CreateMap<RemoveUserRoleViewModel, RemoveUserRoleCommand>().ConstructUsing(c => new RemoveUserRoleCommand(c.Username, c.Role));
48+
CreateMap<SaveUserRoleViewModel, SaveUserRoleCommand>().ConstructUsing(c => new SaveUserRoleCommand(c.Username, c.Role));
4749

4850
/*
4951
* Client commands
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Jp.Application.ViewModels;
6+
7+
namespace Jp.Application.Interfaces
8+
{
9+
public interface IRoleManagerAppService: IDisposable
10+
{
11+
Task<IEnumerable<RoleViewModel>> GetAllRoles();
12+
}
13+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ public interface IUserManageAppService : IDisposable
2525
Task<IEnumerable<ClaimViewModel>> GetClaims(string userName);
2626
Task SaveClaim(SaveUserClaimViewModel model);
2727
Task RemoveClaim(RemoveUserClaimViewModel model);
28+
Task<IEnumerable<RoleViewModel>> GetRoles(string userName);
29+
Task RemoveRole(RemoveUserRoleViewModel model);
30+
Task SaveRole(SaveUserRoleViewModel model);
2831
}
2932
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using AutoMapper;
5+
using Jp.Application.Interfaces;
6+
using Jp.Application.ViewModels;
7+
using Jp.Domain.Core.Bus;
8+
using Jp.Domain.Interfaces;
9+
using Jp.Domain.Models;
10+
11+
namespace Jp.Application.Services
12+
{
13+
public class RoleManagerAppService : IRoleManagerAppService
14+
{
15+
private IEventStoreRepository _eventStoreRepository;
16+
private IMapper _mapper;
17+
private readonly IRoleService _roleService;
18+
19+
public IMediatorHandler Bus { get; set; }
20+
public RoleManagerAppService(IMapper mapper,
21+
IRoleService roleService,
22+
IMediatorHandler bus,
23+
IEventStoreRepository eventStoreRepository
24+
)
25+
{
26+
_mapper = mapper;
27+
_roleService = roleService;
28+
Bus = bus;
29+
_eventStoreRepository = eventStoreRepository;
30+
}
31+
32+
33+
34+
public void Dispose()
35+
{
36+
GC.SuppressFinalize(this);
37+
}
38+
39+
public async Task<IEnumerable<RoleViewModel>> GetAllRoles()
40+
{
41+
return _mapper.Map<IEnumerable<RoleViewModel>>(await _roleService.GetAllRoles());
42+
}
43+
}
44+
}

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Threading.Tasks;
4-
using AutoMapper;
1+
using AutoMapper;
52
using Jp.Application.EventSourcedNormalizers;
63
using Jp.Application.Interfaces;
74
using Jp.Application.ViewModels;
@@ -10,6 +7,10 @@
107
using Jp.Domain.Commands.UserManagement;
118
using Jp.Domain.Core.Bus;
129
using Jp.Domain.Interfaces;
10+
using System;
11+
using System.Collections.Generic;
12+
using System.Linq;
13+
using System.Threading.Tasks;
1314

1415
namespace Jp.Application.Services
1516
{
@@ -120,7 +121,25 @@ public Task SaveClaim(SaveUserClaimViewModel model)
120121

121122
public Task RemoveClaim(RemoveUserClaimViewModel model)
122123
{
123-
var registerCommand = _mapper.Map<RemoveUserClaimCommand>(model);
124+
var removeCommand = _mapper.Map<RemoveUserClaimCommand>(model);
125+
return Bus.SendCommand(removeCommand);
126+
}
127+
128+
public async Task<IEnumerable<RoleViewModel>> GetRoles(string userName)
129+
{
130+
var roles = await _userService.GetRoles(userName);
131+
return roles.Select(s => new RoleViewModel() { Name = s });
132+
}
133+
134+
public Task RemoveRole(RemoveUserRoleViewModel model)
135+
{
136+
var removeCommand = _mapper.Map<RemoveUserRoleCommand>(model);
137+
return Bus.SendCommand(removeCommand);
138+
}
139+
140+
public Task SaveRole(SaveUserRoleViewModel model)
141+
{
142+
var registerCommand = _mapper.Map<SaveUserRoleCommand>(model);
124143
return Bus.SendCommand(registerCommand);
125144
}
126145
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace Jp.Application.ViewModels
4+
{
5+
public class RoleViewModel
6+
{
7+
public Guid Id { get; set; }
8+
public string Name { get; set; }
9+
}
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace Jp.Application.ViewModels.UserViewModels
5+
{
6+
public class RemoveUserRoleViewModel
7+
{
8+
[Required]
9+
public string Role { get; set; }
10+
[Required]
11+
public string Username { get; set; }
12+
13+
public Guid UserId { get; set; }
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace Jp.Application.ViewModels.UserViewModels
4+
{
5+
public class SaveUserRoleViewModel
6+
{
7+
[Required]
8+
public string Role { get; set; }
9+
[Required]
10+
public string Username { get; set; }
11+
}
12+
}

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,22 @@ public class UserManagementCommandHandler : CommandHandler,
2020
IRequestHandler<RemoveAccountCommand>,
2121
IRequestHandler<UpdateUserCommand>,
2222
IRequestHandler<SaveUserClaimCommand>,
23-
IRequestHandler<RemoveUserClaimCommand>
23+
IRequestHandler<RemoveUserClaimCommand>,
24+
IRequestHandler<RemoveUserRoleCommand>
2425
{
2526
private readonly IUserService _userService;
27+
private readonly ISystemUser _user;
2628

2729
public UserManagementCommandHandler(
2830
IUnitOfWork uow,
2931
IMediatorHandler bus,
3032
INotificationHandler<DomainNotification> notifications,
31-
IUserService userService)
33+
IUserService userService,
34+
ISystemUser user)
3235
: base(uow, bus, notifications)
3336
{
3437
_userService = userService;
38+
_user = user;
3539
}
3640

3741
public async Task Handle(UpdateProfileCommand request, CancellationToken cancellationToken)
@@ -173,5 +177,28 @@ public async Task Handle(RemoveUserClaimCommand request, CancellationToken cance
173177
await Bus.RaiseEvent(new UserClaimRemovedEvent(request.Username, request.Type));
174178
}
175179
}
180+
181+
public async Task Handle(RemoveUserRoleCommand request, CancellationToken cancellationToken)
182+
{
183+
if (!request.IsValid())
184+
{
185+
NotifyValidationErrors(request);
186+
return;
187+
}
188+
189+
var userDb = await _userService.FindByNameAsync(request.Username);
190+
if (userDb == null)
191+
{
192+
await Bus.RaiseEvent(new DomainNotification("1", "User not found"));
193+
return;
194+
}
195+
196+
var success = await _userService.RemoveRole(userDb.Id, request.Role);
197+
198+
if (success)
199+
{
200+
await Bus.RaiseEvent(new UserRoleRemovedEvent(_user.UserId, request.Username, request.Role));
201+
}
202+
}
176203
}
177204
}

0 commit comments

Comments
 (0)