LANCommander/LANCommander.Server.Services/RoleService.cs
Pat Hartl b28e8a51ee Fix auditing
The way updates are being handled means that the auditing interceptor doesn't work as the entities are not showing in the change tracker at that stage as modified. Added the auditing to BaseDatabaseService instead.
2025-02-09 18:53:40 -06:00

94 lines
3.1 KiB
C#

using AutoMapper;
using LANCommander.Server.Data;
using LANCommander.Server.Data.Models;
using LANCommander.Server.Services.Factories;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using System.Data;
using LANCommander.Server.Services.Exceptions;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using ZiggyCreatures.Caching.Fusion;
namespace LANCommander.Server.Services
{
public sealed class RoleService(
ILogger<RoleService> logger,
IFusionCache cache,
IMapper mapper,
IHttpContextAccessor httpContextAccessor,
IDbContextFactory<DatabaseContext> contextFactory,
CollectionService collectionService,
IdentityContextFactory identityContextFactory,
RoleManager<Role> roleManager) : BaseDatabaseService<Role>(logger, cache, mapper, httpContextAccessor, contextFactory)
{
public const string AdministratorRoleName = "Administrator";
private IdentityContext _identityContext;
public override void Initialize()
{
_identityContext = identityContextFactory.Create();
}
public override async Task<Role> UpdateAsync(Role entity)
{
return await base.UpdateAsync(entity, async context =>
{
await context.UpdateRelationshipAsync(r => r.Collections);
await context.UpdateRelationshipAsync(r => r.Users);
});
}
public async Task<Role> AddAsync(Role role)
{
var result = await roleManager.CreateAsync(role);
if (result.Succeeded)
return await roleManager.FindByNameAsync(role.Name);
throw new AddRoleException(result, "Could not create role");
}
public async Task<Role> GetAsync(string roleName)
{
return await FirstOrDefaultAsync(r => r.Name == roleName);
}
public async Task<T> GetAsync<T>(string roleName)
{
var role = await FirstOrDefaultAsync(r => r.Name == roleName);
return mapper.Map<T>(role);
}
public async Task<Role> AssignCollections(Guid roleId, IEnumerable<Guid> collectionIds)
{
var role = await Include(r => r.Collections).GetAsync(roleId);
if (role.Collections == null)
role.Collections = new List<Collection>();
foreach (var collectionId in collectionIds.Where(id => !role.Collections.Any(c => c.Id == id)))
{
var collection = await collectionService.GetAsync(collectionId);
role.Collections.Add(collection);
}
foreach (var collection in role.Collections.Where(c => !collectionIds.Contains(c.Id)))
{
role.Collections.Remove(collection);
}
role = await UpdateAsync(role);
return role;
}
public async Task<IEnumerable<User>> GetUsersAsync(string roleName)
{
return await _identityContext.UserManager.GetUsersInRoleAsync(roleName);
}
}
}