Skip to content

Commit 83de122

Browse files
Redesign role editing - MVC side
1 parent 6af3c5b commit 83de122

File tree

9 files changed

+85
-23
lines changed

9 files changed

+85
-23
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace AbpCompanyName.AbpProjectName.Roles.Dto
2+
{
3+
public class FlatPermissionDto
4+
{
5+
public string ParentName { get; set; }
6+
7+
public string Name { get; set; }
8+
9+
public string DisplayName { get; set; }
10+
11+
public string Description { get; set; }
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
3+
namespace AbpCompanyName.AbpProjectName.Roles.Dto
4+
{
5+
public class GetRoleForEditOutput
6+
{
7+
public RoleEditDto Role { get; set; }
8+
9+
public List<FlatPermissionDto> Permissions { get; set; }
10+
11+
public List<string> GrantedPermissionNames { get; set; }
12+
}
13+
}

aspnet-core/src/AbpCompanyName.AbpProjectName.Application/Roles/Dto/RoleDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ public class RoleDto : EntityDto<int>
2727

2828
public List<string> Permissions { get; set; }
2929
}
30-
}
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace AbpCompanyName.AbpProjectName.Roles.Dto
4+
{
5+
public class RoleEditDto
6+
{
7+
public int Id { get; set; }
8+
9+
[Required]
10+
public string Name { get; set; }
11+
12+
[Required]
13+
public string DisplayName { get; set; }
14+
15+
public string Description { get; set; }
16+
17+
public bool IsStatic { get; set; }
18+
}
19+
}

aspnet-core/src/AbpCompanyName.AbpProjectName.Application/Roles/IRoleAppService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ namespace AbpCompanyName.AbpProjectName.Roles
88
public interface IRoleAppService : IAsyncCrudAppService<RoleDto, int, PagedResultRequestDto, CreateRoleDto, RoleDto>
99
{
1010
Task<ListResultDto<PermissionDto>> GetAllPermissions();
11+
12+
Task<GetRoleForEditOutput> GetRoleForEdit(EntityDto input);
1113
}
12-
}
14+
}

aspnet-core/src/AbpCompanyName.AbpProjectName.Application/Roles/RoleAppService.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,19 @@ protected virtual void CheckErrors(IdentityResult identityResult)
112112
identityResult.CheckErrors(LocalizationManager);
113113
}
114114

115-
public override async Task<RoleDto> Get(EntityDto<int> input)
115+
public async Task<GetRoleForEditOutput> GetRoleForEdit(EntityDto input)
116116
{
117-
CheckGetPermission();
118-
119-
var role = await GetEntityByIdAsync(input.Id);
120-
role.Permissions = role.Permissions.Where(p => p.IsGranted).ToList();
117+
var permissions = PermissionManager.GetAllPermissions();
118+
var role = await _roleManager.GetRoleByIdAsync(input.Id);
119+
var grantedPermissions = (await _roleManager.GetGrantedPermissionsAsync(role)).ToArray();
120+
var roleEditDto = ObjectMapper.Map<RoleEditDto>(role);
121121

122-
return MapToEntityDto(role);
122+
return new GetRoleForEditOutput
123+
{
124+
Role = roleEditDto,
125+
Permissions = ObjectMapper.Map<List<FlatPermissionDto>>(permissions).OrderBy(p => p.DisplayName).ToList(),
126+
GrantedPermissionNames = grantedPermissions.Select(p => p.Name).ToList()
127+
};
123128
}
124129
}
125130
}

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Mvc/Controllers/RolesController.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,9 @@ public async Task<IActionResult> Index()
3434

3535
public async Task<ActionResult> EditRoleModal(int roleId)
3636
{
37-
var role = await _roleAppService.Get(new EntityDto(roleId));
38-
var permissions = (await _roleAppService.GetAllPermissions()).Items;
39-
var model = new EditRoleModalViewModel
40-
{
41-
Role = role,
42-
Permissions = permissions
43-
};
37+
var output = await _roleAppService.GetRoleForEdit(new EntityDto(roleId));
38+
var model = new EditRoleModalViewModel(output);
39+
4440
return View("_EditRoleModal", model);
4541
}
4642
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections.Generic;
2+
using AbpCompanyName.AbpProjectName.Roles.Dto;
3+
4+
namespace AbpCompanyName.AbpProjectName.Web.Models.Common
5+
{
6+
public interface IPermissionsEditViewModel
7+
{
8+
List<FlatPermissionDto> Permissions { get; set; }
9+
10+
List<string> GrantedPermissionNames { get; set; }
11+
}
12+
}
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
1+
using Abp.AutoMapper;
32
using AbpCompanyName.AbpProjectName.Roles.Dto;
3+
using AbpCompanyName.AbpProjectName.Web.Models.Common;
44

55
namespace AbpCompanyName.AbpProjectName.Web.Models.Roles
66
{
7-
public class EditRoleModalViewModel
7+
[AutoMapFrom(typeof(GetRoleForEditOutput))]
8+
public class EditRoleModalViewModel : GetRoleForEditOutput, IPermissionsEditViewModel
89
{
9-
public RoleDto Role { get; set; }
10-
11-
public IReadOnlyList<PermissionDto> Permissions { get; set; }
10+
public EditRoleModalViewModel(GetRoleForEditOutput output)
11+
{
12+
output.MapTo(this);
13+
}
1214

13-
public bool HasPermission(PermissionDto permission)
15+
public bool HasPermission(FlatPermissionDto permission)
1416
{
15-
return Permissions != null && Role.Permissions.Any(p => p == permission.Name);
17+
return GrantedPermissionNames.Contains(permission.Name);
1618
}
1719
}
1820
}

0 commit comments

Comments
 (0)