private static async Task CommitBatch(DateTime cursorTime)
            {
                await _context.SaveChangesAsync();

                await _cursor.WriteCursor(cursorTime);

                Console.Write("+");
            }
        protected override async Task Handle(TemperatureNoteUpdateCommand request, CancellationToken cancellationToken)
        {
            var temperature = await entities.Temperatures.SingleOrDefaultAsync(t => t.Id == request.Id, cancellationToken);

            if (temperature == null)
            {
                return;
            }
            temperature.Note = request.Note;
            await entities.SaveChangesAsync(cancellationToken);
        }
예제 #3
0
        private async Task CommitBatch(EntitiesContext context, FileCursor cursor, Logger logger, DateTime?cursorTime)
        {
            logger.Log("Committing batch...");

            var count = await context.SaveChangesAsync();

            if (cursorTime.HasValue)
            {
                await cursor.Write(cursorTime.Value);
            }

            logger.Log($"{count} packages saved.");
        }
        /// <summary>
        /// Unsubscribe a user from one or more security policies.
        /// </summary>
        public async Task UnsubscribeAsync(User user, IUserSecurityPolicySubscription subscription, bool commitChanges = true)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (subscription == null)
            {
                throw new ArgumentNullException(nameof(subscription));
            }

            var matches = FindPolicies(user, subscription).ToList();

            if (matches.Any())
            {
                foreach (var policy in matches)
                {
                    user.SecurityPolicies.Remove(policy);

                    EntitiesContext.UserSecurityPolicies.Remove(policy);
                }

                await subscription.OnUnsubscribeAsync(new UserSecurityPolicySubscriptionContext(this, user));

                await Auditing.SaveAuditRecordAsync(
                    new UserAuditRecord(user, AuditedUserAction.UnsubscribeFromPolicies, subscription.Policies));

                if (commitChanges)
                {
                    await EntitiesContext.SaveChangesAsync();
                }

                Diagnostics.Information($"User is now unsubscribed from '{subscription.SubscriptionName}'.");
            }
            else
            {
                Diagnostics.Information($"User is already unsubscribed from '{subscription.SubscriptionName}'.");
            }
        }
        /// <summary>
        /// Subscribe a user to one or more security policies.
        /// </summary>
        /// <returns>True if user was subscribed, false if not (i.e., was already subscribed).</returns>
        public async Task <bool> SubscribeAsync(User user, IUserSecurityPolicySubscription subscription, bool commitChanges = true)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (subscription == null)
            {
                throw new ArgumentNullException(nameof(subscription));
            }

            if (IsSubscribed(user, subscription))
            {
                Diagnostics.Information($"User is already subscribed to '{subscription.SubscriptionName}'.");

                return(false);
            }
            else
            {
                foreach (var policy in subscription.Policies)
                {
                    user.SecurityPolicies.Add(new UserSecurityPolicy(policy));
                }

                await subscription.OnSubscribeAsync(new UserSecurityPolicySubscriptionContext(this, user));

                await Auditing.SaveAuditRecordAsync(
                    new UserAuditRecord(user, AuditedUserAction.SubscribeToPolicies, subscription.Policies));

                if (commitChanges)
                {
                    await EntitiesContext.SaveChangesAsync();
                }

                Diagnostics.Information($"User is now subscribed to '{subscription.SubscriptionName}'.");

                return(true);
            }
        }
예제 #6
0
 public async Task SaveAsync()
 {
     await context.SaveChangesAsync();
 }
예제 #7
0
        private static async Task Hash(string connectionString, bool whatIf)
        {
            using (var context = new EntitiesContext(connectionString, readOnly: whatIf))
            {
                var allCredentials = (IQueryable <Credential>)context.Set <Credential>();

                // Get all V1/V2 credentials that are active:
                // V1 credentials that have no expiration date, but were used in the last year
                // V1/V2 credentials that have a future expiration date

                var validLastUsed = DateTime.UtcNow - TimeSpan.FromDays(365);
                Expression <Func <Credential, bool> > predicate = x => (x.Type == CredentialTypes.ApiKey.V1 || x.Type == CredentialTypes.ApiKey.V2) &&
                                                                  ((x.Expires == null && x.LastUsed != null && x.LastUsed > validLastUsed) ||
                                                                   (x.Expires != null && x.Expires > DateTime.UtcNow));

                var activeCredentialsCount = allCredentials.Count(predicate);

                Console.WriteLine($"Found {activeCredentialsCount} active V1/V2 ApiKeys.");

                IList <Credential> batch;
                int  batchNumber = 1;
                bool failures    = false;

                do
                {
                    batch = allCredentials.Where(predicate).OrderBy(x => x.Key).Take(BatchSize).ToList();

                    if (batch.Count > 0)
                    {
                        Console.WriteLine($"Hashing batch {batchNumber}/{Math.Ceiling((double)activeCredentialsCount / (double)BatchSize)}. Batch size: {batch.Count}...");

                        foreach (var v1v2ApiKey in batch)
                        {
                            ApiKeyV3 hashedApiKey = null;

                            try
                            {
                                hashedApiKey = ApiKeyV3.CreateFromV1V2ApiKey(v1v2ApiKey.Value);

                                v1v2ApiKey.Type  = CredentialTypes.ApiKey.V3;
                                v1v2ApiKey.Value = hashedApiKey.HashedApiKey;
                            }
                            catch (ArgumentException e)
                            {
                                failures = true;
                                Console.WriteLine($"Failed to hash key: {v1v2ApiKey.Key} Type: {v1v2ApiKey.Type} Old value: {v1v2ApiKey.Value}. Reason: {e}");
                            }
                        }

                        if (!failures && !whatIf)
                        {
                            var stopwatch = Stopwatch.StartNew();
                            Console.WriteLine($"Saving batch {batchNumber} to DB...");
                            await context.SaveChangesAsync();

                            Console.WriteLine($"Saved changes to DB. Took: {stopwatch.Elapsed.TotalSeconds} seconds");
                        }
                        else
                        {
                            Console.WriteLine("Skipping DB save.");
                            allCredentials = allCredentials.Where(predicate).OrderBy(x => x.Key).Skip(batch.Count);
                        }

                        batchNumber++;
                    }
                }while (batch.Count > 0);
            }
        }
 private async Task CommitAsync()
 {
     await DbContext.SaveChangesAsync();
 }
예제 #9
0
 public async Task SaveAsync() => await _entitiesContext.SaveChangesAsync();
예제 #10
0
 public async Task SaveAsync()
 {
     await entityContext.SaveChangesAsync();
 }
예제 #11
0
 public async Task CreateAsync(Livro livro)
 {
     context.Livros.Add(livro);
     await context.SaveChangesAsync();
 }