Added a new policy to require being an admin. Implemented ability to delete a user.

This commit is contained in:
Joseph Milazzo 2020-12-24 08:13:58 -06:00
parent bb276a5984
commit f0919042b0
6 changed files with 44 additions and 5 deletions

View file

@ -6,6 +6,7 @@ using API.Entities;
using API.Interfaces;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
namespace API.Data
@ -14,11 +15,13 @@ namespace API.Data
{
private readonly DataContext _context;
private readonly IMapper _mapper;
private readonly UserManager<AppUser> _userManager;
public UserRepository(DataContext context, IMapper mapper)
public UserRepository(DataContext context, IMapper mapper, UserManager<AppUser> userManager)
{
_context = context;
_mapper = mapper;
_userManager = userManager;
}
public void Update(AppUser user)
@ -26,6 +29,11 @@ namespace API.Data
_context.Entry(user).State = EntityState.Modified;
}
public void Delete(AppUser user)
{
_context.Users.Remove(user);
}
public async Task<bool> SaveAllAsync()
{
return await _context.SaveChangesAsync() > 0;
@ -49,6 +57,23 @@ namespace API.Data
public async Task<IEnumerable<MemberDto>> GetMembersAsync()
{
return await _userManager.Users
.Include(x => x.Libraries)
.Include(r => r.UserRoles)
.ThenInclude(r => r.Role)
.OrderBy(u => u.UserName)
.Select(u => new MemberDto
{
Id = u.Id,
Username = u.UserName,
Created = u.Created,
LastActive = u.LastActive,
Roles = u.UserRoles.Select(r => r.Role.Name).ToList()
})
.ToListAsync();
//return await _context.Users.Include(x => x.Libraries)
return await _context.Users.Include(x => x.Libraries)
.Include(x => x.Libraries)
.ProjectTo<MemberDto>(_mapper.ConfigurationProvider)