#region ========================================================================= USING ===================================================================================== using ErrorOr; using Lumina.Application.Common.DataAccess.Entities.Authorization; using Lumina.Application.Common.DataAccess.Entities.UsersManagement; using Lumina.Application.Common.DataAccess.Repositories.Authorization; using Lumina.Application.Common.DataAccess.Repositories.Users; using Lumina.Application.Common.DataAccess.Seed; using Lumina.Application.Common.DataAccess.UoW; using Lumina.Application.Common.Errors; using Lumina.Application.Common.Infrastructure.Time; using Lumina.Domain.Common.Enums.Authorization; using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; #endregion namespace Lumina.DataAccess.Core.Seed; /// /// Service for seeding initial data in the persistence medium. /// public class DataSeedService : IDataSeedService { private readonly IUnitOfWork _unitOfWork; private readonly IDateTimeProvider _dateTimeProvider; /// /// Initializes a new instance of the class. /// /// Injected unit of work for interacting with the data access layer repositories.Injected service for time related concerns. public DataSeedService(IUnitOfWork unitOfWork, IDateTimeProvider dateTimeProvider) { _unitOfWork = unitOfWork; _dateTimeProvider = dateTimeProvider; } /// /// Sets up default authorization permissions in the system. /// /// The Id of the admin admin user who will own these permissions. /// Cancellation token that can be used to stop the execution. /// An representing either a successfull operation, or an error. public async Task> SetDefaultAuthorizationPermissionsAsync(Guid adminId, CancellationToken cancellationToken) { IPermissionRepository permissionRepository = _unitOfWork.GetRepository(); // create the default authorization permissions and add them to the repository. PermissionEntity[] defaultPermissions = [ new() { PermissionName = AuthorizationPermission.CanViewUsers, CreatedBy = adminId, CreatedOnUtc = _dateTimeProvider.UtcNow }, new() { PermissionName = AuthorizationPermission.CanDeleteUsers, CreatedBy = adminId, CreatedOnUtc = _dateTimeProvider.UtcNow }, new() { PermissionName = AuthorizationPermission.CanRegisterUsers, CreatedBy = adminId, CreatedOnUtc = _dateTimeProvider.UtcNow }, new() { PermissionName = AuthorizationPermission.canCreateLibraries, CreatedBy = adminId, CreatedOnUtc = _dateTimeProvider.UtcNow } ]; foreach (PermissionEntity permission in defaultPermissions) { ErrorOr insertPermissionResult = await permissionRepository.InsertAsync(permission, cancellationToken).ConfigureAwait(false); if (insertPermissionResult.IsError) return insertPermissionResult.Errors; } await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false); return Result.Created; } /// /// Sets up default authorization roles in the system. /// /// The Id of the admin user for whom roles will be set. /// Cancellation token that can be used to stop the execution. /// An representing either a successfull operation, or an error. public async Task> SetDefaultAuthorizationRolesAsync(Guid userId, CancellationToken cancellationToken) { IRoleRepository roleRepository = _unitOfWork.GetRepository(); // create the default authorization roles and add them to the repository RoleEntity[] defaultRoles = [ new() { RoleName = "Admin", CreatedBy = userId, CreatedOnUtc = _dateTimeProvider.UtcNow } ]; foreach (RoleEntity role in defaultRoles) { ErrorOr insertRoleResult = await roleRepository.InsertAsync(role, cancellationToken).ConfigureAwait(false); if (insertRoleResult.IsError) return insertRoleResult.Errors; } await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false); return Result.Created; } /// /// Assigns admin role permissions to the admin user. /// /// The Id of the admin user to receive admin role permissions. /// Cancellation token that can be used to stop the execution. /// An representing either a successfull operation, or an error. public async Task> SetAdminRolePermissionsAsync(Guid userId, CancellationToken cancellationToken) { IRolePermissionRepository rolePermissionRepository = _unitOfWork.GetRepository(); IRoleRepository roleRepository = _unitOfWork.GetRepository(); IPermissionRepository permissionRepository = _unitOfWork.GetRepository(); // get the admin role ErrorOr getAdminRoleResult = await roleRepository.GetByNameAsync("Admin", cancellationToken).ConfigureAwait(false); if (getAdminRoleResult.IsError) return getAdminRoleResult.Errors; if (getAdminRoleResult.Value is null) return Errors.Authorization.AdminAccountNotFound; // get all permissions ErrorOr> getPermissionsResult = await permissionRepository.GetAllAsync(cancellationToken).ConfigureAwait(false); if (getPermissionsResult.IsError) return getPermissionsResult.Errors; // add each permission to the admin role foreach (PermissionEntity permission in getPermissionsResult.Value) { RolePermissionEntity rolePermissionEntity = new() { Permission = permission, PermissionId = permission.Id, Role = getAdminRoleResult.Value, RoleId = getAdminRoleResult.Value.Id, CreatedBy = userId, CreatedOnUtc = _dateTimeProvider.UtcNow }; ErrorOr insertRolePermissionResult = await rolePermissionRepository.InsertAsync(rolePermissionEntity, cancellationToken).ConfigureAwait(false); if (insertRolePermissionResult.IsError) return insertRolePermissionResult.Errors; } await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false); return Result.Created; } /// /// Assigns admin role to the admin user. /// /// The Id of the admin user to receive the admin role. /// Cancellation token that can be used to stop the execution. /// An representing either a successfull operation, or an error. public async Task> SetAdminRoleToAdministratorAccount(Guid userId, CancellationToken cancellationToken) { IUserRoleRepository userRoleRepository = _unitOfWork.GetRepository(); IRoleRepository roleRepository = _unitOfWork.GetRepository(); IUserRepository userRepository = _unitOfWork.GetRepository(); // get the admin role ErrorOr getAdminRoleResult = await roleRepository.GetByNameAsync("Admin", cancellationToken).ConfigureAwait(false); if (getAdminRoleResult.IsError) return getAdminRoleResult.Errors; if (getAdminRoleResult.Value is null) return Errors.Authorization.AdminRoleNotFound; // get admin user ErrorOr getUserResult = await userRepository.GetByIdAsync(userId, cancellationToken).ConfigureAwait(false); if (getUserResult.IsError) return getUserResult.Errors; if (getUserResult.Value is null) return Errors.Authorization.AdminAccountNotFound; // add the admin role to the admin user UserRoleEntity userRole = new() { CreatedBy = userId, CreatedOnUtc = _dateTimeProvider.UtcNow, Role = getAdminRoleResult.Value, RoleId = getAdminRoleResult.Value.Id, User = getUserResult.Value, UserId = getUserResult.Value.Id }; ErrorOr insertUserRoleResult = await userRoleRepository.InsertAsync(userRole, cancellationToken).ConfigureAwait(false); if (insertUserRoleResult.IsError) return insertUserRoleResult.Errors; await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false); return Result.Created; } }