using System; using Bit.Core.Models.Table; using System.Collections.Generic; using Newtonsoft.Json; using Bit.Core.Enums; using System.Linq; namespace Bit.Core.Models.Api { public class DomainsResponseModel : ResponseModel { public DomainsResponseModel(User user, bool excluded = true) : base("domains") { if (user == null) { throw new ArgumentNullException(nameof(user)); } EquivalentDomains = user.EquivalentDomains != null ? JsonConvert.DeserializeObject>>(user.EquivalentDomains) : null; var excludedGlobalEquivalentDomains = user.ExcludedGlobalEquivalentDomains != null ? JsonConvert.DeserializeObject>(user.ExcludedGlobalEquivalentDomains) : new List(); var globalDomains = new List(); var domainsToInclude = excluded ? Core.Utilities.StaticStore.GlobalDomains : Core.Utilities.StaticStore.GlobalDomains.Where(d => !excludedGlobalEquivalentDomains.Contains(d.Key)); foreach (var domain in domainsToInclude) { globalDomains.Add(new GlobalDomains(domain.Key, domain.Value, excludedGlobalEquivalentDomains, excluded)); } GlobalEquivalentDomains = !globalDomains.Any() ? null : globalDomains; } public IEnumerable> EquivalentDomains { get; set; } public IEnumerable GlobalEquivalentDomains { get; set; } public class GlobalDomains { public GlobalDomains( GlobalEquivalentDomainsType globalDomain, IEnumerable domains, IEnumerable excludedDomains, bool excluded) { Type = globalDomain; Domains = domains; Excluded = excluded && (excludedDomains?.Contains(globalDomain) ?? false); } public GlobalEquivalentDomainsType Type { get; set; } public IEnumerable Domains { get; set; } public bool Excluded { get; set; } } } }