Exemplo n.º 1
0
        public async Task HandleAsync(KickMemberCommand command)
        {
            var groupId = GroupId.From(command.GroupId);
            var group   = await _groupRepository.GetAsync(groupId) ?? throw new GroupNotFoundException(groupId);

            group.KickMember(UserId.From(command.UserId), UserId.From(command.UserIdOfMember));
        }
        public async Task HandleAsync(RemovePermissionFromRoleCommand command)
        {
            var groupId = GroupId.From(command.GroupId);
            var group   = await _groupRepository.GetAsync(groupId) ?? throw new GroupNotFoundException(groupId);

            group.RemovePermissionFromRole(UserId.From(command.UserId), RoleId.From(command.RoleId), Permission.FromName(command.PermissionName));
        }
Exemplo n.º 3
0
        public async Task HandleAsync(MoveUpRoleInHierarchyCommand command)
        {
            var groupId = GroupId.From(command.GroupId);
            var group   = await _groupRepository.GetAsync(groupId) ?? throw new GroupNotFoundException(groupId);

            group.MoveUpRole(UserId.From(command.UserId), RoleId.From(command.RoleId));
        }
        public async Task HandleAsync(MessageCreatedEvent @event)
        {
            var group = await _context.Groups.AsNoTracking()
                        .Where(g => g.Id == GroupId.From(@event.GroupId))
                        .FirstOrDefaultAsync();

            var channel = await _context.Channels.AsNoTracking()
                          .Where(c => c.GroupId == GroupId.From(@event.GroupId) && c.Id == ChannelId.From(@event.ChannelId))
                          .FirstOrDefaultAsync();

            var message = await _context.GroupMessages
                          .AsNoTracking()
                          .Where(x => x.GroupId == GroupId.From(@event.GroupId))
                          .Where(x => x.ChannelId == @event.ChannelId)
                          .Where(x => x.MessageId == MessageId.From(@event.MessageId))
                          .FirstOrDefaultAsync();

            if (message is null || group is null || channel is null)
            {
                return;
            }
            var allowedMembers = group.Members
                                 .Where(m => group.CanAccessMessages(m.UserId, channel))
                                 .Select(m => m.UserId.Value.ToString("N"))
                                 .ToList();

            await _hubContext.Clients.Users(allowedMembers).OnMessageCreated(message);
        }
Exemplo n.º 5
0
        public async Task HandleAsync(LeaveGroupCommand command)
        {
            var groupId = GroupId.From(command.GroupId);
            var group   = await _groupRepository.GetAsync(groupId) ?? throw new GroupNotFoundException(groupId);

            group.LeaveGroup(UserId.From(command.UserId));
        }
Exemplo n.º 6
0
        public async Task <IEnumerable <MemberDto> > HandleAsync(GetMembersQuery query)
        {
            var members = _context.Groups.AsNoTracking()
                          .Where(x => x.Id == GroupId.From(query.GroupId))
                          .Where(x => x.Members.Select(m => m.UserId).Any(id => id == UserId.From(query.UserId)))
                          .SelectMany(x => x.Members);

            if (query.IsOwner)
            {
                members = members.Where(x => x.IsOwner);
            }

            if (query.UserIdOfMember.HasValue)
            {
                members = members.Where(x => x.UserId == UserId.From(query.UserIdOfMember.Value));
            }
            return(await members
                   .Select(
                       x => new MemberDto
            {
                GroupId = query.GroupId,
                UserId = x.UserId.Value,
                MemberId = x.Id.Value,
                IsOwner = x.IsOwner,
                CreatedAt = x.CreatedAt,
                Name = x.Name.Value
            }
                       )
                   .ToListAsync());
        }
Exemplo n.º 7
0
        public async Task HandleAsync(ChangeRoleNameCommand command)
        {
            var group = await _groupRepository.GetAsync(GroupId.From(command.GroupId)) ??
                        throw new GroupNotFoundException(GroupId.From(command.GroupId));

            group.RenameRole(UserId.From(command.UserId), RoleId.From(command.RoleId), RoleName.Create(command.Name));
        }
Exemplo n.º 8
0
        public async Task HandleAsync(AddRoleCommand command)
        {
            var groupId = GroupId.From(command.GroupId);
            var group   = await _groupRepository.GetAsync(groupId) ?? throw new GroupNotFoundException(groupId);

            group.AddRole(UserId.From(command.UserId), RoleId.From(command.RoleId), RoleName.Create(command.Name));
        }
Exemplo n.º 9
0
        public async Task <IEnumerable <GroupMessageView> > HandleAsync(GetMessagesQuery query)
        {
            var group = await _groupsContext.Groups.AsNoTracking()
                        .Where(g => g.Members.Select(m => m.UserId).Any(id => id == UserId.From(query.UserId)))
                        .FirstOrDefaultAsync(g => g.Id == GroupId.From(query.GroupId));

            var channel = await _groupsContext.Channels.AsNoTracking().FirstOrDefaultAsync(
                c => c.GroupId == GroupId.From(query.GroupId) && c.Id == ChannelId.From(query.ChannelId)
                );

            if (group is null || channel is null)
            {
                return(new List <GroupMessageView>());
            }
            if (!group.CanAccessMessages(UserId.From(query.UserId), channel))
            {
                return(new List <GroupMessageView>());
            }
            var groupMessageDtos = await _groupsContext.GroupMessages
                                   .Where(m => m.GroupId == query.GroupId &&
                                          m.ChannelId == query.ChannelId)
                                   .ToListAsync();

            return(groupMessageDtos);
        }
Exemplo n.º 10
0
        public async Task <ChannelDto> HandleAsync(GetGroupChannelQuery query)
        {
            var group = await _context.Groups.AsNoTracking()
                        .Where(g => g.Id == GroupId.From(query.GroupId))
                        .Where(g => g.Members.Select(m => m.UserId).Any(id => id == UserId.From(query.UserId)))
                        .FirstOrDefaultAsync();

            if (group is null)
            {
                return(null);
            }

            var channels = await _context.Channels.AsNoTracking()
                           .Where(
                x => _context.Groups.Where(g => g.Id == query.GroupId).SelectMany(g => g.Members).Select(m => m.UserId)
                .Any(u => u == UserId.From(query.UserId))
                )
                           .Where(x => x.GroupId == GroupId.From(query.GroupId))
                           .Where(x => x.Id == ChannelId.From(query.ChannelId))
                           .ToListAsync();

            return(channels.Where(c => group.CanAccessChannel(UserId.From(query.UserId), c))
                   .Select(
                       x => new ChannelDto
            {
                ChannelId = x.Id.Value,
                GroupId = x.GroupId.Value,
                ChannelName = x.Name.Value
            }
                       ).FirstOrDefault());
        }
        public async Task HandleAsync(AssignRoleToMemberCommand command)
        {
            var groupId = GroupId.From(command.GroupId);
            var group   = await _groupRepository.GetAsync(groupId) ?? throw new GroupNotFoundException(groupId);

            group.AssignRole(UserId.From(command.UserId), UserId.From(command.UserIdOfMember), RoleId.From(command.RoleId));
        }
        public async Task HandleAsync(RemoveOverrideForMemberCommand command)
        {
            var group = await _groupRepository.GetAsync(GroupId.From(command.GroupId)) ?? throw new GroupNotFoundException(GroupId.From(command.GroupId));

            var channel = await _channelRepository.GetAsync(group.Id, ChannelId.From(command.ChannelId));

            group.RemoveOverride(UserId.From(command.UserId), channel, UserId.From(command.MemberUserId), Permission.FromName(command.Permission));
        }
Exemplo n.º 13
0
        public async Task HandleAsync(RemoveGroupCommand command)
        {
            var group = await _groupRepository.GetAsync(GroupId.From(command.GroupId)) ?? throw new GroupNotFoundException(GroupId.From(command.GroupId));

            group.Remove(UserId.From(command.UserId));

            await _groupRepository.RemoveAsync(group);
        }
Exemplo n.º 14
0
        public async Task HandleAsync(DenyPermissionForRoleCommand command)
        {
            var group = await _groupRepository.GetAsync(GroupId.From(command.GroupId)) ?? throw new GroupNotFoundException(GroupId.From(command.GroupId));

            var channel = await _channelRepository.GetAsync(group.Id, ChannelId.From(command.ChannelId)) ?? throw new ChannelNotFoundException(ChannelId.From(command.ChannelId));

            group.DenyPermission(UserId.From(command.UserId), channel, RoleId.From(command.RoleId), Permission.FromName(command.Permission));
        }
        public async Task HandleAsync(ChannelRemovedEvent @event)
        {
            var group = await _groupRepository.GetAsync(GroupId.From(@event.GroupId));

            var members        = group.Members;
            var allowedMembers = members.Select(m => m.UserId.Value.ToString("N")).ToList();
            await _hubContext.Clients.Users(allowedMembers)
            .OnChannelRemoved(new ChannelDto(@event.GroupId, @event.ChannelId, @event.ChannelName));
        }
Exemplo n.º 16
0
        public async Task HandleAsync(AddGroupMember command)
        {
            var groupId = GroupId.From(command.GroupId);
            var group   = await _groupRepository.GetAsync(groupId) ?? throw new GroupNotFoundException(groupId);

            var user = await _moduleClient.GetAsync <UserDto>("/identity/me", new MeQuery(command.UserIdOfMember));

            var avatar = await GetAvatar(user.Avatar);

            group.AddMember(UserId.From(command.UserIdOfMember), MemberName.Create(user.Nickname), avatar, _clock);
        }
Exemplo n.º 17
0
        public async Task HandleAsync(RevokeInvitationCommand command)
        {
            var group = await _groupRepository.GetAsync(GroupId.From(command.GroupId)) ??
                        throw new GroupNotFoundException(GroupId.From(command.GroupId));

            var invitation = await _invitationRepository.GetAsync(InvitationId.From(command.InvitationId));

            group.RevokeInvitation(UserId.From(command.UserId), invitation);

            await _invitationRepository.RemoveAsync(invitation);
        }
        public async Task HandleAsync(UpdateRolePermissionOverrideCommand command)
        {
            var group = await _groupRepository.GetAsync(GroupId.From(command.GroupId)) ??
                        throw new GroupNotFoundException(GroupId.From(command.GroupId));

            var channel = await _channelRepository.GetAsync(group.Id, ChannelId.From(command.ChannelId)) ??
                          throw new ChannelNotFoundException(ChannelId.From(command.ChannelId));

            group.UpdateOverrides(UserId.From(command.UserId), channel, RoleId.From(command.RoleId),
                                  command.Overrides.Select(o => (Permission.FromName(o.Permission), (PermissionOverrideType)o.Type)));
        }
Exemplo n.º 19
0
        public async Task HandleAsync(RenameChannelCommand command)
        {
            var group = await _groupRepository.GetAsync(GroupId.From(command.GroupId)) ??
                        throw new GroupNotFoundException(GroupId.From(command.GroupId));

            var channel = await _channelRepository.GetAsync(group.Id, ChannelId.From(command.ChannelId)) ??
                          throw new ChannelNotFoundException(ChannelId.From(command.ChannelId));

            var name = ChannelName.Create(command.Name);

            group.RenameChannel(UserId.From(command.UserId), channel, name);
        }
        public async Task HandleAsync(InvitationCreatedEvent @event)
        {
            var group = await _groupsContext.Groups.AsNoTracking()
                        .Where(g => g.Id == GroupId.From(@event.GroupId))
                        .FirstOrDefaultAsync();

            var users = group.Members.Where(m => group.CanAccessInvitations(m.UserId))
                        .Select(u => u.UserId.Value.ToString("N"))
                        .ToList();

            await _hubContext.Clients.Users(users).OnInvitationCreated(new InvitationDto(@event.GroupId, @event.InvitationId, @event.Code, @event.ExpirationTime, @event.UsageCounter));
        }
Exemplo n.º 21
0
        public async Task <InvitationDto> HandleAsync(GetInvitationQuery query)
        {
            var group = await _context.Groups.AsNoTracking().SingleOrDefaultAsync(x => x.Id == GroupId.From(query.GroupId));

            if (!group?.CanAccessInvitations(UserId.From(query.UserId)) ?? false)
            {
                return(null);
            }
            var result = await _context.Invitations
                         .Where(x => _context.Groups.Where(g => g.Id == GroupId.From(query.GroupId)).SelectMany(g => g.Members).Any(m => m.UserId == UserId.From(query.UserId)))
                         .Where(x => x.GroupId == GroupId.From(query.GroupId))
                         .Where(x => x.Id == InvitationId.From(query.InvitationId))
                         .Select(
                x => new
            {
                GroupId            = x.GroupId.Value,
                InvitationId       = x.Id.Value,
                Code               = x.InvitationCode.Value,
                ExpirationTimeType =
                    EF.Property <ExpirationTimeType>(EF.Property <ExpirationTimeContainer>(x, "_expirationTime"), "_type"),
                ExpirationStart =
                    EF.Property <DateTimeOffset>(EF.Property <ExpirationTimeContainer>(x, "_expirationTime"), "_start"),
                ExpirationPeriod =
                    EF.Property <TimeSpan?>(EF.Property <ExpirationTimeContainer>(x, "_expirationTime"), "_period"),
                UsageCounterType =
                    EF.Property <UsageCounterType>(EF.Property <UsageCounterContainer>(x, "_usageCounter"), "_type"),
                UsageCounter =
                    EF.Property <int?>(EF.Property <UsageCounterContainer>(x, "_usageCounter"), "_value"),
            }
                ).ToListAsync();

            return(result.Select(
                       x => new InvitationDto
            {
                GroupId = x.GroupId,
                InvitationId = x.InvitationId,
                Code = x.Code,
                ExpirationTime = new ExpirationTimeDto
                {
                    Type = (ExpirationTimeTypeDto)(int)x.ExpirationTimeType,
                    Start = x.ExpirationStart,
                    Period = x.ExpirationPeriod
                },
                UsageCounter = new UsageCounterDto
                {
                    Type = (UsageCounterTypeDto)(int)x.UsageCounterType,
                    Value = x.UsageCounter
                }
            }
                       ).FirstOrDefault());
        }
Exemplo n.º 22
0
        public async Task HandleAsync(CreateChannelCommand command)
        {
            var groupId = GroupId.From(command.GroupId);
            var group   = await _groupRepository.GetAsync(groupId) ?? throw new GroupNotFoundException(groupId);

            if (await _channelRepository.ExistsAsync(group.Id, ChannelId.From(command.ChannelId)))
            {
                throw new ChannelAlreadyExistsException(command.ChannelId);
            }

            var channel = group.CreateChannel(UserId.From(command.UserId), ChannelId.From(command.ChannelId), ChannelName.Create(command.ChannelName));

            await _channelRepository.AddAsync(channel);
        }
        public async Task HandleAsync(RoleAddedToMemberEvent @event)
        {
            var members = (await _groupsContext.Groups
                           .AsNoTracking()
                           .Where(g => g.Id == GroupId.From(@event.GroupId))
                           .SelectMany(g => g.Members)
                           .Select(m => m.UserId)
                           .ToListAsync())
                          .Select(id => id.Value.ToString("N"))
                          .ToList();
            await _contract.Clients.Users(members).OnRoleAddedToMember(new MemberRoleDto(@event.UserId, @event.MemberId, new RoleDto(@event.GroupId, @event.RoleId, @event.RoleName, @event.RolePriority)));

            await _contract.Clients.Users(members).OnAllowedActionsChanged(new AllowedActionDto(@event.GroupId));
        }
        public async Task HandleAsync(ChannelMemberPermissionOverridesChangedEvent @event)
        {
            var members = (await _groupsContext.Groups
                           .AsNoTracking()
                           .Where(g => g.Id == GroupId.From(@event.GroupId))
                           .SelectMany(g => g.Members)
                           .Select(m => m.UserId)
                           .ToListAsync())
                          .Select(id => id.Value.ToString("N"))
                          .ToList();

            await _contract.Clients.Users(members).OnMemberPermissionOverridesChanged(@event);

            await _contract.Clients.Users(members).OnAllowedActionsChanged(new AllowedActionDto(@event.GroupId));
        }
Exemplo n.º 25
0
        public async Task HandleAsync(SendMessageCommand command)
        {
            var group = await _groupRepository.GetAsync(GroupId.From(command.GroupId)) ?? throw new GroupNotFoundException(GroupId.From(command.GroupId));

            var channel = await _channelRepository.GetAsync(group.Id, ChannelId.From(command.ChannelId)) ?? throw new ChannelNotFoundException(ChannelId.From(command.ChannelId));

            if (await _messageRepository.ExistsAsync(ChannelId.From(command.ChannelId), MessageId.From(command.MessageId)))
            {
                throw new MessageAlreadyExistsException(command.MessageId);
            }

            var message = group.SendMessage(UserId.From(command.UserId), channel, MessageId.From(command.MessageId), MessageContent.Create(command.Content), _clock);

            await _messageRepository.AddAsync(message);
        }
Exemplo n.º 26
0
 public async Task <IEnumerable <PermissionOverrideDto> > HandleAsync(GetChannelRolePermissionOverridesQuery query)
 {
     return(await _context.Channels
            .AsNoTracking()
            .Where(x => x.GroupId == GroupId.From(query.GroupId))
            .Where(x => x.Id == ChannelId.From(query.ChannelId))
            .SelectMany(x => x.RolePermissionOverrides)
            .Where(x => x.RoleId == RoleId.From(query.RoleId))
            .Select(
                x => new PermissionOverrideDto
     {
         Permission = x.Permission.Name,
         Type = x.Type == OverrideType.Allow ? OverrideTypeDto.Allow : OverrideTypeDto.Deny
     }
                ).ToListAsync());
 }
        public async Task HandleAsync(PermissionRemovedFromRoleEvent @event)
        {
            var members = (await _groupsContext.Groups
                           .AsNoTracking()
                           .Where(g => g.Id == GroupId.From(@event.GroupId))
                           .SelectMany(g => g.Members)
                           .Select(m => m.UserId)
                           .ToListAsync())
                          .Select(id => id.Value.ToString("N"))
                          .ToList();
            await _contract.Clients.Users(members).OnPermissionRemovedFromRole(
                new RolePermissionDto(@event.GroupId, @event.RoleId, new PermissionDto(@event.PermissionName, @event.PermissionValue))
                );

            await _contract.Clients.Users(members).OnAllowedActionsChanged(new AllowedActionDto(@event.GroupId));
        }
Exemplo n.º 28
0
        public async Task HandleAsync(UpdateRolePermissionsCommand command)
        {
            var groupId = GroupId.From(command.GroupId);
            var group   = await _groupRepository.GetAsync(groupId) ?? throw new GroupNotFoundException(groupId);

            var actions = command.Permissions.Select(
                x => x.IsOn
                    ? (Action)(() => group.AddPermissionToRole(UserId.From(command.UserId), RoleId.From(command.RoleId), Permission.FromName(x.Permission)))
                    : () => group.RemovePermissionFromRole(UserId.From(command.UserId), RoleId.From(command.RoleId), Permission.FromName(x.Permission))
                );

            foreach (var action in actions)
            {
                action.Invoke();
            }
        }
Exemplo n.º 29
0
        public async Task HandleAsync(CreateGroupCommand command)
        {
            var groupId = GroupId.From(command.GroupId);

            if (await _groupRepository.ExistsAsync(groupId))
            {
                throw new GroupAlreadyExistsException(groupId);
            }

            var me = await _client.GetAsync <UserDto>("/identity/me", new MeQuery(command.UserId));

            var avatar = await GetAvatar(me.Avatar);

            var group = Domain.Entities.Group.Create(groupId, GroupName.Create(command.GroupName), UserId.From(command.UserId), MemberName.Create(me.Nickname), avatar, _clock);

            await _groupRepository.AddAsync(group);
        }
Exemplo n.º 30
0
        public async Task <IEnumerable <RoleDto> > HandleAsync(GetMemberRolesQuery query)
        {
            var memberRoleIds = await _context.Groups
                                .AsNoTracking()
                                .Where(x => x.Id == GroupId.From(query.GroupId))
                                .SelectMany(x => x.Members)
                                .Where(x => x.UserId == UserId.From(query.UserIdOfMember))
                                .SelectMany(x => x.Roles).ToListAsync();

            return(await _context.Groups.AsNoTracking()
                   .Where(x => x.Id == GroupId.From(query.GroupId))
                   .SelectMany(x => x.Roles)
                   .Where(x => memberRoleIds.Contains(x.Id))
                   .OrderByDescending(x => x.Priority)
                   .Select(x => new RoleDto(query.GroupId, x.Id.Value, x.Name.Value, x.Priority.Value))
                   .ToListAsync());
        }