protected async Task UserBumpManyAccountRevisionDates(ICollection <Guid> userIds) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var users = dbContext.Users.Where(u => userIds.Contains(u.Id)); await users.ForEachAsync(u => { dbContext.Attach(u); u.RevisionDate = DateTime.UtcNow; }); await dbContext.SaveChangesAsync(); } }
public async Task<ICollection<Grant>> GetManyAsync(string subjectId, string sessionId, string clientId, string type) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var query = from g in dbContext.Grants where g.SubjectId == subjectId && g.ClientId == clientId && g.SessionId == sessionId && g.Type == type select g; var grants = await query.ToListAsync(); return (ICollection<Grant>)grants; } }
public async Task RevokeToken() { using (var scope = ServiceScopeFactory.CreateScope()) { var service = scope.ServiceProvider.GetServices <IStrykerService>() .Where(s => s.GetType() == typeof(TwitchService)) .FirstOrDefault() as TwitchService; var endpoint = $"oauth2/revoke" + $"?client_id={ClientId}" + $"&token={AccessToken.AccessToken}"; await service?.Post(endpoint); } }
public void Execute(TestClass aTestClass) { if (ServiceScopeFactory == null) { ConfigureServiceProvider(); } aTestClass.RunCases(aCase => { using IServiceScope serviceScope = ServiceScopeFactory.CreateScope(); object instance = serviceScope.ServiceProvider.GetService(aTestClass.Type); Setup(instance); aCase.Execute(instance); }); }
public async Task <OrganizationUserOrganizationDetails> GetDetailsByUserAsync(Guid userId, Guid organizationId, OrganizationUserStatusType?status = null) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var view = new OrganizationUserOrganizationDetailsViewQuery(); var t = await(view.Run(dbContext)).ToArrayAsync(); var entity = await view.Run(dbContext) .FirstOrDefaultAsync(o => o.UserId == userId && o.OrganizationId == organizationId && (status == null || o.Status == status)); return(entity); } }
public async Task <IEnumerable <ProviderUserOrganizationDetails> > GetManyOrganizationDetailsByUserAsync(Guid userId, ProviderUserStatusType?status = null) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var view = new ProviderUserOrganizationDetailsViewQuery(); var query = from ou in view.Run(dbContext) where ou.UserId == userId && (status == null || ou.Status == status) select ou; var organizationUsers = await query.ToListAsync(); return(organizationUsers); } }
public async Task <ICollection <Core.Entities.TaxRate> > SearchAsync(int skip, int count) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var results = await dbContext.TaxRates .Skip(skip) .Take(count) .Where(t => t.Active) .OrderBy(t => t.Country).ThenByDescending(t => t.PostalCode) .ToListAsync(); return(Mapper.Map <List <Core.Entities.TaxRate> >(results)); } }
public virtual async Task ReplaceAsync(T obj) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var entity = await GetDbSet(dbContext).FindAsync(obj.Id); if (entity != null) { var mappedEntity = Mapper.Map <TEntity>(obj); dbContext.Entry(entity).CurrentValues.SetValues(mappedEntity); await dbContext.SaveChangesAsync(); } } }
public virtual async Task <T> CreateAsync(T obj) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); obj.SetNewId(); var entity = Mapper.Map <TEntity>(obj); await dbContext.AddAsync(entity); await dbContext.SaveChangesAsync(); obj.Id = entity.Id; return(obj); } }
public async Task DeleteByIdsOrganizationIdAsync(IEnumerable <Guid> ids, Guid organizationId) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var ciphers = from c in dbContext.Ciphers where c.OrganizationId == organizationId && ids.Contains(c.Id) select c; dbContext.RemoveRange(ciphers); await dbContext.SaveChangesAsync(); } await OrganizationUpdateStorage(organizationId); await UserBumpAccountRevisionDateByOrganizationId(organizationId); }
public async Task DeleteManyAsync(string subjectId, string sessionId, string clientId, string type) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var query = from g in dbContext.Grants where g.SubjectId == subjectId && g.ClientId == clientId && g.SessionId == sessionId && g.Type == type select g; dbContext.Remove(query); await dbContext.SaveChangesAsync(); } }
public async Task UpdateCollectionsForCiphersAsync(IEnumerable <Guid> cipherIds, Guid userId, Guid organizationId, IEnumerable <Guid> collectionIds) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var availibleCollections = from c in dbContext.Collections join o in dbContext.Organizations on c.OrganizationId equals o.Id join ou in dbContext.OrganizationUsers on o.Id equals ou.OrganizationId where ou.UserId == userId join cu in dbContext.CollectionUsers on ou.Id equals cu.OrganizationUserId into cu_g from cu in cu_g.DefaultIfEmpty() where !ou.AccessAll && cu.CollectionId == c.Id join gu in dbContext.GroupUsers on ou.Id equals gu.OrganizationUserId into gu_g from gu in gu_g.DefaultIfEmpty() where cu.CollectionId == null && !ou.AccessAll join g in dbContext.Groups on gu.GroupId equals g.Id into g_g from g in g_g.DefaultIfEmpty() join cg in dbContext.CollectionGroups on gu.GroupId equals cg.GroupId into cg_g from cg in cg_g.DefaultIfEmpty() where !g.AccessAll && cg.CollectionId == c.Id && (o.Id == organizationId && o.Enabled && ou.Status == OrganizationUserStatusType.Confirmed && (ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly)) select new { c, o, ou, cu, gu, g, cg }; var count = await availibleCollections.CountAsync(); if (await availibleCollections.CountAsync() < 1) { return; } var insertData = from collectionId in collectionIds from cipherId in cipherIds where availibleCollections.Select(x => x.c.Id).Contains(collectionId) select new EfModel.CollectionCipher { CollectionId = collectionId, CipherId = cipherId, }; await dbContext.AddRangeAsync(insertData); await UserBumpAccountRevisionDateByOrganizationId(organizationId); } }
private void Timer_Elapsed(object sender, System.EventArgs e) { try { using (var scope = ServiceScopeFactory.CreateScope()) { AsyncHelper.RunSync( () => DoWorkAsync(new PeriodicBackgroundWorkerContext(scope.ServiceProvider)) ); } } catch (Exception ex) { Logger.LogException(ex); } }
private void ConfirmSkippedMessages() { using (var scope = ServiceScopeFactory.CreateScope()) { foreach (var kvp in _periodicSkipJobs) { while (kvp.Value.Count > 0) { if (kvp.Value.TryTake(out var deliveryTag)) { OnMessageProcessed(scope, deliveryTag); } } } } }
public async Task <int> GetCountByProviderAsync(Guid providerId, string email, bool onlyRegisteredUsers) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var query = from pu in dbContext.ProviderUsers join u in dbContext.Users on pu.UserId equals u.Id into u_g from u in u_g.DefaultIfEmpty() where pu.ProviderId == providerId && ((!onlyRegisteredUsers && (pu.Email == email || u.Email == email)) || (onlyRegisteredUsers && u.Email == email)) select new { pu, u }; return(await query.CountAsync()); } }
public async Task <ICollection <TableModel.Organization> > SearchAsync(string name, string userEmail, bool?paid, int skip, int take) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); // TODO: more filters var organizations = await GetDbSet(dbContext) .Where(e => name == null || e.Name.StartsWith(name)) .OrderBy(e => e.Name) .Skip(skip).Take(take) .ToListAsync(); return(Mapper.Map <List <TableModel.Organization> >(organizations)); } }
public async Task <ICollection <Core.Entities.GroupUser> > GetManyGroupUsersByOrganizationIdAsync(Guid organizationId) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var query = from gu in dbContext.GroupUsers join g in dbContext.Groups on gu.GroupId equals g.Id where g.OrganizationId == organizationId select gu; var groupUsers = await query.ToListAsync(); return(Mapper.Map <List <Core.Entities.GroupUser> >(groupUsers)); } }
public async Task UpdateRenewalReminderDateAsync(Guid id, DateTime renewalReminderDate) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var user = new EFModel.User { Id = id, RenewalReminderDate = renewalReminderDate }; var set = GetDbSet(dbContext); set.Attach(user); dbContext.Entry(user).Property(e => e.RenewalReminderDate).IsModified = true; await dbContext.SaveChangesAsync(); } }
protected async Task UserBumpAccountRevisionDateByOrganizationId(Guid organizationId) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var query = new UserBumpAccountRevisionDateByOrganizationIdQuery(organizationId); var users = query.Run(dbContext); await users.ForEachAsync(e => { dbContext.Attach(e); e.RevisionDate = DateTime.UtcNow; }); await dbContext.SaveChangesAsync(); } }
public async Task UpdateUsersAsync(Guid id, IEnumerable <SelectionReadOnly> users) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var procedure = new CollectionUserUpdateUsersQuery(id, users); var updateData = await procedure.Update.BuildInMemory(dbContext); dbContext.UpdateRange(updateData); var insertData = await procedure.Insert.BuildInMemory(dbContext); await dbContext.AddRangeAsync(insertData); dbContext.RemoveRange(await procedure.Delete.Run(dbContext).ToListAsync()); } }
public async Task <ICollection <DataModel.OrganizationAbility> > GetManyAbilitiesAsync() { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); return(await GetDbSet(dbContext) .Select(e => new DataModel.OrganizationAbility { Enabled = e.Enabled, Id = e.Id, Use2fa = e.Use2fa, UseEvents = e.UseEvents, UsersGetPremium = e.UsersGetPremium, Using2fa = e.Use2fa && e.TwoFactorProviders != null, }).ToListAsync()); } }
public async Task <CollectionCipher> CreateAsync(CollectionCipher obj) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var entity = Mapper.Map <EfModel.CollectionCipher>(obj); dbContext.Add(entity); await dbContext.SaveChangesAsync(); var organizationId = (await dbContext.Ciphers.FirstOrDefaultAsync(c => c.Id.Equals(obj.CipherId))).OrganizationId; if (organizationId.HasValue) { await UserBumpAccountRevisionDateByCollectionId(obj.CollectionId, organizationId.Value); } return(obj); } }
public virtual async Task <ClaimsPrincipal> CreateAsync() { using (var scope = ServiceScopeFactory.CreateScope()) { var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity()); var context = new AbpClaimsPrincipalContributorContext(claimsPrincipal, scope.ServiceProvider); foreach (var contributorType in Options.Contributors) { var contributor = (IAbpClaimsPrincipalContributor)scope.ServiceProvider.GetRequiredService(contributorType); await contributor.ContributeAsync(context); } return(claimsPrincipal); } }
public async Task UpdateCollectionsForAdminAsync(Guid cipherId, Guid organizationId, IEnumerable <Guid> collectionIds) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var availableCollectionsCte = from c in dbContext.Collections where c.OrganizationId == organizationId select c; var target = from cc in dbContext.CollectionCiphers where cc.CipherId == cipherId select new { cc.CollectionId, cc.CipherId }; var source = collectionIds.Select(x => new { CollectionId = x, CipherId = cipherId }); var merge1 = from t in target join s in source on t.CollectionId equals s.CollectionId into s_g from s in s_g.DefaultIfEmpty() where t.CipherId == s.CipherId select new { t, s }; var merge2 = from s in source join t in target on s.CollectionId equals t.CollectionId into t_g from t in t_g.DefaultIfEmpty() where t.CipherId == s.CipherId select new { t, s }; var union = merge1.Union(merge2).Distinct(); var insert = union .Where(x => x.t == null && collectionIds.Contains(x.s.CollectionId)) .Select(x => new EfModel.CollectionCipher { CollectionId = x.s.CollectionId, CipherId = x.s.CipherId, }); var delete = union .Where(x => x.s == null && x.t.CipherId == cipherId) .Select(x => new EfModel.CollectionCipher { CollectionId = x.t.CollectionId, CipherId = x.t.CipherId, }); await dbContext.AddRangeAsync(insert); dbContext.RemoveRange(delete); await dbContext.SaveChangesAsync(); await UserBumpAccountRevisionDateByOrganizationId(organizationId); } }
public virtual async Task SeedAsync(TMTTenantDataSeedContext context) { using (var scope = ServiceScopeFactory.CreateScope()) { foreach (var contributorType in Options.Contributors) { if (contributorType.Name.StartsWith("TMT")) { var contributor = (ITMTDataSeedContributor)scope .ServiceProvider .GetRequiredService(contributorType); await contributor.SeedAsync(context); } } } }
private void Timer_Elapsed(object sender, System.EventArgs e) { using (var scope = ServiceScopeFactory.CreateScope()) { try { DoWork(new PeriodicBackgroundWorkerContext(scope.ServiceProvider)); } catch (Exception ex) { var exceptionNotifier = scope.ServiceProvider.GetRequiredService <IExceptionNotifier>(); AsyncHelper.RunSync(() => exceptionNotifier.NotifyAsync(new ExceptionNotificationContext(ex))); Logger.LogException(ex); } } }
public void CreateScope_ShouldCreateAndUseChildScope() { // Arrange var parentScope = new ContainerBuilder().Build(); ILifetimeScope capturedChildScope = null; parentScope.ChildLifetimeScopeBeginning += (_, e) => capturedChildScope = e.LifetimeScope; var factory = new ServiceScopeFactory(parentScope); // Act var scope = factory.CreateScope(); // Assert scope.Should().NotBeNull(); capturedChildScope.Should().NotBeNull(); scope.GetService(typeof(ILifetimeScope)).Should().Be(capturedChildScope); }
private async Task DoWorkAsync() { using (var scope = ServiceScopeFactory.CreateScope()) { try { await DoWorkAsync(new PeriodicBackgroundWorkerContext(scope.ServiceProvider)); } catch (Exception ex) { await scope.ServiceProvider .GetRequiredService <IExceptionNotifier>() .NotifyAsync(new ExceptionNotificationContext(ex)); Logger.LogException(ex); } } }
protected async Task UserUpdateKeys(User user) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var entity = await dbContext.Users.FindAsync(user.Id); if (entity == null) { return; } entity.SecurityStamp = user.SecurityStamp; entity.Key = user.Key; entity.PrivateKey = user.PrivateKey; entity.RevisionDate = DateTime.UtcNow; await dbContext.SaveChangesAsync(); } }
public async Task <ICollection <SelectionReadOnly> > GetManyUsersByIdAsync(Guid id) { using (var scope = ServiceScopeFactory.CreateScope()) { var dbContext = GetDatabaseContext(scope); var query = from cu in dbContext.CollectionUsers where cu.CollectionId == id select cu; var collectionUsers = await query.ToListAsync(); return(collectionUsers.Select(cu => new SelectionReadOnly { Id = cu.OrganizationUserId, ReadOnly = cu.ReadOnly, HidePasswords = cu.HidePasswords, }).ToArray()); } }