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="deleteUserPermissionsByTemplateUuid" parameterType="string">
32 template_uuid = #{templateUuid,jdbcType=VARCHAR}
35 <delete id="deleteUserPermission" parameterType="PermissionTemplateUser">
36 DELETE FROM perm_templates_users
37 WHERE template_uuid = #{templateUuid}
38 AND user_uuid = #{userUuid}
39 AND permission_reference = #{permission}
42 <delete id="deleteUserPermissionsByUserUuid" parameterType="String">
43 delete from perm_templates_users
45 user_uuid = #{userUuid,jdbcType=VARCHAR}
48 <delete id="deleteGroupPermissionsByTemplateUuid" parameterType="String">
52 template_uuid = #{templateUuid,jdbcType=VARCHAR}
55 <delete id="deleteGroupPermission" parameterType="PermissionTemplateGroup">
56 DELETE FROM perm_templates_groups
57 WHERE template_uuid = #{templateUuid}
58 AND permission_reference = #{permission}
61 <when test="groupUuid != null">
62 group_uuid = #{groupUuid}
70 <insert id="insertUserPermission" parameterType="PermissionTemplateUser">
71 INSERT INTO perm_templates_users (uuid, template_uuid, user_uuid, permission_reference, created_at, updated_at)
72 VALUES (#{uuid}, #{templateUuid}, #{userUuid}, #{permission}, #{createdAt}, #{updatedAt})
75 <insert id="insertGroupPermission" parameterType="PermissionTemplateGroup">
76 INSERT INTO perm_templates_groups (uuid, template_uuid, group_uuid, permission_reference, created_at, updated_at)
78 #{uuid,jdbcType=VARCHAR},
79 #{templateUuid,jdbcType=VARCHAR},
80 #{groupUuid,jdbcType=VARCHAR},
81 #{permission,jdbcType=VARCHAR},
82 #{createdAt,jdbcType=TIMESTAMP},
83 #{updatedAt,jdbcType=TIMESTAMP}
87 <delete id="deleteByGroupUuid" parameterType="string">
88 delete from perm_templates_groups
89 where group_uuid = #{groupUuid,jdbcType=VARCHAR}
92 <select id="selectUserLoginsByQueryAndTemplate" resultType="string">
93 select u.login as login
94 <include refid="userLoginsByQueryAndTemplate"/>
95 group by u.login, u.name
96 order by case when (count(ptu.permission_reference) > 0) then 1 else 2 end asc, u.name
97 <include refid="pagination"/>
100 <select id="countUserLoginsByQueryAndTemplate" parameterType="map" resultType="int">
103 SELECT DISTINCT u.login AS login, u.name AS name
104 <include refid="userLoginsByQueryAndTemplate"/>) u
107 <sql id="userLoginsByQueryAndTemplate">
109 LEFT JOIN perm_templates_users ptu ON ptu.user_uuid=u.uuid AND ptu.template_uuid=#{templateUuid}
112 <if test="query.getSearchQueryToSql() != null">
113 AND (lower(u.name) like #{query.searchQueryToSqlLowercase} ESCAPE '/'
114 OR lower(u.login) like #{query.searchQueryToSqlLowercase} ESCAPE '/'
115 OR lower(u.email) like #{query.searchQueryToSqlLowercase} ESCAPE '/')
117 <if test="query.withAtLeastOnePermission()">
118 and ptu.permission_reference is not null
119 <if test="query.getPermission()!=null">
120 and ptu.permission_reference=#{query.permission}
126 <select id="selectGroupNamesByQueryAndTemplate" resultType="string">
127 select groups.name, lower(groups.name), groups.group_uuid
128 <include refid="groupNamesByQueryAndTemplate"/>
129 group by groups.name, lower(groups.name), groups.group_uuid
130 order by case when (count(groups.permission) > 0) then 1 else 2 end asc, lower(groups.name), groups.name, groups.group_uuid
131 <include refid="pagination"/>
134 <select id="countGroupNamesByQueryAndTemplate" parameterType="map" resultType="int">
137 SELECT DISTINCT group_uuid
138 <include refid="groupNamesByQueryAndTemplate"/>) g
141 <sql id="pagination">
142 offset #{pagination.offset,jdbcType=INTEGER} rows fetch next #{pagination.pageSize,jdbcType=INTEGER} rows only
145 <sql id="groupNamesByQueryAndTemplate">
148 g.uuid AS group_uuid,
150 ptg.permission_reference AS permission,
151 ptg.template_uuid AS templateUuid
153 LEFT JOIN perm_templates_groups ptg ON
154 ptg.group_uuid=g.uuid
155 AND ptg.template_uuid=#{templateUuid}
158 'Anyone' AS group_uuid,
160 ptg.permission_reference AS permission,
161 ptg.template_uuid AS templateUuid
163 LEFT JOIN perm_templates_groups ptg ON
164 ptg.template_uuid=#{templateUuid}
165 <if test="query.withAtLeastOnePermission()">
167 ptg.group_uuid IS NULL
172 <if test="query.searchQueryToSql != null">
173 AND LOWER(groups.name) LIKE #{query.searchQueryToSqlLowercase} ESCAPE '/'
175 <if test="query.withAtLeastOnePermission()">
176 AND groups.permission IS NOT NULL
177 AND groups.templateUuid=#{templateUuid}
178 <if test="query.permission != null">
179 AND groups.permission=#{query.permission}
185 <sql id="templateColumns">
186 name, uuid, description, key_pattern AS keyPattern, created_at AS createdAt, updated_at AS updatedAt
189 <select id="selectByUuid" parameterType="String" resultType="PermissionTemplate">
191 <include refid="templateColumns"/>
192 FROM permission_templates
196 <select id="selectAll" parameterType="map" resultType="PermissionTemplate">
198 <include refid="templateColumns"/>
199 from permission_templates
200 <if test="upperCaseNameLikeSql != null">
201 where upper(name) like #{upperCaseNameLikeSql} escape '/'
203 order by upper(name), name
206 <select id="selectByName" parameterType="map" resultType="PermissionTemplate">
208 <include refid="templateColumns"/>
209 from permission_templates
211 upper(name) = #{name,jdbcType=VARCHAR}
214 <sql id="permissionTemplateUserColumns">
216 ptu.template_uuid as templateUuid,
217 ptu.permission_reference AS permission,
218 ptu.user_uuid AS userUuid,
220 u.login AS userLogin,
221 ptu.created_at AS createdAt,
222 ptu.updated_at AS updatedAt
225 <select id="selectUserPermissionsByTemplateUuidAndUserLogins" parameterType="String" resultType="PermissionTemplateUser">
227 <include refid="permissionTemplateUserColumns"/>
228 FROM perm_templates_users ptu
229 INNER JOIN users u ON u.uuid = ptu.user_uuid AND u.active = ${_true}
231 AND ptu.template_uuid = #{templateUuid}
232 <if test="!logins.isEmpty()">
233 AND u.login IN <foreach collection="logins" open="(" close=")" item="login" separator=",">
240 <select id="selectGroupPermissionsByTemplateUuidAndGroupNames" parameterType="String" resultType="PermissionTemplateGroup">
252 ptg.template_uuid as templateUuid,
253 ptg.permission_reference AS permission,
254 ptg.group_uuid AS groupUuid,
256 ptg.created_at as createdAt,
257 ptg.updated_at as updatedAt
258 FROM perm_templates_groups ptg
259 INNER JOIN groups g ON
260 g.uuid=ptg.group_uuid
264 ptg.template_uuid as templateUuid,
265 ptg.permission_reference AS permission,
266 'Anyone' AS groupUuid,
267 'Anyone' AS groupName,
268 ptg.created_at as createdAt,
269 ptg.updated_at as updatedAt
270 FROM perm_templates_groups ptg
271 WHERE ptg.group_uuid IS NULL
274 sub.templateUuid=#{templateUuid}
275 <if test="!groups.isEmpty()">
276 AND sub.groupName IN <foreach collection="groups" open="(" close=")" item="group" separator=",">
277 #{group,jdbcType=VARCHAR}
283 <select id="selectPotentialPermissionsByUserUuidAndTemplateUuid" parameterType="map" resultType="String">
284 <if test="userUuid!=null">
285 -- from template users
286 select ptu.permission_reference as permission_key
287 from perm_templates_users ptu
289 and ptu.user_uuid=#{userUuid}
290 and ptu.template_uuid=#{templateUuid}
293 -- from template groups except anyone group
294 select ptg.permission_reference as permission_key
295 from perm_templates_groups ptg
296 inner join groups_users gu on ptg.group_uuid = gu.group_uuid
298 and gu.user_uuid=#{userUuid}
299 and ptg.template_uuid=#{templateUuid}
302 -- from template characteristics
303 select ptc.permission_key as permission_key
304 from perm_tpl_characteristics ptc
306 and with_project_creator = ${_true}
307 and ptc.template_uuid = #{templateUuid}
312 select ptg.permission_reference as permission_key
313 from perm_templates_groups ptg
314 where ptg.template_uuid=#{templateUuid}
315 and ptg.group_uuid IS NULL
318 <select id="usersCountByTemplateUuidAndPermission" parameterType="map"
319 resultType="org.sonar.db.permission.template.CountByTemplateAndPermissionDto">
320 SELECT ptu.template_uuid as templateUuid, ptu.permission_reference as permission, count(u.login) as count
322 INNER JOIN perm_templates_users ptu ON ptu.user_uuid=u.uuid
323 AND ptu.template_uuid in
324 <foreach collection="templateUuids" open="(" close=")" item="id" separator=",">
328 AND u.active = ${_true}
330 GROUP BY ptu.template_uuid, ptu.permission_reference
333 <select id="groupsCountByTemplateUuidAndPermission" parameterType="map"
334 resultType="org.sonar.db.permission.template.CountByTemplateAndPermissionDto">
335 SELECT count(1) as count, permission, templateUuid
337 (SELECT g.name as name, ptg.permission_reference as permission, ptg.template_uuid as templateUuid
339 INNER JOIN perm_templates_groups ptg ON ptg.group_uuid=g.uuid
341 -- Add Anyone group permission
342 SELECT #{anyoneGroup} as name, ptg.permission_reference as permission, ptg.template_uuid as templateUuid
343 FROM perm_templates_groups ptg
345 AND ptg.group_uuid IS NULL
349 AND groups.templateUuid in
350 <foreach collection="templateUuids" open="(" close=")" item="id" separator=",">
354 GROUP BY groups.permission, groups.templateUuid
357 <select id="countGroupsWithPermission" resultType="int" parameterType="map">
359 from perm_templates_groups ptg
360 where ptg.template_uuid = #{templateUuid}
361 and ptg.permission_reference = #{permission}
363 <if test="groupUuid == null">
364 ptg.group_uuid is null
366 <if test="groupUuid != null">
367 ptg.group_uuid = #{groupUuid}
371 <select id="selectAllGroupPermissionTemplatesByGroupUuid" parameterType="string" resultType="PermissionTemplateGroup">
374 ptg.template_uuid as templateUuid,
375 ptg.permission_reference AS permission,
376 ptg.group_uuid AS groupUuid,
378 ptg.created_at as createdAt,
379 ptg.updated_at as updatedAt
380 FROM perm_templates_groups ptg
381 INNER JOIN groups g ON g.uuid=ptg.group_uuid
383 ptg.group_uuid=#{groupUuid,jdbcType=VARCHAR}