Exemplo n.º 1
0
        async Task <StudyParticipantDto> AddAzureUserAsync(int studyId, ParticipantLookupDto user, string role)
        {
            UserDto addedUser;

            if (_userService.IsMockUser()) //If mock user, he can only add him self
            {
                addedUser = await _userService.GetCurrentUserAsync();

                await _userService.EnsureExists(addedUser);
            }
            else
            {
                var newUserFromAzure = await _azureADUsersService.GetUserAsync(user.ObjectId);

                if (newUserFromAzure == null)
                {
                    throw new NotFoundException($"AD User with id {user.ObjectId} not found!");
                }

                addedUser = await _userService.EnsureExists(new UserDto(user.ObjectId, newUserFromAzure.UserPrincipalName, newUserFromAzure.DisplayName, newUserFromAzure.Mail));
            }

            var studyFromDb = await GetStudyForParticipantOperation(studyId, role);

            if (RoleAllreadyExistsForUser(studyFromDb, addedUser.Id, role))
            {
                throw new ArgumentException($"Role {role} allready granted for user {user.DatabaseId.Value} on study {studyFromDb.Id}");
            }

            StudyParticipant createdStudyParticipant = null;

            try
            {
                createdStudyParticipant = new StudyParticipant {
                    UserId = addedUser.Id, StudyId = studyFromDb.Id, RoleName = role
                };
                studyFromDb.StudyParticipants = new List <StudyParticipant> {
                    createdStudyParticipant
                };

                await _db.SaveChangesAsync();

                return(ConvertToDto(createdStudyParticipant, addedUser));
            }
            catch (Exception)
            {
                await RemoveIfExist(createdStudyParticipant);

                throw;
            }
        }
Exemplo n.º 2
0
        public async Task <StudyParticipantDto> AddAsync(int studyId, ParticipantLookupDto user, string role)
        {
            List <CloudResourceOperationDto> updateOperations = null;

            try
            {
                ValidateRoleNameThrowIfInvalid(role);



                StudyParticipantDto newlyAddedParticipant = null;

                if (user.Source == ParticipantSource.Db)
                {
                    newlyAddedParticipant = await AddDbUserAsync(studyId, user.DatabaseId.Value, role);
                }
                else if (user.Source == ParticipantSource.Azure)
                {
                    newlyAddedParticipant = await AddAzureUserAsync(studyId, user, role);
                }
                else
                {
                    throw new ArgumentException($"Unknown source for user {user.UserName}");
                }

                await CreateRoleUpdateOperationsAsync(newlyAddedParticipant.StudyId);

                return(newlyAddedParticipant);
            }
            catch (Exception ex)
            {
                if (updateOperations != null)
                {
                    foreach (var curOperation in updateOperations)
                    {
                        await _cloudResourceOperationUpdateService.AbortAndAllowDependentOperationsToRun(curOperation.Id, ex.Message);
                    }
                }

                if (ex is ForbiddenException)
                {
                    throw;
                }

                throw new Exception($"Add participant failed: {ex.Message}", ex);
            }
        }
Exemplo n.º 3
0
 public static async Task <ApiConversation <ParticipantLookupDto, ErrorResponse> > AddAndExpectFailure(RestHelper restHelper, int studyId, string role, ParticipantLookupDto requestDto)
 {
     return(await Add <ErrorResponse>(restHelper, studyId, role, requestDto));
 }
Exemplo n.º 4
0
 public static async Task <ApiConversation <ParticipantLookupDto, StudyParticipantListItem> > AddAndExpectSuccess(RestHelper restHelper, int studyId, string role, ParticipantLookupDto requestDto)
 {
     return(await Add <StudyParticipantListItem>(restHelper, studyId, role, requestDto));
 }
Exemplo n.º 5
0
        static async Task <ApiConversation <ParticipantLookupDto, TResponse> > Add <TResponse>(RestHelper restHelper, int studyId, string role, ParticipantLookupDto requestDto)
        {
            var response = await restHelper.Put <TResponse, ParticipantLookupDto>($"api/studies/{studyId}/participants/{role}", requestDto);

            return(new ApiConversation <ParticipantLookupDto, TResponse>(requestDto, response));
        }
Exemplo n.º 6
0
        public async Task <StudyParticipantDto> SetupAndAddParticipantForSet(int studyId, string role, string source)
        {
            //SETUP
            var userName     = "******";
            var userEmail    = userName + "@somedomain.com";
            var userFullName = "Newly Added User";

            var participantToAdd = new ParticipantLookupDto()
            {
                DatabaseId = TestUserConstants.COMMON_NEW_PARTICIPANT_DB_ID, ObjectId = TestUserConstants.COMMON_NEW_PARTICIPANT_OBJECTID, EmailAddress = userEmail, FullName = userFullName, UserName = userName, Source = source
            };

            await RefreshAndPopulateTestDb();

            //GET REQUIRED SERVICES
            var db     = _serviceProvider.GetService <SepesDbContext>();
            var mapper = _serviceProvider.GetService <IMapper>();
            var logger = _serviceProvider.GetService <ILogger <StudyParticipantCreateService> >();

            var adUserServiceMock = new Mock <IAzureUserService>();

            adUserServiceMock.Setup(service => service.GetUserAsync(It.IsAny <string>())).ReturnsAsync(new AzureUserDto()
            {
                DisplayName = userFullName, Mail = userEmail
            });

            //Study model service
            var studyModelService = StudyServiceMockFactory.StudyEfModelService(_serviceProvider);

            //Used to get current user
            var userServiceMock = GetUserServiceMock(TestUserConstants.COMMON_CUR_USER_DB_ID, TestUserConstants.COMMON_CUR_USER_OBJECTID);

            userServiceMock.Setup(service => service.GetByDbIdAsync(TestUserConstants.COMMON_NEW_PARTICIPANT_DB_ID)).ReturnsAsync(new UserDto()
            {
                Id = TestUserConstants.COMMON_NEW_PARTICIPANT_DB_ID, ObjectId = TestUserConstants.COMMON_NEW_PARTICIPANT_OBJECTID
            });

            //Queue service mock
            var queueServiceMock = new Mock <IProvisioningQueueService>();

            queueServiceMock.Setup(service => service.SendMessageAsync(It.IsAny <ProvisioningQueueParentDto>(), It.IsAny <TimeSpan?>(), It.IsAny <CancellationToken>())).ReturnsAsync(new ProvisioningQueueParentDto());

            var resourceReadServiceMock = new Mock <ICloudResourceReadService>();

            resourceReadServiceMock.Setup(service => service.GetDatasetResourceGroupIdsForStudy(It.IsAny <int>())).ReturnsAsync(new List <int> {
                1
            });
            resourceReadServiceMock.Setup(service => service.GetSandboxResourceGroupIdsForStudy(It.IsAny <int>())).ReturnsAsync(new List <int> {
                2
            });


            var operationCreateServiceMock = new Mock <ICloudResourceOperationCreateService>();

            var operationUpdateServiceMock = new Mock <ICloudResourceOperationUpdateService>();

            var configuration = _serviceProvider.GetService <IConfiguration>();

            var studyParticipantService = new StudyParticipantCreateService(db, mapper, logger, userServiceMock.Object, studyModelService, adUserServiceMock.Object, queueServiceMock.Object, resourceReadServiceMock.Object, operationCreateServiceMock.Object, operationUpdateServiceMock.Object);

            return(await studyParticipantService.AddAsync(studyId, participantToAdd, role));
        }
        public async Task <IActionResult> AddParticipantAsync(int studyId, ParticipantLookupDto user, string role)
        {
            var addedParticipantDto = await _studyParticipantCreateService.AddAsync(studyId, user, role);

            return(new JsonResult(addedParticipantDto));
        }