Search in sources :

Example 1 with StudyParticipantValidator

use of org.sagebionetworks.bridge.validators.StudyParticipantValidator in project BridgeServer2 by Sage-Bionetworks.

the class ParticipantService method createParticipant.

/**
 * Create a study participant. A password must be provided, even if it is added on behalf of a user before
 * triggering a reset password request.
 */
public IdentifierHolder createParticipant(App app, StudyParticipant participant, boolean shouldSendVerification) {
    checkNotNull(app);
    checkNotNull(participant);
    if (app.getAccountLimit() > 0) {
        throwExceptionIfLimitMetOrExceeded(app);
    }
    StudyParticipantValidator validator = new StudyParticipantValidator(studyService, organizationService, app, true);
    Validate.entityThrowingException(validator, participant);
    // Set basic params from inputs.
    Account account = getAccount();
    account.setId(generateGUID());
    account.setAppId(app.getIdentifier());
    account.setEmail(participant.getEmail());
    account.setPhone(participant.getPhone());
    account.setEmailVerified(FALSE);
    account.setPhoneVerified(FALSE);
    account.setHealthCode(generateGUID());
    account.setStatus(UNVERIFIED);
    // Otherwise this field is ignored on create.
    if (CAN_EDIT_MEMBERS.check(ORG_ID, participant.getOrgMembership())) {
        account.setOrgMembership(participant.getOrgMembership());
    }
    // Hash password if it has been supplied.
    if (participant.getPassword() != null) {
        try {
            PasswordAlgorithm passwordAlgorithm = DEFAULT_PASSWORD_ALGORITHM;
            String passwordHash = passwordAlgorithm.generateHash(participant.getPassword());
            account.setPasswordAlgorithm(passwordAlgorithm);
            account.setPasswordHash(passwordHash);
        } catch (InvalidKeyException | InvalidKeySpecException | NoSuchAlgorithmException ex) {
            throw new BridgeServiceException("Error creating password: " + ex.getMessage(), ex);
        }
    }
    updateAccountAndRoles(app, account, participant, true);
    // enabled unless we need any kind of verification
    boolean sendEmailVerification = shouldSendVerification && app.isEmailVerificationEnabled();
    if (participant.getEmail() != null && !sendEmailVerification) {
        // not verifying, so consider it verified
        account.setEmailVerified(true);
    }
    if (participant.getPhone() != null && !shouldSendVerification) {
        // not verifying, so consider it verified
        account.setPhoneVerified(true);
    }
    account.setSynapseUserId(participant.getSynapseUserId());
    // not save if the account is inaccessible after construction.
    if (BridgeUtils.hasValidIdentifier(account)) {
        accountService.createAccount(app, account);
    }
    // send verify email
    if (sendEmailVerification && !app.isAutoVerificationEmailSuppressed()) {
        accountWorkflowService.sendEmailVerificationToken(app, account.getId(), account.getEmail());
    }
    // If you create an account with a phone number, this opts the phone number in to receiving SMS. We do this
    // _before_ phone verification / sign-in, because we need to opt the user in to SMS in order to send phone
    // verification / sign-in.
    Phone phone = account.getPhone();
    if (phone != null) {
        // Note that there is no object with both accountId and phone, so we need to pass them in separately.
        smsService.optInPhoneNumber(account.getId(), phone);
    }
    // send verify phone number
    if (shouldSendVerification && !app.isAutoVerificationPhoneSuppressed()) {
        accountWorkflowService.sendPhoneVerificationToken(app, account.getId(), phone);
    }
    return new IdentifierHolder(account.getId());
}
Also used : Account(org.sagebionetworks.bridge.models.accounts.Account) IdentifierHolder(org.sagebionetworks.bridge.models.accounts.IdentifierHolder) PasswordAlgorithm(org.sagebionetworks.bridge.models.accounts.PasswordAlgorithm) Phone(org.sagebionetworks.bridge.models.accounts.Phone) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) StudyParticipantValidator(org.sagebionetworks.bridge.validators.StudyParticipantValidator) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 2 with StudyParticipantValidator

use of org.sagebionetworks.bridge.validators.StudyParticipantValidator in project BridgeServer2 by Sage-Bionetworks.

the class ParticipantService method updateParticipant.

public void updateParticipant(App app, StudyParticipant participant) {
    checkNotNull(app);
    checkNotNull(participant);
    StudyParticipantValidator validator = new StudyParticipantValidator(studyService, organizationService, app, false);
    Validate.entityThrowingException(validator, participant);
    Account account = getAccountThrowingException(app.getIdentifier(), participant.getId());
    updateAccountAndRoles(app, account, participant, false);
    // fields of the Account (see the Account.getStatus() accessor).
    if (participant.getStatus() != null) {
        if (RequestContext.get().isInRole(ADMIN, WORKER)) {
            account.setStatus(participant.getStatus());
        }
    }
    accountService.updateAccount(account);
}
Also used : Account(org.sagebionetworks.bridge.models.accounts.Account) StudyParticipantValidator(org.sagebionetworks.bridge.validators.StudyParticipantValidator)

Aggregations

Account (org.sagebionetworks.bridge.models.accounts.Account)2 StudyParticipantValidator (org.sagebionetworks.bridge.validators.StudyParticipantValidator)2 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 BridgeServiceException (org.sagebionetworks.bridge.exceptions.BridgeServiceException)1 IdentifierHolder (org.sagebionetworks.bridge.models.accounts.IdentifierHolder)1 PasswordAlgorithm (org.sagebionetworks.bridge.models.accounts.PasswordAlgorithm)1 Phone (org.sagebionetworks.bridge.models.accounts.Phone)1