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));
        }
예제 #2
0
        public ActionResult <User> Get(Guid tenantId, Guid userId)
        {
            var unverifiedTenantId = UnverifiedTenantId.From(tenantId);
            var userIdVal          = UserId.From(userId);

            try
            {
                var knownTenantId = TenantIdExistsQueryHandler.Execute(new TenantIdExistsQuery(unverifiedTenantId));

                try
                {
                    var user = UserQueryHandler.Execute(new UserQuery(userIdVal, knownTenantId));

                    return(new JsonResult(user));
                }
                catch (Exception)
                {
                    return(NotFound());
                }
            }
            catch (Exception)
            {
                return(UnprocessableEntity());
            }
        }
예제 #3
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));
        }
예제 #4
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());
        }
예제 #5
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));
        }
예제 #6
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));
        }
예제 #7
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));
        }
예제 #8
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));
        }
예제 #9
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());
        }
예제 #10
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);
        }
        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));
        }
        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));
        }
예제 #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);
        }
예제 #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));
        }
예제 #15
0
        public async Task HandleAsync(JoinGroupCommand command)
        {
            var invitationCode = InvitationCode.From(command.Code);
            var invitation     = await _invitationRepository.GetAsync(invitationCode) ?? throw new InvalidInvitationCodeException(command.Code);

            var group = await _groupRepository.GetAsync(invitation.GroupId) ?? throw new GroupNotFoundException(invitation.GroupId);

            invitation.Use(UserId.From(command.UserId), group, _clock);
        }
예제 #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);
        }
        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)));
        }
예제 #18
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);
        }
예제 #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);
        }
예제 #20
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());
        }
예제 #21
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);
        }
예제 #22
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);
        }
예제 #23
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();
            }
        }
예제 #24
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());
        }
예제 #25
0
        private static void Build(EntityTypeBuilder <Group> group)
        {
            group.ToTable("Groups");
            group.HasKey(x => x.Id)
            .HasName("PK_Groups");
            group.Property(x => x.Id)
            .HasConversion(x => x.Value, x => GroupId.From(x))
            .ValueGeneratedNever()
            .HasColumnName("GroupId");
            group.OwnsOne(
                x => x.Name,
                n =>
            {
                n.Property(x => x.Value).HasColumnName(nameof(Group.Name))
                .IsRequired();
            }
                );
            group.Property(x => x.CreatedAt).IsRequired();

            group.Metadata.FindNavigation(nameof(Group.Members)).SetPropertyAccessMode(PropertyAccessMode.Field);
            group.OwnsMany(
                x => x.Members,
                m =>
            {
                m.ToTable("Members");
                m.HasKey(x => x.Id)
                .HasName("PK_Members");
                m.Property(x => x.Id)
                .HasConversion(x => x.Value, x => MemberId.From(x))
                .ValueGeneratedNever()
                .IsRequired();
                m.Property(x => x.UserId)
                .HasConversion(x => x.Value, x => UserId.From(x))
                .ValueGeneratedNever()
                .IsRequired();
                m.Property(x => x.IsOwner).IsRequired();
                m.Property(x => x.Name)
                .HasConversion(x => x.Value, x => MemberName.Create(x))
                .IsRequired();
                m.OwnsOne(
                    u => u.Avatar,
                    b =>
                {
                    b.Property(x => x !.Value).IsRequired().HasColumnName("Avatar");
                }
예제 #26
0
        public async Task <IEnumerable <PermissionOverrideDto> > HandleAsync(GetChannelMemberPermissionOverridesQuery query)
        {
            var channels = await _context.Channels.ToListAsync();

            return(await _context.Channels
                   .AsNoTracking()
                   .Where(x => x.GroupId == GroupId.From(query.GroupId))
                   .Where(x => x.Id == ChannelId.From(query.ChannelId))
                   .SelectMany(x => x.MemberPermissionOverrides)
                   .Where(x => x.UserIdOfMember == UserId.From(query.MemberUserId))
                   .Select(
                       x => new PermissionOverrideDto
            {
                Permission = x.Permission.Name,
                Type = x.Type == OverrideType.Allow ? OverrideTypeDto.Allow : OverrideTypeDto.Deny
            }
                       ).ToListAsync());
        }
        public async Task HandleAsync(GenerateInvitationCommand command)
        {
            var groupId = GroupId.From(command.GroupId);
            var group   = await _groupRepository.GetAsync(groupId) ?? throw new GroupNotFoundException(groupId);


            var expirationTime = GetExpirationTime(command);
            var usageCounter   = GetUsageCounter(command);

            var invitation = await group.GenerateInvitation(
                UserId.From(command.UserId),
                InvitationId.From(command.InvitationId),
                expirationTime,
                usageCounter,
                _uniqueInvitationCodeRule
                );

            await _invitationRepository.AddAsync(invitation);
        }
예제 #28
0
        public async Task <IEnumerable <GroupDto> > HandleAsync(GetGroupsQuery query)
        {
            var groups = _context.Groups.AsNoTracking()
                         .Include(x => x.Members)
                         .Where(x => x.Members.Select(m => m.UserId).Any(x => x == UserId.From(query.UserId)));

            if (query.GroupId.HasValue)
            {
                groups = groups.Where(x => x.Id == GroupId.From(query.GroupId.Value));
            }

            return(await groups.Select(
                       x => new GroupDto
            {
                GroupId = x.Id.Value,
                Name = x.Name.Value,
                CreatedAt = x.CreatedAt,
                OwnerId = x.Members.First(m => m.IsOwner).UserId.Value
            }
                       ).ToListAsync());
        }
 public static User ToUser(this UserState state)
 => User.From(
     UserId.From(state.Id),
     Name.From(state.Name),
     VersionId.From(BitConverter.ToInt32(state.RowVersion)));
예제 #30
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);
        }