Example #1
0
        public async Task <GroupCreation> AttachFile(int groupId, int userId, GroupCreation groupCreation)
        {
            GroupUserEntity userGroup = await DbContext.GroupsUsersRepo.GetByUserIdAndGroupId(userId, groupId);

            if (userGroup == null)
            {
                ThrowHttpResponseException(System.Net.HttpStatusCode.Unauthorized, "Access is denied.");
            }

            ValidateUserAsAuthenticated(userGroup.UserId);

            CreationEntity creation = await DbContext.CreationsRepo.GetSingleOrDefaultAsync(groupCreation.CreationId);

            if (creation == null)
            {
                ThrowHttpResponseException(System.Net.HttpStatusCode.NotFound, string.Format("Creation with id {0} not found.", groupCreation.CreationId));
            }

            GroupCreationEntity groupCreationEntity = await DbContext.GroupCreationsRepo.CreateAsync(
                new GroupCreationEntity()
            {
                CreationId = creation.Id,
                GroupId    = groupCreation.GroupId,
            }).ConfigureAwait(false);

            return(groupCreationEntity.ToContract());
        }
        private Result RemoveGroupRole(string userId, string roleId)
        {
            BaseSpecification <GroupUserEntity> baseSpecification = new BaseSpecification <GroupUserEntity>();

            baseSpecification.AddFilter(x => x.UserId == userId);
            baseSpecification.AddFilter(x => x.RoleId == roleId);

            GroupUserEntity groupUser = _groupUserRepository.SingleOrDefault(baseSpecification);

            if (groupUser == null)
            {
                _logger.LogError($"No GroupUser. UserId {userId}, roleId {roleId}");
                return(Result.Fail("no_group_user", "No GroupUser"));
            }

            groupUser.UpdateRole(null);

            bool updateResult = _groupUserRepository.Update(groupUser);

            if (!updateResult)
            {
                _logger.LogError($"Failed to update GroupUser. UserId {userId}, roleId {roleId}");
                return(Result.Fail("failed_to_update_group_user", "Failed to update GroupUser"));
            }

            return(Result.Ok());
        }
Example #3
0
        /// <summary>
        /// This method requires that you already validated all parameters
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="groupId"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task <Result <GroupUserEntity> > AddUserToGroupWithoutValidation(string userId, string groupId, string roleId)
        {
            Result beforeResult = await _addGroupUserFilter.BeforeAdd(userId, groupId, roleId);

            if (beforeResult.Failure)
            {
                return(Result.Fail <GroupUserEntity>(beforeResult));
            }

            GroupUserEntity groupUser = new GroupUserEntity(
                userId: userId,
                groupId: groupId,
                roleId: roleId);

            bool addResult = await _groupUserDAO.Add(groupUser);

            if (!addResult)
            {
                _logger.LogError($"Failed to add GroupUser. GroupId {groupId}, UserId {userId}, RoleId {roleId}");
                return(Result.Fail <GroupUserEntity>(FAILED_TO_ADD_GROUP_USER));
            }

            Result afterResult = await _addGroupUserFilter.AfterAdded(groupUser);

            if (afterResult.Failure)
            {
                return(Result.Fail <GroupUserEntity>(afterResult));
            }

            return(Result.Ok(groupUser));
        }
Example #4
0
        public async Task <Result> RemoveAsync(long groupUserId)
        {
            _logger.LogInformation($"Removing GroupUser. GroupUserId {groupUserId}");

            Result <GroupUserEntity> getGroupUserResult = await _groupUserStore.SingleOrDefault(groupUserId);

            if (getGroupUserResult.Failure)
            {
                return(Result.Fail(getGroupUserResult));
            }

            GroupUserEntity groupUser = getGroupUserResult.Value;

            Result canManageResult = await _groupUserStore.CanManageUser(groupUser);

            if (canManageResult.Failure)
            {
                return(Result.Fail(canManageResult));
            }

            bool removeResult = await _groupUserDAO.Remove(groupUser);

            if (!removeResult)
            {
                _logger.LogError($"Failed to remove GroupUser. GroupUserId {groupUserId}");
                return(Result.Fail(FAILED_TO_REMOVE_GROUP_USER));
            }

            return(Result.Ok());
        }
        public Result Remove(long groupUserId)
        {
            _logger.LogInformation($"Removing GroupUser. GroupUserId {groupUserId}");

            Result <GroupUserEntity> getGroupUserResult = _groupUserStore.Get(groupUserId);

            if (getGroupUserResult.Failure)
            {
                return(Result.Fail(getGroupUserResult.Errors));
            }

            GroupUserEntity groupUser = getGroupUserResult.Value;

            List <RoleListData> rolesList = _groupUserStore.CanManageGroupRoles();

            if (!rolesList.Any(x => x.Id == groupUser.RoleId))
            {
                _logger.LogError($"User does not have a permission to remove user. GroupUserId {groupUserId}");
                return(Result.Fail("no_permission", "No Permission"));
            }

            bool removeResult = _groupUserRepository.Remove(groupUser);

            if (!removeResult)
            {
                _logger.LogError($"Failed to remove GroupUser. GroupUserId {groupUserId}");
                return(Result.Fail("failed_to_remove_group_user", "Failed to remove group user"));
            }

            return(Result.Ok());
        }
Example #6
0
        public async Task <IEnumerable <GroupMessage> > GetMessagesInGroup(int groupId, [FromUri] int userId)
        {
            GroupUserEntity userGroup = await DbContext.GroupsUsersRepo.GetByUserIdAndGroupId(userId, groupId);

            if (userGroup == null)
            {
                ThrowHttpResponseException(System.Net.HttpStatusCode.Unauthorized, "Access is denied.");
            }

            GroupEntity group = await DbContext.GroupsRepo.GetSingleOrDefaultAsync(groupId);

            if (group == null)
            {
                ThrowHttpResponseException(System.Net.HttpStatusCode.NotFound, string.Format("Group with id {0} was not found.", groupId));
            }

            IEnumerable <GroupMessageEntity> dbMessages = await DbContext.GroupMessagesRepo.GetGroupMessages(groupId);

            var authorsIds = dbMessages.Select(i => i.UserId).Distinct();
            var authors    = await DbContext.UsersRepo.GetByKeysAsync(authorsIds);

            var authorIdToNameMapping     = authors.ToDictionary(u => u.Id, u => string.Format("{0} {1}", u.FirstName, u.LastName));
            var authorIdToUsernameMapping = authors.ToDictionary(u => u.Id, u => u.UserName);

            return(dbMessages.Select(m =>
            {
                var contract = m.ToContract();
                contract.AuthorName = authorIdToNameMapping[m.UserId];
                contract.Username = authorIdToUsernameMapping[m.UserId];
                contract.AuthorId = m.UserId;
                contract.UserId = m.UserId;

                return contract;
            }).ToList());
        }
Example #7
0
 public static GroupParticipants ToContract(this GroupUserEntity groupUserEntity)
 {
     return(new GroupParticipants
     {
         GroupId = groupUserEntity.GroupId,
         ParticipantId = groupUserEntity.UserId,
     });
 }
        public Result ChangeRole(long groupUserId, string roleId, string userId)
        {
            _logger.LogInformation($"Changing GroupUser role. GroupUserId {groupUserId}, roleId {roleId}");

            Result roleValidResult = RoleIsValid(roleId);

            if (roleValidResult.Failure)
            {
                return(Result.Fail(roleValidResult.Errors));
            }

            List <RoleListData> canAssigneGroupRoles = _groupUserStore.CanAssigneGroupRoles();

            if (!canAssigneGroupRoles.Any(x => x.Id == roleId))
            {
                _logger.LogError($"User does not have permission to assign role. RoleId {roleId}");
                return(Result.Fail("no_permission", "No Permission"));
            }

            Result <GroupUserEntity> getGroupUserResult = _groupUserStore.Get(groupUserId);

            if (getGroupUserResult.Failure)
            {
                return(Result.Fail(getGroupUserResult.Errors));
            }

            GroupUserEntity groupUser = getGroupUserResult.Value;

            List <RoleListData> canManageGroupRoles = _groupUserStore.CanManageGroupRoles();

            if (!canManageGroupRoles.Any(x => x.Id != groupUser.RoleId))
            {
                _logger.LogError($"User does not have permission to manage role. GroupUserId {groupUserId} RoleId {roleId}");
                return(Result.Fail("no_permission", "No Permission"));
            }

            if (!_groupUserStore.CanChangeOwnRole())
            {
                if (groupUser.UserId == userId)
                {
                    _logger.LogError($"User can not change his own role");
                    return(Result.Fail("user_can_not_change_his_own_role", "User can not change his own role"));
                }
            }

            groupUser.UpdateRole(roleId);

            bool updateResult = _groupUserRepository.Update(groupUser);

            if (!updateResult)
            {
                _logger.LogError($"Failed to change group user role. GroupUserId {groupUserId}, roleId {roleId}");
                return(Result.Fail("failed_to_cahnge_group_user_role", "Failed to change GroupUser role"));
            }

            return(Result.Ok());
        }
Example #9
0
        private async Task <Result> ChangeRoleAsync(long groupUserId, string roleId, string userId)
        {
            _logger.LogInformation($"Changing GroupUser role. GroupUserId {groupUserId}, roleId {roleId}");

            Result roleValidResult = await RoleIsValid(roleId);

            if (roleValidResult.Failure)
            {
                return(Result.Fail(roleValidResult));
            }

            List <RoleListData> canAssigneGroupRoles = _groupUserStore.CanAssigneGroupRoles();

            if (!canAssigneGroupRoles.Any(x => x.Id == roleId))
            {
                _logger.LogError($"User does not have permission to assign role. RoleId {roleId}");
                return(Result.Fail(NO_PERMISSION));
            }

            Core.Models.Result.Result <GroupUserEntity> getGroupUserResult = _groupUserStore.Get(groupUserId);
            if (getGroupUserResult.Failure)
            {
                return(getGroupUserResult.ToNewResult());
            }

            GroupUserEntity groupUser = getGroupUserResult.Value;

            List <RoleListData> canManageGroupRoles = _groupUserStore.CanManageGroupRoles();

            if (!canManageGroupRoles.Any(x => x.Id != groupUser.RoleId))
            {
                _logger.LogError($"User does not have permission to manage role. GroupUserId {groupUserId} RoleId {roleId}");
                return(Result.Fail(NO_PERMISSION));
            }

            if (!_groupUserStore.CanChangeOwnRole())
            {
                if (groupUser.UserId == userId)
                {
                    _logger.LogError($"User can not change his own role");
                    return(Result.Fail(USER_CAN_NOT_CHANGE_HIS_OWN_ROLE));
                }
            }

            groupUser.UpdateRole(roleId);

            bool updateResult = await _groupUserDAO.Update(groupUser);

            if (!updateResult)
            {
                _logger.LogError($"Failed to change group user role. GroupUserId {groupUserId}, roleId {roleId}");
                return(Result.Fail(FAILED_TO_CAHNGE_GROUP_USER_ROLE));
            }

            return(Result.Ok());
        }
Example #10
0
        /// <summary>
        /// 修改组中用户
        /// </summary>
        /// <param name="Input"></param>
        public void UpdateGroupUser(GroupUserInput input)
        {
            GroupUserEntity groupUser = new GroupUserEntity
            {
                GroupId = input.GroupId,
                UserId  = input.UserId
            };

            _iGroupUserRepository.Update(groupUser);
        }
Example #11
0
        public async Task <Result> CanManageUser(GroupUserEntity groupUser)
        {
            List <RoleListData> rolesList = await CanManageRoles();

            if (!rolesList.Any(x => x.Id == groupUser.RoleId))
            {
                _logger.LogError($"User does not have a permission to remove user. GroupUserId {groupUser.Id}");
                return(Result.Fail(NO_PERMISSION));
            }

            return(Result.Ok());
        }
Example #12
0
        public async Task <Result> CanManageUser(long groupUserId)
        {
            Result <GroupUserEntity> getGroupUserResult = await SingleOrDefault(groupUserId);

            if (getGroupUserResult.Failure)
            {
                return(Result.Fail(getGroupUserResult));
            }

            GroupUserEntity groupUser = getGroupUserResult.Value;

            return(await CanManageUser(groupUser));
        }
Example #13
0
        public Core.Models.Result.Result <GroupUserEntity> Get(BaseSpecification <GroupUserEntity> baseSpecification)
        {
            baseSpecification = ApplayGroupUserFilter(baseSpecification);

            GroupUserEntity groupUser = _groupUserRepository.SingleOrDefault(baseSpecification);

            if (groupUser == null)
            {
                _logger.LogError($"No GroupUser. No GroupUser");
                return(Core.Models.Result.Result.Fail <GroupUserEntity>("no_group_user", "No GroupUser"));
            }

            return(Core.Models.Result.Result.Ok(groupUser));
        }
Example #14
0
        /// <summary>
        /// 往组里添加用户
        /// </summary>
        /// <param name="Input"></param>
        public void IntsertGroupUser(GroupUserComparaInput input)
        {
            input.ExistUser.ForEach(x =>
            {
                input.InputArr.RemoveAll(aa => aa.UserId == x.UserId && aa.GroupId == x.GroupId);
            });

            foreach (GroupUserInput item in input.InputArr)
            {
                GroupUserEntity groupUser = new GroupUserEntity
                {
                    Id      = Guid.NewGuid().ToString(),
                    GroupId = item.GroupId,
                    UserId  = item.UserId
                };
                _iGroupUserRepository.Insert(groupUser);
            }
        }
Example #15
0
        private Result AddToGroup(string userId, string groupId, string groupRoleId)
        {
            _logger.LogInformation($"Adding user to group. UserId {userId}, GroupId {groupId}");

            BaseSpecification <GroupEntity> groupExistsSpecification = new BaseSpecification <GroupEntity>();

            groupExistsSpecification.AddFilter(x => x.Id == groupId);

            bool groupExists = _groupRepository.Exist(groupExistsSpecification);

            if (!groupExists)
            {
                _logger.LogError($"No Group. GroupId {groupId}");
                return(Result.Fail("no_group", "No Group"));
            }

            BaseSpecification <RoleEntity> groupRoleExistsSpecification = new BaseSpecification <RoleEntity>();

            groupRoleExistsSpecification.AddFilter(x => x.Id == groupRoleId);
            groupRoleExistsSpecification.AddFilter(x => x.Type == Data.Enums.Entity.RoleTypes.Group);

            bool groupRoleExists = _roleRepository.Exist(groupRoleExistsSpecification);

            if (!groupRoleExists)
            {
                _logger.LogWarning($"No GroupRole, adding GroupUser without GroupRole");
                groupRoleId = null;
            }

            GroupUserEntity groupUser = new GroupUserEntity(
                userId: userId,
                groupId: groupId,
                roleId: groupRoleId);

            bool addGroupUser = _groupUserRepository.Add(groupUser);

            if (!addGroupUser)
            {
                _logger.LogError($"Failed to add GroupUser. GroupId {groupId}, UserId {userId}");
                return(Result.Fail("failed_to_add_group_user", "Failed to add GroupUser"));
            }

            return(Result.Ok());
        }
Example #16
0
        public async Task <GroupParticipants> AddParticipants(int groupId, [FromBody] int participantId)
        {
            GroupEntity group = await DbContext.GroupsRepo.GetSingleOrDefaultAsync(groupId);

            if (group == null)
            {
                ThrowHttpResponseException(System.Net.HttpStatusCode.BadRequest, string.Format("Group with id {0} not exists.", groupId));
            }

            ValidateUserAsAuthenticated(group.AuthorId);

            GroupUserEntity participant = await DbContext.GroupsUsersRepo.CreateAsync(new GroupUserEntity
            {
                GroupId = groupId,
                UserId  = participantId,
                IsAdmin = false
            });

            return(participant.ToContract());
        }
Example #17
0
        public async Task <GroupMessage> CreateMessageInGroup(int groupId, int userId, [FromBody] GroupMessage message)
        {
            GroupUserEntity userGroup = await DbContext.GroupsUsersRepo.GetByUserIdAndGroupId(userId, groupId);

            if (userGroup == null)
            {
                ThrowHttpResponseException(System.Net.HttpStatusCode.Unauthorized, "Access is denied.");
            }
            ValidateUserAsAuthenticated(userId);

            GroupEntity group = await DbContext.GroupsRepo.GetSingleOrDefaultAsync(groupId);

            if (group == null)
            {
                ThrowHttpResponseException(System.Net.HttpStatusCode.NotFound, string.Format("Group with id {0} was not found.", groupId));
            }

            GroupMessageEntity entity = await DbContext.GroupMessagesRepo.CreateAsync(message.ToEntity());

            return(entity.ToContract());
        }
        private Result AddUserToGroup(string userId, string groupId, string roleId)
        {
            Result groupExist = _groupStore.Exists(groupId);

            if (groupExist.Failure)
            {
                return(Result.Fail(groupExist.Errors));
            }

            Result roleIsValid = RoleIsValid(roleId);

            if (roleIsValid.Failure)
            {
                return(Result.Fail(roleIsValid.Errors));
            }

            Result userExist = ValidateUser(userId);

            if (userExist.Failure)
            {
                return(Result.Fail(userExist.Errors));
            }

            GroupUserEntity groupUser = new GroupUserEntity(
                userId: userId,
                groupId: groupId,
                roleId: roleId);

            bool addResult = _groupUserRepository.Add(groupUser);

            if (!addResult)
            {
                _logger.LogError($"Failed to add GroupUser. GroupId {groupId}, UserId {userId}, RoleId {roleId}");
                return(Result.Fail("failed_to_add_group_user", "Failed to add GroupUser"));
            }

            return(Result.Ok());
        }
        public Result Leave(string userId, string groupId)
        {
            _logger.LogInformation($"GroupUser is leaving. UserId {userId}, GroupId {groupId}");

            Result <GroupUserEntity> getGroupUserResult = _groupUserStore.Get(userId, groupId);

            if (getGroupUserResult.Failure)
            {
                return(Result.Fail(getGroupUserResult.Errors));
            }

            GroupUserEntity groupUser = getGroupUserResult.Value;

            bool removeResult = _groupUserRepository.Remove(groupUser);

            if (!removeResult)
            {
                _logger.LogError($"Failed to remove GroupUser. UserId {userId}, GroupId {groupId}");
                return(Result.Fail("failed_to_remove_group_user", "Failed to remove group user"));
            }

            return(Result.Ok());
        }
Example #20
0
        private async Task <Result> LeaveAsync(string userId, string groupId)
        {
            _logger.LogInformation($"GroupUser is leaving. UserId {userId}, GroupId {groupId}");

            Result <GroupUserEntity> getGroupUserResult = await _groupUserStore.SingleOrDefault(userId, groupId);

            if (getGroupUserResult.Failure)
            {
                return(Result.Fail(getGroupUserResult));
            }

            GroupUserEntity groupUser = getGroupUserResult.Value;

            bool removeResult = await _groupUserDAO.Remove(groupUser);

            if (!removeResult)
            {
                _logger.LogError($"Failed to remove GroupUser. UserId {userId}, GroupId {groupId}");
                return(Result.Fail("failed_to_remove_group_user", "Failed to remove group user"));
            }

            return(Result.Ok());
        }
Example #21
0
 public Task <Result> AfterAdded(GroupUserEntity groupUser)
 {
     return(Task.FromResult(Result.Ok()));
 }