public async Task <bool> Insert(Core.Models.Setting configuration)
        {
            if (configuration == null)
            {
                return(false);
            }

            using (var transaction = await _context.Database.BeginTransactionAsync().ConfigureAwait(false))
            {
                try
                {
                    _context.Settings.Add(new Models.Setting
                    {
                        Key   = configuration.Key,
                        Value = configuration.Value
                    });
                    await _context.SaveChangesAsync().ConfigureAwait(false);

                    transaction.Commit();
                    return(true);
                }
                catch (Exception ex)
                {
                    _configurationEventSource.Failure(ex);
                    transaction.Rollback();
                    return(false);
                }
            }
        }
Esempio n. 2
0
        public async Task <bool> UpdateAuthenticationProvider(AuthenticationProvider authenticationProvider)
        {
            using (var transaction = await _simpleIdentityServerConfigurationContext.Database.BeginTransactionAsync())
            {
                try
                {
                    var record = await _simpleIdentityServerConfigurationContext
                                 .AuthenticationProviders
                                 .Include(a => a.Options)
                                 .FirstOrDefaultAsync(a => a.Name == authenticationProvider.Name)
                                 .ConfigureAwait(false);

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

                    record.IsEnabled    = authenticationProvider.IsEnabled;
                    record.CallbackPath = authenticationProvider.CallbackPath;
                    record.ClassName    = authenticationProvider.ClassName;
                    record.Code         = authenticationProvider.Code;
                    record.Namespace    = authenticationProvider.Namespace;
                    record.Type         = authenticationProvider.Type;
                    var optsNotToBeDeleted = new List <string>();
                    if (authenticationProvider.Options != null)
                    {
                        foreach (var opt in authenticationProvider.Options)
                        {
                            var option = record.Options.FirstOrDefault(o => o.Id == opt.Id);
                            if (option != null)
                            {
                                option.Key   = opt.Key;
                                option.Value = opt.Value;
                            }
                            else
                            {
                                option = new Models.Option
                                {
                                    Id    = Guid.NewGuid().ToString(),
                                    Key   = opt.Key,
                                    Value = opt.Value
                                };
                                record.Options.Add(option);
                            }
                            optsNotToBeDeleted.Add(option.Id);
                        }
                    }

                    var optionIds = record.Options.Select(o => o.Id).ToList();
                    foreach (var optId in optionIds.Where(id => !optsNotToBeDeleted.Contains(id)))
                    {
                        record.Options.Remove(record.Options.First(o => o.Id == optId));
                    }

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

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