Example #1
0
  @Test
  public void count_users_with_one_permission_when_the_last_one_is_in_a_group() {
    DbClient dbClient = db.getDbClient();

    UserDto user = dbClient.userDao().insert(db.getSession(), new UserDto().setActive(true));
    GroupDto group = dbClient.groupDao().insert(db.getSession(), new GroupDto());
    dbClient
        .userGroupDao()
        .insert(
            db.getSession(), new UserGroupDto().setGroupId(group.getId()).setUserId(user.getId()));
    dbClient
        .roleDao()
        .insertGroupRole(
            db.getSession(),
            new GroupRoleDto().setGroupId(group.getId()).setRole(GlobalPermissions.SYSTEM_ADMIN));

    int resultWithoutExcludingGroup =
        underTest.countUserPermissions(db.getSession(), GlobalPermissions.SYSTEM_ADMIN, null);
    int resultWithGroupExclusion =
        underTest.countUserPermissions(
            db.getSession(), GlobalPermissions.SYSTEM_ADMIN, group.getId());

    assertThat(resultWithoutExcludingGroup).isEqualTo(1);
    assertThat(resultWithGroupExclusion).isEqualTo(0);
  }
Example #2
0
  /**
   * * Creates an entry into the ODocument-Collection and create a new Class named "collectionName"
   *
   * @param collectionName
   * @return
   * @throws Throwable
   */
  public ODocument create(String collectionName) throws Throwable {
    Logger.trace("Method Start");
    try {
      if (existsCollection(collectionName))
        throw new InvalidCollectionException("Collection " + collectionName + " already exists");
    } catch (SqlInjectionException e) {
      throw new InvalidCollectionException(e);
    }
    ODocument doc = super.create();
    doc.field("name", collectionName);
    save(doc);

    // create new class
    OClass documentClass = db.getMetadata().getSchema().getClass(CLASS_NODE_NAME);
    db.getMetadata().getSchema().createClass(collectionName, documentClass);

    // grants to the new class
    ORole registeredRole = RoleDao.getRole(DefaultRoles.REGISTERED_USER.toString());
    ORole anonymousRole = RoleDao.getRole(DefaultRoles.ANONYMOUS_USER.toString());
    registeredRole.addRule(
        ODatabaseSecurityResources.CLASS + "." + collectionName, ORole.PERMISSION_ALL);
    registeredRole.addRule(
        ODatabaseSecurityResources.CLUSTER + "." + collectionName, ORole.PERMISSION_ALL);
    anonymousRole.addRule(
        ODatabaseSecurityResources.CLASS + "." + collectionName, ORole.PERMISSION_READ);
    anonymousRole.addRule(
        ODatabaseSecurityResources.CLUSTER + "." + collectionName, ORole.PERMISSION_READ);
    PermissionsHelper.grantRead(doc, registeredRole);
    PermissionsHelper.grantRead(doc, anonymousRole);
    Logger.trace("Method End");
    return doc;
  } // getNewModelInstance(String collectionName)
Example #3
0
  @Test
  public void retrieve_global_user_permissions() {
    db.prepareDbUnit(getClass(), "globalUserPermissions.xml");

    assertThat(underTest.selectUserPermissions(db.getSession(), "admin_user", null))
        .containsOnly(GlobalPermissions.SYSTEM_ADMIN, GlobalPermissions.QUALITY_PROFILE_ADMIN);
    assertThat(underTest.selectUserPermissions(db.getSession(), "profile_admin_user", null))
        .containsOnly(GlobalPermissions.QUALITY_PROFILE_ADMIN);
  }
Example #4
0
  @Test
  public void retrieve_resource_group_permissions() {
    db.prepareDbUnit(getClass(), "resourceGroupPermissions.xml");

    assertThat(underTest.selectGroupPermissions(db.getSession(), "sonar-administrators", 1L))
        .containsOnly(UserRole.ADMIN, UserRole.CODEVIEWER);
    assertThat(underTest.selectGroupPermissions(db.getSession(), "sonar-users", 1L))
        .containsOnly(UserRole.CODEVIEWER);
  }
Example #5
0
  @Test
  public void retrieve_resource_user_permissions() {
    db.prepareDbUnit(getClass(), "resourceUserPermissions.xml");

    assertThat(underTest.selectUserPermissions(db.getSession(), "admin_user", 1L))
        .containsOnly(UserRole.ADMIN, UserRole.USER);
    assertThat(underTest.selectUserPermissions(db.getSession(), "browse_admin_user", 1L))
        .containsOnly(UserRole.USER);
  }
Example #6
0
  @Test
  public void testUpdateRole() throws Exception {
    Role role = dao.getRoleByName("ROLE_USER");
    log.debug(role);
    role.setDescription("test descr");

    dao.save(role);

    role = dao.getRoleByName("ROLE_USER");
    assertEquals(role.getDescription(), "test descr");
  }
Example #7
0
  @Test
  public void select_group_permissions_by_permission_and_user_id() {
    long userId = 11L;

    underTest.insertGroupRole(
        dbSession, new GroupRoleDto().setRole(UserRole.ADMIN).setGroupId(1L).setResourceId(2L));
    groupDb.addUserToGroup(userId, 1L);
    underTest.insertGroupRole(
        dbSession, new GroupRoleDto().setRole(UserRole.ADMIN).setGroupId(2L).setResourceId(3L));
    groupDb.addUserToGroup(userId, 2L);
    // global permission - not returned
    groupDb.addUserToGroup(userId, 3L);
    underTest.insertGroupRole(
        dbSession, new GroupRoleDto().setRole(UserRole.ADMIN).setGroupId(3L).setResourceId(null));
    // project permission on another user id - not returned
    underTest.insertGroupRole(
        dbSession, new GroupRoleDto().setRole(UserRole.ADMIN).setGroupId(4L).setResourceId(4L));
    groupDb.addUserToGroup(12L, 4L);
    // project permission on another permission - not returned
    underTest.insertGroupRole(
        dbSession, new GroupRoleDto().setRole(UserRole.USER).setGroupId(5L).setResourceId(5L));
    groupDb.addUserToGroup(userId, 5L);
    // duplicates on resource id - should be returned once
    underTest.insertUserRole(
        dbSession, new UserRoleDto().setRole(UserRole.ADMIN).setUserId(userId).setResourceId(2L));
    underTest.insertGroupRole(
        dbSession, new GroupRoleDto().setRole(UserRole.ADMIN).setGroupId(3L).setResourceId(3L));
    db.commit();

    List<Long> result =
        underTest.selectComponentIdsByPermissionAndUserId(dbSession, UserRole.ADMIN, userId);

    assertThat(result).hasSize(2).containsExactly(2L, 3L);
  }
Example #8
0
  @Test
  public void count_users_with_one_specific_permission() {
    DbClient dbClient = db.getDbClient();
    UserDto user = dbClient.userDao().insert(db.getSession(), new UserDto().setActive(true));
    dbClient
        .roleDao()
        .insertUserRole(
            db.getSession(),
            new UserRoleDto()
                .setUserId(user.getId())
                .setResourceId(123L)
                .setRole(GlobalPermissions.SYSTEM_ADMIN));
    dbClient
        .roleDao()
        .insertUserRole(
            db.getSession(),
            new UserRoleDto().setUserId(user.getId()).setRole(GlobalPermissions.SYSTEM_ADMIN));
    dbClient
        .roleDao()
        .insertUserRole(
            db.getSession(),
            new UserRoleDto().setUserId(user.getId()).setRole(GlobalPermissions.SCAN_EXECUTION));

    int result =
        underTest.countUserPermissions(db.getSession(), GlobalPermissions.SYSTEM_ADMIN, null);

    assertThat(result).isEqualTo(1);
  }
Example #9
0
  @Test
  public void count_user_twice_when_user_and_group_permission() {
    DbClient dbClient = db.getDbClient();

    UserDto user = dbClient.userDao().insert(db.getSession(), new UserDto().setActive(true));
    GroupDto group = dbClient.groupDao().insert(db.getSession(), new GroupDto());
    dbClient
        .userGroupDao()
        .insert(
            db.getSession(), new UserGroupDto().setGroupId(group.getId()).setUserId(user.getId()));
    dbClient
        .roleDao()
        .insertGroupRole(
            db.getSession(),
            new GroupRoleDto().setGroupId(group.getId()).setRole(GlobalPermissions.SYSTEM_ADMIN));
    dbClient
        .roleDao()
        .insertUserRole(
            db.getSession(),
            new UserRoleDto().setUserId(user.getId()).setRole(GlobalPermissions.SYSTEM_ADMIN));

    int result =
        underTest.countUserPermissions(db.getSession(), GlobalPermissions.SYSTEM_ADMIN, null);

    assertThat(result).isEqualTo(2);
  }
Example #10
0
  @Test
  public void testAddUserRole() throws Exception {
    User user = dao.get(-1L);
    assertEquals(1, user.getRoles().size());

    Role role = rdao.getRoleByName(Constants.ADMIN_ROLE);
    user.addRole(role);
    dao.saveUser(user);
    flush();

    user = dao.get(-1L);
    assertEquals(2, user.getRoles().size());

    // add the same role twice - should result in no additional role
    user.addRole(role);
    dao.saveUser(user);
    flush();

    user = dao.get(-1L);
    assertEquals("more than 2 roles", 2, user.getRoles().size());

    user.getRoles().remove(role);
    dao.saveUser(user);
    flush();

    user = dao.get(-1L);
    assertEquals(1, user.getRoles().size());
  }
Example #11
0
  @Test
  @ExpectedException(DataAccessException.class)
  public void testAddAndRemoveUser() throws Exception {
    User user = new User("testuser");
    user.setPassword("testpass");
    user.setFirstName("Test");
    user.setLastName("Last");
    Address address = new Address();
    address.setCity("Denver");
    address.setProvince("CO");
    address.setCountry("USA");
    address.setPostalCode("80210");
    user.setAddress(address);
    user.setEmail("*****@*****.**");
    user.setWebsite("http://raibledesigns.com");

    Role role = rdao.getRoleByName(Constants.USER_ROLE);
    assertNotNull(role.getId());
    user.addRole(role);

    user = dao.saveUser(user);
    flush();

    assertNotNull(user.getId());
    user = dao.get(user.getId());
    assertEquals("testpass", user.getPassword());

    dao.remove(user);
    flush();

    // should throw DataAccessException
    dao.get(user.getId());
  }
Example #12
0
  public void testAddUserRole() throws Exception {
    User user = dao.get(2L);
    assertEquals(6, user.getRoles().size());

    Role role = rdao.getRoleByName("ROLE_EXTERNAL");
    assertEquals((Long) 4L, (Long) role.getId());
    user.addRole(role);
    user = dao.saveUser(user);
    // flush();

    user = dao.get(2L);
    assertEquals(7, user.getRoles().size());

    // add the same role twice - should result in no additional role
    user.addRole(role);
    dao.saveUser(user);
    // flush();

    user = dao.get(2L);
    assertEquals("more than 7 roles", 7, user.getRoles().size());

    user.getRoles().remove(role);
    dao.saveUser(user);
    // flush();

    user = dao.get(2L);
    assertEquals(6, user.getRoles().size());
  }
Example #13
0
  @Test
  public void retrieve_global_group_permissions() {
    db.prepareDbUnit(getClass(), "globalGroupPermissions.xml");

    assertThat(underTest.selectGroupPermissions(db.getSession(), "sonar-administrators", null))
        .containsOnly(
            GlobalPermissions.SYSTEM_ADMIN,
            GlobalPermissions.QUALITY_PROFILE_ADMIN,
            GlobalPermissions.DASHBOARD_SHARING);
    assertThat(underTest.selectGroupPermissions(db.getSession(), "sonar-users", null))
        .containsOnly(GlobalPermissions.DASHBOARD_SHARING);
    assertThat(underTest.selectGroupPermissions(db.getSession(), DefaultGroups.ANYONE, null))
        .containsOnly(GlobalPermissions.PROVISIONING, GlobalPermissions.SCAN_EXECUTION);
    assertThat(underTest.selectGroupPermissions(db.getSession(), "anyone", null))
        .containsOnly(GlobalPermissions.PROVISIONING, GlobalPermissions.SCAN_EXECUTION);
    assertThat(underTest.selectGroupPermissions(db.getSession(), "AnYoNe", null))
        .containsOnly(GlobalPermissions.PROVISIONING, GlobalPermissions.SCAN_EXECUTION);
  }
Example #14
0
  @Test
  public void delete_all_group_permissions_by_group_id() {
    db.prepareDbUnit(getClass(), "deleteGroupPermissionsByGroupId.xml");

    underTest.deleteGroupRolesByGroupId(db.getSession(), 100L);
    db.getSession().commit();

    db.assertDbUnit(getClass(), "deleteGroupPermissionsByGroupId-result.xml", "group_roles");
  }
Example #15
0
  @Test
  public void testAddAndRemoveRole() throws Exception {
    Role role = new Role("testrole");
    role.setDescription("new role descr");
    dao.save(role);
    // setComplete(); // change behavior from rollback to commit
    // endTransaction();

    // startNewTransaction();
    role = dao.getRoleByName("testrole");
    assertNotNull(role.getDescription());

    dao.removeRole("testrole");
    // setComplete();
    // endTransaction(); // deletes role from database

    role = dao.getRoleByName("testrole");
    assertNull(role);
  }
Example #16
0
  @Test
  public void testFindPage() throws Exception {
    RoleQueryParam queryParam = new RoleQueryParam();

    Page page = roleDao.findPage(queryParam);

    List<Role> roles = page.getData();
    for (Role role : roles) {
      System.out.println(role);
    }
  }
Example #17
0
  @Test
  public void delete_global_user_permission() {
    db.prepareDbUnit(getClass(), "globalUserPermissions.xml");

    UserRoleDto userRoleToDelete =
        new UserRoleDto().setUserId(200L).setRole(GlobalPermissions.QUALITY_PROFILE_ADMIN);

    underTest.deleteUserRole(userRoleToDelete, db.getSession());
    db.getSession().commit();

    db.assertDbUnit(getClass(), "globalUserPermissions-result.xml", "user_roles");
  }
Example #18
0
  @Test
  public void delete_resource_user_permission() {
    db.prepareDbUnit(getClass(), "resourceUserPermissions.xml");

    UserRoleDto userRoleToDelete =
        new UserRoleDto().setUserId(200L).setRole(UserRole.USER).setResourceId(1L);

    underTest.deleteUserRole(userRoleToDelete, db.getSession());
    db.getSession().commit();

    db.assertDbUnit(getClass(), "resourceUserPermissions-result.xml", "user_roles");
  }
Example #19
0
  @Test
  public void delete_global_group_permission() {
    db.prepareDbUnit(getClass(), "globalGroupPermissions.xml");

    GroupRoleDto groupRoleToDelete =
        new GroupRoleDto().setGroupId(100L).setRole(GlobalPermissions.QUALITY_PROFILE_ADMIN);

    underTest.deleteGroupRole(groupRoleToDelete, db.getSession());
    db.getSession().commit();

    db.assertDbUnit(getClass(), "globalGroupPermissions-result.xml", "group_roles");
  }
Example #20
0
  @Test
  public void delete_resource_group_permission() {
    db.prepareDbUnit(getClass(), "resourceGroupPermissions.xml");

    GroupRoleDto groupRoleToDelete =
        new GroupRoleDto().setGroupId(100L).setRole(UserRole.CODEVIEWER).setResourceId(1L);

    underTest.deleteGroupRole(groupRoleToDelete, db.getSession());
    db.getSession().commit();

    db.assertDbUnit(getClass(), "resourceGroupPermissions-result.xml", "group_roles");
  }
Example #21
0
  @Test
  public void select_user_permissions_by_permission_and_user_id() {
    underTest.insertUserRole(
        dbSession, new UserRoleDto().setRole(UserRole.ADMIN).setUserId(1L).setResourceId(2L));
    underTest.insertUserRole(
        dbSession, new UserRoleDto().setRole(UserRole.ADMIN).setUserId(1L).setResourceId(3L));
    // global permission - not returned
    underTest.insertUserRole(
        dbSession, new UserRoleDto().setRole(UserRole.ADMIN).setUserId(1L).setResourceId(null));
    // project permission on another user id - not returned
    underTest.insertUserRole(
        dbSession, new UserRoleDto().setRole(UserRole.ADMIN).setUserId(42L).setResourceId(2L));
    // project permission on another permission - not returned
    underTest.insertUserRole(
        dbSession,
        new UserRoleDto()
            .setRole(GlobalPermissions.SCAN_EXECUTION)
            .setUserId(1L)
            .setResourceId(2L));
    db.commit();

    List<Long> result =
        underTest.selectComponentIdsByPermissionAndUserId(dbSession, UserRole.ADMIN, 1L);

    assertThat(result).hasSize(2).containsExactly(2L, 3L);
  }
Example #22
0
  @Test
  public void should_remove_all_permissions() {
    db.prepareDbUnit(getClass(), "should_remove_all_permissions.xml");

    assertThat(underTest.selectGroupPermissions(db.getSession(), "devs", 123L)).hasSize(1);
    assertThat(underTest.selectGroupPermissions(db.getSession(), "other", 123L)).isEmpty();
    assertThat(underTest.selectUserPermissions(db.getSession(), "dave.loper", 123L)).hasSize(1);
    assertThat(underTest.selectUserPermissions(db.getSession(), "other.user", 123L)).isEmpty();

    underTest.removeAllPermissions(db.getSession(), 123L);
    db.getSession().commit();

    db.assertDbUnitTable(
        getClass(),
        "should_remove_all_permissions-result.xml",
        "group_roles",
        "group_id",
        "resource_id",
        "role");
    db.assertDbUnitTable(
        getClass(),
        "should_remove_all_permissions-result.xml",
        "user_roles",
        "user_id",
        "resource_id",
        "role");

    assertThat(underTest.selectGroupPermissions(db.getSession(), "devs", 123L)).isEmpty();
    assertThat(underTest.selectUserPermissions(db.getSession(), "dave.loper", 123L)).isEmpty();
  }
Example #23
0
  public void testChangeUserRole() throws Exception {
    User user = dao.get(2L);
    assertEquals(6, user.getRoles().size());

    assertTrue(user.hasRole("ROLE_ADMIN"));

    user.getRoles().clear();
    user.addRole(rdao.getRoleByName("ROLE_EXTERNAL"));
    dao.saveUser(user);

    assertEquals(1, user.getRoles().size());

    assertTrue(user.getRoles().iterator().next().getName().equals("ROLE_EXTERNAL"));
  }
Example #24
0
  public void testAddAndRemoveUser() throws Exception {
    User user = new User("testuser");
    user.setPassword("testpass");
    user.setFirstName("Test");
    user.setLastName("Last");
    Address address = new Address();
    address.setCity("Denver");
    address.setProvince("CO");
    address.setCountry("USA");
    address.setPostalCode("80210");
    user.setAddress(address);
    user.setEmail("*****@*****.**");
    user.setWebsite("http://tek42.com");
    user.setTimeZone("US/Central");

    // Here we are creating an org that should already be in the database...
    // Ideally, we somehow have an org object... from the other dao or whatever...
    /*
     * Account org = new Account(); org.setName("Tek42"); org.setId(1L);
     */
    Account org = adao.get(2L);
    System.out.println("Have org: " + org);
    user.setAccount(org);

    Role role = rdao.getRoleByName(Constants.USER_ROLE);
    assertNotNull(role.getId());
    user.addRole(role);

    user = dao.saveUser(user);

    assertNotNull(user.getId());

    user = dao.get(user.getId());

    assertEquals("Vigilant", user.getAccount().getName());
    assertEquals("testpass", user.getPassword());
    assertEquals("US/Central", user.getTimeZone());

    dao.remove(user.getId());

    try {
      dao.get(user.getId());
      fail("getUser didn't throw DataAccessException");
    } catch (DataAccessException d) {
      assertNotNull(d);
    }
  }
Example #25
0
  /**
   * *************************************************** Helper methods
   * ***************************************************
   */
  private Node createNode() throws InterruptedException {
    Comboitem item = typeBox.getSelectedItem();
    if (item == null) {
      Messagebox.show("Node type is required.");
      return null;
    }

    String nodeType = GeneralUtil.getNodeDescriptionByLabel(ntds, item.getLabel()).get("type");
    String firstName = firstNameBox.getValue().trim();
    String lastName = lastNameBox.getValue().trim();
    String midName = midNameBox.getValue().trim();
    String label = labelBox.getValue().trim();
    String username;
    if (nodeType.equals(Constants.NODE_TYPE_USER)) {
      if (firstName.isEmpty()) {
        Messagebox.show("First Name is required.");
        return null;
      } else if (firstName.length() > 80) {
        Messagebox.show("First Name cannot be longer than 80.");
        return null;
      } else if (GeneralUtil.containSpecialCharacter(firstName)) {
        Messagebox.show("First Name cannot contain special characters.");
        return null;
      }

      if (lastName.isEmpty()) {
        Messagebox.show("Last Name is required.");
        return null;
      } else if (lastName.length() > 80) {
        Messagebox.show("Last Name cannot be longer than 80.");
        return null;
      } else if (GeneralUtil.containSpecialCharacter(lastName)) {
        Messagebox.show("Last Name cannot contain special characters.");
        return null;
      }

      username = firstName.replace(" ", "_") + "__" + lastName.replace(" ", "_");
    } else {
      if (label.isEmpty()) {
        Messagebox.show("Label is required.");
        return null;
      } else if (label.length() > 255) {
        Messagebox.show("Label cannot be longer than 255.");
        return null;
      }
      username = GeneralUtil.replaceSpecialCharacter(label, "_").replace(" ", "_");
    }

    if (nodeDao.findByUsername(username) != null) {
      Messagebox.show("Username: "******" has already been used.");
      return null;
    }

    Survey survey = surveyDao.findById(1L);
    Node node = new Node();
    node.setType(nodeType);
    node.setUsername(username);
    node.setLabel(label);
    if (nodeType.equals(Constants.NODE_TYPE_USER)) {
      node.setFirstName(firstName);
      node.setLastName(lastName);
      node.setMidName(midName);
    }

    String defaultPassword = survey.getAttribute(Constants.SURVEY_DEFAULT_PASSWORD);
    if (defaultPassword == null) {
      defaultPassword = "******";
    }
    if (defaultPassword.equals("rAnDoM")) {
      RandomString rs = new RandomString(8);
      defaultPassword = rs.nextString();
    }
    node.setPassword(defaultPassword);

    node.setAttribute(
        Constants.NODE_LOGIN_MODE, survey.getAttribute(Constants.SURVEY_DEFAULT_LOGIN_MODE));

    // role
    node.getRoles().add(roleDao.findByName(Constants.ROLE_USER));

    // groups
    Set<Group> groups = new HashSet<Group>();
    AbstractQuestionRelation p = (AbstractQuestionRelation) getParent();
    groups.addAll(p.getQuestion().getAvailableGroups());
    groups.add(groupDao.findByName(Constants.GROUP_ALL));

    if (nodeType.equals(Constants.NODE_TYPE_USER)) {
      groups.add(groupDao.findByName(Constants.GROUP_USER));
    } else {
      String groupName = Constants.GROUP_NODE_TYPE_PREFIX + nodeType;
      Group group = groupDao.findByName(groupName);
      if (group == null) {
        group = new Group();
        group.setName(groupName);
        groupDao.save(group);
        logger.info("created new group: " + group.getName());
      }
      groups.add(group);
    }

    node.setGroups(groups);

    // save
    nodeDao.save(node);
    logger.debug("Node created: " + username);

    return node;
  }
Example #26
0
 @Override
 public void deleteRole(String roleId) {
   dao.deleteRole(roleId);
 }
Example #27
0
 @Override
 public void updateRole(Role role) {
   dao.updateRole(role);
 }
Example #28
0
 @Override
 public void createRole(Role role) {
   dao.insertRole(role);
 }
Example #29
0
 @Override
 public Role getRole(String roleId) {
   return dao.selectRole(roleId);
 }
Example #30
0
 @Override
 public Set<Role> getMenuRoles(String menuId) {
   return dao.selectMenuRoles(menuId);
 }