Search in sources :

Example 1 with UserRole

use of org.broadinstitute.consent.http.models.UserRole in project consent by DataBiosphere.

the class UserWithRolesMapper method addRole.

private void addRole(ResultSet r, User user) throws SQLException {
    if (r.getObject("user_role_id") != null && r.getObject("user_id") != null && r.getObject("role_id") != null) {
        Integer dacId = (r.getObject("dac_id") == null) ? null : r.getInt("dac_id");
        UserRole role = new UserRole(r.getInt("user_role_id"), r.getInt("user_id"), r.getInt("role_id"), r.getString("name"), dacId);
        user.addRole(role);
    }
}
Also used : UserRole(org.broadinstitute.consent.http.models.UserRole)

Example 2 with UserRole

use of org.broadinstitute.consent.http.models.UserRole in project consent by DataBiosphere.

the class UserWithRolesReducer method accumulate.

@Override
public void accumulate(Map<Integer, User> map, RowView rowView) {
    User user = map.computeIfAbsent(rowView.getColumn("dacuserid", Integer.class), id -> rowView.getRow(User.class));
    try {
        if (Objects.nonNull(rowView.getColumn("user_role_id", Integer.class))) {
            UserRole ur = rowView.getRow(UserRole.class);
            user.addRole(ur);
        }
    } catch (MappingException e) {
    // Ignore any attempt to map a column that doesn't exist
    }
    try {
        if (Objects.nonNull(rowView.getColumn("i_id", Integer.class))) {
            Institution institution = rowView.getRow(Institution.class);
            // There are unusual cases where we somehow create an institution with null values
            if (Objects.nonNull(institution.getId())) {
                user.setInstitution(institution);
            }
        }
    } catch (MappingException e) {
    // Ignore institution mapping errors, possible for new users to not have an institution
    }
    // Below only adds LC if not currently saved on the array
    try {
        if (Objects.nonNull(rowView.getColumn("lc_id", Integer.class))) {
            LibraryCard lc = rowView.getRow(LibraryCard.class);
            try {
                if (Objects.nonNull(rowView.getColumn("lci_id", Integer.class))) {
                    Institution institution = rowView.getRow(Institution.class);
                    // There are unusual cases where we somehow create an institution with null values
                    if (Objects.nonNull(institution.getId())) {
                        lc.setInstitution(institution);
                    }
                }
            } catch (MappingException e) {
            // Ignore institution mapping errors
            }
            if (Objects.isNull(user.getLibraryCards()) || user.getLibraryCards().stream().noneMatch(card -> card.getId().equals(lc.getId()))) {
                user.addLibraryCard(lc);
            }
        }
    } catch (MappingException e) {
    // Ignore exceptions here, user may not have a library card issued under this instiution
    }
    try {
        if (Objects.nonNull(rowView.getColumn("up_property_id", Integer.class))) {
            UserProperty p = rowView.getRow(UserProperty.class);
            user.addProperty(p);
            // Note that the completed field is deprecated and will be removed in a future PR.
            if (p.getPropertyKey().equalsIgnoreCase(UserFields.COMPLETED.getValue())) {
                user.setProfileCompleted(Boolean.valueOf(p.getPropertyValue()));
            }
        }
    } catch (MappingException e) {
    // Ignore any attempt to map a column that doesn't exist
    }
}
Also used : LibraryCard(org.broadinstitute.consent.http.models.LibraryCard) Objects(java.util.Objects) Institution(org.broadinstitute.consent.http.models.Institution) UserProperty(org.broadinstitute.consent.http.models.UserProperty) LinkedHashMapRowReducer(org.jdbi.v3.core.result.LinkedHashMapRowReducer) Map(java.util.Map) RowView(org.jdbi.v3.core.result.RowView) UserRole(org.broadinstitute.consent.http.models.UserRole) MappingException(org.jdbi.v3.core.mapper.MappingException) UserFields(org.broadinstitute.consent.http.enumeration.UserFields) User(org.broadinstitute.consent.http.models.User) LibraryCard(org.broadinstitute.consent.http.models.LibraryCard) User(org.broadinstitute.consent.http.models.User) UserProperty(org.broadinstitute.consent.http.models.UserProperty) UserRole(org.broadinstitute.consent.http.models.UserRole) Institution(org.broadinstitute.consent.http.models.Institution) MappingException(org.jdbi.v3.core.mapper.MappingException)

Example 3 with UserRole

use of org.broadinstitute.consent.http.models.UserRole in project consent by DataBiosphere.

the class UserServiceTest method testCreateUserInvalidRoleCase2.

@Test(expected = BadRequestException.class)
public void testCreateUserInvalidRoleCase2() {
    User u = generateUser();
    List<UserRole> roles = List.of(generateRole(UserRoles.MEMBER.getRoleId()));
    u.setRoles(roles);
    initService();
    service.createUser(u);
}
Also used : AuthUser(org.broadinstitute.consent.http.models.AuthUser) User(org.broadinstitute.consent.http.models.User) SimplifiedUser(org.broadinstitute.consent.http.service.UserService.SimplifiedUser) UserRole(org.broadinstitute.consent.http.models.UserRole) Test(org.junit.Test)

Example 4 with UserRole

use of org.broadinstitute.consent.http.models.UserRole in project consent by DataBiosphere.

the class MetricsServiceTest method generateDac.

private Dac generateDac() {
    Dac dac = new Dac();
    dac.setDacId(1);
    dac.setDescription("description");
    dac.setName("dac1");
    User chairUser = new User();
    chairUser.setDacUserId(1);
    chairUser.setEmail("chair@test.org");
    chairUser.setDisplayName("Chair");
    UserRole chairRole = new UserRole(UserRoles.CHAIRPERSON.getRoleId(), UserRoles.CHAIRPERSON.getRoleName());
    chairUser.setRoles(Collections.singletonList(chairRole));
    User memberUser = new User();
    memberUser.setDacUserId(2);
    memberUser.setEmail("member@test.org");
    memberUser.setDisplayName("Member");
    UserRole memberRole = new UserRole(UserRoles.MEMBER.getRoleId(), UserRoles.MEMBER.getRoleName());
    memberUser.setRoles(Collections.singletonList(memberRole));
    dac.setChairpersons(Collections.singletonList(chairUser));
    dac.setMembers(Collections.singletonList(memberUser));
    return dac;
}
Also used : User(org.broadinstitute.consent.http.models.User) UserRole(org.broadinstitute.consent.http.models.UserRole) Dac(org.broadinstitute.consent.http.models.Dac)

Example 5 with UserRole

use of org.broadinstitute.consent.http.models.UserRole in project consent by DataBiosphere.

the class UserServiceTest method testFindUserByIdWithRoles.

@Test
public void testFindUserByIdWithRoles() {
    User u = generateUser();
    List<UserRole> roleList = List.of(generateRole(UserRoles.RESEARCHER.getRoleId()), generateRole(UserRoles.MEMBER.getRoleId()));
    u.setRoles(roleList);
    when(userDAO.findUserById(any())).thenReturn(u);
    initService();
    User user = service.findUserById(u.getDacUserId());
    assertNotNull(user);
    assertEquals(u.getEmail(), user.getEmail());
    assertFalse(u.getRoles().isEmpty());
    assertEquals(2, u.getRoles().size());
}
Also used : AuthUser(org.broadinstitute.consent.http.models.AuthUser) User(org.broadinstitute.consent.http.models.User) SimplifiedUser(org.broadinstitute.consent.http.service.UserService.SimplifiedUser) UserRole(org.broadinstitute.consent.http.models.UserRole) Test(org.junit.Test)

Aggregations

UserRole (org.broadinstitute.consent.http.models.UserRole)88 User (org.broadinstitute.consent.http.models.User)78 Test (org.junit.Test)71 AuthUser (org.broadinstitute.consent.http.models.AuthUser)54 Response (javax.ws.rs.core.Response)40 DarCollection (org.broadinstitute.consent.http.models.DarCollection)20 Vote (org.broadinstitute.consent.http.models.Vote)18 DataSet (org.broadinstitute.consent.http.models.DataSet)13 Date (java.util.Date)12 PaginationResponse (org.broadinstitute.consent.http.models.PaginationResponse)11 DataAccessRequest (org.broadinstitute.consent.http.models.DataAccessRequest)10 SimplifiedUser (org.broadinstitute.consent.http.service.UserService.SimplifiedUser)10 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 GoogleUser (org.broadinstitute.consent.http.authentication.GoogleUser)8 Consent (org.broadinstitute.consent.http.models.Consent)7 Dac (org.broadinstitute.consent.http.models.Dac)7 NotFoundException (javax.ws.rs.NotFoundException)6 BadRequestException (javax.ws.rs.BadRequestException)5 DataAccessRequestManage (org.broadinstitute.consent.http.models.DataAccessRequestManage)4 Election (org.broadinstitute.consent.http.models.Election)4