From 80283bcd497f5bc45984de38173dda6983eac8ef Mon Sep 17 00:00:00 2001 From: Joseph Milazzo Date: Mon, 18 Jan 2021 16:55:52 -0600 Subject: [PATCH] When registering an admin user, ensure they have access to all libraries. --- API/Controllers/AccountController.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/API/Controllers/AccountController.cs b/API/Controllers/AccountController.cs index 6ce1a4636..78b7f23b7 100644 --- a/API/Controllers/AccountController.cs +++ b/API/Controllers/AccountController.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using API.Constants; using API.DTOs; @@ -54,6 +55,20 @@ namespace API.Controllers 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(); + lib.AppUsers.Add(user); + } + } + + if (!await _unitOfWork.Complete()) _logger.LogInformation("There was an issue granting library access. Please do this manually."); + return new UserDto { Username = user.UserName,