Ejemplo n.º 1
0
        public async Task <bool> Update(ResourceSet resourceSet)
        {
            using (var transaction = await _context.Database.BeginTransactionAsync().ConfigureAwait(false))
            {
                try
                {
                    var record = await _context.ResourceSets.FirstOrDefaultAsync(r => r.Id == resourceSet.Id).ConfigureAwait(false);

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

                    record.Name    = resourceSet.Name;
                    record.Scopes  = MappingExtensions.GetConcatenatedList(resourceSet.Scopes);
                    record.Type    = resourceSet.Type;
                    record.Uri     = resourceSet.Uri;
                    record.IconUri = resourceSet.IconUri;
                    await _context.SaveChangesAsync().ConfigureAwait(false);

                    transaction.Commit();
                    return(true);
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    return(false);
                }
            }
        }
        public async Task <bool> Update(Policy policy)
        {
            using (var transaction = await _context.Database.BeginTransactionAsync().ConfigureAwait(false))
            {
                try
                {
                    var record = await _context.Policies
                                 .FirstOrDefaultAsync(p => p.Id == policy.Id).ConfigureAwait(false);

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

                    var rulesNotToBeDeleted = new List <string>();
                    if (policy.Rules != null)
                    {
                        foreach (var ru in policy.Rules)
                        {
                            var rule = record.Rules.FirstOrDefault(r => r.Id == ru.Id);
                            if (rule == null)
                            {
                                rule = new Models.PolicyRule
                                {
                                    Id       = Guid.NewGuid().ToString(),
                                    PolicyId = policy.Id
                                };
                                record.Rules.Add(rule);
                            }

                            rule.IsResourceOwnerConsentNeeded = ru.IsResourceOwnerConsentNeeded;
                            rule.Script           = ru.Script;
                            rule.ClientIdsAllowed = MappingExtensions.GetConcatenatedList(ru.ClientIdsAllowed);
                            rule.Scopes           = MappingExtensions.GetConcatenatedList(ru.Scopes);
                            rule.OpenIdProvider   = ru.OpenIdProvider;
                            rule.Claims           = JsonConvert.SerializeObject(ru.Claims == null ? new List <Claim>() : ru.Claims);
                            rulesNotToBeDeleted.Add(rule.Id);
                        }
                    }

                    var ruleIds = record.Rules.Select(o => o.Id).ToList();
                    foreach (var ruleId in ruleIds.Where(id => !rulesNotToBeDeleted.Contains(id)))
                    {
                        var removedRule = record.Rules.First(o => o.Id == ruleId);
                        record.Rules.Remove(removedRule);
                        _context.PolicyRules.Remove(removedRule);
                    }

                    var resourceSetIdsNotToBeDeleted = new List <string>();
                    if (policy.ResourceSetIds != null)
                    {
                        foreach (var resourceSetId in policy.ResourceSetIds)
                        {
                            var policyResource = record.PolicyResources.FirstOrDefault(p => p.ResourceSetId == resourceSetId);
                            if (policyResource == null)
                            {
                                policyResource = new Models.PolicyResource
                                {
                                    ResourceSetId = resourceSetId,
                                    PolicyId      = policy.Id
                                };
                                record.PolicyResources.Add(policyResource);
                            }

                            resourceSetIdsNotToBeDeleted.Add(policyResource.ResourceSetId);
                        }
                    }

                    var resourceSetIds = record.PolicyResources.Select(o => o.ResourceSetId).ToList();
                    foreach (var resourceSetId in resourceSetIds.Where(id => !resourceSetIdsNotToBeDeleted.Contains(id)))
                    {
                        var removedResource = record.PolicyResources.First(o => o.ResourceSetId == resourceSetId);
                        record.PolicyResources.Remove(removedResource);
                    }

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

                    transaction.Commit();
                    return(true);
                }
                catch
                {
                    transaction.Rollback();
                    return(false);
                }
            }
        }