*/
package org.sonar.db.alm.pat;
-import java.util.List;
import java.util.Optional;
import org.sonar.api.utils.System2;
import org.sonar.core.util.UuidFactory;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.db.user.UserDto;
public class AlmPatDao implements Dao {
return Optional.ofNullable(getMapper(dbSession).selectByUuid(uuid));
}
- public Optional<AlmPatDto> selectByAlmSetting(DbSession dbSession, String userUuid, AlmSettingDto almSettingDto) {
- return Optional.ofNullable(getMapper(dbSession).selectByAlmSetting(userUuid, almSettingDto.getUuid()));
- }
-
- public List<AlmPatDto> selectAll(DbSession dbSession) {
- return getMapper(dbSession).selectAll();
+ public Optional<AlmPatDto> selectByUserAndAlmSetting(DbSession dbSession, String userUuid, AlmSettingDto almSettingDto) {
+ return Optional.ofNullable(getMapper(dbSession).selectByUserAndAlmSetting(userUuid, almSettingDto.getUuid()));
}
public void insert(DbSession dbSession, AlmPatDto almPatDto) {
getMapper(dbSession).deleteByUuid(almPatDto.getUuid());
}
+ public void deleteByUser(DbSession dbSession, UserDto user) {
+ getMapper(dbSession).deleteByUser(user.getUuid());
+ }
+
+ public void deleteByAlmSetting(DbSession dbSession, AlmSettingDto almSetting) {
+ getMapper(dbSession).deleteByAlmSetting(almSetting.getUuid());
+ }
+
+
+
}
*/
package org.sonar.db.alm.pat;
-import java.util.List;
import javax.annotation.CheckForNull;
import org.apache.ibatis.annotations.Param;
AlmPatDto selectByUuid(@Param("uuid") String uuid);
@CheckForNull
- AlmPatDto selectByAlmSetting(@Param("userUuid") String userUuid, @Param("almSettingUuid") String almSettingUuid);
-
- List<AlmPatDto> selectAll();
+ AlmPatDto selectByUserAndAlmSetting(@Param("userUuid") String userUuid, @Param("almSettingUuid") String almSettingUuid);
void insert(@Param("dto") AlmPatDto almPatDto, @Param("uuid") String uuid, @Param("now") long now);
void deleteByUuid(@Param("uuid") String uuid);
+ void deleteByUser(@Param("userUuid") String userUuid);
+
+ void deleteByAlmSetting(@Param("almSettingUuid") String almSettingUuid);
}
a.uuid = #{uuid, jdbcType=VARCHAR}
</select>
- <select id="selectByAlmSetting" parameterType="string" resultType="org.sonar.db.alm.pat.AlmPatDto">
+ <select id="selectByUserAndAlmSetting" parameterType="string" resultType="org.sonar.db.alm.pat.AlmPatDto">
select <include refid="sqlColumns"/>
from
alm_pats a
a.user_uuid = #{userUuid, jdbcType=VARCHAR}
</select>
- <select id="selectAll" resultType="org.sonar.db.alm.pat.AlmPatDto">
- select <include refid="sqlColumns"/>
- from alm_pats a
- </select>
-
-
<insert id="insert" parameterType="Map" useGeneratedKeys="false">
INSERT INTO alm_pats
(
DELETE FROM alm_pats WHERE uuid = #{uuid, jdbcType=VARCHAR}
</delete>
+ <delete id="deleteByUser" parameterType="String">
+ DELETE FROM alm_pats WHERE user_uuid = #{userUuid, jdbcType=VARCHAR}
+ </delete>
+
+ <delete id="deleteByAlmSetting" parameterType="String">
+ DELETE FROM alm_pats WHERE alm_setting_uuid = #{almSettingUuid, jdbcType=VARCHAR}
+ </delete>
+
</mapper>
CREATE TABLE "ALM_PATS"(
"UUID" VARCHAR(40) NOT NULL,
"PAT" VARCHAR(2000) NOT NULL,
- "USER_UUID" VARCHAR(40) NOT NULL,
+ "USER_UUID" VARCHAR(256) NOT NULL,
"ALM_SETTING_UUID" VARCHAR(40) NOT NULL,
"UPDATED_AT" BIGINT NOT NULL,
"CREATED_AT" BIGINT NOT NULL
*/
package org.sonar.db.alm.pat;
-import java.util.List;
-import org.assertj.core.api.Assertions;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.core.util.UuidFactory;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
-import org.sonar.db.alm.pat.AlmPatDao;
-import org.sonar.db.alm.pat.AlmPatDto;
import org.sonar.db.alm.setting.AlmSettingDao;
import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.db.user.UserDto;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import static org.sonar.db.alm.integration.pat.AlmPatsTesting.newAlmPatDto;
import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubAlmSettingDto;
-import static org.sonar.db.user.UserTesting.newUserDto;
public class ALMPatDaoTest {
almPatDto.setUserUuid(userUuid);
underTest.insert(dbSession, almPatDto);
- assertThat(underTest.selectByAlmSetting(dbSession, userUuid, almSetting).get())
+ assertThat(underTest.selectByUserAndAlmSetting(dbSession, userUuid, almSetting).get())
.extracting(AlmPatDto::getUuid, AlmPatDto::getPersonalAccessToken,
AlmPatDto::getUserUuid, AlmPatDto::getAlmSettingUuid,
AlmPatDto::getCreatedAt, AlmPatDto::getUpdatedAt)
.containsExactly(A_UUID, almPatDto.getPersonalAccessToken(),
userUuid, almSetting.getUuid(), NOW, NOW);
- assertThat(underTest.selectByAlmSetting(dbSession, randomAlphanumeric(40), newGithubAlmSettingDto())).isNotPresent();
- }
-
- @Test
- public void selectAll() {
- when(uuidFactory.create()).thenReturn(A_UUID);
- when(system2.now()).thenReturn(NOW);
- underTest.insert(dbSession, newAlmPatDto());
- when(uuidFactory.create()).thenReturn(A_UUID + "bis");
- underTest.insert(dbSession, newAlmPatDto());
-
- List<AlmPatDto> almPats = underTest.selectAll(dbSession);
-
- Assertions.assertThat(almPats).size().isEqualTo(2);
+ assertThat(underTest.selectByUserAndAlmSetting(dbSession, randomAlphanumeric(40), newGithubAlmSettingDto())).isNotPresent();
}
@Test
underTest.delete(dbSession, almPat);
assertThat(underTest.selectByUuid(dbSession, almPat.getUuid()).isPresent()).isFalse();
+ }
+
+ @Test
+ public void deleteByUser() {
+ when(uuidFactory.create()).thenReturn(A_UUID);
+ when(system2.now()).thenReturn(NOW);
+ UserDto userDto = db.users().insertUser();
+ AlmPatDto almPat = newAlmPatDto();
+ almPat.setUserUuid(userDto.getUuid());
+ underTest.insert(dbSession, almPat);
+
+ underTest.deleteByUser(dbSession, userDto);
+
+ assertThat(underTest.selectByUuid(dbSession, almPat.getUuid()).isPresent()).isFalse();
+ }
+ @Test
+ public void deleteByAlmSetting() {
+ when(uuidFactory.create()).thenReturn(A_UUID);
+ when(system2.now()).thenReturn(NOW);
+ AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
+ AlmPatDto almPat = newAlmPatDto();
+ almPat.setAlmSettingUuid(almSettingDto.getUuid());
+ underTest.insert(dbSession, almPat);
+
+ underTest.deleteByAlmSetting(dbSession, almSettingDto);
+
+ assertThat(underTest.selectByUuid(dbSession, almPat.getUuid()).isPresent()).isFalse();
}
}
dbTester.assertColumnDefinition(TABLE_NAME, "uuid", VARCHAR, 40, false);
dbTester.assertColumnDefinition(TABLE_NAME, "pat", VARCHAR, 2000, false);
- dbTester.assertColumnDefinition(TABLE_NAME, "user_uuid", VARCHAR, 40, false);
+ dbTester.assertColumnDefinition(TABLE_NAME, "user_uuid", VARCHAR, 256, false);
dbTester.assertColumnDefinition(TABLE_NAME, "alm_setting_uuid", VARCHAR, 40, false);
dbTester.assertColumnDefinition(TABLE_NAME, "updated_at", BIGINT, 20, false);
dbTester.assertColumnDefinition(TABLE_NAME, "created_at", BIGINT, 20, false);
dbClient.qProfileEditUsersDao().deleteByUser(dbSession, user);
dbClient.organizationMemberDao().deleteByUserId(dbSession, userId);
dbClient.userPropertiesDao().deleteByUser(dbSession, user);
+ dbClient.almPatDao().deleteByUser(dbSession, user);
deactivateUser(dbSession, user);
userIndexer.commitAndIndex(dbSession, user);
- LOGGER.info("Deactivate user: {}; by admin: {}", login, userSession.isSystemAdministrator());
+ LOGGER.debug("Deactivate user: {}; by admin: {}", login, userSession.isSystemAdministrator());
}
writeResponse(response, login);
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.alm.setting.AlmSettingDto;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.permission.template.PermissionTemplateDto;
assertThat(db.getDbClient().userPropertiesDao().selectByUser(dbSession, anotherUser)).hasSize(1);
}
+ @Test
+ public void deactivate_user_deletes_his_alm_pat() {
+ logInAsSystemAdministrator();
+
+ AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
+
+ UserDto user = db.users().insertUser();
+ db.almPats().insert(p -> p.setUserUuid(user.getUuid()), p -> p.setAlmSettingUuid(almSettingDto.getUuid()));
+ UserDto anotherUser = db.users().insertUser();
+ db.almPats().insert(p -> p.setUserUuid(anotherUser.getUuid()), p -> p.setAlmSettingUuid(almSettingDto.getUuid()));
+
+ deactivate(user.getLogin());
+
+ assertThat(db.getDbClient().almPatDao().selectByUserAndAlmSetting(dbSession, user.getUuid(), almSettingDto)).isEmpty();
+ assertThat(db.getDbClient().almPatDao().selectByUserAndAlmSetting(dbSession, anotherUser.getUuid(), almSettingDto)).isNotNull();
+ }
+
@Test
public void user_can_deactivate_itself_on_sonarcloud() {
WsActionTester customWs = newSonarCloudWs();
.setMediaType(MediaTypes.JSON)).content();
}
- //xx add projectList and repoList
-
}