public async Task <bool> DeleteAsync(Domains.Scope scope)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var connectedScope = await _context.Scopes
                                         .FirstOrDefaultAsync(c => c.Name == scope.Name)
                                         .ConfigureAwait(false);

                    if (connectedScope == null)
                    {
                        return(false);
                    }

                    _context.Scopes.Remove(connectedScope);
                    await _context.SaveChangesAsync();

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    _managerEventSource.Failure(ex);
                    transaction.Rollback();
                    return(false);
                }
            }

            return(true);
        }
        public async Task <bool> InsertAsync(Domains.Scope scope)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var record = new Models.Scope
                    {
                        Name                 = scope.Name,
                        Description          = scope.Description,
                        IsDisplayedInConsent = scope.IsDisplayedInConsent,
                        IsExposed            = scope.IsExposed,
                        IsOpenIdScope        = scope.IsOpenIdScope,
                        Type                 = (Models.ScopeType)scope.Type,
                        ScopeClaims          = new List <Models.ScopeClaim>(),
                        CreateDateTime       = DateTime.UtcNow,
                        UpdateDateTime       = DateTime.UtcNow
                    };

                    if (scope.Claims != null &&
                        scope.Claims.Any())
                    {
                        foreach (var type in scope.Claims)
                        {
                            var rec = _context.Claims.FirstOrDefault(c => c.Code == type);
                            if (rec == null)
                            {
                                rec = new Models.Claim {
                                    Code = type
                                };
                                _context.Claims.Add(rec);
                            }

                            record.ScopeClaims.Add(new Models.ScopeClaim {
                                Claim = rec
                            });
                        }
                    }

                    _context.Scopes.Add(record);
                    await _context.SaveChangesAsync().ConfigureAwait(false);

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    _managerEventSource.Failure(ex);
                    transaction.Rollback();
                    return(false);
                }
            }

            return(true);
        }
Exemple #3
0
 public static Model.Scope ToModel(this Domain.Scope scope)
 {
     return(new Model.Scope
     {
         Name = scope.Name,
         Description = scope.Description,
         IsDisplayedInConsent = scope.IsDisplayedInConsent,
         IsExposed = scope.IsExposed,
         IsOpenIdScope = scope.IsOpenIdScope,
         Type = (Models.ScopeType)scope.Type,
         ScopeClaims = scope.Claims == null ? new List <Model.ScopeClaim>() :
                       scope.Claims.Select(c => new Model.ScopeClaim()
         {
             Claim = new Model.Claim
             {
                 Code = c
             }
         }).ToList()
     });
 }
        public async Task <bool> UpdateAsync(Domains.Scope scope)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var connectedScope = await _context.Scopes
                                         .Include(s => s.ScopeClaims)
                                         .FirstOrDefaultAsync(c => c.Name == scope.Name)
                                         .ConfigureAwait(false);

                    connectedScope.Description          = scope.Description;
                    connectedScope.IsOpenIdScope        = scope.IsOpenIdScope;
                    connectedScope.IsDisplayedInConsent = scope.IsDisplayedInConsent;
                    connectedScope.IsExposed            = scope.IsExposed;
                    connectedScope.Type           = (Models.ScopeType)scope.Type;
                    connectedScope.UpdateDateTime = DateTime.UtcNow;
                    var scopesNotToBeRemoved = new List <string>();
                    if (scope.Claims != null &&
                        scope.Claims.Any())
                    {
                        foreach (var type in scope.Claims)
                        {
                            var rec         = _context.Claims.FirstOrDefault(c => c.Code == type);
                            var scopeClaims = connectedScope.ScopeClaims.FirstOrDefault(c => c.ClaimCode == type);
                            if (rec == null)
                            {
                                rec = new Models.Claim {
                                    Code = type
                                };
                                _context.Claims.Add(rec);
                            }

                            if (scopeClaims == null)
                            {
                                connectedScope.ScopeClaims.Add(new Models.ScopeClaim
                                {
                                    ClaimCode = rec.Code,
                                    ScopeName = connectedScope.Name
                                });
                            }

                            scopesNotToBeRemoved.Add(type);
                        }
                    }

                    foreach (var scopeClaim in connectedScope.ScopeClaims.Where(s => !scopesNotToBeRemoved.Any(c => c == s.ClaimCode)).ToList())
                    {
                        _context.ScopeClaims.Remove(scopeClaim);
                    }

                    await _context.SaveChangesAsync().ConfigureAwait(false);

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    _managerEventSource.Failure(ex);
                    transaction.Rollback();
                    return(false);
                }

                return(true);
            }
        }