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 ZiggyCreatures.Caching.Fusion; namespace LANCommander.Server.Services { public class RoleService : BaseDatabaseService { public const string AdministratorRoleName = "Administrator"; private readonly IdentityContext IdentityContext; private readonly IMapper Mapper; private readonly CollectionService CollectionService; public RoleService( ILogger logger, IMapper mapper, IFusionCache cache, RepositoryFactory repositoryFactory, IdentityContextFactory identityContextFactory, CollectionService collectionService) : base(logger, cache, repositoryFactory) { IdentityContext = identityContextFactory.Create(); Mapper = mapper; CollectionService = collectionService; } public async Task GetAsync(string roleName) { return await FirstOrDefaultAsync(r => r.Name == roleName); } public async Task GetAsync(string roleName) { var role = await FirstOrDefaultAsync(r => r.Name == roleName); return Mapper.Map(role); } public async Task AssignCollections(Guid roleId, IEnumerable collectionIds) { var role = await Include(r => r.Collections).GetAsync(roleId); if (role.Collections == null) role.Collections = new List(); 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> GetUsersAsync(string roleName) { return await IdentityContext.UserManager.GetUsersInRoleAsync(roleName); } } }