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" parameterType="map" 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
99 <select id="countUserLoginsByQueryAndTemplate" parameterType="map" resultType="int">
102 SELECT DISTINCT u.login AS login, u.name AS name
103 <include refid="userLoginsByQueryAndTemplate"/>) u
106 <sql id="userLoginsByQueryAndTemplate">
108 LEFT JOIN perm_templates_users ptu ON ptu.user_uuid=u.uuid AND ptu.template_uuid=#{templateUuid}
111 <if test="query.getSearchQueryToSql() != null">
112 AND (lower(u.name) like #{query.searchQueryToSqlLowercase} ESCAPE '/'
113 OR lower(u.login) like #{query.searchQueryToSqlLowercase} ESCAPE '/'
114 OR lower(u.email) like #{query.searchQueryToSqlLowercase} ESCAPE '/')
116 <if test="query.withAtLeastOnePermission()">
117 and ptu.permission_reference is not null
118 <if test="query.getPermission()!=null">
119 and ptu.permission_reference=#{query.permission}
125 <select id="selectGroupNamesByQueryAndTemplate" parameterType="map" resultType="string">
126 select groups.name, lower(groups.name), groups.group_uuid
127 <include refid="groupNamesByQueryAndTemplate"/>
128 group by groups.name, lower(groups.name), groups.group_uuid
129 order by case when (count(groups.permission) > 0) then 1 else 2 end asc, lower(groups.name), groups.name, groups.group_uuid
132 <select id="countGroupNamesByQueryAndTemplate" parameterType="map" resultType="int">
135 SELECT DISTINCT group_uuid
136 <include refid="groupNamesByQueryAndTemplate"/>) g
139 <sql id="groupNamesByQueryAndTemplate">
142 g.uuid AS group_uuid,
144 ptg.permission_reference AS permission,
145 ptg.template_uuid AS templateUuid
147 LEFT JOIN perm_templates_groups ptg ON
148 ptg.group_uuid=g.uuid
149 AND ptg.template_uuid=#{templateUuid}
152 'Anyone' AS group_uuid,
154 ptg.permission_reference AS permission,
155 ptg.template_uuid AS templateUuid
157 LEFT JOIN perm_templates_groups ptg ON
158 ptg.template_uuid=#{templateUuid}
159 <if test="query.withAtLeastOnePermission()">
161 ptg.group_uuid IS NULL
166 <if test="query.searchQueryToSql != null">
167 AND LOWER(groups.name) LIKE #{query.searchQueryToSqlLowercase} ESCAPE '/'
169 <if test="query.withAtLeastOnePermission()">
170 AND groups.permission IS NOT NULL
171 AND groups.templateUuid=#{templateUuid}
172 <if test="query.permission != null">
173 AND groups.permission=#{query.permission}
179 <sql id="templateColumns">
180 name, uuid, description, key_pattern AS keyPattern, created_at AS createdAt, updated_at AS updatedAt
183 <select id="selectByUuid" parameterType="String" resultType="PermissionTemplate">
185 <include refid="templateColumns"/>
186 FROM permission_templates
190 <select id="selectAll" parameterType="map" resultType="PermissionTemplate">
192 <include refid="templateColumns"/>
193 from permission_templates
194 <if test="upperCaseNameLikeSql != null">
195 where upper(name) like #{upperCaseNameLikeSql} escape '/'
197 order by upper(name), name
200 <select id="selectByName" parameterType="map" resultType="PermissionTemplate">
202 <include refid="templateColumns"/>
203 from permission_templates
205 upper(name) = #{name,jdbcType=VARCHAR}
208 <sql id="permissionTemplateUserColumns">
210 ptu.template_uuid as templateUuid,
211 ptu.permission_reference AS permission,
212 ptu.user_uuid AS userUuid,
214 u.login AS userLogin,
215 ptu.created_at AS createdAt,
216 ptu.updated_at AS updatedAt
219 <select id="selectUserPermissionsByTemplateUuidAndUserLogins" parameterType="String" resultType="PermissionTemplateUser">
221 <include refid="permissionTemplateUserColumns"/>
222 FROM perm_templates_users ptu
223 INNER JOIN users u ON u.uuid = ptu.user_uuid AND u.active = ${_true}
225 AND ptu.template_uuid = #{templateUuid}
226 <if test="!logins.isEmpty()">
227 AND u.login IN <foreach collection="logins" open="(" close=")" item="login" separator=",">
234 <select id="selectGroupPermissionsByTemplateUuidAndGroupNames" parameterType="String" resultType="PermissionTemplateGroup">
246 ptg.template_uuid as templateUuid,
247 ptg.permission_reference AS permission,
248 ptg.group_uuid AS groupUuid,
250 ptg.created_at as createdAt,
251 ptg.updated_at as updatedAt
252 FROM perm_templates_groups ptg
253 INNER JOIN groups g ON
254 g.uuid=ptg.group_uuid
258 ptg.template_uuid as templateUuid,
259 ptg.permission_reference AS permission,
260 'Anyone' AS groupUuid,
261 'Anyone' AS groupName,
262 ptg.created_at as createdAt,
263 ptg.updated_at as updatedAt
264 FROM perm_templates_groups ptg
265 WHERE ptg.group_uuid IS NULL
268 sub.templateUuid=#{templateUuid}
269 <if test="!groups.isEmpty()">
270 AND sub.groupName IN <foreach collection="groups" open="(" close=")" item="group" separator=",">
271 #{group,jdbcType=VARCHAR}
277 <select id="selectPotentialPermissionsByUserUuidAndTemplateUuid" parameterType="map" resultType="String">
278 <if test="userUuid!=null">
279 -- from template users
280 select ptu.permission_reference as permission_key
281 from perm_templates_users ptu
283 and ptu.user_uuid=#{userUuid}
284 and ptu.template_uuid=#{templateUuid}
287 -- from template groups except anyone group
288 select ptg.permission_reference as permission_key
289 from perm_templates_groups ptg
290 inner join groups_users gu on ptg.group_uuid = gu.group_uuid
292 and gu.user_uuid=#{userUuid}
293 and ptg.template_uuid=#{templateUuid}
296 -- from template characteristics
297 select ptc.permission_key as permission_key
298 from perm_tpl_characteristics ptc
300 and with_project_creator = ${_true}
301 and ptc.template_uuid = #{templateUuid}
306 select ptg.permission_reference as permission_key
307 from perm_templates_groups ptg
308 where ptg.template_uuid=#{templateUuid}
309 and ptg.group_uuid IS NULL
312 <select id="usersCountByTemplateUuidAndPermission" parameterType="map"
313 resultType="org.sonar.db.permission.template.CountByTemplateAndPermissionDto">
314 SELECT ptu.template_uuid as templateUuid, ptu.permission_reference as permission, count(u.login) as count
316 INNER JOIN perm_templates_users ptu ON ptu.user_uuid=u.uuid
317 AND ptu.template_uuid in
318 <foreach collection="templateUuids" open="(" close=")" item="id" separator=",">
322 AND u.active = ${_true}
324 GROUP BY ptu.template_uuid, ptu.permission_reference
327 <select id="groupsCountByTemplateUuidAndPermission" parameterType="map"
328 resultType="org.sonar.db.permission.template.CountByTemplateAndPermissionDto">
329 SELECT count(1) as count, permission, templateUuid
331 (SELECT g.name as name, ptg.permission_reference as permission, ptg.template_uuid as templateUuid
333 INNER JOIN perm_templates_groups ptg ON ptg.group_uuid=g.uuid
335 -- Add Anyone group permission
336 SELECT #{anyoneGroup} as name, ptg.permission_reference as permission, ptg.template_uuid as templateUuid
337 FROM perm_templates_groups ptg
339 AND ptg.group_uuid IS NULL
343 AND groups.templateUuid in
344 <foreach collection="templateUuids" open="(" close=")" item="id" separator=",">
348 GROUP BY groups.permission, groups.templateUuid
351 <select id="countGroupsWithPermission" resultType="int" parameterType="map">
353 from perm_templates_groups ptg
354 where ptg.template_uuid = #{templateUuid}
355 and ptg.permission_reference = #{permission}
357 <if test="groupUuid == null">
358 ptg.group_uuid is null
360 <if test="groupUuid != null">
361 ptg.group_uuid = #{groupUuid}
365 <select id="selectAllGroupPermissionTemplatesByGroupUuid" parameterType="string" resultType="PermissionTemplateGroup">
368 ptg.template_uuid as templateUuid,
369 ptg.permission_reference AS permission,
370 ptg.group_uuid AS groupUuid,
372 ptg.created_at as createdAt,
373 ptg.updated_at as updatedAt
374 FROM perm_templates_groups ptg
375 INNER JOIN groups g ON g.uuid=ptg.group_uuid
377 ptg.group_uuid=#{groupUuid,jdbcType=VARCHAR}