Browse Source

#109 User management page (CRUD) for Identity module.

pull/129/head
alirıza adıyahşi 9 years ago
parent
commit
d7a733601a
  1. 9
      src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/GetIdentityRolesInput.cs
  2. 1
      src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/GetIdentityUsersInput.cs
  3. 8
      src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IIdentityRoleAppService.cs
  4. 2
      src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IIdentityUserAppService.cs
  5. 5
      src/Volo.Abp.Identity.Application/Volo/Abp/Identity/AbpDeskApplicationModuleAutoMapperProfile.cs
  6. 13
      src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityRoleAppService.cs
  7. 25
      src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs
  8. 5
      src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs
  9. 14
      src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EfCoreIdentityRoleRepository.cs
  10. 1
      src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EfCoreIdentityUserRepository.cs
  11. 50
      src/Volo.Abp.Identity.Web/Areas/Identity/Controllers/UsersController.cs
  12. 8
      src/Volo.Abp.Identity.Web/Areas/Identity/Models/CreateOrUpdateUserViewModel.cs
  13. 2
      src/Volo.Abp.Identity.Web/Areas/Identity/Views/Users/_Create.cshtml
  14. 4
      src/Volo.Abp.Identity.Web/Areas/Identity/Views/Users/_Update.cshtml
  15. 44
      src/Volo.Abp.Identity.Web/wwwroot/modules/identity/views/users/index.js
  16. 2
      test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityRoleAppService_Tests.cs

9
src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/GetIdentityRolesInput.cs

@ -0,0 +1,9 @@
using Volo.Abp.Application.Dtos;
namespace Volo.Abp.Identity
{
public class GetIdentityRolesInput : PagedAndSortedResultRequestDto
{
public string Filter { get; set; }
}
}

1
src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/GetIdentityUsersInput.cs

@ -1,5 +1,4 @@
using Volo.Abp.Application.Dtos;
using Volo.Abp.Validation;
namespace Volo.Abp.Identity
{

8
src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IIdentityRoleAppService.cs

@ -1,11 +1,13 @@
using System;
using Volo.Abp.Application.Dtos;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
namespace Volo.Abp.Identity
{
public interface IIdentityRoleAppService : IAsyncCrudAppService<IdentityRoleDto, Guid, PagedAndSortedResultRequestDto, IdentityRoleCreateDto, IdentityRoleUpdateDto>
public interface IIdentityRoleAppService : IAsyncCrudAppService<IdentityRoleDto, Guid, GetIdentityRolesInput, IdentityRoleCreateDto, IdentityRoleUpdateDto>
{
//TODO: remove after a better design
Task<List<IdentityRoleDto>> GetAllListAsync();
}
}

2
src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IIdentityUserAppService.cs

@ -10,7 +10,5 @@ namespace Volo.Abp.Identity
Task<ListResultDto<IdentityRoleDto>> GetRolesAsync(Guid id);
Task UpdateRolesAsync(Guid id, IdentityUserUpdateRolesDto input);
Task<IdentityUserCreateOrUpdateOutput> GetUserForCreateOrUpdateAsync(Guid? id);
}
}

5
src/Volo.Abp.Identity.Application/Volo/Abp/Identity/AbpDeskApplicationModuleAutoMapperProfile.cs

@ -1,4 +1,5 @@
using AutoMapper;
using System.Collections.Generic;
using AutoMapper;
namespace Volo.Abp.Identity
{
@ -8,7 +9,7 @@ namespace Volo.Abp.Identity
{
CreateMap<IdentityUser, IdentityUserDto>();
CreateMap<IdentityRole, IdentityRoleDto>();
CreateMap<IdentityRole, IdentityUserRoleDto>();
CreateMap<IdentityRoleDto, IdentityUserRoleDto>();
}
}
}

13
src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityRoleAppService.cs

@ -12,7 +12,7 @@ namespace Volo.Abp.Identity
private readonly IIdentityRoleRepository _roleRepository;
public IdentityRoleAppService(
IdentityRoleManager roleManager,
IdentityRoleManager roleManager,
IIdentityRoleRepository roleRepository)
{
_roleManager = roleManager;
@ -26,10 +26,10 @@ namespace Volo.Abp.Identity
);
}
public async Task<PagedResultDto<IdentityRoleDto>> GetListAsync(PagedAndSortedResultRequestDto input)
public async Task<PagedResultDto<IdentityRoleDto>> GetListAsync(GetIdentityRolesInput input)
{
var count = (int)await _roleRepository.GetCountAsync();
var list = await _roleRepository.GetListAsync(input.Sorting, input.MaxResultCount, input.SkipCount);
var list = await _roleRepository.GetListAsync(input.Sorting, input.MaxResultCount, input.SkipCount, input.Filter);
return new PagedResultDto<IdentityRoleDto>(
count,
@ -37,6 +37,13 @@ namespace Volo.Abp.Identity
);
}
public async Task<List<IdentityRoleDto>> GetAllListAsync()
{
var list = await _roleRepository.GetAllListAsync();
return ObjectMapper.Map<List<IdentityRole>, List<IdentityRoleDto>>(list);
}
public async Task<IdentityRoleDto> CreateAsync(IdentityRoleCreateDto input)
{
var role = new IdentityRole(GuidGenerator.Create(), input.Name);

25
src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs

@ -13,13 +13,11 @@ namespace Volo.Abp.Identity
{
private readonly IdentityUserManager _userManager;
private readonly IIdentityUserRepository _userRepository;
private readonly IIdentityRoleRepository _roleRepository;
public IdentityUserAppService(IdentityUserManager userManager, IIdentityUserRepository userRepository, IIdentityRoleRepository roleRepository)
public IdentityUserAppService(IdentityUserManager userManager, IIdentityUserRepository userRepository)
{
_userManager = userManager;
_userRepository = userRepository;
_roleRepository = roleRepository;
}
public async Task<IdentityUserDto> GetAsync(Guid id)
@ -29,27 +27,6 @@ namespace Volo.Abp.Identity
);
}
public async Task<IdentityUserCreateOrUpdateOutput> GetUserForCreateOrUpdateAsync(Guid? id)
{
var userRoleDtos = ObjectMapper.Map<List<IdentityRole>, IdentityUserRoleDto[]>(await _roleRepository.GetListAsync());
var output = new IdentityUserCreateOrUpdateOutput
{
Roles = userRoleDtos
};
if (!id.HasValue) return output;
var user = await _userManager.GetByIdAsync(id.Value);
output.User = ObjectMapper.Map<IdentityUser, IdentityUserDto>(user);
foreach (var userRoleDto in userRoleDtos)
{
userRoleDto.IsAssigned = await _userManager.IsInRoleAsync(user, userRoleDto.Name);
}
return output;
}
public async Task<PagedResultDto<IdentityUserDto>> GetListAsync(GetIdentityUsersInput input)
{
var count = (int)await _userRepository.GetCountAsync();

5
src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs

@ -9,6 +9,9 @@ namespace Volo.Abp.Identity
{
Task<IdentityRole> FindByNormalizedNameAsync(string normalizedRoleName, CancellationToken cancellationToken);
Task<List<IdentityRole>> GetListAsync(string sorting, int maxResultCount, int skipCount);
Task<List<IdentityRole>> GetListAsync(string sorting, int maxResultCount, int skipCount, string filter);
//TODO: remove after a better design
Task<List<IdentityRole>> GetAllListAsync();
}
}

14
src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EfCoreIdentityRoleRepository.cs

@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Identity.EntityFrameworkCore;
using System;
namespace Volo.Abp.Identity
{
@ -22,9 +23,18 @@ namespace Volo.Abp.Identity
return DbSet.FirstOrDefaultAsync(r => r.NormalizedName == normalizedRoleName, cancellationToken);
}
public async Task<List<IdentityRole>> GetListAsync(string sorting, int maxResultCount, int skipCount)
public async Task<List<IdentityRole>> GetListAsync(string sorting, int maxResultCount, int skipCount, string filter)
{
return await this.OrderBy(sorting ?? nameof(IdentityRole.Name)).PageBy(skipCount, maxResultCount).ToListAsync();
return await this.WhereIf(
!filter.IsNullOrWhiteSpace(),
r => r.Name.Contains(filter)
).OrderBy(sorting ?? nameof(IdentityRole.Name))
.PageBy(skipCount, maxResultCount).ToListAsync();
}
public async Task<List<IdentityRole>> GetAllListAsync()
{
return await GetQueryable().ToListAsync();
}
}
}

1
src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EfCoreIdentityUserRepository.cs

@ -99,6 +99,7 @@ namespace Volo.Abp.Identity
{
var query = from userRole in DbContext.UserRoles
join role in DbContext.Roles on userRole.RoleId equals role.Id
where userRole.UserId == userId
select role;
return await query.ToListAsync();

50
src/Volo.Abp.Identity.Web/Areas/Identity/Controllers/UsersController.cs

@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Identity.Web.Areas.Identity.Models;
namespace Volo.Abp.Identity.Web.Areas.Identity.Controllers
{
@ -11,10 +14,12 @@ namespace Volo.Abp.Identity.Web.Areas.Identity.Controllers
public class UsersController : AbpController
{
private readonly IIdentityUserAppService _identityUserAppService;
private readonly IIdentityRoleAppService _identityRoleAppService;
public UsersController(IIdentityUserAppService identityUserAppService)
public UsersController(IIdentityUserAppService identityUserAppService, IIdentityRoleAppService identityRoleAppService)
{
_identityUserAppService = identityUserAppService;
_identityRoleAppService = identityRoleAppService;
}
public async Task<ActionResult> Index()
@ -22,18 +27,49 @@ namespace Volo.Abp.Identity.Web.Areas.Identity.Controllers
return View();
}
public async Task<ActionResult> _Create()
public async Task<PartialViewResult> Create()
{
var identityUser = await _identityUserAppService.GetUserForCreateOrUpdateAsync(null);
var model = await CreateViewModel(null);
return PartialView(identityUser);
return PartialView("_Create", model);
}
public async Task<ActionResult> _Update(Guid id)
public async Task<PartialViewResult> Update(Guid id)
{
var identityUser = await _identityUserAppService.GetUserForCreateOrUpdateAsync(id);
var user = await _identityUserAppService.GetAsync(id);
await _identityRoleAppService.GetAllListAsync();
var model = await CreateViewModel(user);
return PartialView(identityUser);
return PartialView("_Update", model);
}
private async Task<CreateOrUpdateUserViewModel> CreateViewModel(IdentityUserDto user)
{
var allRoles = await _identityRoleAppService.GetAllListAsync();
var model = new CreateOrUpdateUserViewModel
{
User = user ?? new IdentityUserDto(),
Roles = ObjectMapper.Map<List<IdentityRoleDto>, IdentityUserRoleDto[]>(allRoles)
};
var userRoles = new List<IdentityRoleDto>();
if (user != null)
{
userRoles = (await _identityUserAppService.GetRolesAsync(user.Id)).Items.ToList();
}
foreach (var role in model.Roles)
{
if (userRoles.Select(x=>x.Name).Contains(role.Name))
{
role.IsAssigned = true;
}
}
return model;
}
}
}

8
src/Volo.Abp.Identity.Web/Areas/Identity/Models/CreateOrUpdateUserViewModel.cs

@ -0,0 +1,8 @@
namespace Volo.Abp.Identity.Web.Areas.Identity.Models
{
public class CreateOrUpdateUserViewModel
{
public IdentityUserDto User { get; set; }
public IdentityUserRoleDto[] Roles { get; set; }
}
}

2
src/Volo.Abp.Identity.Web/Areas/Identity/Views/Users/_Create.cshtml

@ -1,4 +1,4 @@
@model Volo.Abp.Identity.IdentityUserCreateOrUpdateOutput
@model Volo.Abp.Identity.Web.Areas.Identity.Controllers.CreateOrUpdateUserViewModel
<div class="modal-header">
<h5 class="modal-title" id="userModalLabel">Create User</h5>

4
src/Volo.Abp.Identity.Web/Areas/Identity/Views/Users/_Update.cshtml

@ -1,4 +1,4 @@
@model Volo.Abp.Identity.IdentityUserCreateOrUpdateOutput
@model Volo.Abp.Identity.Web.Areas.Identity.Controllers.CreateOrUpdateUserViewModel
<div class="modal-header">
<h5 class="modal-title" id="userModalLabel">Update User</h5>
@ -45,7 +45,7 @@
{
<div class="form-check">
<label class="form-check-label">
<input id="UpdateUser_@(role.Name)" class="form-check-input" type="checkbox" name="@(role.Name)" value="true" @Html.Raw(role.IsAssigned ? "checked=\"checked\"" : "")>
<input id="CreateUser_@(role.Name)" class="form-check-input" type="checkbox" name="@(role.Name)" value="true" @Html.Raw(role.IsAssigned ? "checked=\"checked\"" : "")>
@role.Name
</label>
</div>

44
src/Volo.Abp.Identity.Web/wwwroot/modules/identity/views/users/index.js

@ -50,13 +50,13 @@
defaultContent: '',
render: function (list, type, record, meta) {
return '<div class="dropdown">' +
'<button class="btn btn-primary btn-sm dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' +
'Actions' +
'</button>' +
'<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">' +
'<a class="dropdown-item update-user" href="#" data-id="' + record.id + '">Edit</a>' +
'<a class="dropdown-item delete-user" href="#" data-id="' + record.id + '">Delete</a>' +
'</div>' +
'<button class="btn btn-primary btn-sm dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' +
'Actions' +
'</button>' +
'<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">' +
'<a class="dropdown-item update-user" href="#" data-id="' + record.id + '">Edit</a>' +
'<a class="dropdown-item delete-user" href="#" data-id="' + record.id + '">Delete</a>' +
'</div>' +
'</div>';
}
},
@ -75,15 +75,15 @@
]
});
$(document).on('click', '.update-user', function () {
$('#IdentityUsersTable').on('click', '.update-user', function () {
var id = $(this).data('id');
$('#createUpdateUserModal').modal('show')
.find('.modal-content')
.load(abp.appPath + 'Identity/Users/_Update', { id: id });
.load(abp.appPath + 'Identity/Users/Update', { id: id });
});
$(document).on('click', '.delete-user', function () {
$('#IdentityUsersTable').on('click', '.delete-user', function () {
var id = $(this).data('id');
if (confirm('Are you sure you want to delete?')) {
@ -96,30 +96,28 @@
$('.create-user').click(function () {
$('#createUpdateUserModal').modal('show')
.find('.modal-content')
.load(abp.appPath + 'Identity/Users/_Create');
.load(abp.appPath + 'Identity/Users/Create');
});
$(document).on('click', '#btnCreateUserSave', function () {
$('#createUpdateUserModal').on('click', '#btnCreateUserSave', function () {
var $createUserForm = $('#createUserForm');
var user = $createUserForm.serializeFormToObject();
user.RoleNames = findAssignedRoleNames();
_identityUserAppService.create(user).done(function (result) {
_identityUserAppService.updateRoles(result.id, { roleNames: findAssignedRoleNames() }).done(function () {
$('#createUpdateUserModal').modal('hide');
dataTable.ajax.reload();
});
_identityUserAppService.create(user).done(function () {
$('#createUpdateUserModal').modal('hide');
dataTable.ajax.reload();
});
});
$(document).on('click', '#btnUpdateUserSave', function () {
$('#createUpdateUserModal').on('click', '#btnUpdateUserSave', function () {
var $updateUserForm = $('#updateUserForm');
var user = $updateUserForm.serializeFormToObject();
user.RoleNames = findAssignedRoleNames();
_identityUserAppService.update(user.Id, user).done(function (result) {
_identityUserAppService.updateRoles(result.id, { roleNames: findAssignedRoleNames() }).done(function () {
$('#createUpdateUserModal').modal('hide');
dataTable.ajax.reload();
});
_identityUserAppService.update(user.Id, user).done(function () {
$('#createUpdateUserModal').modal('hide');
dataTable.ajax.reload();
});
});
});

2
test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityRoleAppService_Tests.cs

@ -40,7 +40,7 @@ namespace Volo.Abp.Identity
{
//Act
var result = await _roleAppService.GetListAsync(new PagedAndSortedResultRequestDto());
var result = await _roleAppService.GetListAsync(new GetIdentityRolesInput());
//Assert

Loading…
Cancel
Save