Search in sources :

Example 1 with SubpopulationGuid

use of org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid in project BridgeServer2 by Sage-Bionetworks.

the class CacheProviderTest method assertSession.

private void assertSession(String json) {
    JedisOps jedisOps = mock(JedisOps.class);
    when(jedisOps.get(TOKEN_TO_USER_ID.toString())).thenReturn(USER_ID);
    when(jedisOps.get(USER_ID_TO_SESSION.toString())).thenReturn(json);
    cacheProvider.setJedisOps(jedisOps);
    UserSession session = cacheProvider.getUserSession(DECRYPTED_SESSION_TOKEN);
    assertTrue(session.isAuthenticated());
    assertEquals(session.getEnvironment(), Environment.LOCAL);
    assertEquals(session.getSessionToken(), DECRYPTED_SESSION_TOKEN);
    assertEquals(session.getInternalSessionToken(), "4f0937a5-6ebf-451b-84bc-fbf649b9e93c");
    assertEquals(session.getId(), "6gq4jGXLmAxVbLLmVifKN4");
    assertEquals(session.getAppId(), TEST_APP_ID);
    StudyParticipant participant = session.getParticipant();
    assertEquals(participant.getFirstName(), "Bridge");
    assertEquals(participant.getLastName(), "IT");
    assertEquals(participant.getEmail(), "bridgeit@sagebase.org");
    assertEquals(participant.getSharingScope(), SharingScope.NO_SHARING);
    assertEquals(participant.getCreatedOn(), DateTime.parse("2016-04-21T16:48:22.386Z"));
    assertEquals(participant.getRoles(), Sets.newHashSet(Roles.ADMIN));
    assertEquals(participant.getLanguages(), ImmutableList.of("en", "fr"));
    assertEquals(participant.getExternalId(), "ABC");
    assertEquals(ENCRYPTOR.decrypt(ENCRYPTED_SESSION_TOKEN), participant.getHealthCode());
    SubpopulationGuid apiGuid = SubpopulationGuid.create(TEST_APP_ID);
    Map<SubpopulationGuid, ConsentStatus> consentStatuses = session.getConsentStatuses();
    ConsentStatus status = consentStatuses.get(apiGuid);
    assertEquals(status.getName(), "Default Consent Group");
    assertEquals(status.getSubpopulationGuid(), apiGuid.getGuid());
    assertTrue(status.getSignedMostRecentConsent());
    assertTrue(status.isRequired());
    assertFalse(status.isConsented());
}
Also used : JedisOps(org.sagebionetworks.bridge.redis.JedisOps) ConsentStatus(org.sagebionetworks.bridge.models.accounts.ConsentStatus) UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) SubpopulationGuid(org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid) StudyParticipant(org.sagebionetworks.bridge.models.accounts.StudyParticipant)

Example 2 with SubpopulationGuid

use of org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid in project BridgeServer2 by Sage-Bionetworks.

the class ConsentService method withdrawFromApp.

/**
 * Withdraw user from any and all consents, turn off sharing, unregister the device from any notifications, and
 * delete the identifiers of the account. Because a user's criteria for being included in a consent can change
 * over time, this is really the best method for ensuring a user is withdrawn from everything. But in cases where
 * there are apps with distinct and separate consents, you can also selectively withdraw from the consent for
 * a specific subpopulation without dropping out of the app.
 */
public void withdrawFromApp(App app, StudyParticipant participant, Withdrawal withdrawal, long withdrewOn) {
    checkNotNull(app);
    checkNotNull(withdrawal);
    checkArgument(withdrewOn > 0);
    AccountId accountId = AccountId.forId(app.getIdentifier(), participant.getId());
    Account account = accountService.getAccount(accountId).orElseThrow(() -> new EntityNotFoundException(Account.class));
    for (SubpopulationGuid subpopGuid : account.getAllConsentSignatureHistories().keySet()) {
        if (withdrawSignatures(account, subpopGuid, withdrewOn)) {
            Subpopulation subpop = subpopService.getSubpopulation(app.getIdentifier(), subpopGuid);
            account.getDataGroups().removeAll(subpop.getDataGroupsAssignedWhileConsented());
        }
    }
    sendWithdrawEmail(app, account, withdrawal, withdrewOn);
    // Forget this person. If the user registers again at a later date, it is as if they have
    // created a new account. But we hold on to this record so we can still retrieve the consent
    // records for a given healthCode. We also don't delete external ID/study relationships
    // so studies can continue to view withdrawals by health code.
    account.setSharingScope(SharingScope.NO_SHARING);
    account.setFirstName(null);
    account.setLastName(null);
    account.setNotifyByEmail(false);
    account.setEmail(null);
    account.setEmailVerified(false);
    account.setPhone(null);
    account.setPhoneVerified(false);
    for (Enrollment enrollment : account.getActiveEnrollments()) {
        Enrollment withdrawnEnrollment = Enrollment.create(enrollment.getAppId(), enrollment.getStudyId(), enrollment.getAccountId());
        withdrawnEnrollment.setWithdrawnOn(new DateTime(withdrewOn));
        withdrawnEnrollment.setWithdrawalNote(withdrawal.getReason());
        enrollmentService.unenroll(account, withdrawnEnrollment);
    }
    accountService.updateAccount(account);
    notificationsService.deleteAllRegistrations(app.getIdentifier(), participant.getHealthCode());
}
Also used : Account(org.sagebionetworks.bridge.models.accounts.Account) AccountId(org.sagebionetworks.bridge.models.accounts.AccountId) Subpopulation(org.sagebionetworks.bridge.models.subpopulations.Subpopulation) Enrollment(org.sagebionetworks.bridge.models.studies.Enrollment) SubpopulationGuid(org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid) EntityNotFoundException(org.sagebionetworks.bridge.exceptions.EntityNotFoundException) DateTime(org.joda.time.DateTime)

Example 3 with SubpopulationGuid

use of org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid in project BridgeServer2 by Sage-Bionetworks.

the class ConsentService method getConsentStatuses.

/**
 * Get all the consent status objects for this user. From these, we determine if the user
 * has consented to the right consents to have access to the app, and whether or not those
 * consents are up-to-date.
 */
public Map<SubpopulationGuid, ConsentStatus> getConsentStatuses(CriteriaContext context, Account account) {
    checkNotNull(context);
    ImmutableMap.Builder<SubpopulationGuid, ConsentStatus> builder = new ImmutableMap.Builder<>();
    for (Subpopulation subpop : subpopService.getSubpopulationsForUser(context)) {
        ConsentSignature signature = account.getActiveConsentSignature(subpop.getGuid());
        boolean hasConsented = (signature != null);
        boolean hasSignedActiveConsent = (hasConsented && signature.getConsentCreatedOn() == subpop.getPublishedConsentCreatedOn());
        ConsentStatus status = new ConsentStatus.Builder().withName(subpop.getName()).withGuid(subpop.getGuid()).withRequired(subpop.isRequired()).withConsented(hasConsented).withSignedMostRecentConsent(hasSignedActiveConsent).withSignedOn(hasConsented ? signature.getSignedOn() : null).build();
        builder.put(subpop.getGuid(), status);
    }
    return builder.build();
}
Also used : ConsentSignature(org.sagebionetworks.bridge.models.subpopulations.ConsentSignature) ConsentStatus(org.sagebionetworks.bridge.models.accounts.ConsentStatus) Subpopulation(org.sagebionetworks.bridge.models.subpopulations.Subpopulation) SubpopulationGuid(org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 with SubpopulationGuid

use of org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid in project BridgeServer2 by Sage-Bionetworks.

the class IntentService method submitIntentToParticipate.

public void submitIntentToParticipate(IntentToParticipate intent) {
    Validate.entityThrowingException(IntentToParticipateValidator.INSTANCE, intent);
    // If the account exists, do nothing.
    AccountId accountId = null;
    if (intent.getPhone() != null) {
        accountId = AccountId.forPhone(intent.getAppId(), intent.getPhone());
    } else {
        accountId = AccountId.forEmail(intent.getAppId(), intent.getEmail());
    }
    Account account = accountService.getAccount(accountId).orElse(null);
    if (account != null) {
        return;
    }
    // validate app exists
    App app = appService.getApp(intent.getAppId());
    // validate subpopulation exists
    SubpopulationGuid guid = SubpopulationGuid.create(intent.getSubpopGuid());
    subpopService.getSubpopulation(app.getIdentifier(), guid);
    // validate it has not yet been submitted
    // the validator has ensured that phone or email, but not both, have been provided;
    CacheKey cacheKey = (intent.getPhone() == null) ? CacheKey.itp(guid, app.getIdentifier(), intent.getEmail()) : CacheKey.itp(guid, app.getIdentifier(), intent.getPhone());
    if (cacheProvider.getObject(cacheKey, IntentToParticipate.class) == null) {
        cacheProvider.setObject(cacheKey, intent, EXPIRATION_IN_SECONDS);
        // sent immediately after consenting.
        if (!app.getInstallLinks().isEmpty()) {
            participantService.sendInstallLinkMessage(app, TRANSACTIONAL, null, intent.getEmail(), intent.getPhone(), intent.getOsName());
        }
    }
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) Account(org.sagebionetworks.bridge.models.accounts.Account) AccountId(org.sagebionetworks.bridge.models.accounts.AccountId) SubpopulationGuid(org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid) IntentToParticipate(org.sagebionetworks.bridge.models.itp.IntentToParticipate) CacheKey(org.sagebionetworks.bridge.cache.CacheKey)

Example 5 with SubpopulationGuid

use of org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid in project BridgeServer2 by Sage-Bionetworks.

the class HibernateAccountTest method consentSignatureHistories.

@Test
public void consentSignatureHistories() {
    HibernateAccount account = new HibernateAccount();
    addConsentHistories(account);
    // Test getAllConsentSignaturehistories()
    Map<SubpopulationGuid, List<ConsentSignature>> histories = account.getAllConsentSignatureHistories();
    List<ConsentSignature> history1 = histories.get(GUID1);
    assertEquals(history1.size(), 3);
    // Signed on values are copied over from keys
    assertEquals(history1.get(0).getSignedOn(), TIME1);
    assertEquals(history1.get(1).getSignedOn(), TIME2);
    assertEquals(history1.get(2).getSignedOn(), TIME3);
    List<ConsentSignature> history2 = histories.get(GUID2);
    assertEquals(history2.size(), 2);
    // Signed on values are copied over from keys
    assertEquals(history2.get(0).getSignedOn(), TIME4);
    assertEquals(history2.get(1).getSignedOn(), TIME5);
    // Test getConsentSignatureHistory(guid). Should produce identical results.
    history1 = account.getConsentSignatureHistory(GUID1);
    assertEquals(history1.size(), 3);
    // Signed on values are copied over from keys
    assertEquals(history1.get(0).getSignedOn(), TIME1);
    assertEquals(history1.get(1).getSignedOn(), TIME2);
    assertEquals(history1.get(2).getSignedOn(), TIME3);
    history2 = account.getConsentSignatureHistory(GUID2);
    assertEquals(history2.size(), 2);
    // Signed on values are copied over from keys
    assertEquals(history2.get(0).getSignedOn(), TIME4);
    assertEquals(history2.get(1).getSignedOn(), TIME5);
    // The last consent in the series was withdrawn, so this consent is not active.
    ConsentSignature sig1 = account.getActiveConsentSignature(GUID1);
    assertNull(sig1);
    ConsentSignature sig2 = account.getActiveConsentSignature(GUID2);
    assertEquals(history2.get(1), sig2);
    // Add a consent to the withdrawn series.
    ConsentSignature sig3 = new ConsentSignature.Builder().withBirthdate("1980-01-01").withConsentCreatedOn(1L).withName("Name").withSignedOn(600L).build();
    List<ConsentSignature> signatures = Lists.newArrayList();
    signatures.addAll(history1);
    signatures.add(sig3);
    account.setConsentSignatureHistory(GUID1, signatures);
    sig1 = account.getActiveConsentSignature(GUID1);
    assertEquals(account.getAllConsentSignatureHistories().get(GUID1).get(3), sig1);
}
Also used : ConsentSignature(org.sagebionetworks.bridge.models.subpopulations.ConsentSignature) SubpopulationGuid(org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid) List(java.util.List) Test(org.testng.annotations.Test)

Aggregations

SubpopulationGuid (org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid)42 UserSession (org.sagebionetworks.bridge.models.accounts.UserSession)20 Test (org.testng.annotations.Test)16 App (org.sagebionetworks.bridge.models.apps.App)14 ConsentStatus (org.sagebionetworks.bridge.models.accounts.ConsentStatus)13 StudyParticipant (org.sagebionetworks.bridge.models.accounts.StudyParticipant)11 Subpopulation (org.sagebionetworks.bridge.models.subpopulations.Subpopulation)11 Enrollment (org.sagebionetworks.bridge.models.studies.Enrollment)6 ConsentSignature (org.sagebionetworks.bridge.models.subpopulations.ConsentSignature)6 PostMapping (org.springframework.web.bind.annotation.PostMapping)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 RequestContext (org.sagebionetworks.bridge.RequestContext)5 CriteriaContext (org.sagebionetworks.bridge.models.CriteriaContext)5 GetMapping (org.springframework.web.bind.annotation.GetMapping)5 DateTime (org.joda.time.DateTime)4 Account (org.sagebionetworks.bridge.models.accounts.Account)4 AccountId (org.sagebionetworks.bridge.models.accounts.AccountId)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 DynamoApp (org.sagebionetworks.bridge.dynamodb.DynamoApp)3 EntityNotFoundException (org.sagebionetworks.bridge.exceptions.EntityNotFoundException)3