1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd">
4 <mapper namespace="org.sonar.db.permission.template.PermissionTemplateMapper">
6 <insert id="insert" parameterType="PermissionTemplate" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
7 INSERT INTO permission_templates (organization_uuid, name, kee, description, key_pattern, created_at, updated_at)
9 #{organizationUuid,jdbcType=VARCHAR},
10 #{name,jdbcType=VARCHAR},
11 #{kee,jdbcType=VARCHAR},
12 #{description,jdbcType=VARCHAR},
13 #{keyPattern,jdbcType=VARCHAR},
18 <update id="update" parameterType="PermissionTemplate">
19 UPDATE permission_templates
20 SET name = #{name}, description = #{description}, key_pattern = #{keyPattern}, updated_at = #{updatedAt}
24 <delete id="deleteById" parameterType="long">
25 DELETE FROM permission_templates
26 WHERE id = #{templateId}
29 <delete id="deleteByIds" parameterType="long">
33 id in <foreach collection="templateIds" open="(" close=")" item="templateId" separator=",">
34 #{templateId,jdbcType=BIGINT}
38 <delete id="deleteUserPermissionsByTemplateId" parameterType="long">
42 template_id = #{templateId,jdbcType=BIGINT}
45 <delete id="deleteUserPermissionsByTemplateIds">
49 template_id in <foreach collection="templateIds" open="(" close=")" item="templateId" separator=",">
50 #{templateId,jdbcType=BIGINT}
54 <delete id="deleteUserPermission" parameterType="PermissionTemplateUser">
55 DELETE FROM perm_templates_users
56 WHERE template_id = #{templateId}
57 AND user_id = #{userId}
58 AND permission_reference = #{permission}
61 <delete id="deleteUserPermissionsByOrganization" parameterType="map">
62 delete from perm_templates_users
64 user_id = #{userId,jdbcType=INTEGER}
65 and template_id in (select id from permission_templates where organization_uuid=#{organizationUuid,jdbcType=VARCHAR})
68 <delete id="deleteUserPermissionsByUserId" parameterType="integer">
69 delete from perm_templates_users
71 user_id = #{userId,jdbcType=INTEGER}
74 <delete id="deleteGroupPermissionsByTemplateId" parameterType="long">
78 template_id = #{templateId,jdbcType=BIGINT}
81 <delete id="deleteGroupPermissionsByTemplateIds">
85 template_id in <foreach collection="templateIds" open="(" close=")" item="templateId" separator=",">
86 #{templateId,jdbcType=BIGINT}
90 <delete id="deleteGroupPermission" parameterType="PermissionTemplateGroup">
91 DELETE FROM perm_templates_groups
92 WHERE template_id = #{templateId}
93 AND permission_reference = #{permission}
96 <when test="groupId != null">
105 <insert id="insertUserPermission" parameterType="PermissionTemplateUser">
106 INSERT INTO perm_templates_users (template_id, user_id, permission_reference, created_at, updated_at)
107 VALUES (#{templateId}, #{userId}, #{permission}, #{createdAt}, #{updatedAt})
110 <insert id="insertGroupPermission" parameterType="PermissionTemplateGroup">
111 INSERT INTO perm_templates_groups (template_id, group_id, permission_reference, created_at, updated_at)
113 #{templateId,jdbcType=BIGINT},
114 #{groupId,jdbcType=INTEGER},
115 #{permission,jdbcType=VARCHAR},
116 #{createdAt,jdbcType=TIMESTAMP},
117 #{updatedAt,jdbcType=TIMESTAMP}
121 <delete id="deleteByGroupId" parameterType="int">
122 delete from perm_templates_groups
123 where group_id = #{groupId,jdbcType=INTEGER}
126 <select id="selectUserLoginsByQueryAndTemplate" parameterType="map" resultType="string">
127 select u.login as login
128 <include refid="userLoginsByQueryAndTemplate"/>
129 group by u.login, u.name
130 order by case when (count(ptu.permission_reference) > 0) then 1 else 2 end asc, u.name
133 <select id="countUserLoginsByQueryAndTemplate" parameterType="map" resultType="int">
136 SELECT DISTINCT u.login AS login, u.name AS name
137 <include refid="userLoginsByQueryAndTemplate"/>) u
140 <sql id="userLoginsByQueryAndTemplate">
142 LEFT JOIN perm_templates_users ptu ON ptu.user_id=u.id AND ptu.template_id=#{templateId}
143 INNER JOIN organization_members om ON u.id=om.user_id AND om.organization_uuid=#{query.organizationUuid}
146 <if test="query.getSearchQueryToSql() != null">
147 AND lower(u.name) like #{query.searchQueryToSqlLowercase} ESCAPE '/'
149 <if test="query.withAtLeastOnePermission()">
150 and ptu.permission_reference is not null
151 <if test="query.getPermission()!=null">
152 and ptu.permission_reference=#{query.permission}
158 <select id="selectGroupNamesByQueryAndTemplate" parameterType="map" resultType="string">
159 select groups.name, lower(groups.name), groups.group_id
160 <include refid="groupNamesByQueryAndTemplate" />
161 group by groups.name, lower(groups.name), groups.group_id
162 order by case when (count(groups.permission) > 0) then 1 else 2 end asc, lower(groups.name), groups.name, groups.group_id
165 <select id="countGroupNamesByQueryAndTemplate" parameterType="map" resultType="int">
168 SELECT DISTINCT group_id
169 <include refid="groupNamesByQueryAndTemplate" />) g
172 <sql id="groupNamesByQueryAndTemplate">
177 ptg.permission_reference AS permission,
178 ptg.template_id AS templateId
180 LEFT JOIN perm_templates_groups ptg ON
183 g.organization_uuid=#{query.organizationUuid,jdbcType=VARCHAR}
188 ptg.permission_reference AS permission,
189 ptg.template_id AS templateId
190 FROM perm_templates_groups ptg
192 <if test="query.withAtLeastOnePermission()">
193 AND ptg.group_id IS NULL
198 <if test="query.searchQueryToSql != null">
199 AND LOWER(groups.name) LIKE #{query.searchQueryToSqlLowercase} ESCAPE '/'
201 <if test="query.withAtLeastOnePermission()">
202 AND groups.permission IS NOT NULL
203 AND groups.templateId=#{templateId}
204 <if test="query.permission != null">
205 AND groups.permission=#{query.permission}
211 <sql id="templateColumns">
212 id, organization_uuid as organizationUuid, name, kee, description, key_pattern AS keyPattern, created_at AS createdAt, updated_at AS updatedAt
215 <select id="selectByUuid" parameterType="String" resultType="PermissionTemplate">
217 <include refid="templateColumns"/>
218 FROM permission_templates
222 <select id="selectAll" parameterType="map" resultType="PermissionTemplate">
224 <include refid="templateColumns"/>
225 from permission_templates
227 organization_uuid = #{organizationUuid,jdbcType=VARCHAR}
228 <if test="upperCaseNameLikeSql != null">
229 and upper(name) like #{upperCaseNameLikeSql} escape '/'
231 order by upper(name), name
234 <select id="selectByName" parameterType="map" resultType="PermissionTemplate">
236 <include refid="templateColumns"/>
237 from permission_templates
239 organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
240 upper(name) = #{name,jdbcType=VARCHAR}
243 <sql id="permissionTemplateUserColumns">
245 ptu.template_id as templateId,
246 ptu.permission_reference AS permission,
247 ptu.user_id AS userId,
249 u.login AS userLogin,
250 ptu.created_at AS createdAt,
251 ptu.updated_at AS updatedAt
254 <select id="selectUserPermissionsByTemplateIdAndUserLogins" parameterType="Long" resultType="PermissionTemplateUser">
256 <include refid="permissionTemplateUserColumns"/>
257 FROM perm_templates_users ptu
258 INNER JOIN users u ON u.id = ptu.user_id AND u.active = ${_true}
260 AND ptu.template_id = #{templateId}
261 <if test="!logins.isEmpty()">
262 AND u.login IN <foreach collection="logins" open="(" close=")" item="login" separator=",">
269 <select id="selectGroupPermissionsByTemplateIdAndGroupNames" parameterType="Long" resultType="PermissionTemplateGroup">
281 ptg.template_id as templateId,
282 ptg.permission_reference AS permission,
283 ptg.group_id AS groupId,
285 ptg.created_at as createdAt,
286 ptg.updated_at as updatedAt
287 FROM perm_templates_groups ptg
288 INNER JOIN groups g ON
293 ptg.template_id as templateId,
294 ptg.permission_reference AS permission,
296 'Anyone' AS groupName,
297 ptg.created_at as createdAt,
298 ptg.updated_at as updatedAt
299 FROM perm_templates_groups ptg
300 WHERE ptg.group_id IS NULL
303 sub.templateId=#{templateId}
304 <if test="!groups.isEmpty()">
305 AND sub.groupName IN <foreach collection="groups" open="(" close=")" item="group" separator=",">
312 <select id="selectPotentialPermissionsByUserIdAndTemplateId" parameterType="map" resultType="String">
313 <if test="userId!=null">
314 -- from template users
315 select ptu.permission_reference as permission_key
316 from perm_templates_users ptu
318 and ptu.user_id=#{userId}
319 and ptu.template_id=#{templateId}
322 -- from template groups except anyone group
323 select ptg.permission_reference as permission_key
324 from perm_templates_groups ptg
325 inner join groups_users gu on ptg.group_id = gu.group_id
327 and gu.user_id=#{userId}
328 and ptg.template_id=#{templateId}
331 -- from template characteristics
332 select ptc.permission_key as permission_key
333 from perm_tpl_characteristics ptc
335 and with_project_creator = ${_true}
336 and ptc.template_id = #{templateId}
341 select ptg.permission_reference as permission_key
342 from perm_templates_groups ptg
343 where ptg.template_id=#{templateId}
344 and ptg.group_id IS NULL
347 <select id="usersCountByTemplateIdAndPermission" parameterType="map"
348 resultType="org.sonar.db.permission.template.CountByTemplateAndPermissionDto">
349 SELECT ptu.template_id as templateId, ptu.permission_reference as permission, count(u.login) as count
351 INNER JOIN perm_templates_users ptu ON ptu.user_id=u.id
352 AND ptu.template_id in
353 <foreach collection="templateIds" open="(" close=")" item="id" separator=",">
357 AND u.active = ${_true}
359 GROUP BY ptu.template_id, ptu.permission_reference
362 <select id="groupsCountByTemplateIdAndPermission" parameterType="map"
363 resultType="org.sonar.db.permission.template.CountByTemplateAndPermissionDto">
364 SELECT count(1) as count, permission, templateId
366 (SELECT g.name as name, ptg.permission_reference as permission, ptg.template_id as templateId
368 INNER JOIN perm_templates_groups ptg ON ptg.group_id=g.id
370 -- Add Anyone group permission
371 SELECT #{anyoneGroup} as name, ptg.permission_reference as permission, ptg.template_id as templateId
372 FROM perm_templates_groups ptg
374 AND ptg.group_id IS NULL
378 AND groups.templateId in
379 <foreach collection="templateIds" open="(" close=")" item="id" separator=",">
383 GROUP BY groups.permission, groups.templateId
386 <select id="countGroupsWithPermission" resultType="int" parameterType="map">
388 from perm_templates_groups ptg
389 where ptg.template_id = #{templateId}
390 and ptg.permission_reference = #{permission}
392 <if test="groupId == null">
395 <if test="groupId != null">
396 ptg.group_id = #{groupId}
400 <select id="selectTemplateIdsByOrganization" resultType="Long">
406 organization_uuid = #{organizationUuid,jdbcType=VARCHAR}
409 <select id="selectAllGroupPermissionTemplatesByGroupId" parameterType="Long" resultType="PermissionTemplateGroup">
412 ptg.template_id as templateId,
413 ptg.permission_reference AS permission,
414 ptg.group_id AS groupId,
416 ptg.created_at as createdAt,
417 ptg.updated_at as updatedAt
418 FROM perm_templates_groups ptg
419 INNER JOIN groups g ON g.id=ptg.group_id
421 ptg.group_id=#{groupId,jdbcType=INTEGER}