using DAL.Models.Entities; using DAL.Models.Filters; using HRD.LDAPService; using HRD.WebApi.Repositories; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace DAL.Repositories { public class WebAppToWebAppAdditionalRoleRepository : BaseRepository { public WebAppToWebAppAdditionalRoleRepository() : base(new WebApiContext()) { } public async Task> GetListByFilterAsync(EmployeeToWebAppFilter filter, bool asNoTracking = true) { var items = this.RepositoryContext.Set().AsQueryable(); if (filter.EmployeeToWebAppId != null && filter.EmployeeToWebAppId != 0) { items = items.Where(x => x.EmployeeToWebAppId == filter.EmployeeToWebAppId); } return asNoTracking ? await items.AsNoTracking().ToListAsync() : await items.ToListAsync(); } private async Task AddUserToAdGroup(WebAppToWebAppAdditionalRole entity) { return await ProcessGroup(entity, "adding"); } private async Task RemoveUserFromAdGroup(WebAppToWebAppAdditionalRole entity) { return await ProcessGroup(entity, "deleting"); } private async Task ProcessGroup(WebAppToWebAppAdditionalRole entity, string action) { if (entity == default) throw new ArgumentNullException(nameof(entity)); var employee2Web = await GetEmployeeToWebAppById(entity.EmployeeToWebAppId); if (employee2Web == default) throw new ArgumentNullException(nameof(employee2Web)); var webappRole = await GetWebAppAddRoleById(entity.WebAppAdditionalRoleId); if (webappRole == default) throw new ArgumentNullException(nameof(webappRole)); if (string.IsNullOrEmpty(webappRole.AdWebAppAdditionalRoleName)) return true; var employee = await GetEmployeeById(employee2Web.EmployeeId); if (employee == default) throw new ArgumentNullException(nameof(employee)); var webapp = await GetWebAppById(employee2Web.WebAppId); if (webapp == default) throw new ArgumentNullException(nameof(webapp)); bool result; var isAdding = action == "adding"; var groupSuffix = $"{webapp.AdWebAppName}_{webappRole.AdWebAppAdditionalRoleName}"; try { if (isAdding) result = LdapManager.AD_AddUserloginToGroup(employee.LoginName, groupSuffix); else result = LdapManager.AD_RemoveUserFromGroup(employee.LoginName, groupSuffix); } catch (Exception ex) { WriteLogException(ex, $"An error occurred while '{action}' the '{employee.LoginName}' into '{groupSuffix}'."); return false; } if (!result) { WriteLogError($"An error occurred while '{action}' the '{employee.LoginName}' into '{groupSuffix}'."); return false; }; return true; } public override async Task AddAsync(WebAppToWebAppAdditionalRole entity, bool saveEntity = true) { if (await AddUserToAdGroup(entity)) { var result = await base.AddAsync(entity, saveEntity); return result; } return false; } public override async Task AddListAsync(List entities, bool saveEntity = true) { var result = true; for (int i = 0; i < entities.Count; i++) { if (!await AddAsync(entities[i], saveEntity)) result = false; } return result; } public override async Task DeleteByIdAsync(int id, bool saveEntity = true) { try { var entity = await base.GetByIdAsync(id); if (entity == default) { throw new ArgumentNullException(nameof(entity)); } if (await RemoveUserFromAdGroup(entity)) { var result = await base.DeleteByIdAsync(id, saveEntity); return result; } return false; } catch (Exception ex) { WriteLogException(ex, $"An error occurred while deleting the Id {id}"); return false; } } public async Task GetEmployeeToWebAppById(int entityId) { try { return await this.RepositoryContext.Set().FindAsync(entityId); } catch (Exception ex) { WriteLogException(ex, $"{typeof(EmployeeToWebApp).Name} id:{entityId}"); } return default; } public async Task GetWebAppById(int entityId) { try { return await this.RepositoryContext.Set().FindAsync(entityId); } catch (Exception ex) { WriteLogException(ex, $"{typeof(WebApp).Name} id:{entityId}"); } return default; } public async Task GetWebAppAddRoleById(int entityId) { try { return await this.RepositoryContext.Set().FindAsync(entityId); } catch (Exception ex) { WriteLogException(ex, $"{typeof(WebAppAdditionalRole).Name} id:{entityId}"); } return default; } public async Task GetEmployeeById(int entityId) { try { return await this.RepositoryContext.Set().FindAsync(entityId); } catch (Exception ex) { WriteLogException(ex, $"{typeof(Employee).Name} id:{entityId}"); } return default; } } }