Search in sources :

Example 1 with ContactPermission

use of org.orcid.core.salesforce.model.ContactPermission in project ORCID-Source by ORCID.

the class SalesForceManagerImpl method checkContactUpdatePermissions.

@Override
public void checkContactUpdatePermissions(Collection<Contact> existingContacts, Collection<Contact> updatedContacts) {
    List<ContactPermission> permissions = calculateContactPermissions(existingContacts);
    Map<String, ContactPermission> permissionsMap = ContactPermission.mapByContactRoleId(permissions);
    Map<String, Contact> existingContactsMap = Contact.mapByContactRoleId(existingContacts);
    for (Contact updatedContact : updatedContacts) {
        String updatedContactRoleId = updatedContact.getRole().getId();
        Contact existingContact = existingContactsMap.get(updatedContactRoleId);
        if (existingContact == null) {
            throw new IllegalStateException("Should be able to update a non-existent contact");
        }
        if (contactChanged(existingContact, updatedContact)) {
            ContactPermission permission = permissionsMap.get(existingContact.getRole().getId());
            if (permission == null) {
                throw new IllegalStateException("Can't find permissions for existing contact");
            }
            if (!permission.isAllowedEdit()) {
                throw new OrcidUnauthorizedException("Insufficient permissions to update contact");
            }
        }
    }
}
Also used : ContactPermission(org.orcid.core.salesforce.model.ContactPermission) OrcidUnauthorizedException(org.orcid.core.exception.OrcidUnauthorizedException) OrcidString(org.orcid.core.cache.OrcidString) Contact(org.orcid.core.salesforce.model.Contact)

Example 2 with ContactPermission

use of org.orcid.core.salesforce.model.ContactPermission in project ORCID-Source by ORCID.

the class SalesForceManagerImpl method calculateContactPermissions.

@Override
public List<ContactPermission> calculateContactPermissions(Collection<Contact> contacts) {
    String currentUser = sourceManager.retrieveRealUserOrcid();
    boolean isCurrentUserSuperContact = contacts.stream().anyMatch(c -> currentUser.equals(c.getOrcid()) && isSuperContact(c));
    List<ContactPermission> permissions = new ArrayList<>();
    for (Contact contact : contacts) {
        ContactPermission permission = new ContactPermission();
        permission.setContactRoleId(contact.getRole().getId());
        if (isSuperContact(contact) || isVotingContact(contact)) {
            permission.setAllowedEdit(isCurrentUserSuperContact);
        } else {
            permission.setAllowedEdit(true);
        }
        permissions.add(permission);
    }
    return permissions;
}
Also used : ContactPermission(org.orcid.core.salesforce.model.ContactPermission) ArrayList(java.util.ArrayList) OrcidString(org.orcid.core.cache.OrcidString) Contact(org.orcid.core.salesforce.model.Contact)

Example 3 with ContactPermission

use of org.orcid.core.salesforce.model.ContactPermission in project ORCID-Source by ORCID.

the class SalesForceManagerImplTest method testCalculateContactPermissions.

@Test
public void testCalculateContactPermissions() {
    List<Contact> contacts = new ArrayList<>();
    contacts.add(createContactWithRole("contact1", "account1", "contact1@test.com", "0000-0000-0000-0001", "role1", ContactRoleType.MAIN_CONTACT));
    contacts.add(createContactWithRole("contact2", "account1", "contact2@test.com", "0000-0000-0000-0002", "role2", ContactRoleType.AGREEMENT_SIGNATORY));
    contacts.add(createContactWithRole("contact3", "account1", "contact3@test.com", "0000-0000-0000-0003", "role3", ContactRoleType.INVOICE_CONTACT));
    Contact votingContact = createContactWithRole("contact4", "account1", "contact4@test.com", "0000-0000-0000-0004", "role4", ContactRoleType.OTHER_CONTACT);
    votingContact.getRole().setVotingContact(true);
    contacts.add(votingContact);
    when(sourceManager.retrieveRealUserOrcid()).thenReturn("0000-0000-0000-0001");
    List<ContactPermission> permissions = salesForceManager.calculateContactPermissions(contacts);
    assertNotNull(permissions);
    assertEquals(4, permissions.size());
    Map<String, ContactPermission> permissionsMap = ContactPermission.mapByContactRoleId(permissions);
    assertTrue(permissionsMap.get("role1").isAllowedEdit());
    assertTrue(permissionsMap.get("role2").isAllowedEdit());
    assertTrue(permissionsMap.get("role3").isAllowedEdit());
    assertTrue(permissionsMap.get("role4").isAllowedEdit());
    when(sourceManager.retrieveRealUserOrcid()).thenReturn("0000-0000-0000-0003");
    permissions = salesForceManager.calculateContactPermissions(contacts);
    assertNotNull(permissions);
    assertEquals(4, permissions.size());
    permissionsMap = ContactPermission.mapByContactRoleId(permissions);
    assertFalse(permissionsMap.get("role1").isAllowedEdit());
    assertFalse(permissionsMap.get("role2").isAllowedEdit());
    assertTrue(permissionsMap.get("role3").isAllowedEdit());
    assertFalse(permissionsMap.get("role4").isAllowedEdit());
}
Also used : ContactPermission(org.orcid.core.salesforce.model.ContactPermission) ArrayList(java.util.ArrayList) Contact(org.orcid.core.salesforce.model.Contact) Test(org.junit.Test)

Aggregations

Contact (org.orcid.core.salesforce.model.Contact)3 ContactPermission (org.orcid.core.salesforce.model.ContactPermission)3 ArrayList (java.util.ArrayList)2 OrcidString (org.orcid.core.cache.OrcidString)2 Test (org.junit.Test)1 OrcidUnauthorizedException (org.orcid.core.exception.OrcidUnauthorizedException)1