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" useGeneratedKeys="false">
7 INSERT INTO permission_templates (name, uuid, description, key_pattern, created_at, updated_at)
9 #{name,jdbcType=VARCHAR},
10 #{uuid,jdbcType=VARCHAR},
11 #{description,jdbcType=VARCHAR},
12 #{keyPattern,jdbcType=VARCHAR},
17 <update id="update" parameterType="PermissionTemplate">
18 UPDATE permission_templates
19 SET name = #{name}, description = #{description}, key_pattern = #{keyPattern}, updated_at = #{updatedAt}
23 <delete id="deleteByUuid" parameterType="String">
24 DELETE FROM permission_templates
28 <delete id="deleteByUuids" parameterType="String">
32 uuid in <foreach collection="templateUuids" open="(" close=")" item="uuid" separator=",">
33 #{uuid,jdbcType=BIGINT}
37 <delete id="deleteUserPermissionsByTemplateUuid" parameterType="string">
41 template_uuid = #{templateUuid,jdbcType=VARCHAR}
44 <delete id="deleteUserPermissionsByTemplateUuids">
48 template_uuid in <foreach collection="templateUuids" open="(" close=")" item="templateUuid" separator=",">
49 #{templateUuid,jdbcType=BIGINT}
53 <delete id="deleteUserPermission" parameterType="PermissionTemplateUser">
54 DELETE FROM perm_templates_users
55 WHERE template_uuid = #{templateUuid}
56 AND user_uuid = #{userUuid}
57 AND permission_reference = #{permission}
60 <delete id="deleteUserPermissionsByUserUuid" parameterType="String">
61 delete from perm_templates_users
63 user_uuid = #{userUuid,jdbcType=VARCHAR}
66 <delete id="deleteGroupPermissionsByTemplateUuid" parameterType="String">
70 template_uuid = #{templateUuid,jdbcType=VARCHAR}
73 <delete id="deleteGroupPermissionsByTemplateUuids">
77 template_uuid in <foreach collection="templateUuids" open="(" close=")" item="templateUuid" separator=",">
78 #{templateUuid,jdbcType=VARCHAR}
82 <delete id="deleteGroupPermission" parameterType="PermissionTemplateGroup">
83 DELETE FROM perm_templates_groups
84 WHERE template_uuid = #{templateUuid}
85 AND permission_reference = #{permission}
88 <when test="groupUuid != null">
89 group_uuid = #{groupUuid}
97 <insert id="insertUserPermission" parameterType="PermissionTemplateUser">
98 INSERT INTO perm_templates_users (uuid, template_uuid, user_uuid, permission_reference, created_at, updated_at)
99 VALUES (#{uuid}, #{templateUuid}, #{userUuid}, #{permission}, #{createdAt}, #{updatedAt})
102 <insert id="insertGroupPermission" parameterType="PermissionTemplateGroup">
103 INSERT INTO perm_templates_groups (uuid, template_uuid, group_uuid, permission_reference, created_at, updated_at)
105 #{uuid,jdbcType=VARCHAR},
106 #{templateUuid,jdbcType=VARCHAR},
107 #{groupUuid,jdbcType=VARCHAR},
108 #{permission,jdbcType=VARCHAR},
109 #{createdAt,jdbcType=TIMESTAMP},
110 #{updatedAt,jdbcType=TIMESTAMP}
114 <delete id="deleteByGroupUuid" parameterType="string">
115 delete from perm_templates_groups
116 where group_uuid = #{groupUuid,jdbcType=VARCHAR}
119 <select id="selectUserLoginsByQueryAndTemplate" parameterType="map" resultType="string">
120 select u.login as login
121 <include refid="userLoginsByQueryAndTemplate"/>
122 group by u.login, u.name
123 order by case when (count(ptu.permission_reference) > 0) then 1 else 2 end asc, u.name
126 <select id="countUserLoginsByQueryAndTemplate" parameterType="map" resultType="int">
129 SELECT DISTINCT u.login AS login, u.name AS name
130 <include refid="userLoginsByQueryAndTemplate"/>) u
133 <sql id="userLoginsByQueryAndTemplate">
135 LEFT JOIN perm_templates_users ptu ON ptu.user_uuid=u.uuid AND ptu.template_uuid=#{templateUuid}
138 <if test="query.getSearchQueryToSql() != null">
139 AND lower(u.name) like #{query.searchQueryToSqlLowercase} ESCAPE '/'
141 <if test="query.withAtLeastOnePermission()">
142 and ptu.permission_reference is not null
143 <if test="query.getPermission()!=null">
144 and ptu.permission_reference=#{query.permission}
150 <select id="selectGroupNamesByQueryAndTemplate" parameterType="map" resultType="string">
151 select groups.name, lower(groups.name), groups.group_uuid
152 <include refid="groupNamesByQueryAndTemplate"/>
153 group by groups.name, lower(groups.name), groups.group_uuid
154 order by case when (count(groups.permission) > 0) then 1 else 2 end asc, lower(groups.name), groups.name, groups.group_uuid
157 <select id="countGroupNamesByQueryAndTemplate" parameterType="map" resultType="int">
160 SELECT DISTINCT group_uuid
161 <include refid="groupNamesByQueryAndTemplate"/>) g
164 <sql id="groupNamesByQueryAndTemplate">
167 g.uuid AS group_uuid,
169 ptg.permission_reference AS permission,
170 ptg.template_uuid AS templateUuid
172 LEFT JOIN perm_templates_groups ptg ON
173 ptg.group_uuid=g.uuid
174 AND ptg.template_uuid=#{templateUuid}
177 'Anyone' AS group_uuid,
179 ptg.permission_reference AS permission,
180 ptg.template_uuid AS templateUuid
182 LEFT JOIN perm_templates_groups ptg ON
183 ptg.template_uuid=#{templateUuid}
184 <if test="query.withAtLeastOnePermission()">
186 ptg.group_uuid IS NULL
191 <if test="query.searchQueryToSql != null">
192 AND LOWER(groups.name) LIKE #{query.searchQueryToSqlLowercase} ESCAPE '/'
194 <if test="query.withAtLeastOnePermission()">
195 AND groups.permission IS NOT NULL
196 AND groups.templateUuid=#{templateUuid}
197 <if test="query.permission != null">
198 AND groups.permission=#{query.permission}
204 <sql id="templateColumns">
205 name, uuid, description, key_pattern AS keyPattern, created_at AS createdAt, updated_at AS updatedAt
208 <select id="selectByUuid" parameterType="String" resultType="PermissionTemplate">
210 <include refid="templateColumns"/>
211 FROM permission_templates
215 <select id="selectAll" parameterType="map" resultType="PermissionTemplate">
217 <include refid="templateColumns"/>
218 from permission_templates
219 <if test="upperCaseNameLikeSql != null">
220 where upper(name) like #{upperCaseNameLikeSql} escape '/'
222 order by upper(name), name
225 <select id="selectByName" parameterType="map" resultType="PermissionTemplate">
227 <include refid="templateColumns"/>
228 from permission_templates
230 upper(name) = #{name,jdbcType=VARCHAR}
233 <sql id="permissionTemplateUserColumns">
235 ptu.template_uuid as templateUuid,
236 ptu.permission_reference AS permission,
237 ptu.user_uuid AS userUuid,
239 u.login AS userLogin,
240 ptu.created_at AS createdAt,
241 ptu.updated_at AS updatedAt
244 <select id="selectUserPermissionsByTemplateUuidAndUserLogins" parameterType="String" resultType="PermissionTemplateUser">
246 <include refid="permissionTemplateUserColumns"/>
247 FROM perm_templates_users ptu
248 INNER JOIN users u ON u.uuid = ptu.user_uuid AND u.active = ${_true}
250 AND ptu.template_uuid = #{templateUuid}
251 <if test="!logins.isEmpty()">
252 AND u.login IN <foreach collection="logins" open="(" close=")" item="login" separator=",">
259 <select id="selectGroupPermissionsByTemplateUuidAndGroupNames" parameterType="String" resultType="PermissionTemplateGroup">
271 ptg.template_uuid as templateUuid,
272 ptg.permission_reference AS permission,
273 ptg.group_uuid AS groupUuid,
275 ptg.created_at as createdAt,
276 ptg.updated_at as updatedAt
277 FROM perm_templates_groups ptg
278 INNER JOIN groups g ON
279 g.uuid=ptg.group_uuid
283 ptg.template_uuid as templateUuid,
284 ptg.permission_reference AS permission,
285 'Anyone' AS groupUuid,
286 'Anyone' AS groupName,
287 ptg.created_at as createdAt,
288 ptg.updated_at as updatedAt
289 FROM perm_templates_groups ptg
290 WHERE ptg.group_uuid IS NULL
293 sub.templateUuid=#{templateUuid}
294 <if test="!groups.isEmpty()">
295 AND sub.groupName IN <foreach collection="groups" open="(" close=")" item="group" separator=",">
296 #{group,jdbcType=VARCHAR}
302 <select id="selectPotentialPermissionsByUserUuidAndTemplateUuid" parameterType="map" resultType="String">
303 <if test="userUuid!=null">
304 -- from template users
305 select ptu.permission_reference as permission_key
306 from perm_templates_users ptu
308 and ptu.user_uuid=#{userUuid}
309 and ptu.template_uuid=#{templateUuid}
312 -- from template groups except anyone group
313 select ptg.permission_reference as permission_key
314 from perm_templates_groups ptg
315 inner join groups_users gu on ptg.group_uuid = gu.group_uuid
317 and gu.user_uuid=#{userUuid}
318 and ptg.template_uuid=#{templateUuid}
321 -- from template characteristics
322 select ptc.permission_key as permission_key
323 from perm_tpl_characteristics ptc
325 and with_project_creator = ${_true}
326 and ptc.template_uuid = #{templateUuid}
331 select ptg.permission_reference as permission_key
332 from perm_templates_groups ptg
333 where ptg.template_uuid=#{templateUuid}
334 and ptg.group_uuid IS NULL
337 <select id="usersCountByTemplateUuidAndPermission" parameterType="map"
338 resultType="org.sonar.db.permission.template.CountByTemplateAndPermissionDto">
339 SELECT ptu.template_uuid as templateUuid, ptu.permission_reference as permission, count(u.login) as count
341 INNER JOIN perm_templates_users ptu ON ptu.user_uuid=u.uuid
342 AND ptu.template_uuid in
343 <foreach collection="templateUuids" open="(" close=")" item="id" separator=",">
347 AND u.active = ${_true}
349 GROUP BY ptu.template_uuid, ptu.permission_reference
352 <select id="groupsCountByTemplateUuidAndPermission" parameterType="map"
353 resultType="org.sonar.db.permission.template.CountByTemplateAndPermissionDto">
354 SELECT count(1) as count, permission, templateUuid
356 (SELECT g.name as name, ptg.permission_reference as permission, ptg.template_uuid as templateUuid
358 INNER JOIN perm_templates_groups ptg ON ptg.group_uuid=g.uuid
360 -- Add Anyone group permission
361 SELECT #{anyoneGroup} as name, ptg.permission_reference as permission, ptg.template_uuid as templateUuid
362 FROM perm_templates_groups ptg
364 AND ptg.group_uuid IS NULL
368 AND groups.templateUuid in
369 <foreach collection="templateUuids" open="(" close=")" item="id" separator=",">
373 GROUP BY groups.permission, groups.templateUuid
376 <select id="countGroupsWithPermission" resultType="int" parameterType="map">
378 from perm_templates_groups ptg
379 where ptg.template_uuid = #{templateUuid}
380 and ptg.permission_reference = #{permission}
382 <if test="groupUuid == null">
383 ptg.group_uuid is null
385 <if test="groupUuid != null">
386 ptg.group_uuid = #{groupUuid}
390 <select id="selectTemplateUuidsn" resultType="String">
397 <select id="selectAllGroupPermissionTemplatesByGroupUuid" parameterType="string" resultType="PermissionTemplateGroup">
400 ptg.template_uuid as templateUuid,
401 ptg.permission_reference AS permission,
402 ptg.group_uuid AS groupUuid,
404 ptg.created_at as createdAt,
405 ptg.updated_at as updatedAt
406 FROM perm_templates_groups ptg
407 INNER JOIN groups g ON g.uuid=ptg.group_uuid
409 ptg.group_uuid=#{groupUuid,jdbcType=VARCHAR}