import static org.sonar.core.util.stream.MoreCollectors.toList;
import static org.sonar.db.DatabaseUtils.executeLargeInputs;
+import static org.sonar.db.DatabaseUtils.executeLargeUpdates;
public class QProfileEditGroupsDao implements Dao {
mapper(dbSession).delete(profile.getKee(), group.getId());
}
+ public void deleteByQProfiles(DbSession dbSession, List<QProfileDto> qProfiles) {
+ executeLargeUpdates(qProfiles.stream().map(QProfileDto::getKee).collect(toList()), p -> mapper(dbSession).deleteByQProfiles(p));
+ }
+
private static QProfileEditGroupsMapper mapper(DbSession dbSession) {
return dbSession.getMapper(QProfileEditGroupsMapper.class);
}
*/
package org.sonar.db.qualityprofile;
+import java.util.Collection;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.sonar.db.Pagination;
void delete(@Param("qProfileUuid") String qProfileUuid, @Param("groupId") int groupId);
+ void deleteByQProfiles(@Param("qProfileUuids") Collection<String> qProfileUuids);
+
}
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.user.UserDto;
+import static org.sonar.core.util.stream.MoreCollectors.toList;
+import static org.sonar.db.DatabaseUtils.executeLargeUpdates;
+
public class QProfileEditUsersDao implements Dao {
private final System2 system2;
return mapper(dbSession).selectByQProfileAndUser(profile.getKee(), user.getId()) != null;
}
- public int countByQuery(DbSession dbSession, SearchUsersQuery query){
+ public int countByQuery(DbSession dbSession, SearchUsersQuery query) {
return mapper(dbSession).countByQuery(query);
}
- public List<UserMembershipDto> selectByQuery(DbSession dbSession, SearchUsersQuery query, Pagination pagination){
+ public List<UserMembershipDto> selectByQuery(DbSession dbSession, SearchUsersQuery query, Pagination pagination) {
return mapper(dbSession).selectByQuery(query, pagination);
}
- public List<String> selectQProfileUuidsByOrganizationAndUser(DbSession dbSession, OrganizationDto organization, UserDto userDto){
+ public List<String> selectQProfileUuidsByOrganizationAndUser(DbSession dbSession, OrganizationDto organization, UserDto userDto) {
return mapper(dbSession).selectQProfileUuidsByOrganizationAndUser(organization.getUuid(), userDto.getId());
}
mapper(dbSession).delete(profile.getKee(), user.getId());
}
+ public void deleteByQProfiles(DbSession dbSession, List<QProfileDto> qProfiles) {
+ executeLargeUpdates(qProfiles.stream().map(QProfileDto::getKee).collect(toList()), p -> mapper(dbSession).deleteByQProfiles(p));
+ }
+
private static QProfileEditUsersMapper mapper(DbSession dbSession) {
return dbSession.getMapper(QProfileEditUsersMapper.class);
}
*/
package org.sonar.db.qualityprofile;
+import java.util.Collection;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.sonar.db.Pagination;
void insert(@Param("dto") QProfileEditUsersDto dto, @Param("now") long now);
void delete(@Param("qProfileUuid") String qProfileUuid, @Param("userId") int userId);
+
+ void deleteByQProfiles(@Param("qProfileUuids") Collection<String> qProfileUuids);
}
and group_id = #{groupId, jdbcType=INTEGER}
</delete>
+ <delete id="deleteByQProfiles" parameterType="map">
+ delete from qprofile_edit_groups
+ where qprofile_uuid in <foreach collection="qProfileUuids" open="(" close=")" item="qProfileUuid" separator=",">#{qProfileUuid, jdbcType=VARCHAR}</foreach>
+ </delete>
+
</mapper>
and user_id = #{userId, jdbcType=INTEGER}
</delete>
+ <delete id="deleteByQProfiles" parameterType="map">
+ delete from qprofile_edit_users
+ where qprofile_uuid in <foreach collection="qProfileUuids" open="(" close=")" item="qProfileUuid" separator=",">#{qProfileUuid, jdbcType=VARCHAR}</foreach>
+ </delete>
+
</mapper>
assertThat(underTest.exists(db.getSession(), profile, group)).isFalse();
}
+ @Test
+ public void deleteByQProfiles() {
+ OrganizationDto organization = db.organizations().insert();
+ OrganizationDto anotherOrganization = db.organizations().insert();
+ QProfileDto profile1 = db.qualityProfiles().insert(organization);
+ QProfileDto profile2 = db.qualityProfiles().insert(organization);
+ QProfileDto profile3 = db.qualityProfiles().insert(organization);
+ QProfileDto anotherProfile = db.qualityProfiles().insert(anotherOrganization);
+ GroupDto group1 = db.users().insertGroup(organization);
+ GroupDto group2 = db.users().insertGroup(organization);
+ db.qualityProfiles().addGroupPermission(profile1, group1);
+ db.qualityProfiles().addGroupPermission(profile2, group2);
+ db.qualityProfiles().addGroupPermission(profile3, group1);
+ db.qualityProfiles().addGroupPermission(anotherProfile, group1);
+
+ underTest.deleteByQProfiles(db.getSession(), asList(profile1, profile2));
+
+ assertThat(underTest.exists(db.getSession(), profile1, group1)).isFalse();
+ assertThat(underTest.exists(db.getSession(), profile2, group2)).isFalse();
+ assertThat(underTest.exists(db.getSession(), profile3, group1)).isTrue();
+ assertThat(underTest.exists(db.getSession(), anotherProfile, group1)).isTrue();
+ }
+
}
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.user.UserDto;
+import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.assertj.core.api.Assertions.tuple;
assertThat(underTest.exists(db.getSession(), profile, user)).isFalse();
}
+ @Test
+ public void deleteByQProfiles() {
+ OrganizationDto organization = db.organizations().insert();
+ OrganizationDto anotherOrganization = db.organizations().insert();
+ QProfileDto profile1 = db.qualityProfiles().insert(organization);
+ QProfileDto profile2 = db.qualityProfiles().insert(organization);
+ QProfileDto profile3 = db.qualityProfiles().insert(organization);
+ QProfileDto anotherProfile = db.qualityProfiles().insert(anotherOrganization);
+ UserDto user1 = db.users().insertUser();
+ UserDto user2 = db.users().insertUser();
+ db.qualityProfiles().addUserPermission(profile1, user1);
+ db.qualityProfiles().addUserPermission(profile2, user2);
+ db.qualityProfiles().addUserPermission(profile3, user1);
+ db.qualityProfiles().addUserPermission(anotherProfile, user1);
+
+ underTest.deleteByQProfiles(db.getSession(), asList(profile1, profile2));
+
+ assertThat(underTest.exists(db.getSession(), profile1, user1)).isFalse();
+ assertThat(underTest.exists(db.getSession(), profile2, user2)).isFalse();
+ assertThat(underTest.exists(db.getSession(), profile3, user1)).isTrue();
+ assertThat(underTest.exists(db.getSession(), anotherProfile, user1)).isTrue();
+ }
}
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
+import static com.google.common.base.Preconditions.checkArgument;
import static org.apache.commons.lang.math.RandomUtils.nextInt;
import static org.apache.commons.lang.math.RandomUtils.nextLong;
import static org.sonar.db.qualityprofile.ActiveRuleDto.createFor;
}
public void addUserPermission(QProfileDto profile, UserDto user){
+ checkArgument(!profile.isBuiltIn(), "Built-In profile cannot be used");
dbClient.qProfileEditUsersDao().insert(dbSession, new QProfileEditUsersDto()
.setUuid(UuidFactoryFast.getInstance().create())
.setUserId(user.getId())
}
public void addGroupPermission(QProfileDto profile, GroupDto group){
+ checkArgument(!profile.isBuiltIn(), "Built-In profile cannot be used");
dbClient.qProfileEditGroupsDao().insert(dbSession, new QProfileEditGroupsDto()
.setUuid(UuidFactoryFast.getInstance().create())
.setGroupId(group.getId())
db.defaultQProfileDao().deleteByQProfileUuids(dbSession, uuids);
db.qualityProfileDao().deleteOrgQProfilesByUuids(dbSession, uuids);
+ // Permissions are only available on custom profiles
+ db.qProfileEditUsersDao().deleteByQProfiles(dbSession, customProfiles);
+ db.qProfileEditGroupsDao().deleteByQProfiles(dbSession, customProfiles);
+
// tables related to rules_profiles and active_rules are deleted
// only for custom profiles. Built-in profiles are never
// deleted from table rules_profiles.
"ruleUpdatedAt": "2016-12-22T19:10:03+0100",
"userUpdatedAt": "2016-06-29T21:57:01+0200",
"actions": {
- "edit": false,
+ "edit": true,
"setAsDefault": false,
"copy": false
}
import org.sonar.db.qualityprofile.RulesProfileDto;
import org.sonar.db.rule.RuleDefinitionDto;
import org.sonar.db.rule.RuleParamDto;
+import org.sonar.db.user.GroupDto;
+import org.sonar.db.user.UserDto;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
assertQualityProfileFromDb(profile).isNotNull();
}
+ @Test
+ public void delete_removes_qprofile_edit_permissions() {
+ OrganizationDto organization = db.organizations().insert();
+ QProfileDto profile = db.qualityProfiles().insert(organization);
+ UserDto user = db.users().insertUser();
+ db.qualityProfiles().addUserPermission(profile, user);
+ GroupDto group = db.users().insertGroup(organization);
+ db.qualityProfiles().addGroupPermission(profile, group);
+
+ underTest.delete(dbSession, asList(profile));
+
+ assertThat(db.countRowsOfTable(dbSession, "qprofile_edit_users")).isZero();
+ assertThat(db.countRowsOfTable(dbSession, "qprofile_edit_groups")).isZero();
+ }
+
private QProfileDto createCustomProfile(OrganizationDto org) {
QProfileDto profile = db.qualityProfiles().insert(org, p -> p.setLanguage("xoo").setIsBuiltIn(false));
ActiveRuleDto activeRuleDto = db.qualityProfiles().activateRule(profile, rule);
.forEach(project -> db.qualityProfiles().associateWithProject(project, myBuProfile));
// User
UserDto user = db.users().insertUser();
- db.qualityProfiles().addUserPermission(sonarWayCs, user);
+ db.qualityProfiles().addUserPermission(myCompanyProfile, user);
db.qualityProfiles().addUserPermission(myBuProfile, user);
userSession.logIn(user);