Пример #1
0
        public async Task UpdateApiResourceAsync(ApiResourceViewModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var apiResource = await this.GetApiResourceByName(model.Name);

            if (apiResource == null)
            {
                throw new ArgumentNullException(nameof(apiResource));
            }

            apiResource.Enabled     = model.Enabled;
            apiResource.Name        = model.Name;
            apiResource.DisplayName = model.DisplayName;
            apiResource.Description = string.Empty;

            HandleCollectionProperties(model, apiResource);

            this.context.ApiResources.Update(apiResource);

            await this.context.SaveChangesAsync();
        }
Пример #2
0
        private static void HandleCollectionProperties(
            ApiResourceViewModel model,
            ApiResource apiResource
            )
        {
            // deassign them
            if (apiResource.Scopes != null)
            {
                apiResource.Scopes.Clear();
            }
            if (apiResource.UserClaims != null)
            {
                apiResource.UserClaims.Clear();
            }

            // assign them
            apiResource.Scopes = model.Scopes
                                 .Select(s => new ApiResourceScope
            {
                ApiResource = apiResource,
                Scope       = s
            }).ToList();

            apiResource.UserClaims = model.UserClaims
                                     .Select(x => new ApiResourceClaim
            {
                ApiResource = apiResource,
                Type        = x
            }).ToList();
        }
Пример #3
0
        public async Task <IActionResult> UpdateAsync([FromBody] ApiResourceViewModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            await this.service.UpdateApiResourceAsync(model);

            return(NoContent());
        }
Пример #4
0
        public async Task <IActionResult> CreateAsync([FromBody] ApiResourceViewModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var result = await this.service.CreateApiResourceAsync(model);

            return(Created($"api/resources/{result}", this.Json(result)));
        }
Пример #5
0
        public async Task <string> CreateApiResourceAsync(ApiResourceViewModel model)
        {
            if (model is null)
            {
                throw new System.ArgumentNullException(nameof(model));
            }

            var apiResource = new ApiResource
            {
                Enabled     = model.Enabled,
                Name        = model.Name,
                DisplayName = model.DisplayName,
                Description = string.Empty
            };

            HandleCollectionProperties(model, apiResource);

            this.context.ApiResources.Add(apiResource);

            await this.context.SaveChangesAsync();

            return(apiResource.Name);
        }