Search in sources :

Example 1 with UserGroup

use of org.sagebionetworks.repo.model.UserGroup in project Synapse-Repository-Services by Sage-Bionetworks.

the class JDONodeQueryAuthorizationTest method createGroup.

/**
 * Helper to create a group.
 * @param name
 * @return
 * @throws DatastoreException
 * @throws InvalidModelException
 * @throws NotFoundException
 */
private UserGroup createGroup(String name) throws DatastoreException, InvalidModelException, NotFoundException {
    UserGroup group = userGroupDAO.findGroup(name, false);
    String id = null;
    if (group == null) {
        group = new UserGroup();
        group.setName(name);
        group.setIsIndividual(false);
        id = userGroupDAO.create(group);
    } else {
        id = group.getId();
    }
    groupsToDelete.add(id);
    return userGroupDAO.get(id);
}
Also used : UserGroup(org.sagebionetworks.repo.model.UserGroup)

Example 2 with UserGroup

use of org.sagebionetworks.repo.model.UserGroup in project Synapse-Repository-Services by Sage-Bionetworks.

the class JDONodeQueryAuthorizationTest method createUser.

/**
 * Helper for creating a new user.
 * @param name
 * @param isAdmin
 * @return
 * @throws DatastoreException
 * @throws InvalidModelException
 * @throws NotFoundException
 */
private UserInfo createUser(String name, boolean isAdmin) throws DatastoreException, InvalidModelException, NotFoundException {
    User user = new User();
    user.setUserId(name);
    // Create a group for this user
    String userGroupName = name + "group";
    // new UserGroup();
    UserGroup group = userGroupDAO.findGroup(userGroupName, true);
    String id = null;
    if (group == null) {
        group = new UserGroup();
        group.setName(userGroupName);
        group.setIsIndividual(true);
        id = userGroupDAO.create(group);
    } else {
        id = group.getId();
    }
    groupsToDelete.add(id);
    group = userGroupDAO.get(id);
    UserInfo info = new UserInfo(isAdmin);
    info.setUser(user);
    info.setIndividualGroup(group);
    info.setGroups(new ArrayList<UserGroup>());
    info.getGroups().add(group);
    return info;
}
Also used : User(org.sagebionetworks.repo.model.User) UserInfo(org.sagebionetworks.repo.model.UserInfo) UserGroup(org.sagebionetworks.repo.model.UserGroup)

Example 3 with UserGroup

use of org.sagebionetworks.repo.model.UserGroup in project Synapse-Repository-Services by Sage-Bionetworks.

the class JDONodeQueryUnitTest method testAuthorizationSqlNonAdminWithGroups.

@Test
public void testAuthorizationSqlNonAdminWithGroups() throws Exception {
    HashMap<String, Object> params = new HashMap<String, Object>();
    UserInfo nonAdminUserInfo = Mockito.mock(UserInfo.class);
    when(nonAdminUserInfo.isAdmin()).thenReturn(false);
    ArrayList<UserGroup> groups = new ArrayList<UserGroup>();
    UserGroup group = Mockito.mock(UserGroup.class);
    when(group.getId()).thenReturn("123");
    groups.add(group);
    group = Mockito.mock(UserGroup.class);
    when(group.getId()).thenReturn("124");
    groups.add(group);
    when(nonAdminUserInfo.getGroups()).thenReturn(groups);
    // This should build a query with two groups
    String sql = QueryUtils.buildAuthorizationFilter(nonAdminUserInfo, params);
    assertNotNull(sql);
    // It should not be an empty string.
    assertFalse("".equals(sql.trim()));
    log.info(sql);
    log.info(params);
    // Check the bind variables.
    assertEquals(ACCESS_TYPE.READ.name(), params.get(AuthorizationSqlUtil.ACCESS_TYPE_BIND_VAR));
    Long groupBindValue0 = (Long) params.get(AuthorizationSqlUtil.BIND_VAR_PREFIX + "0");
    assertNotNull(groupBindValue0);
    Long groupBindValue1 = (Long) params.get(AuthorizationSqlUtil.BIND_VAR_PREFIX + "1");
    assertNotNull(groupBindValue1);
    assertTrue(123L == groupBindValue0.longValue() || 123L == groupBindValue1.longValue());
    assertTrue(124L == groupBindValue0.longValue() || 124L == groupBindValue1.longValue());
    System.out.print(sql);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UserInfo(org.sagebionetworks.repo.model.UserInfo) UserGroup(org.sagebionetworks.repo.model.UserGroup) Test(org.junit.Test)

Example 4 with UserGroup

use of org.sagebionetworks.repo.model.UserGroup in project Synapse-Repository-Services by Sage-Bionetworks.

the class JDONodeQueryUnitTest method testAuthorizationSqlNonAdminuserEmptyGroups.

@Test(expected = IllegalArgumentException.class)
public void testAuthorizationSqlNonAdminuserEmptyGroups() throws Exception {
    UserInfo nonAdminUserInfo = Mockito.mock(UserInfo.class);
    when(nonAdminUserInfo.isAdmin()).thenReturn(false);
    when(nonAdminUserInfo.getGroups()).thenReturn(new ArrayList<UserGroup>());
    HashMap<String, Object> params = new HashMap<String, Object>();
    // Should throw an exception.
    String sql = QueryUtils.buildAuthorizationFilter(nonAdminUserInfo, params);
}
Also used : HashMap(java.util.HashMap) UserInfo(org.sagebionetworks.repo.model.UserInfo) UserGroup(org.sagebionetworks.repo.model.UserGroup) Test(org.junit.Test)

Example 5 with UserGroup

use of org.sagebionetworks.repo.model.UserGroup in project Synapse-Repository-Services by Sage-Bionetworks.

the class UserGroupCacheImplTest method testCache.

@Test
public void testCache() throws DatastoreException, NotFoundException {
    assertNotNull(userGroupCache);
    // Look up even object using the ID and Odd using the name
    for (int i = 0; i < userGroups.size(); i++) {
        UserGroup ug = userGroups.get(i);
        Long expectedId = KeyFactory.stringToKey(ug.getId());
        if (i % 2 == 0) {
            Long id = userGroupCache.getIdForUserGroupName(ug.getName());
            assertNotNull(id);
            assertEquals(id, expectedId);
            // Try the other way
            String name = userGroupCache.getUserGroupNameForId(expectedId);
            assertNotNull(name);
            assertEquals(ug.getName(), name);
        } else {
            String name = userGroupCache.getUserGroupNameForId(expectedId);
            assertNotNull(name);
            assertEquals(ug.getName(), name);
            // try the other way
            Long id = userGroupCache.getIdForUserGroupName(ug.getName());
            assertNotNull(id);
            assertEquals(id, expectedId);
        }
    }
}
Also used : UserGroup(org.sagebionetworks.repo.model.UserGroup) Test(org.junit.Test)

Aggregations

UserGroup (org.sagebionetworks.repo.model.UserGroup)106 Test (org.junit.Test)43 UserInfo (org.sagebionetworks.repo.model.UserInfo)29 ArrayList (java.util.ArrayList)20 Before (org.junit.Before)19 Date (java.util.Date)18 HashSet (java.util.HashSet)17 UserProfile (org.sagebionetworks.repo.model.UserProfile)17 AccessControlList (org.sagebionetworks.repo.model.AccessControlList)16 ACCESS_TYPE (org.sagebionetworks.repo.model.ACCESS_TYPE)11 HashMap (java.util.HashMap)10 ResourceAccess (org.sagebionetworks.repo.model.ResourceAccess)10 DBOUserGroup (org.sagebionetworks.repo.model.dbo.persistence.DBOUserGroup)10 DatastoreException (org.sagebionetworks.repo.model.DatastoreException)9 ObjectSchema (org.sagebionetworks.schema.ObjectSchema)9 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)8 InvalidModelException (org.sagebionetworks.repo.model.InvalidModelException)6 Node (org.sagebionetworks.repo.model.Node)6 User (org.sagebionetworks.repo.model.User)6 NotFoundException (org.sagebionetworks.repo.web.NotFoundException)5