public IActionResult Edit(ApiScopeModel model)
        {
            ApiScope api;

            if (model.Id == 0)
            {
                api = new ApiScope
                {
                };
            }
            else
            {
                api = _configurationDbContext.ApiScopes
                      .Include(x => x.UserClaims)
                      .Include(x => x.Properties)
                      .FirstOrDefault(x => x.Id == model.Id);
            }

            model.UpdateEntity(api);

            if (!string.IsNullOrEmpty(model.UserClaimsItems))
            {
                var userClaims = JsonSerializer.Deserialize <List <string> >(model.UserClaimsItems);

                api.UserClaims.AddRange(userClaims.Select(x => new ApiScopeClaim
                {
                    Type = x,
                }));
            }

            _configurationDbContext.SaveChanges();

            return(RedirectToAction(nameof(Scopes), new { id = api.Id }));
        }
Example #2
0
        public async Task <IActionResult> Edit(ApiScopeModel model)
        {
            if (model.Id == 0)
            {
                return(RedirectToAction(nameof(Index)));
            }

            var apiScope = await _apiScopeService.GetApiScopeByIdAsync(model.Id);

            if (apiScope == null)
            {
                return(RedirectToAction(nameof(Index)));
            }

            model.UserClaims = model.UserClaimsItems.Deserialize <List <string> >()?.Select(x => new ApiScopeClaimModel()
            {
                Type = x
            }).ToList();

            apiScope = CommonMappers.Mapper.Map <ApiScope>(model);

            var updatedResult = await _apiScopeService.UpdateApiScopeAsync(apiScope);

            if (updatedResult)
            {
                SuccessNotification(await _localizationService.GetResourceAsync("ApiResource.Updated"));

                return(RedirectToAction(nameof(Index)));
            }

            return(View(model));
        }
        public IActionResult Delete(int id)
        {
            var api = _configurationDbContext.ApiScopes
                      .Include(x => x.UserClaims)
                      .Include(x => x.Properties)
                      .FirstOrDefault(x => x.Id == id);

            return(View(ApiScopeModel.FromEntity(api)));
        }
        public IActionResult Delete(ApiScopeModel model)
        {
            var api = _configurationDbContext.ApiScopes
                      .Include(x => x.UserClaims)
                      .Include(x => x.Properties)
                      .FirstOrDefault(x => x.Id == model.Id);

            _configurationDbContext.ApiScopes.Remove(api);
            _configurationDbContext.SaveChanges();

            return(RedirectToAction(nameof(Scopes), new { id = api.Id }));
        }
Example #5
0
        public async Task <IActionResult> Delete(ApiScopeModel model)
        {
            if (model.Id == 0)
            {
                return(RedirectToAction(nameof(Index)));
            }

            var result = await _apiScopeService.DeleteApiScopeAsync(CommonMappers.Mapper.Map <ApiScope>(model));

            if (result)
            {
                SuccessNotification(await _localizationService.GetResourceAsync("ApiResource.Updated"));
                return(RedirectToAction(nameof(Index)));
            }

            return(View(model));
        }
Example #6
0
        public async Task <IActionResult> Create(ApiScopeModel model)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index)));
            }

            model.UserClaims = model.UserClaimsItems.Deserialize <List <string> >()?.Select(x => new ApiScopeClaimModel()
            {
                Type = x
            }).ToList();

            await _apiScopeService.InsertApiScopeAsync(CommonMappers.Mapper.Map <ApiScope>(model));

            SuccessNotification(await _localizationService.GetResourceAsync(""));

            return(RedirectToAction(nameof(Index)));
        }
    public async Task CreateAsync(ApiScopeModel model)
    {
        var scope = new Duende.IdentityServer.Models.ApiScope()
        {
            Name        = model.Name,
            DisplayName = model.DisplayName?.Trim()
        };

        var claims = model.UserClaims?.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToArray() ?? Enumerable.Empty <string>();

        if (claims.Any())
        {
            scope.UserClaims = claims.ToList();
        }

        _context.ApiScopes.Add(scope.ToEntity());
        await _context.SaveChangesAsync();
    }
        public IActionResult Edit(int id)
        {
            if (id != 0)
            {
                var api = _configurationDbContext.ApiScopes
                          .Include(x => x.UserClaims)
                          .Include(x => x.Properties)
                          .FirstOrDefault(x => x.Id == id);

                return(View(ApiScopeModel.FromEntity(api)));
            }
            else
            {
                return(View(new ApiScopeModel
                {
                }));
            }
        }
    public async Task UpdateAsync(ApiScopeModel model)
    {
        var scope = await _context.ApiScopes
                    .Include(x => x.UserClaims)
                    .SingleOrDefaultAsync(x => x.Name == model.Name);

        if (scope == null)
        {
            throw new Exception("Invalid Api Scope");
        }

        if (scope.DisplayName != model.DisplayName)
        {
            scope.DisplayName = model.DisplayName?.Trim();
        }

        var claims        = model.UserClaims?.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToArray() ?? Enumerable.Empty <string>();
        var currentClaims = (scope.UserClaims.Select(x => x.Type) ?? Enumerable.Empty <String>()).ToArray();

        var claimsToAdd    = claims.Except(currentClaims).ToArray();
        var claimsToRemove = currentClaims.Except(claims).ToArray();

        if (claimsToRemove.Any())
        {
            scope.UserClaims.RemoveAll(x => claimsToRemove.Contains(x.Type));
        }
        if (claimsToAdd.Any())
        {
            scope.UserClaims.AddRange(claimsToAdd.Select(x => new ApiScopeClaim
            {
                Type = x,
            }));
        }

        await _context.SaveChangesAsync();
    }