예제 #1
0
        public static CredentialDataStore Load(IHostingEnvironment env, string subpath)
        {
            if (env == null)
            {
                throw new ArgumentNullException("env");
            }

            CredentialDataStore cached = _cached;

            if (cached != null)
            {
                if (env != null)
                {
                    return(cached);
                }
                if (cached._env == null)
                {
                    cached._env = env;
                    return(cached);
                }
                else if (ReferenceEquals(cached._env, env))
                {
                    return(cached);
                }
            }
            IFileInfo fileInfo = env.ContentRootFileProvider.GetFileInfo(subpath);

            if (fileInfo.Exists)
            {
                using (Stream stream = fileInfo.CreateReadStream())
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(CredentialDataStore));
                    cached = (CredentialDataStore)(serializer.ReadObject(stream));
                }
                cached._env = env;
                cached.Normalize();
            }
            else
            {
                cached      = new CredentialDataStore();
                cached._env = env;
                cached.Save();
            }
            _cached = cached;
            return(cached);
        }
예제 #2
0
        private static void HandleListApi(string path, HttpRequest request, HttpResponse response, IHostingEnvironment env)
        {
            if (path.StartsWith("/"))
            {
                path = (path.Length == 1) ? "" : path.Substring(1);
            }

            CredentialDataStore dataStore = CredentialDataStore.Load(env, "credentials.json");
            Guid id;

            switch (path)
            {
            case "domain":
                HandleReturnResult <CredentialDomainInfo[]>(dataStore.Domains.Select(d => new CredentialDomainInfo(d)).ToArray(), response, env);
                break;

            case "host":
                HostWithCredentials[] hwc = new HostWithCredentials[0];
                if (request.Query.ContainsKey("domain") && Guid.TryParse(request.Query["domain"], out id))
                {
                    CredentialDomain domain = dataStore.Domains.FirstOrDefault(d => d.ID.Equals(id));
                    if (domain != null)
                    {
                        if (request.Query.ContainsKey("id"))
                        {
                            if (Guid.TryParse(request.Query["id"], out id))
                            {
                                hwc = domain.Hosts.Where(h => h.ID.Equals(id)).Select((h, i) => new HostWithCredentials(domain, i)).ToArray();
                            }
                        }
                        else
                        {
                            hwc = domain.Hosts.Select((h, i) => new HostWithCredentials(domain, i)).ToArray();
                        }
                    }
                }
                HandleReturnResult <HostWithCredentials[]>(hwc, response, env);
                break;
            }
        }
 public override void Normalize()
 {
     CredentialDataStore.NormalizeIDs <ResourcePath>(_paths);
 }
예제 #4
0
        public override void Normalize()
        {
            CredentialDataStore.NormalizeIDs <ResourceAuthority>(_hosts);
            CredentialDataStore.NormalizeIDs <CredentialInfo>(_credentials);
            if (_credentials == null || _credentials.Count == 0)
            {
                return;
            }

            if (_hosts == null || _hosts.Count == 0)
            {
                foreach (CredentialInfo c in _credentials)
                {
                    if (c.Hosts.Count > 0)
                    {
                        c.Hosts.Clear();
                    }
                    if (c.Paths.Count > 0)
                    {
                        c.Paths.Clear();
                    }
                }
                return;
            }

            IEnumerable <Guid> hostIds = _hosts.Select(h => h.ID);

            foreach (CredentialInfo c in _credentials)
            {
                if (c.Hosts.Count == 0)
                {
                    continue;
                }
                List <Guid> hosts = c.Hosts.Where(i => hostIds.Any(h => h.Equals(i))).ToList();
                if (hosts.Count < c.Hosts.Count)
                {
                    for (int i = 0; i < hosts.Count; i++)
                    {
                        c.Hosts[i] = hosts[i];
                    }
                    while (c.Hosts.Count > hosts.Count)
                    {
                        c.Hosts.RemoveAt(hosts.Count);
                    }
                }
            }

            List <Guid> pathIds = _hosts[0].Paths.Select(p => p.ID).ToList();

            for (int i = 1; i < _hosts.Count; i++)
            {
                pathIds.AddRange(_hosts[i].Paths.Select(p =>
                {
                    if (pathIds.Any(g => p.ID.Equals(g)))
                    {
                        p.ID = Guid.NewGuid();
                    }
                    return(p.ID);
                }));
            }

            if (pathIds.Count == 0)
            {
                foreach (CredentialInfo c in _credentials)
                {
                    if (c.Paths.Count > 0)
                    {
                        c.Paths.Clear();
                    }
                }
                return;
            }

            foreach (CredentialInfo c in _credentials)
            {
                if (c.Paths.Count == 0)
                {
                    continue;
                }
                List <Guid> paths = c.Paths.Where(i => pathIds.Contains(i)).ToList();
                if (paths.Count < c.Paths.Count)
                {
                    for (int i = 0; i < paths.Count; i++)
                    {
                        c.Paths[i] = paths[i];
                    }
                    while (c.Paths.Count > paths.Count)
                    {
                        c.Paths.RemoveAt(paths.Count);
                    }
                }
            }
        }