diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-09-21 14:00:30 +0200 |
---|---|---|
committer | Stas Vilchik <stas.vilchik@sonarsource.com> | 2017-10-02 17:18:15 +0200 |
commit | 92f07538cf9928e2107d201f8518061a69ac1341 (patch) | |
tree | 16a35624af0a10b8f712958aa52769a3ff774235 /server/sonar-db-dao/src | |
parent | d899635a55db0dc8ff37c9ce88df82af190c469c (diff) | |
download | sonarqube-92f07538cf9928e2107d201f8518061a69ac1341.tar.gz sonarqube-92f07538cf9928e2107d201f8518061a69ac1341.zip |
SONAR-1330 Add user permission to edit single quality profile
Diffstat (limited to 'server/sonar-db-dao/src')
10 files changed, 264 insertions, 1 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java index d791dbbff31..d6fa06ecacd 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java @@ -60,6 +60,7 @@ import org.sonar.db.qualitygate.QualityGateDao; import org.sonar.db.qualityprofile.ActiveRuleDao; import org.sonar.db.qualityprofile.DefaultQProfileDao; import org.sonar.db.qualityprofile.QProfileChangeDao; +import org.sonar.db.qualityprofile.QProfileEditUsersDao; import org.sonar.db.qualityprofile.QualityProfileDao; import org.sonar.db.rule.RuleDao; import org.sonar.db.rule.RuleRepositoryDao; @@ -114,6 +115,7 @@ public class DaoModule extends Module { PropertiesDao.class, PurgeDao.class, QProfileChangeDao.class, + QProfileEditUsersDao.class, QualityGateConditionDao.class, QualityGateDao.class, QualityProfileDao.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java index f694b925d14..bb269e0d2c4 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java @@ -58,6 +58,7 @@ import org.sonar.db.qualitygate.QualityGateDao; import org.sonar.db.qualityprofile.ActiveRuleDao; import org.sonar.db.qualityprofile.DefaultQProfileDao; import org.sonar.db.qualityprofile.QProfileChangeDao; +import org.sonar.db.qualityprofile.QProfileEditUsersDao; import org.sonar.db.qualityprofile.QualityProfileDao; import org.sonar.db.rule.RuleDao; import org.sonar.db.rule.RuleRepositoryDao; @@ -126,6 +127,7 @@ public class DbClient { private final EsQueueDao esQueueDao; private final PluginDao pluginDao; private final BranchDao branchDao; + private final QProfileEditUsersDao qProfileEditUsersDao; public DbClient(Database database, MyBatis myBatis, DBSessions dbSessions, Dao... daos) { this.database = database; @@ -185,6 +187,7 @@ public class DbClient { esQueueDao = getDao(map, EsQueueDao.class); pluginDao = getDao(map, PluginDao.class); branchDao = getDao(map, BranchDao.class); + qProfileEditUsersDao = getDao(map, QProfileEditUsersDao.class); } public DbSession openSession(boolean batch) { @@ -391,6 +394,10 @@ public class DbClient { return branchDao; } + public QProfileEditUsersDao qProfileEditUsersDao() { + return qProfileEditUsersDao; + } + protected <K extends Dao> K getDao(Map<Class, Dao> map, Class<K> clazz) { return (K) map.get(clazz); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java index b8ee31f72e7..244cb18af54 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java @@ -103,6 +103,7 @@ import org.sonar.db.qualityprofile.ActiveRuleMapper; import org.sonar.db.qualityprofile.ActiveRuleParamDto; import org.sonar.db.qualityprofile.DefaultQProfileMapper; import org.sonar.db.qualityprofile.QProfileChangeMapper; +import org.sonar.db.qualityprofile.QProfileEditUsersMapper; import org.sonar.db.qualityprofile.QualityProfileMapper; import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleMapper; @@ -228,6 +229,7 @@ public class MyBatis implements Startable { PropertiesMapper.class, PurgeMapper.class, QProfileChangeMapper.class, + QProfileEditUsersMapper.class, QualityGateConditionMapper.class, QualityGateMapper.class, QualityProfileMapper.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersDao.java new file mode 100644 index 00000000000..f5443fb5c11 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersDao.java @@ -0,0 +1,46 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.qualityprofile; + +import org.sonar.api.utils.System2; +import org.sonar.db.Dao; +import org.sonar.db.DbSession; +import org.sonar.db.user.UserDto; + +public class QProfileEditUsersDao implements Dao { + + private final System2 system2; + + public QProfileEditUsersDao(System2 system2) { + this.system2 = system2; + } + + public boolean exists(DbSession dbSession, QProfileDto profile, UserDto user) { + return mapper(dbSession).selectByQProfileAndUser(profile.getKee(), user.getId()) != null; + } + + public void insert(DbSession dbSession, QProfileEditUsersDto dto) { + mapper(dbSession).insert(dto, system2.now()); + } + + private static QProfileEditUsersMapper mapper(DbSession dbSession) { + return dbSession.getMapper(QProfileEditUsersMapper.class); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersDto.java new file mode 100644 index 00000000000..4dd5f30bb82 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersDto.java @@ -0,0 +1,55 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.qualityprofile; + +public class QProfileEditUsersDto { + + private String uuid; + private int userId; + private String qProfileUuid; + + public String getUuid() { + return uuid; + } + + public QProfileEditUsersDto setUuid(String uuid) { + this.uuid = uuid; + return this; + } + + public int getUserId() { + return userId; + } + + public QProfileEditUsersDto setUserId(int userId) { + this.userId = userId; + return this; + } + + public String getQProfileUuid() { + return qProfileUuid; + } + + public QProfileEditUsersDto setQProfileUuid(String qProfileUuid) { + this.qProfileUuid = qProfileUuid; + return this; + } + +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersMapper.java new file mode 100644 index 00000000000..8efa5fd5609 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QProfileEditUsersMapper.java @@ -0,0 +1,29 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.qualityprofile; + +import org.apache.ibatis.annotations.Param; + +public interface QProfileEditUsersMapper { + + QProfileEditUsersDto selectByQProfileAndUser(@Param("qProfileUuid") String qProfileUuid, @Param("userId") int userId); + + void insert(@Param("dto") QProfileEditUsersDto dto, @Param("now") long now); +} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileEditUsersMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileEditUsersMapper.xml new file mode 100644 index 00000000000..1e391294d54 --- /dev/null +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QProfileEditUsersMapper.xml @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd"> + +<mapper namespace="org.sonar.db.qualityprofile.QProfileEditUsersMapper"> + + <sql id="sqlColumns"> + qeu.uuid as "uuid", + qeu.user_id as "userId", + qeu.qprofile_uuid as "qProfileUuid" + </sql> + + <select id="selectByQProfileAndUser" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileEditUsersDto"> + select + <include refid="sqlColumns"/> + from qprofile_edit_users qeu + where + qeu.user_id = #{userId, jdbcType=INTEGER} + and qeu.qprofile_uuid = #{qProfileUuid, jdbcType=VARCHAR} + </select> + + <insert id="insert" useGeneratedKeys="false" parameterType="map"> + insert into qprofile_edit_users( + uuid, + user_id, + qprofile_uuid, + created_at, + ) values ( + #{dto.uuid, jdbcType=VARCHAR}, + #{dto.userId, jdbcType=INTEGER}, + #{dto.qProfileUuid, jdbcType=VARCHAR}, + #{now, jdbcType=BIGINT} + ) + </insert> + +</mapper> + diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java index 9d7a3d2786a..986fbd7e2c3 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java @@ -29,6 +29,6 @@ public class DaoModuleTest { public void verify_count_of_added_components() { ComponentContainer container = new ComponentContainer(); new DaoModule().configure(container); - assertThat(container.size()).isEqualTo(2 + 49); + assertThat(container.size()).isEqualTo(2 + 50); } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditUsersDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditUsersDaoTest.java new file mode 100644 index 00000000000..a3442583975 --- /dev/null +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QProfileEditUsersDaoTest.java @@ -0,0 +1,74 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.qualityprofile; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.api.utils.internal.TestSystem2; +import org.sonar.db.DbTester; +import org.sonar.db.organization.OrganizationDto; +import org.sonar.db.user.UserDto; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + +public class QProfileEditUsersDaoTest { + + private static final long NOW = 10_000_000_000L; + + private System2 system2 = new TestSystem2().setNow(NOW); + + @Rule + public DbTester db = DbTester.create(system2); + + private QProfileEditUsersDao underTest = db.getDbClient().qProfileEditUsersDao(); + + @Test + public void exists() { + OrganizationDto organization = db.organizations().insert(); + QProfileDto profile = db.qualityProfiles().insert(organization); + QProfileDto anotherProfile = db.qualityProfiles().insert(organization); + UserDto user = db.users().insertUser(); + UserDto anotherUser = db.users().insertUser(); + db.qualityProfiles().addUserPermission(profile, user); + + assertThat(underTest.exists(db.getSession(), profile, user)).isTrue(); + assertThat(underTest.exists(db.getSession(), profile, anotherUser)).isFalse(); + assertThat(underTest.exists(db.getSession(), anotherProfile, user)).isFalse(); + assertThat(underTest.exists(db.getSession(), anotherProfile, anotherUser)).isFalse(); + } + + @Test + public void insert() { + underTest.insert(db.getSession(), new QProfileEditUsersDto() + .setUuid("ABCD") + .setUserId(100) + .setQProfileUuid("QPROFILE") + ); + + assertThat(db.selectFirst(db.getSession(), "select uuid as \"uuid\", user_id as \"userId\", qprofile_uuid as \"qProfileUuid\", created_at as \"createdAt\" from qprofile_edit_users")).contains( + entry("uuid", "ABCD"), + entry("userId", 100L), + entry("qProfileUuid", "QPROFILE"), + entry("createdAt", NOW)); + } + +} diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDbTester.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDbTester.java index 02c53fad43d..7dc2a64ea4b 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDbTester.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDbTester.java @@ -23,12 +23,14 @@ import java.util.Arrays; import java.util.Optional; import java.util.function.Consumer; import org.sonar.api.rule.Severity; +import org.sonar.core.util.UuidFactoryFast; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.organization.OrganizationDto; import org.sonar.db.rule.RuleDefinitionDto; +import org.sonar.db.user.UserDto; import static org.apache.commons.lang.math.RandomUtils.nextInt; import static org.apache.commons.lang.math.RandomUtils.nextLong; @@ -108,4 +110,14 @@ public class QualityProfileDbTester { dbSession.commit(); return this; } + + public void addUserPermission(QProfileDto profile, UserDto user){ + dbClient.qProfileEditUsersDao().insert(dbSession, new QProfileEditUsersDto() + .setUuid(UuidFactoryFast.getInstance().create()) + .setUserId(user.getId()) + .setQProfileUuid(profile.getKee()) + ); + dbSession.commit(); + + } } |