示例#1
0
        public ActionResult Edit(ApiResourceViewModel collection)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    ApiResourceEntity resource = new ApiResourceEntity()
                    {
                        //ApiSecrets = collection.ApiSecrets,
                        //Scopes = collection.Scopes,
                        Name = collection.Name,
                        //Enabled = collection.Enabled,
                        Description = collection.Description,
                        DisplayName = collection.DisplayName,
                        //UserClaims = collection.UserClaims
                    };
                    _resourceStoreExtended.EditApiResource(resource);

                    return(RedirectToAction(nameof(Index)));
                }
                return(View(collection));
            }
            catch
            {
                return(View());
            }
        }
        public DataResult <int> CreateApiResource(ApiResourceModel apiResourceModel, IEnumerable <int> claimsIds)
        {
            var defaultScope = new ScopeEntity
            {
                Name                    = apiResourceModel.Name,
                Description             = apiResourceModel.Description,
                Required                = apiResourceModel.Required,
                ShowInDiscoveryDocument = apiResourceModel.ShowInDiscoveryDocument
            };

            var apiResourceEntity = new ApiResourceEntity
            {
                Name                    = apiResourceModel.Name,
                Description             = apiResourceModel.Description,
                Required                = apiResourceModel.Required,
                ShowInDiscoveryDocument = apiResourceModel.ShowInDiscoveryDocument,
            };

            try
            {
                var result = m_apiResourceUoW.CreateApiResource(apiResourceEntity, claimsIds, defaultScope);
                return(Success(result));
            }
            catch (DatabaseException e)
            {
                m_logger.LogWarning(e);
                return(Error <int>(e.Message));
            }
        }
        public DataResult <bool> UpdateApiResource(int id, ApiResourceModel apiResourceModel, IEnumerable <int> claimsIds)
        {
            var apiResource = new ApiResourceEntity
            {
                Name                    = apiResourceModel.Name,
                Description             = apiResourceModel.Description,
                Required                = apiResourceModel.Required,
                ShowInDiscoveryDocument = apiResourceModel.ShowInDiscoveryDocument
            };

            try
            {
                m_apiResourceUoW.UpdateApiResource(id, apiResource, claimsIds);
                return(Success(true));
            }
            catch (NoResultException <ApiResourceEntity> e)
            {
                m_logger.LogWarning(e);
                return(Error <bool>(m_translator.Translate("invalid-api-resource-id"), DataResultErrorCode.ApiResourceNotExistId));
            }
            catch (DatabaseException e)
            {
                m_logger.LogWarning(e);
                return(Error <bool>(e.Message));
            }
        }
        public virtual void CanUpdateApiResource()
        {
            using (var uow = DataService.StartUnitOfWork())
            {
                var resource = new ApiResourceEntity
                {
                    Name        = "openid",
                    Description = "OpenId-Scope",
                    DisplayName = "OpenId",
                    Enabled     = true
                };
                uow.ApiResourceRepository.Add(resource);

                var claim = new ApiResourceClaimEntity
                {
                    ApiResourceId = resource.Id,
                    ClaimType     = "ClaimType"
                };
                uow.ApiResourceClaimRepository.Add(claim);

                claim.ClaimType = "changed";
                uow.ApiResourceClaimRepository.Update(claim);

                Assert.AreEqual(claim.ClaimType, uow.ApiResourceClaimRepository.Get(claim.Id).ClaimType);
            }
        }
        public virtual void CanAddAndGetApiResourceClaims()
        {
            using (var uow = DataService.StartUnitOfWork())
            {
                var resource = new ApiResourceEntity
                {
                    Name        = "openid",
                    Description = "OpenId-Scope",
                    DisplayName = "OpenId",
                    Enabled     = true
                };
                uow.ApiResourceRepository.Add(resource);

                var claims = new[]
                {
                    new ApiResourceClaimEntity
                    {
                        ApiResourceId = resource.Id,
                        ClaimType     = "ClaimType"
                    },
                    new ApiResourceClaimEntity
                    {
                        ApiResourceId = resource.Id,
                        ClaimType     = "ClaimType2"
                    }
                };
                uow.ApiResourceClaimRepository.AddRange(claims);
                Assert.AreEqual(claims.Length, uow.ApiResourceClaimRepository.GetAll().Count());
            }
        }
示例#6
0
        public virtual void CanGetGetByScopeNamesCompound()
        {
            using (var uow = DataService.StartUnitOfWork())
            {
                var resource = new ApiResourceEntity
                {
                    Name        = "openid",
                    Description = "OpenId-Scope",
                    DisplayName = "OpenId",
                    Enabled     = true
                };
                uow.ApiResourceRepository.Add(resource);

                var scope = new ScopeEntity
                {
                    Name                    = "openid",
                    Description             = "OpenId-Scope",
                    DisplayName             = "OpenId",
                    Emphasize               = true,
                    Required                = true,
                    ShowInDiscoveryDocument = true
                };
                uow.ScopeRepository.Add(scope);

                var resScope = new ApiResourceScopeEntity
                {
                    ApiResourceId = resource.Id,
                    ScopeId       = scope.Id
                };
                uow.ApiResourceScopeRepository.Add(resScope);


                Assert.IsNotNull(uow.ApiResourceRepository.GetByScopeNamesCompound(new[] { scope.Name }).First());
            }
        }
示例#7
0
        public ActionResult Create(ApiResourceViewModel collection)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    ApiResourceEntity resource = new ApiResourceEntity()
                    {
                        //ApiSecrets = collection.ApiSecrets,
                        //Scopes = collection.Scopes,
                        Name        = collection.Name,
                        Enabled     = collection.Enabled,
                        Description = collection.Description,
                        DisplayName = collection.DisplayName,
                        //UserClaims = collection.UserClaims
                    };
                    // TODO: Add update logic here

                    _resourceStoreExtended.CreateApiResource(resource);

                    return(RedirectToAction(nameof(Index)));
                }
                else
                {
                    return(View(collection));
                }
            }
            catch
            {
                return(View());
            }
        }
        public virtual void CanAddAndGetApiResourceScope()
        {
            using (var uow = DataService.StartUnitOfWork())
            {
                var resource = new ApiResourceEntity
                {
                    Name        = "openid",
                    Description = "OpenId-Scope",
                    DisplayName = "OpenId",
                    Enabled     = true
                };
                uow.ApiResourceRepository.Add(resource);

                var scope = new ScopeEntity
                {
                    Name                    = "openid",
                    Description             = "OpenId-Scope",
                    DisplayName             = "OpenId",
                    Emphasize               = true,
                    Required                = true,
                    ShowInDiscoveryDocument = true
                };
                uow.ScopeRepository.Add(scope);

                var resourceScope = new ApiResourceScopeEntity
                {
                    ApiResourceId = resource.Id,
                    ScopeId       = scope.Id
                };
                uow.ApiResourceScopeRepository.Add(resourceScope);

                Assert.AreEqual(resourceScope.ApiResourceId,
                                uow.ApiResourceScopeRepository.Get(resourceScope.Id).ApiResourceId);
            }
        }
示例#9
0
        public async Task <bool> AddApiResourceAsync(ApiResourceEntity apiResource)
        {
            var result = await _identityContex.ApiResources.AddAsync(apiResource);

            await _identityContex.SaveChangesAsync();

            return(result.State == EntityState.Added);
        }
示例#10
0
        public static IEnumerable <ApiResourceEntity> GetApiResources()
        {
            List <ApiResourceEntity> apiResources = new List <ApiResourceEntity>();

            foreach (var apiResource in GetApiResourcesInternal())
            {
                var apiResourceEntity = new ApiResourceEntity();


                apiResourceEntity = apiResourceEntity.AddDataToEntity(apiResource);
                apiResources.Add(apiResourceEntity);
            }

            return(apiResources);
        }
示例#11
0
 public virtual void CanGetByNameCompountd()
 {
     using (var uow = DataService.StartUnitOfWork())
     {
         var resource = new ApiResourceEntity
         {
             Name        = "openid",
             Description = "OpenId-Scope",
             DisplayName = "OpenId",
             Enabled     = true
         };
         uow.ApiResourceRepository.Add(resource);
         Assert.IsNotNull(uow.ApiResourceRepository.GetByName(resource.Name));
     }
 }
示例#12
0
 public virtual void CanAddAndGetByIds()
 {
     using (var uow = DataService.StartUnitOfWork())
     {
         var resource = new ApiResourceEntity
         {
             Name        = "openid",
             Description = "OpenId-Scope",
             DisplayName = "OpenId",
             Enabled     = true
         };
         uow.ApiResourceRepository.Add(resource);
         Assert.AreEqual(resource.Name, uow.ApiResourceRepository.GetByIds(new[] { resource.Id }).Single().Name);
     }
 }
示例#13
0
        public async Task <IActionResult> Enable(string id)
        {
            ApiResource resource = await _resourceStoreExtended.FindApiResourcesByNameAsync(id);

            ApiResourceEntity entity = new ApiResourceEntity()
            {
                ApiResource = resource
            };

            entity.AddDataToEntity();
            entity.Enabled = true;

            _resourceStoreExtended.EditApiResource(entity);

            return(RedirectToAction(nameof(Index)));
        }
示例#14
0
        public virtual int CreateApiResource(ApiResourceEntity apiResourceEntity, IEnumerable <int> claimsIds, ScopeEntity defaultScope)
        {
            var claimTypes = m_claimTypeRepository.GetClaimTypesById(claimsIds);

            apiResourceEntity.ClaimTypes = new HashSet <ClaimTypeEntity>(claimTypes);

            var result = (int)m_apiResourceRepository.Create(apiResourceEntity);

            var apiResource = m_apiResourceRepository.Load <ApiResourceEntity>(result);

            defaultScope.ApiResource = apiResource;
            defaultScope.ClaimTypes  = new HashSet <ClaimTypeEntity>(claimTypes);

            var scopeResult = (int)m_scopeRepository.Create(defaultScope);

            return(result);
        }
示例#15
0
        public virtual void CanDeleteResource()
        {
            using (var uow = DataService.StartUnitOfWork())
            {
                var resource = new ApiResourceEntity
                {
                    Name        = "openid",
                    Description = "OpenId-Scope",
                    DisplayName = "OpenId",
                    Enabled     = true
                };
                uow.ApiResourceRepository.Add(resource);

                uow.ApiResourceRepository.Delete(resource);

                Assert.AreEqual(null, uow.ApiResourceRepository.Get(resource.Id));
            }
        }
示例#16
0
        public virtual void UpdateApiResource(int id, ApiResourceEntity apiResource, IEnumerable <int> claimsIds)
        {
            var apiResourceEntity = m_apiResourceRepository.FindApiResourceById(id);

            if (apiResourceEntity == null)
            {
                throw new NoResultException <ApiResourceEntity>();
            }

            apiResourceEntity.Name                    = apiResource.Name;
            apiResourceEntity.Description             = apiResource.Description;
            apiResourceEntity.Required                = apiResource.Required;
            apiResourceEntity.ShowInDiscoveryDocument = apiResource.ShowInDiscoveryDocument;

            apiResourceEntity.ClaimTypes =
                new HashSet <ClaimTypeEntity>(m_claimTypeRepository.GetClaimTypesById(claimsIds));
            m_apiResourceRepository.Update(apiResourceEntity);
        }
        public virtual void CanGetByApiIds()
        {
            using (var uow = DataService.StartUnitOfWork())
            {
                var resource = new ApiResourceEntity
                {
                    Name        = "openid",
                    Description = "OpenId-Scope",
                    DisplayName = "OpenId",
                    Enabled     = true
                };
                uow.ApiResourceRepository.Add(resource);

                var claim = new ApiResourceClaimEntity
                {
                    ApiResourceId = resource.Id,
                    ClaimType     = "ClaimType"
                };
                uow.ApiResourceClaimRepository.Add(claim);

                Assert.IsNotNull(uow.ApiResourceClaimRepository.GetByApiId(resource.Id).Single());
            }
        }
示例#18
0
 public static ApiResource MapDataFromEntity(this ApiResourceEntity apiResourceEntity)
 {
     return(JsonConvert.DeserializeObject <ApiResource>(apiResourceEntity.ApiResourceData));
 }
示例#19
0
 public static ApiResourceEntity AddDataToEntity(this ApiResourceEntity apiResourceEntity, ApiResource apiResource)
 {
     apiResourceEntity.ApiResourceName = apiResource.Name;
     apiResourceEntity.ApiResourceData = JsonConvert.SerializeObject(apiResource);
     return(apiResourceEntity);
 }
示例#20
0
        public async Task <bool> AddApiResourceAsync(ApiResourceEntity apiResource)
        {
            await _apiResourceCollection.InsertOneAsync(apiResource);

            return(true);
        }
示例#21
0
        private static void EnsureSeedData(MedParkContext context)
        {
            Console.WriteLine("Seeding database...");

            Console.WriteLine("Clients being populated");

            foreach (var client in IdentityConfig.GetClients())
            {
                if (context.Clients.Where(x => x.ClientId == client.ClientId).FirstOrDefault() == null)
                {
                    var newClient = new ClientEntity {
                        ClientId = client.ClientId, Client = client
                    };
                    newClient.AddDataToEntity();

                    context.Clients.Add(newClient);

                    context.SaveChanges();
                }
                else
                {
                    var newClient = context.Clients.Where(x => x.ClientId == client.ClientId).FirstOrDefault();
                    newClient.Client = client;

                    newClient.AddDataToEntity();

                    context.Clients.Update(newClient);

                    context.SaveChanges();
                }
            }

            Console.WriteLine("IdentityResources being populated");
            foreach (var resource in IdentityConfig.GetIdentityResources())
            {
                if (context.IdentityResources.Where(x => x.IdentityResourceName == resource.Name).FirstOrDefault() == null)
                {
                    var res = new IdentityResourceEntity {
                        IdentityResource = resource
                    };
                    res.AddDataToEntity();

                    context.IdentityResources.Add(res);

                    context.SaveChanges();
                }
                else
                {
                    var newres = context.IdentityResources.Where(x => x.IdentityResourceName == resource.Name).FirstOrDefault();
                    newres.IdentityResource = resource;

                    newres.AddDataToEntity();

                    context.IdentityResources.Update(newres);

                    context.SaveChanges();
                }
            }

            if (!context.ApiResources.Any())
            {
                Console.WriteLine("ApiResources being populated");
                foreach (var resource in IdentityConfig.GetApiResources())
                {
                    var api = new ApiResourceEntity {
                        ApiResource = resource
                    };
                    api.AddDataToEntity();

                    context.ApiResources.Add(api);
                }
                context.SaveChanges();
            }
            else
            {
                Console.WriteLine("ApiResources already populated");
            }

            Console.WriteLine("Done seeding database.");
            Console.WriteLine();
        }