public MongoAuthenticatedUserAccessor(ICache cache, ICacheKey cacheKey, IReadOnlyCollection <ISubAccessor> subAccessors, IMongoCollection <User> usersCollection) { _authenticatedUserFactory = () => { var sub = subAccessors.FirstOrDefault(sa => sa.Sub != null)?.Sub; if (sub == null) { throw new UnknownSubException(); } var user = cache.GetOrSet ( cacheKey.FindAuthenticatedUser(sub), () => usersCollection.Find(m => m.Sub == sub) .SortByDescending(u => u.Id) .FirstOrDefault() ); if (user == null) { throw new AuthenticatedUserNotFoundException(sub); } return(user); }; _authenticatedUser = new Lazy <User>(_authenticatedUserFactory); }
public Task HandleAsync(TeamInvitationAcceptedEvent @event, CancellationToken cancellationToken) { _cache.Remove(_cacheKey.FindAuthenticatedUser ( _authenticatedUserAccessor.AuthenticatedUser.Sub )); return(Task.CompletedTask); }
public async Task HandleAsync(TeamMemberRemovedEvent @event, CancellationToken cancellationToken) { var sub = await _usersCollection.Find(m => m.Id == @event.MemberId) .Project(m => m.Sub) .SingleAsync(cancellationToken); _cache.Remove(_cacheKey.FindAuthenticatedUser ( sub )); }
public async Task HandleAsync(TeamDeletedEvent @event, CancellationToken cancellationToken) { var subs = await _usersCollection.Find(m => m.Teams.Any(t => t.Id == @event.TeamId)) .Project(m => m.Sub) .ToListAsync(cancellationToken); foreach (var sub in subs) { _cache.Remove(_cacheKey.FindAuthenticatedUser ( sub )); } }