import org.sonar.core.util.UuidFactory;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
+import org.sonar.db.organization.OrganizationDto;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
return Optional.ofNullable(mapper.selectByInstallationId(alm.getId(), installationId));
}
+ public Optional<AlmAppInstallDto> selectByOrganization(DbSession dbSession, ALM alm, OrganizationDto organization) {
+ AlmAppInstallMapper mapper = getMapper(dbSession);
+ return Optional.ofNullable(mapper.selectByOrganizationUuid(alm.getId(), organization.getUuid()));
+ }
+
+
public List<AlmAppInstallDto> selectUnboundByUserExternalId(DbSession dbSession, String userExternalId) {
return getMapper(dbSession).selectUnboundByUserExternalId(userExternalId);
}
@CheckForNull
AlmAppInstallDto selectByUuid(@Param("uuid") String uuid);
+ @CheckForNull
+ AlmAppInstallDto selectByOrganizationUuid(@Param("almId") String almId, @Param("organizationUuid") String organizationUuid);
+
List<AlmAppInstallDto> selectUnboundByUserExternalId(@Param("userExternalId") String userExternalId);
void insert(@Param("uuid") String uuid, @Param("almId") String almId, @Param("ownerId") String ownerId,
uuid = #{uuid, jdbcType=VARCHAR}
</select>
+ <select id="selectByOrganizationUuid" parameterType="Map" resultType="org.sonar.db.alm.AlmAppInstallDto">
+ select <include refid="sqlColumns"/>
+ from
+ alm_app_installs aai
+ inner join organization_alm_bindings oab on oab.alm_app_install_uuid = aai.uuid
+ where
+ oab.alm_id = #{almId, jdbcType=VARCHAR}
+ and oab.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
+ </select>
+
<select id="selectUnboundByUserExternalId" parameterType="Map" resultType="org.sonar.db.alm.AlmAppInstallDto">
select <include refid="sqlColumns" />
from
alm_app_installs aai
- left outer join organization_alm_bindings bind on bind.alm_app_install_uuid = aai.uuid
+ left outer join organization_alm_bindings bind on bind.alm_app_install_uuid = aai.uuid
where
aai.user_external_id = #{userExternalId, jdbcType=VARCHAR}
and bind.uuid is null
assertThat(underTest.selectUnboundByUserExternalId(dbSession, user2.getExternalId())).isEmpty();
}
+ @Test
+ public void selectByOrganization() {
+ OrganizationDto organization = db.organizations().insert();
+ db.getDbClient().almAppInstallDao().insertOrUpdate(db.getSession(), ALM.GITHUB, "the-owner", false, "123456", null);
+ // could be improved, insertOrUpdate should return the DTO with its uuid
+ Optional<AlmAppInstallDto> install = db.getDbClient().almAppInstallDao().selectByOwnerId(db.getSession(), ALM.GITHUB, "the-owner");
+ db.getDbClient().organizationAlmBindingDao().insert(db.getSession(), organization, install.get(), "xxx", "xxx");
+ db.commit();
+
+ assertThat(underTest.selectByOrganization(db.getSession(), GITHUB, organization).get().getUuid()).isEqualTo(install.get().getUuid());
+ assertThat(underTest.selectByOrganization(db.getSession(), BITBUCKETCLOUD, organization)).isEmpty();
+ assertThat(underTest.selectByOrganization(db.getSession(), GITHUB, new OrganizationDto().setUuid("other-organization"))).isEmpty();
+ }
+
@Test
public void insert_throws_NPE_if_alm_is_null() {
expectAlmNPE();
if (realm == null) {
return Optional.empty();
}
- return Optional.of(doAuthenticate(sanitize(credentials), request, method));
+ return Optional.of(doAuthenticate(fixCase(credentials), request, method));
}
private UserDto doAuthenticate(Credentials credentials, HttpServletRequest request, AuthenticationEvent.Method method) {
.build());
}
- private Credentials sanitize(Credentials credentials) {
+ private Credentials fixCase(Credentials credentials) {
if (config.getBoolean("sonar.authenticator.downcase").orElse(false)) {
return new Credentials(credentials.getLogin().toLowerCase(Locale.ENGLISH), credentials.getPassword());
}