public GroupDto findDefaultGroup(DbSession dbSession, String organizationUuid) { int defaultGroupId = dbClient.organizationDao().getDefaultGroupId(dbSession, organizationUuid) .orElseThrow(() -> new IllegalStateException(format("Default group cannot be found on organization '%s'", organizationUuid))); return requireNonNull(dbClient.groupDao().selectById(dbSession, defaultGroupId), format("Group '%s' cannot be found", defaultGroupId)); }
private void insertPermissionsForDefaultGroup(DbSession dbSession, PermissionTemplateDto template) { String organizationUuid = template.getOrganizationUuid(); Integer defaultGroupId = dbClient.organizationDao().getDefaultGroupId(dbSession, organizationUuid) .orElseThrow(() -> new IllegalStateException(format("Default group for organization %s is not defined", organizationUuid))); GroupDto defaultGroup = Optional.ofNullable(dbClient.groupDao().selectById(dbSession, defaultGroupId)) .orElseThrow(() -> new IllegalStateException(format("Default group with id %s for organization %s doesn't exist", defaultGroupId, organizationUuid))); insertGroupPermission(dbSession, template, UserRole.USER, defaultGroup); insertGroupPermission(dbSession, template, UserRole.CODEVIEWER, defaultGroup); }
@CheckForNull public GroupDto selectGroupById(int groupId) { return db.getDbClient().groupDao().selectById(db.getSession(), groupId); }
public GroupDto findGroupDto(DbSession dbSession, GroupWsRef ref) { if (ref.hasId()) { GroupDto group = dbClient.groupDao().selectById(dbSession, ref.getId()); checkFound(group, "No group with id '%s'", ref.getId()); return group; } OrganizationDto org = findOrganizationByKey(dbSession, ref.getOrganizationKey()); Optional<GroupDto> group = dbClient.groupDao().selectByName(dbSession, org.getUuid(), ref.getName()); checkFoundWithOptional(group, "No group with name '%s' in organization '%s'", ref.getName(), org.getKey()); return group.get(); }
public GroupIdOrAnyone findGroupOrAnyone(DbSession dbSession, GroupWsRef ref) { if (ref.hasId()) { GroupDto group = dbClient.groupDao().selectById(dbSession, ref.getId()); checkFound(group, "No group with id '%s'", ref.getId()); return GroupIdOrAnyone.from(group); } OrganizationDto org = findOrganizationByKey(dbSession, ref.getOrganizationKey()); if (ref.isAnyone()) { return GroupIdOrAnyone.forAnyone(org.getUuid()); } Optional<GroupDto> group = dbClient.groupDao().selectByName(dbSession, org.getUuid(), ref.getName()); checkFoundWithOptional(group, "No group with name '%s' in organization '%s'", ref.getName(), org.getKey()); return GroupIdOrAnyone.from(group.get()); }
@Test public void create_default_group() { OrganizationDto organizationDto = db.organizations().insert(); underTest.create(db.getSession(), organizationDto.getUuid()); Optional<Integer> defaultGroupId = db.getDbClient().organizationDao().getDefaultGroupId(db.getSession(), organizationDto.getUuid()); assertThat(defaultGroupId).isPresent(); assertThat(db.getDbClient().groupDao().selectById(db.getSession(), defaultGroupId.get())) .extracting(GroupDto::getName, GroupDto::getDescription) .containsOnly("Members", "All members of the organization"); }
@Override public void handle(Request request, Response response) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { int groupId = request.mandatoryParamAsInt(PARAM_GROUP_ID); GroupDto group = dbClient.groupDao().selectById(dbSession, groupId); checkFound(group, "Could not find a user group with id '%s'.", groupId); Optional<OrganizationDto> org = dbClient.organizationDao().selectByUuid(dbSession, group.getOrganizationUuid()); checkFoundWithOptional(org, "Could not find organization with id '%s'.", group.getOrganizationUuid()); userSession.checkPermission(ADMINISTER, org.get()); support.checkGroupIsNotDefault(dbSession, group); boolean changed = false; String newName = request.param(PARAM_GROUP_NAME); if (newName != null) { changed = true; UserGroupValidation.validateGroupName(newName); support.checkNameDoesNotExist(dbSession, group.getOrganizationUuid(), newName); group.setName(newName); } String description = request.param(PARAM_GROUP_DESCRIPTION); if (description != null) { changed = true; group.setDescription(description); } if (changed) { dbClient.groupDao().update(dbSession, group); dbSession.commit(); } writeResponse(dbSession, request, response, org.get(), group); } }
@Test public void enabling_support_creates_default_members_group_and_associate_org_members() { OrganizationDto defaultOrganization = dbTester.getDefaultOrganization(); OrganizationDto anotherOrganization = dbTester.organizations().insert(); UserDto user1 = dbTester.users().insertUser(); UserDto user2 = dbTester.users().insertUser(); UserDto userInAnotherOrganization = dbTester.users().insertUser(); dbTester.organizations().addMember(defaultOrganization, user1); dbTester.organizations().addMember(defaultOrganization, user2); dbTester.organizations().addMember(anotherOrganization, userInAnotherOrganization); dbTester.users().insertDefaultGroup(dbTester.getDefaultOrganization(), "sonar-users"); call(user1.getLogin()); Optional<Integer> defaultGroupId = dbTester.getDbClient().organizationDao().getDefaultGroupId(dbTester.getSession(), defaultOrganization.getUuid()); assertThat(defaultGroupId).isPresent(); GroupDto membersGroup = dbTester.getDbClient().groupDao().selectById(dbTester.getSession(), defaultGroupId.get()); assertThat(membersGroup).isNotNull(); assertThat(membersGroup.getName()).isEqualTo("Members"); assertThat(dbTester.getDbClient().groupMembershipDao().selectGroupIdsByUserId(dbTester.getSession(), user1.getId())).containsOnly(defaultGroupId.get()); assertThat(dbTester.getDbClient().groupMembershipDao().selectGroupIdsByUserId(dbTester.getSession(), user2.getId())).containsOnly(defaultGroupId.get()); assertThat(dbTester.getDbClient().groupMembershipDao().selectGroupIdsByUserId(dbTester.getSession(), userInAnotherOrganization.getId())).isEmpty(); }
public GroupDto findDefaultGroup(DbSession dbSession, String organizationUuid) { int defaultGroupId = dbClient.organizationDao().getDefaultGroupId(dbSession, organizationUuid) .orElseThrow(() -> new IllegalStateException(format("Default group cannot be found on organization '%s'", organizationUuid))); return requireNonNull(dbClient.groupDao().selectById(dbSession, defaultGroupId), format("Group '%s' cannot be found", defaultGroupId)); }
private void insertPermissionsForDefaultGroup(DbSession dbSession, PermissionTemplateDto template) { String organizationUuid = template.getOrganizationUuid(); Integer defaultGroupId = dbClient.organizationDao().getDefaultGroupId(dbSession, organizationUuid) .orElseThrow(() -> new IllegalStateException(format("Default group for organization %s is not defined", organizationUuid))); GroupDto defaultGroup = Optional.ofNullable(dbClient.groupDao().selectById(dbSession, defaultGroupId)) .orElseThrow(() -> new IllegalStateException(format("Default group with id %s for organization %s doesn't exist", defaultGroupId, organizationUuid))); insertGroupPermission(dbSession, template, UserRole.USER, defaultGroup); insertGroupPermission(dbSession, template, UserRole.CODEVIEWER, defaultGroup); }
public GroupDto findGroupDto(DbSession dbSession, GroupWsRef ref) { if (ref.hasId()) { GroupDto group = dbClient.groupDao().selectById(dbSession, ref.getId()); checkFound(group, "No group with id '%s'", ref.getId()); return group; } OrganizationDto org = findOrganizationByKey(dbSession, ref.getOrganizationKey()); Optional<GroupDto> group = dbClient.groupDao().selectByName(dbSession, org.getUuid(), ref.getName()); checkFoundWithOptional(group, "No group with name '%s' in organization '%s'", ref.getName(), org.getKey()); return group.get(); }
public GroupIdOrAnyone findGroupOrAnyone(DbSession dbSession, GroupWsRef ref) { if (ref.hasId()) { GroupDto group = dbClient.groupDao().selectById(dbSession, ref.getId()); checkFound(group, "No group with id '%s'", ref.getId()); return GroupIdOrAnyone.from(group); } OrganizationDto org = findOrganizationByKey(dbSession, ref.getOrganizationKey()); if (ref.isAnyone()) { return GroupIdOrAnyone.forAnyone(org.getUuid()); } Optional<GroupDto> group = dbClient.groupDao().selectByName(dbSession, org.getUuid(), ref.getName()); checkFoundWithOptional(group, "No group with name '%s' in organization '%s'", ref.getName(), org.getKey()); return GroupIdOrAnyone.from(group.get()); }
@Override public void handle(Request request, Response response) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { int groupId = request.mandatoryParamAsInt(PARAM_GROUP_ID); GroupDto group = dbClient.groupDao().selectById(dbSession, groupId); checkFound(group, "Could not find a user group with id '%s'.", groupId); Optional<OrganizationDto> org = dbClient.organizationDao().selectByUuid(dbSession, group.getOrganizationUuid()); checkFoundWithOptional(org, "Could not find organization with id '%s'.", group.getOrganizationUuid()); userSession.checkPermission(ADMINISTER, org.get()); support.checkGroupIsNotDefault(dbSession, group); boolean changed = false; String newName = request.param(PARAM_GROUP_NAME); if (newName != null) { changed = true; UserGroupValidation.validateGroupName(newName); support.checkNameDoesNotExist(dbSession, group.getOrganizationUuid(), newName); group.setName(newName); } String description = request.param(PARAM_GROUP_DESCRIPTION); if (description != null) { changed = true; group.setDescription(description); } if (changed) { dbClient.groupDao().update(dbSession, group); dbSession.commit(); } writeResponse(dbSession, request, response, org.get(), group); } }