예제 #1
0
        public IdentityResourceDetailResource(IdentityResourceDetail identityResource, IUrlHelper url, IdentityResourceMetaData metaData)
        {
            if (identityResource == null)
            {
                throw new ArgumentNullException(nameof(identityResource));
            }
            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }
            if (metaData == null)
            {
                throw new ArgumentNullException(nameof(metaData));
            }

            Data = new IdentityResourceDetailDataResource(identityResource, url, metaData);

            var links = new Dictionary <string, string>();

            if (metaData.SupportsDelete)
            {
                links["Delete"] = url.RelativeLink(Constants.RouteNames.DeleteIdentityResource, new { subject = identityResource.Subject });
            }
            Links = links;
        }
        public Task <IdentityAdminResult <IdentityResourceDetail> > GetAsync(string subject)
        {
            int parsedId;

            if (int.TryParse(subject, out parsedId))
            {
                using (var db = new ConfigurationDbContext(connection))
                {
                    var inMemoryApiResource = db.IdentityResources.FirstOrDefault(p => p.Id == parsedId);
                    if (inMemoryApiResource == null)
                    {
                        return(Task.FromResult(new IdentityAdminResult <IdentityResourceDetail>((IdentityResourceDetail)null)));
                    }

                    var result = new IdentityResourceDetail
                    {
                        Subject     = subject,
                        Name        = inMemoryApiResource.Name,
                        Description = inMemoryApiResource.Description
                    };

                    var metadata = GetMetadata();
                    var props    = from prop in metadata.UpdateProperties
                                   select new PropertyValue
                    {
                        Type  = prop.Type,
                        Value = GetProperty(prop, inMemoryApiResource),
                    };

                    result.Properties             = props.ToArray();
                    result.IdentityResourceClaims = inMemoryApiResource.UserClaims.Select(x => new IdentityResourceClaimValue
                    {
                        Id   = x.Id.ToString(),
                        Type = x.Type
                    });

                    return(Task.FromResult(new IdentityAdminResult <IdentityResourceDetail>(result)));
                }
            }
            return(Task.FromResult(new IdentityAdminResult <IdentityResourceDetail>((IdentityResourceDetail)null)));
        }
        public IdentityResourceDetailDataResource(IdentityResourceDetail identityResource, IUrlHelper url, IdentityResourceMetaData metaData)
        {
            if (identityResource == null)
            {
                throw new ArgumentNullException(nameof(identityResource));
            }
            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }
            if (metaData == null)
            {
                throw new ArgumentNullException(nameof(metaData));
            }

            this["Name"]        = identityResource.Name;
            this["Description"] = identityResource.Description;
            this["Subject"]     = identityResource.Subject;

            if (identityResource.Properties != null)
            {
                var props = (from p in identityResource.Properties
                             let m = (from m in metaData.UpdateProperties where m.Type == p.Type select m).SingleOrDefault()
                                     where m != null
                                     select new
                {
                    Data = m.Convert(p.Value),
                    Meta = m,
                    Links = new
                    {
                        update = url.RelativeLink(Constants.RouteNames.UpdateIdentityResourceProperty,
                                                  new
                        {
                            subject = identityResource.Subject,
                            type = p.Type.ToBase64UrlEncoded()
                        }
                                                  )
                    }
                }).ToList();

                if (props.Any())
                {
                    this["Properties"] = props.ToArray();
                }
            }

            if (identityResource.IdentityResourceClaims != null)
            {
                var identityResourceClaims = from c in identityResource.IdentityResourceClaims.ToArray()
                                             select new
                {
                    Data  = c,
                    Links = new
                    {
                        delete = url.RelativeLink(Constants.RouteNames.RemoveIdentityResourceClaim, new
                        {
                            subject = identityResource.Subject,
                            id      = c.Id
                        })
                    }
                };
                this["Claims"] = new
                {
                    Data  = identityResourceClaims.ToArray(),
                    Links = new
                    {
                        create = url.RelativeLink(Constants.RouteNames.AddIdentityResourceClaim, new { subject = identityResource.Subject })
                    }
                };
            }
        }