diff options
author | Zipeng WU <zipeng.wu@sonarsource.com> | 2021-10-11 17:54:08 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-10-22 20:03:27 +0000 |
commit | 2d5130b8f3150af215721aa44d76fa97612874f1 (patch) | |
tree | 6b4a54205cb94c1c876c3400dd2fcef553d29b35 /server/sonar-db-dao | |
parent | cae2da28bfa432b9ec440219b514b057f5897c4f (diff) | |
download | sonarqube-2d5130b8f3150af215721aa44d76fa97612874f1.tar.gz sonarqube-2d5130b8f3150af215721aa44d76fa97612874f1.zip |
SONAR-15500 Create api/qualitygates/add_user service
Diffstat (limited to 'server/sonar-db-dao')
9 files changed, 277 insertions, 0 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 f288a33053d..2212356d10d 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 @@ -67,6 +67,7 @@ import org.sonar.db.purge.PurgeDao; import org.sonar.db.qualitygate.ProjectQgateAssociationDao; import org.sonar.db.qualitygate.QualityGateConditionDao; import org.sonar.db.qualitygate.QualityGateDao; +import org.sonar.db.qualitygate.QualityGateUserPermissionsDao; import org.sonar.db.qualityprofile.ActiveRuleDao; import org.sonar.db.qualityprofile.DefaultQProfileDao; import org.sonar.db.qualityprofile.QProfileChangeDao; @@ -146,6 +147,7 @@ public class DaoModule extends Module { QProfileEditUsersDao.class, QualityGateConditionDao.class, QualityGateDao.class, + QualityGateUserPermissionsDao.class, QualityProfileDao.class, QualityProfileExportDao.class, RoleDao.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 3efb4aee142..2eb29ee9eb0 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 @@ -65,6 +65,7 @@ import org.sonar.db.purge.PurgeDao; import org.sonar.db.qualitygate.ProjectQgateAssociationDao; import org.sonar.db.qualitygate.QualityGateConditionDao; import org.sonar.db.qualitygate.QualityGateDao; +import org.sonar.db.qualitygate.QualityGateUserPermissionsDao; import org.sonar.db.qualityprofile.ActiveRuleDao; import org.sonar.db.qualityprofile.DefaultQProfileDao; import org.sonar.db.qualityprofile.QProfileChangeDao; @@ -134,6 +135,7 @@ public class DbClient { private final PurgeDao purgeDao; private final QualityGateDao qualityGateDao; private final QualityGateConditionDao gateConditionDao; + private final QualityGateUserPermissionsDao qualityGateUserPermissionsDao; private final ProjectQgateAssociationDao projectQgateAssociationDao; private final DuplicationDao duplicationDao; private final NotificationQueueDao notificationQueueDao; @@ -209,6 +211,7 @@ public class DbClient { eventComponentChangeDao = getDao(map, EventComponentChangeDao.class); purgeDao = getDao(map, PurgeDao.class); qualityGateDao = getDao(map, QualityGateDao.class); + qualityGateUserPermissionsDao = getDao(map, QualityGateUserPermissionsDao.class); gateConditionDao = getDao(map, QualityGateConditionDao.class); projectQgateAssociationDao = getDao(map, ProjectQgateAssociationDao.class); duplicationDao = getDao(map, DuplicationDao.class); @@ -417,6 +420,10 @@ public class DbClient { return gateConditionDao; } + public QualityGateUserPermissionsDao qualityGateUserPermissionDao() { + return qualityGateUserPermissionsDao; + } + public ProjectQgateAssociationDao projectQgateAssociationDao() { return projectQgateAssociationDao; } 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 5d25b239f8f..03b4dd33ab3 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 @@ -115,6 +115,7 @@ import org.sonar.db.qualitygate.ProjectQgateAssociationMapper; import org.sonar.db.qualitygate.QualityGateConditionDto; import org.sonar.db.qualitygate.QualityGateConditionMapper; import org.sonar.db.qualitygate.QualityGateDto; +import org.sonar.db.qualitygate.QualityGateUserPermissionsMapper; import org.sonar.db.qualitygate.QualityGateMapper; import org.sonar.db.qualityprofile.ActiveRuleDto; import org.sonar.db.qualityprofile.ActiveRuleMapper; @@ -278,6 +279,7 @@ public class MyBatis implements Startable { QProfileEditUsersMapper.class, QualityGateConditionMapper.class, QualityGateMapper.class, + QualityGateUserPermissionsMapper.class, QualityProfileMapper.class, QualityProfileExportMapper.class, RoleMapper.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDao.java new file mode 100644 index 00000000000..e79dc9ecfde --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDao.java @@ -0,0 +1,58 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.qualitygate; + +import javax.annotation.Nullable; +import org.sonar.api.utils.System2; +import org.sonar.db.Dao; +import org.sonar.db.DbSession; +import org.sonar.db.user.UserDto; + +public class QualityGateUserPermissionsDao implements Dao { + + private final System2 system2; + + public QualityGateUserPermissionsDao(System2 system2) { + this.system2 = system2; + } + + public boolean exists(DbSession dbSession, QualityGateDto qualityGate, UserDto user) { + return this.exists(dbSession, qualityGate.getUuid(), user.getUuid()); + } + + public boolean exists(DbSession dbSession, @Nullable String qualityGateUuid, @Nullable String userUuid) { + if (qualityGateUuid == null || userUuid == null) { + return false; + } + return selectByQualityGateAndUser(dbSession, qualityGateUuid, userUuid) != null; + } + + public QualityGateUserPermissionsDto selectByQualityGateAndUser(DbSession dbSession, String qualityGateUuid, String userUuid) { + return mapper(dbSession).selectByQualityGateAndUser(qualityGateUuid, userUuid); + } + + public void insert(DbSession dbSession, QualityGateUserPermissionsDto dto) { + mapper(dbSession).insert(dto, system2.now()); + } + + private static QualityGateUserPermissionsMapper mapper(DbSession dbSession) { + return dbSession.getMapper(QualityGateUserPermissionsMapper.class); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDto.java new file mode 100644 index 00000000000..5d0dcc1a995 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDto.java @@ -0,0 +1,64 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.qualitygate; + +public class QualityGateUserPermissionsDto { + + private String uuid; + private String userUuid; + private String qualityGateUuid; + + public QualityGateUserPermissionsDto() { + } + + public QualityGateUserPermissionsDto(String uuid, String userUuid, String qualityGateUuid) { + this.uuid = uuid; + this.userUuid = userUuid; + this.qualityGateUuid = qualityGateUuid; + } + + public String getUuid() { + return uuid; + } + + public QualityGateUserPermissionsDto setUuid(String uuid) { + this.uuid = uuid; + return this; + } + + public String getUserUuid() { + return userUuid; + } + + public QualityGateUserPermissionsDto setUserUuid(String userUuid) { + this.userUuid = userUuid; + return this; + } + + public String getQualityGateUuid() { + return qualityGateUuid; + } + + public QualityGateUserPermissionsDto setQualityGateUuid(String qProfileUuid) { + this.qualityGateUuid = qProfileUuid; + return this; + } + +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsMapper.java new file mode 100644 index 00000000000..b67289f69dc --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualitygate/QualityGateUserPermissionsMapper.java @@ -0,0 +1,30 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.qualitygate; + +import org.apache.ibatis.annotations.Param; + +public interface QualityGateUserPermissionsMapper { + + QualityGateUserPermissionsDto selectByQualityGateAndUser(@Param("qualityGateUuid") String qualityGateUuid, @Param("userUuid") String userUuid); + + void insert(@Param("dto") QualityGateUserPermissionsDto dto, @Param("now") long now); + +} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateUserPermissionsMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateUserPermissionsMapper.xml new file mode 100644 index 00000000000..411e29b1904 --- /dev/null +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualitygate/QualityGateUserPermissionsMapper.xml @@ -0,0 +1,37 @@ +<?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.qualitygate.QualityGateUserPermissionsMapper"> + + <sql id="sqlColumns"> + qup.uuid as "uuid", + qup.user_uuid as "userUuid", + qup.quality_gate_uuid as "qualityGateUuid" + </sql> + + <select id="selectByQualityGateAndUser" parameterType="String" resultType="org.sonar.db.qualitygate.QualityGateUserPermissionsDto"> + select + <include refid="sqlColumns"/> + from qgate_user_permissions qup + where + qup.user_uuid = #{userUuid} + and qup.quality_gate_uuid = #{qualityGateUuid} + </select> + + <insert id="insert" useGeneratedKeys="false" parameterType="map"> + insert into qgate_user_permissions( + uuid, + user_uuid, + quality_gate_uuid, + created_at + ) values ( + #{dto.uuid, jdbcType=VARCHAR}, + #{dto.userUuid, jdbcType=VARCHAR}, + #{dto.qualityGateUuid, jdbcType=VARCHAR}, + #{now, jdbcType=BIGINT} + ) + </insert> + + +</mapper> + diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDaoTest.java new file mode 100644 index 00000000000..1ce212780c9 --- /dev/null +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateUserPermissionsDaoTest.java @@ -0,0 +1,67 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.qualitygate; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.utils.System2; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; +import org.sonar.db.user.UserDbTester; +import org.sonar.db.user.UserDto; + +import static org.assertj.core.api.Assertions.assertThat; + +public class QualityGateUserPermissionsDaoTest { + @Rule + public final DbTester db = DbTester.create(System2.INSTANCE); + + private final DbSession dbSession = db.getSession(); + private final UserDbTester userDbTester = new UserDbTester(db); + private final QualityGateDbTester qualityGateDbTester = new QualityGateDbTester(db); + private final QualityGateUserPermissionsDao underTest = db.getDbClient().qualityGateUserPermissionDao(); + + @Test + public void insert() { + UserDto user = userDbTester.insertUser(); + QualityGateDto qualityGate = qualityGateDbTester.insertQualityGate(); + QualityGateUserPermissionsDto qualityGateUserPermissions = new QualityGateUserPermissionsDto("uuid", user.getUuid(), qualityGate.getUuid()); + underTest.insert(dbSession, qualityGateUserPermissions); + dbSession.commit(); + + QualityGateUserPermissionsDto fromDB = underTest.selectByQualityGateAndUser(dbSession, qualityGate.getUuid(), user.getUuid()); + assertThat(fromDB.getQualityGateUuid()).isEqualTo(qualityGate.getUuid()); + assertThat(fromDB.getUserUuid()).isEqualTo(user.getUuid()); + assertThat(fromDB.getUuid()).isEqualTo("uuid"); + } + + @Test + public void exist() { + UserDto user = userDbTester.insertUser(); + QualityGateDto qualityGate = qualityGateDbTester.insertQualityGate(); + QualityGateUserPermissionsDto qualityGateUserPermissions = new QualityGateUserPermissionsDto("uuid", user.getUuid(), qualityGate.getUuid()); + underTest.insert(dbSession, qualityGateUserPermissions); + dbSession.commit(); + + assertThat(underTest.exists(dbSession, qualityGate.getUuid(), user.getUuid())).isTrue(); + assertThat(underTest.exists(dbSession, qualityGate, user)).isTrue(); + } + +}
\ No newline at end of file diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/qualitygate/QualityGateDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/qualitygate/QualityGateDbTester.java index e6da05b13dd..6dbbe27587e 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/qualitygate/QualityGateDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/qualitygate/QualityGateDbTester.java @@ -30,6 +30,7 @@ import org.sonar.db.DbTester; import org.sonar.db.metric.MetricDto; import org.sonar.db.project.ProjectDto; import org.sonar.db.property.PropertyDto; +import org.sonar.db.user.UserDto; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.apache.commons.lang.RandomStringUtils.randomNumeric; @@ -102,4 +103,13 @@ public class QualityGateDbTester { public Optional<String> selectQGateUuidByComponentUuid(String componentUuid) { return dbClient.projectQgateAssociationDao().selectQGateUuidByProjectUuid(dbSession, componentUuid); } + + public void addUserPermission(QualityGateDto qualityGateDto, UserDto user) { + dbClient.qualityGateUserPermissionDao().insert(dbSession, new QualityGateUserPermissionsDto() + .setUuid(Uuids.createFast()) + .setUserUuid(user.getUuid()) + .setQualityGateUuid(qualityGateDto.getUuid()) + ); + dbSession.commit(); + } } |