When registering an admin user, ensure they have access to all libraries.

This commit is contained in:
Joseph Milazzo 2021-01-18 16:55:52 -06:00
parent 26660a9bb3
commit 80283bcd49

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using API.Constants; using API.Constants;
using API.DTOs; using API.DTOs;
@ -54,6 +55,20 @@ namespace API.Controllers
if (!roleResult.Succeeded) return BadRequest(result.Errors); if (!roleResult.Succeeded) return BadRequest(result.Errors);
// When we register an admin, we need to grant them access to all Libraries.
if (registerDto.IsAdmin)
{
_logger.LogInformation($"{user.UserName} is being registered as admin. Granting access to all libraries.");
var libraries = await _unitOfWork.LibraryRepository.GetLibrariesAsync();
foreach (var lib in libraries)
{
lib.AppUsers ??= new List<AppUser>();
lib.AppUsers.Add(user);
}
}
if (!await _unitOfWork.Complete()) _logger.LogInformation("There was an issue granting library access. Please do this manually.");
return new UserDto return new UserDto
{ {
Username = user.UserName, Username = user.UserName,