示例#1
0
        public async Task <HttpResponseMessage> CreateGroup([FromBody] GroupDto group)
        {
            if (group == null)
            {
                throw new BadRequestException(ErrorMessages.GroupModelIsEmpty, ErrorCodes.BadRequest);
            }

            await _privilegesManager.Demand(Session.UserId, InstanceAdminPrivileges.ManageGroups);

            GroupValidator.ValidateModel(group, OperationMode.Create);

            int groupId = 0;
            Func <IDbTransaction, long, Task> action = async(transaction, transactionId) =>
            {
                groupId = await _groupRepository.AddGroupAsync(group, transaction);

                var topRevisionId = await _itemInfoRepository.GetTopRevisionId(transaction);

                var groupIds = new[]
                {
                    groupId
                };
                var message = new UsersGroupsChangedMessage(new int[0], groupIds)
                {
                    TransactionId = transactionId,
                    RevisionId    = topRevisionId,
                    ChangeType    = UsersGroupsChangedType.Create
                };
                await _sendMessageExecutor.Execute(_applicationSettingsRepository, _serviceLogRepository, message, transaction);
            };

            await RunInTransactionAsync(action);

            return(Request.CreateResponse(HttpStatusCode.Created, groupId));
        }
示例#2
0
        public void UsersGroupsChangedMessageHandler_RethrowsException()
        {
            // arrange
            _actionHelperMock.Setup(m => m.HandleAction(It.IsAny <TenantInformation>(), It.IsAny <ActionMessage>(), It.IsAny <BaseRepository>())).Throws(new TestException());
            var handler = new UsersGroupsChangedMessageHandler(_actionHelperMock.Object, _tenantInfoRetrieverMock.Object, _configHelperMock.Object, _transactionValidatorMock.Object);
            var message = new UsersGroupsChangedMessage();

            // act
            TestHandlerAndMessageWithHeader(handler, message);
            // assert
            _actionHelperMock.Verify(m => m.HandleAction(It.IsAny <TenantInformation>(), It.IsAny <ActionMessage>(), It.IsAny <BaseRepository>()), Times.Once);
        }
示例#3
0
        public void UsersGroupsChangedMessageHandler_HandlesMessageSuccessfully()
        {
            // arrange
            _actionHelperMock.Setup(m => m.HandleAction(It.IsAny <TenantInformation>(), It.IsAny <ActionMessage>(), It.IsAny <BaseRepository>())).Returns(Task.FromResult(true));
            var handler = new UsersGroupsChangedMessageHandler(_actionHelperMock.Object, _tenantInfoRetrieverMock.Object, _configHelperMock.Object, _transactionValidatorMock.Object);
            var message = new UsersGroupsChangedMessage();

            // act
            TestHandlerAndMessageWithHeader(handler, message);
            // assert
            Assert.IsNotNull(handler);
            _actionHelperMock.Verify(m => m.HandleAction(It.IsAny <TenantInformation>(), It.IsAny <ActionMessage>(), It.IsAny <BaseRepository>()), Times.Once);
        }
 public void TestInitialize()
 {
     _helper  = new UsersGroupsChangedActionHelper();
     _message = new UsersGroupsChangedMessage
     {
         ChangeType = UsersGroupsChangedType.Update,
         UserId     = 1,
         RevisionId = 2
     };
     _repositoryMock = new Mock <IUsersGroupsChangedRepository>(MockBehavior.Strict);
     _workflowMessagingProcessorMock = new Mock <IWorkflowMessagingProcessor>(MockBehavior.Strict);
     _tenantInformation = new TenantInformation();
 }
示例#5
0
        public async Task <IHttpActionResult> UpdateUser(int userId, [FromBody] UserDto user)
        {
            if (user == null)
            {
                throw new BadRequestException(ErrorMessages.UserModelIsEmpty, ErrorCodes.BadRequest);
            }

            await _privilegesManager.Demand(Session.UserId, InstanceAdminPrivileges.ManageUsers);

            var existingUser = await _userRepository.GetUserAsync(userId);

            if (existingUser == null)
            {
                throw new ResourceNotFoundException(ErrorMessages.UserNotExist, ErrorCodes.ResourceNotFound);
            }

            if (existingUser.InstanceAdminRoleId != user.InstanceAdminRoleId)
            {
                await _privilegesManager.Demand(Session.UserId, InstanceAdminPrivileges.AssignAdminRoles);
            }

            var databaseUser = await UsersHelper.CreateDbUserFromDtoAsync(user, OperationMode.Edit, _settingsRepository, userId);

            Func <IDbTransaction, long, Task> action = async(transaction, transactionId) =>
            {
                await _userRepository.UpdateUserAsync(databaseUser, transaction);

                var topRevisionId = await _itemInfoRepository.GetTopRevisionId(transaction);

                var userIds = new[]
                {
                    userId
                };
                var message = new UsersGroupsChangedMessage(userIds, new int[0])
                {
                    TransactionId = transactionId,
                    RevisionId    = topRevisionId,
                    ChangeType    = UsersGroupsChangedType.Update
                };
                await _sendMessageExecutor.Execute(_applicationSettingsRepository, _log, message, transaction);
            };

            await RunInTransactionAsync(action);

            return(Ok());
        }
示例#6
0
        public async Task <HttpResponseMessage> CreateUser([FromBody] UserDto user)
        {
            if (user == null)
            {
                throw new BadRequestException(ErrorMessages.UserModelIsEmpty, ErrorCodes.BadRequest);
            }

            var privileges = user.InstanceAdminRoleId.HasValue ? InstanceAdminPrivileges.AssignAdminRoles : InstanceAdminPrivileges.ManageUsers;
            await _privilegesManager.Demand(Session.UserId, privileges);

            var isAdminCanCreateUsers = await _userRepository.CheckIfAdminCanCreateUsers();

            if (!isAdminCanCreateUsers)
            {
                throw new ConflictException(ErrorMessages.MaxUsersPerInstanceLimitReached, ErrorCodes.ExceedsLimit);
            }

            var databaseUser = await UsersHelper.CreateDbUserFromDtoAsync(user, OperationMode.Create, _settingsRepository);

            int userId = 0;
            Func <IDbTransaction, long, Task> action = async(transaction, transactionId) =>
            {
                userId = await _userRepository.AddUserAsync(databaseUser, transaction);

                var topRevisionId = await _itemInfoRepository.GetTopRevisionId(transaction);

                var userIds = new[]
                {
                    userId
                };
                var message = new UsersGroupsChangedMessage(userIds, new int[0])
                {
                    TransactionId = transactionId,
                    RevisionId    = topRevisionId,
                    ChangeType    = UsersGroupsChangedType.Create
                };
                await _sendMessageExecutor.Execute(_applicationSettingsRepository, _log, message, transaction);
            };

            await RunInTransactionAsync(action);

            return(Request.CreateResponse(HttpStatusCode.Created, userId));
        }
示例#7
0
        public async Task <IHttpActionResult> UpdateGroup(int groupId, [FromBody] GroupDto group)
        {
            if (group == null)
            {
                throw new BadRequestException(ErrorMessages.GroupModelIsEmpty, ErrorCodes.BadRequest);
            }

            await _privilegesManager.Demand(Session.UserId, InstanceAdminPrivileges.ManageGroups);

            var existingGroup = await _groupRepository.GetGroupDetailsAsync(groupId);

            if (existingGroup == null || existingGroup.Id == 0)
            {
                throw new ResourceNotFoundException(ErrorMessages.GroupNotExist, ErrorCodes.ResourceNotFound);
            }

            GroupValidator.ValidateModel(group, OperationMode.Edit, existingGroup.ProjectId);

            Func <IDbTransaction, long, Task> action = async(transaction, transactionId) =>
            {
                await _groupRepository.UpdateGroupAsync(groupId, group, transaction);

                var topRevisionId = await _itemInfoRepository.GetTopRevisionId(transaction);

                var groupIds = new[]
                {
                    groupId
                };
                var message = new UsersGroupsChangedMessage(new int[0], groupIds)
                {
                    TransactionId = transactionId,
                    RevisionId    = topRevisionId,
                    ChangeType    = UsersGroupsChangedType.Update
                };
                await _sendMessageExecutor.Execute(_applicationSettingsRepository, _serviceLogRepository, message, transaction);
            };

            await RunInTransactionAsync(action);

            return(Ok());
        }
示例#8
0
        public async Task <IHttpActionResult> DeleteGroups([FromBody] OperationScope scope, string search = null)
        {
            SearchFieldValidator.Validate(search);

            if (scope == null)
            {
                return(BadRequest(ErrorMessages.InvalidDeleteGroupsParameters));
            }

            if (scope.IsEmpty())
            {
                return(Ok(DeleteResult.Empty));
            }

            await _privilegesManager.Demand(Session.UserId, InstanceAdminPrivileges.ManageGroups);

            var deletedGroupIds = new List <int>();
            Func <IDbTransaction, long, Task> action = async(transaction, transactionId) =>
            {
                var groupIds = await _groupRepository.DeleteGroupsAsync(scope, search, transaction);

                deletedGroupIds.AddRange(groupIds);

                var topRevisionId = await _itemInfoRepository.GetTopRevisionId(transaction);

                var message = new UsersGroupsChangedMessage(new int[0], deletedGroupIds)
                {
                    TransactionId = transactionId,
                    RevisionId    = topRevisionId,
                    ChangeType    = UsersGroupsChangedType.Delete
                };
                await _sendMessageExecutor.Execute(_applicationSettingsRepository, _serviceLogRepository, message, transaction);
            };

            await RunInTransactionAsync(action);

            return(Ok(new DeleteResult {
                TotalDeleted = deletedGroupIds.Count
            }));
        }