void update(PermissionTemplateCharacteristicDto templatePermissionDto);
void deleteByTemplateId(long id);
+
+ void deleteByTemplateIds(@Param("templateIds") List<Long> subList);
}
public void deleteById(DbSession session, long templateId) {
PermissionTemplateMapper mapper = mapper(session);
- mapper.deleteUserPermissions(templateId);
- mapper.deleteGroupPermissions(templateId);
+ mapper.deleteUserPermissionsByTemplateId(templateId);
+ mapper.deleteGroupPermissionsByTemplateId(templateId);
session.getMapper(PermissionTemplateCharacteristicMapper.class).deleteByTemplateId(templateId);
- mapper.delete(templateId);
+ mapper.deleteById(templateId);
}
public PermissionTemplateDto update(DbSession session, PermissionTemplateDto permissionTemplate) {
private static PermissionTemplateMapper mapper(DbSession session) {
return session.getMapper(PermissionTemplateMapper.class);
}
+
+ public void deleteByOrganization(DbSession dbSession, String organizationUuid) {
+ PermissionTemplateMapper templateMapper = mapper(dbSession);
+ PermissionTemplateCharacteristicMapper templateCharacteristicMapper = dbSession.getMapper(PermissionTemplateCharacteristicMapper.class);
+ List<Long> templateIds = templateMapper.selectTemplateIdsByOrganization(organizationUuid);
+ executeLargeInputsWithoutOutput(templateIds, subList -> {
+ templateCharacteristicMapper.deleteByTemplateIds(subList);
+ templateMapper.deleteGroupPermissionsByTemplateIds(subList);
+ templateMapper.deleteUserPermissionsByTemplateIds(subList);
+ templateMapper.deleteByIds(subList);
+ return null;
+ });
+ }
}
void update(PermissionTemplateDto permissionTemplate);
- void delete(long templateId);
+ void deleteById(long templateId);
- void deleteUserPermissions(long templateId);
+ void deleteByIds(@Param("templateIds") List<Long> templateIds);
+
+ void deleteUserPermissionsByTemplateId(long templateId);
+
+ void deleteUserPermissionsByTemplateIds(@Param("templateIds") List<Long> templateIds);
void deleteUserPermission(PermissionTemplateUserDto permissionTemplateUser);
- void deleteGroupPermissions(long templateId);
+ void deleteGroupPermissionsByTemplateId(long templateId);
+
+ void deleteGroupPermissionsByTemplateIds(@Param("templateIds") List<Long> templateIds);
void deleteGroupPermission(PermissionTemplateGroupDto permissionTemplateGroup);
List<String> selectPotentialPermissionsByUserIdAndTemplateId(@Param("userId") @Nullable Long currentUserId, @Param("templateId") long templateId);
int countGroupsWithPermission(@Param("templateId") long templateId, @Param("permission") String permission, @Nullable @Param("groupId") Long groupId);
+
+ List<Long> selectTemplateIdsByOrganization(@Param("organizationUuid") String organizationUuid);
+
}
DELETE FROM perm_tpl_characteristics
WHERE template_id = #{permissionTemplateId}
</delete>
+
+ <delete id="deleteByTemplateIds" parameterType="long">
+ delete from
+ perm_tpl_characteristics
+ where
+ template_id in
+ <foreach collection="templateIds" open="(" close=")" item="templateId" separator=",">
+ #{templateId}
+ </foreach>
+ </delete>
</mapper>
WHERE id = #{id}
</update>
- <delete id="delete" parameterType="long">
+ <delete id="deleteById" parameterType="long">
DELETE FROM permission_templates
WHERE id = #{templateId}
</delete>
- <delete id="deleteUserPermissions" parameterType="long">
- DELETE FROM perm_templates_users
- WHERE template_id = #{templateId}
+ <delete id="deleteByIds" parameterType="long">
+ delete from
+ permission_templates
+ where
+ id in <foreach collection="templateIds" open="(" close=")" item="templateId" separator=",">
+ #{templateId,jdbcType=BIGINT}
+ </foreach>
</delete>
- <delete id="deleteGroupPermissions" parameterType="long">
- DELETE FROM perm_templates_groups
- WHERE template_id = #{templateId}
+ <delete id="deleteUserPermissionsByTemplateId" parameterType="long">
+ delete from
+ perm_templates_users
+ where
+ template_id = #{templateId,jdbcType=BIGINT}
</delete>
- <insert id="insertUserPermission" parameterType="PermissionTemplateUser">
- INSERT INTO perm_templates_users (template_id, user_id, permission_reference, created_at, updated_at)
- VALUES (#{templateId}, #{userId}, #{permission}, #{createdAt}, #{updatedAt})
- </insert>
+ <delete id="deleteUserPermissionsByTemplateIds">
+ delete from
+ perm_templates_users
+ where
+ template_id in <foreach collection="templateIds" open="(" close=")" item="templateId" separator=",">
+ #{templateId,jdbcType=BIGINT}
+ </foreach>
+ </delete>
<delete id="deleteUserPermission" parameterType="PermissionTemplateUser">
DELETE FROM perm_templates_users
AND permission_reference = #{permission}
</delete>
- <insert id="insertGroupPermission" parameterType="PermissionTemplateGroup">
- INSERT INTO perm_templates_groups (template_id, group_id, permission_reference, created_at, updated_at)
- VALUES (
- #{templateId,jdbcType=BIGINT},
- #{groupId,jdbcType=BIGINT},
- #{permission,jdbcType=VARCHAR},
- #{createdAt,jdbcType=TIMESTAMP},
- #{updatedAt,jdbcType=TIMESTAMP}
- )
- </insert>
+ <delete id="deleteGroupPermissionsByTemplateId" parameterType="long">
+ delete from
+ perm_templates_groups
+ where
+ template_id = #{templateId,jdbcType=BIGINT}
+ </delete>
+
+ <delete id="deleteGroupPermissionsByTemplateIds">
+ delete from
+ perm_templates_groups
+ where
+ template_id in <foreach collection="templateIds" open="(" close=")" item="templateId" separator=",">
+ #{templateId,jdbcType=BIGINT}
+ </foreach>
+ </delete>
<delete id="deleteGroupPermission" parameterType="PermissionTemplateGroup">
DELETE FROM perm_templates_groups
</choose>
</delete>
+ <insert id="insertUserPermission" parameterType="PermissionTemplateUser">
+ INSERT INTO perm_templates_users (template_id, user_id, permission_reference, created_at, updated_at)
+ VALUES (#{templateId}, #{userId}, #{permission}, #{createdAt}, #{updatedAt})
+ </insert>
+
+ <insert id="insertGroupPermission" parameterType="PermissionTemplateGroup">
+ INSERT INTO perm_templates_groups (template_id, group_id, permission_reference, created_at, updated_at)
+ VALUES (
+ #{templateId,jdbcType=BIGINT},
+ #{groupId,jdbcType=BIGINT},
+ #{permission,jdbcType=VARCHAR},
+ #{createdAt,jdbcType=TIMESTAMP},
+ #{updatedAt,jdbcType=TIMESTAMP}
+ )
+ </insert>
+
<delete id="deleteByGroupId" parameterType="long">
delete from perm_templates_groups
where group_id = #{groupId,jdbcType=BIGINT}
ptg.group_id = #{groupId}
</if>
</select>
+
+ <select id="selectTemplateIdsByOrganization" resultType="Long">
+ select
+ id
+ from
+ permission_templates
+ where
+ organization_uuid = #{organizationUuid,jdbcType=VARCHAR}
+ </select>
</mapper>
import org.sonar.api.web.UserRole;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
public ExpectedException expectedException = ExpectedException.none();
@Rule
public DbTester db = DbTester.create(system);
+
private DbSession dbSession = db.getSession();
private PermissionTemplateDbTester templateDb = db.permissionTemplates();
assertThat(resultWithoutUser).containsOnly(UserRole.ISSUE_ADMIN);
}
+ @Test
+ public void deleteByOrganization_does_not_fail_on_empty_db() {
+ underTest.deleteByOrganization(dbSession, "some uuid");
+ dbSession.commit();
+ }
+
+ @Test
+ public void deleteByOrganization_does_not_fail_when_organization_has_no_template() {
+ OrganizationDto organization = db.organizations().insert();
+
+ underTest.deleteByOrganization(dbSession, organization.getUuid());
+ dbSession.commit();
+ }
+
+ @Test
+ public void deleteByOrganization_delete_all_templates_of_organization_and_content_of_child_tables() {
+ OrganizationDto organization1 = db.organizations().insert();
+ OrganizationDto organization2 = db.organizations().insert();
+ OrganizationDto organization3 = db.organizations().insert();
+
+ PermissionTemplateDto[] templates = {
+ createTemplate(organization1),
+ createTemplate(organization2),
+ createTemplate(organization3),
+ createTemplate(organization1),
+ createTemplate(organization2)
+ };
+
+ verifyTemplateIdsInDb(templates[0].getId(), templates[1].getId(), templates[2].getId(), templates[3].getId(), templates[4].getId());
+
+ underTest.deleteByOrganization(dbSession, organization2.getUuid());
+ dbSession.commit();
+ verifyTemplateIdsInDb(templates[0].getId(), templates[2].getId(), templates[3].getId());
+
+ underTest.deleteByOrganization(dbSession, organization3.getUuid());
+ dbSession.commit();
+ verifyTemplateIdsInDb(templates[0].getId(), templates[3].getId());
+
+ underTest.deleteByOrganization(dbSession, organization1.getUuid());
+ dbSession.commit();
+ verifyTemplateIdsInDb();
+ }
+
+ private PermissionTemplateDto createTemplate(OrganizationDto organization) {
+ UserDto user = db.users().insertUser();
+ GroupDto group = db.users().insertGroup();
+ db.users().insertMember(group, user);
+ PermissionTemplateDto template = templateDb.insertTemplate(organization);
+ templateDb.addProjectCreatorToTemplate(template.getId(), SCAN_EXECUTION);
+ templateDb.addProjectCreatorToTemplate(template.getId(), UserRole.ADMIN);
+ templateDb.addUserToTemplate(template.getId(), user.getId(), UserRole.USER);
+ templateDb.addUserToTemplate(template.getId(), user.getId(), UserRole.ADMIN);
+ templateDb.addGroupToTemplate(template.getId(), group.getId(), UserRole.CODEVIEWER);
+ templateDb.addGroupToTemplate(template.getId(), group.getId(), UserRole.ADMIN);
+ templateDb.addGroupToTemplate(template.getId(), null, UserRole.ISSUE_ADMIN);
+ return template;
+ }
+
+ private void verifyTemplateIdsInDb(Long... expectedTemplateIds) {
+ assertThat(db.select("select distinct template_id as \"templateId\" from perm_templates_groups"))
+ .extracting((row) -> (Long) row.get("templateId"))
+ .containsOnly(expectedTemplateIds);
+ assertThat(db.select("select distinct template_id as \"templateId\" from perm_templates_users"))
+ .extracting((row) -> (Long) row.get("templateId"))
+ .containsOnly(expectedTemplateIds);
+ assertThat(db.select("select distinct template_id as \"templateId\" from perm_tpl_characteristics"))
+ .extracting((row) -> (Long) row.get("templateId"))
+ .containsOnly(expectedTemplateIds);
+ assertThat(db.select("select distinct id as \"templateId\" from permission_templates"))
+ .extracting((row) -> (Long) row.get("templateId"))
+ .containsOnly(expectedTemplateIds);
+ }
+
private void commit() {
dbSession.commit();
}
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.organization.OrganizationDto;
import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateCharacteristicDto;
import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
return insertTemplate(newPermissionTemplateDto());
}
+ public PermissionTemplateDto insertTemplate(OrganizationDto organizationDto) {
+ return insertTemplate(newPermissionTemplateDto().setOrganizationUuid(organizationDto.getUuid()));
+ }
+
public PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
PermissionTemplateDto templateInDb = dbClient.permissionTemplateDao().insert(dbSession, template);
db.commit();