Search in sources :

Example 11 with BadRequestException

use of org.sagebionetworks.bridge.exceptions.BadRequestException in project BridgeServer2 by Sage-Bionetworks.

the class AppService method deleteApp.

public void deleteApp(String identifier, boolean physical) {
    checkArgument(isNotBlank(identifier));
    if (appWhitelist.contains(identifier)) {
        throw new UnauthorizedException(identifier + " is protected by whitelist.");
    }
    // only admin can call this method, should contain deactivated ones.
    App existing = getApp(identifier, true);
    if (!physical) {
        // deactivate
        if (!existing.isActive()) {
            throw new BadRequestException("App '" + identifier + "' already deactivated.");
        }
        appDao.deactivateApp(existing.getIdentifier());
    } else {
        // delete app data
        accountService.deleteAllAccounts(existing.getIdentifier());
        studyService.deleteAllStudies(existing.getIdentifier());
        scheduleService.deleteAllSchedules(existing.getIdentifier());
        assessmentResourceService.deleteAllAssessmentResources(existing.getIdentifier());
        assessmentService.deleteAllAssessments(existing.getIdentifier());
        organizationService.deleteAllOrganizations(existing.getIdentifier());
        templateService.deleteAllTemplates(existing.getIdentifier());
        compoundActivityDefinitionService.deleteAllCompoundActivityDefinitionsInApp(existing.getIdentifier());
        subpopService.deleteAllSubpopulations(existing.getIdentifier());
        topicService.deleteAllTopics(existing.getIdentifier());
        fileService.deleteAllAppFiles(existing.getIdentifier());
        // actual delete
        appDao.deleteApp(existing);
    }
    cacheProvider.removeApp(identifier);
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) UnauthorizedException(org.sagebionetworks.bridge.exceptions.UnauthorizedException) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException)

Example 12 with BadRequestException

use of org.sagebionetworks.bridge.exceptions.BadRequestException in project BridgeServer2 by Sage-Bionetworks.

the class AccountWorkflowService method resetPassword.

/**
 * Use a supplied password reset token to change the password on an account. If the supplied
 * token is not valid, this method throws an exception. If the token is valid but the account
 * does not exist, an exception is also thrown (this would be unusual).
 */
public void resetPassword(PasswordReset passwordReset) {
    checkNotNull(passwordReset);
    // This pathway is unusual as the token may have been sent via email or phone, so test for both.
    CacheKey emailCacheKey = CacheKey.passwordResetForEmail(passwordReset.getSptoken(), passwordReset.getAppId());
    CacheKey phoneCacheKey = CacheKey.passwordResetForPhone(passwordReset.getSptoken(), passwordReset.getAppId());
    String email = cacheProvider.getObject(emailCacheKey, String.class);
    Phone phone = cacheProvider.getObject(phoneCacheKey, Phone.class);
    if (email == null && phone == null) {
        throw new BadRequestException(PASSWORD_RESET_TOKEN_EXPIRED);
    }
    cacheProvider.removeObject(emailCacheKey);
    cacheProvider.removeObject(phoneCacheKey);
    App app = appService.getApp(passwordReset.getAppId());
    ChannelType channelType = null;
    AccountId accountId = null;
    if (email != null) {
        accountId = AccountId.forEmail(app.getIdentifier(), email);
        channelType = ChannelType.EMAIL;
    } else if (phone != null) {
        accountId = AccountId.forPhone(app.getIdentifier(), phone);
        channelType = ChannelType.PHONE;
    } else {
        throw new BridgeServiceException("Could not reset password");
    }
    Account account = accountService.getAccount(accountId).orElseThrow(() -> new EntityNotFoundException(Account.class));
    accountService.changePassword(account, channelType, passwordReset.getPassword());
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) Account(org.sagebionetworks.bridge.models.accounts.Account) AccountId(org.sagebionetworks.bridge.models.accounts.AccountId) Phone(org.sagebionetworks.bridge.models.accounts.Phone) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) EntityNotFoundException(org.sagebionetworks.bridge.exceptions.EntityNotFoundException) ChannelType(org.sagebionetworks.bridge.services.AuthenticationService.ChannelType) CacheKey(org.sagebionetworks.bridge.cache.CacheKey)

Example 13 with BadRequestException

use of org.sagebionetworks.bridge.exceptions.BadRequestException in project BridgeServer2 by Sage-Bionetworks.

the class AccountWorkflowService method resendVerificationToken.

/**
 * Send another verification token via email or phone. This creates and sends a new verification token
 * using the specified channel (an email or SMS message).
 */
public void resendVerificationToken(ChannelType type, AccountId accountId) {
    checkNotNull(accountId);
    App app = appService.getApp(accountId.getAppId());
    Account account = accountService.getAccount(accountId).orElseThrow(() -> new EntityNotFoundException(Account.class));
    if (type == ChannelType.EMAIL) {
        if (account.getEmail() == null) {
            throw new BadRequestException("Email address has not been set.");
        }
        if (TRUE.equals(account.getEmailVerified())) {
            throw new BadRequestException("Email address is already verified.");
        }
        sendEmailVerificationToken(app, account.getId(), account.getEmail());
    } else if (type == ChannelType.PHONE) {
        if (account.getPhone() == null) {
            throw new BadRequestException("Phone number has not been set.");
        }
        if (TRUE.equals(account.getPhoneVerified())) {
            throw new BadRequestException("Phone number is already verified.");
        }
        sendPhoneVerificationToken(app, account.getId(), account.getPhone());
    } else {
        throw new UnsupportedOperationException("Channel type not implemented");
    }
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) Account(org.sagebionetworks.bridge.models.accounts.Account) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) EntityNotFoundException(org.sagebionetworks.bridge.exceptions.EntityNotFoundException)

Example 14 with BadRequestException

use of org.sagebionetworks.bridge.exceptions.BadRequestException in project BridgeServer2 by Sage-Bionetworks.

the class ActivityEventService method deleteCustomEvent.

/**
 * Delete a custom event.
 */
public void deleteCustomEvent(App app, String healthCode, String eventKey) {
    if (!app.getCustomEvents().containsKey(eventKey) && !app.getAutomaticCustomEvents().containsKey(eventKey)) {
        throw new BadRequestException("App's customEvents do not contain event ID: " + eventKey);
    }
    ActivityEvent event = new DynamoActivityEvent.Builder().withHealthCode(healthCode).withObjectType(CUSTOM).withUpdateType(app.getCustomEvents().get(eventKey)).withObjectId(eventKey).build();
    activityEventDao.deleteCustomEvent(event);
}
Also used : DynamoActivityEvent(org.sagebionetworks.bridge.dynamodb.DynamoActivityEvent) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent) ActivityEvent(org.sagebionetworks.bridge.models.activities.ActivityEvent) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) DynamoActivityEvent(org.sagebionetworks.bridge.dynamodb.DynamoActivityEvent)

Example 15 with BadRequestException

use of org.sagebionetworks.bridge.exceptions.BadRequestException in project BridgeServer2 by Sage-Bionetworks.

the class AssessmentConfigService method customizeAssessmentConfig.

public AssessmentConfig customizeAssessmentConfig(String appId, String ownerId, String guid, Map<String, Map<String, JsonNode>> updates) {
    if (updates == null) {
        throw new BadRequestException("Updates to configuration are missing");
    }
    Assessment assessment = assessmentService.getAssessmentByGuid(appId, ownerId, guid);
    CAN_EDIT_ASSESSMENTS.checkAndThrow(ORG_ID, assessment.getOwnerId());
    Map<String, Set<PropertyInfo>> fields = assessment.getCustomizationFields();
    AssessmentConfigCustomizer customizer = new AssessmentConfigCustomizer(fields, updates);
    AssessmentConfig existing = dao.getAssessmentConfig(guid).orElseThrow(() -> new EntityNotFoundException(AssessmentConfig.class));
    BridgeUtils.walk(existing.getConfig(), customizer);
    if (!customizer.hasUpdated()) {
        return existing;
    }
    Validate.entityThrowingException(getValidator(), existing);
    existing.setModifiedOn(getModifiedOn());
    return dao.customizeAssessmentConfig(guid, existing);
}
Also used : AssessmentConfig(org.sagebionetworks.bridge.models.assessments.config.AssessmentConfig) Set(java.util.Set) Assessment(org.sagebionetworks.bridge.models.assessments.Assessment) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) EntityNotFoundException(org.sagebionetworks.bridge.exceptions.EntityNotFoundException) AssessmentConfigCustomizer(org.sagebionetworks.bridge.models.assessments.config.AssessmentConfigCustomizer)

Aggregations

BadRequestException (org.sagebionetworks.bridge.exceptions.BadRequestException)104 EntityNotFoundException (org.sagebionetworks.bridge.exceptions.EntityNotFoundException)23 Test (org.testng.annotations.Test)19 UserSession (org.sagebionetworks.bridge.models.accounts.UserSession)17 App (org.sagebionetworks.bridge.models.apps.App)17 DateTime (org.joda.time.DateTime)13 StudyParticipant (org.sagebionetworks.bridge.models.accounts.StudyParticipant)10 Study (org.sagebionetworks.bridge.models.studies.Study)10 ArrayList (java.util.ArrayList)9 Account (org.sagebionetworks.bridge.models.accounts.Account)9 StudyActivityEvent (org.sagebionetworks.bridge.models.activities.StudyActivityEvent)9 PostMapping (org.springframework.web.bind.annotation.PostMapping)9 UploadSchema (org.sagebionetworks.bridge.models.upload.UploadSchema)8 CacheKey (org.sagebionetworks.bridge.cache.CacheKey)7 AccountId (org.sagebionetworks.bridge.models.accounts.AccountId)7 ScheduleContext (org.sagebionetworks.bridge.models.schedules.ScheduleContext)7 List (java.util.List)5 Survey (org.sagebionetworks.bridge.models.surveys.Survey)5 GetMapping (org.springframework.web.bind.annotation.GetMapping)5 Map (java.util.Map)4