예제 #1
0
        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);
        }
예제 #3
0
        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
                          ));
        }
예제 #4
0
        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
                              ));
            }
        }