-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
AuthorizationMapper.xml
434 lines (375 loc) · 12.9 KB
/
AuthorizationMapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?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.permission.AuthorizationMapper">
<select id="selectOrganizationPermissions" parameterType="map" resultType="string">
select gr.role
from group_roles gr
inner join groups_users gu on gr.group_id=gu.group_id
where
gr.organization_uuid=#{organizationUuid,jdbcType=VARCHAR} and
gr.resource_id is null and
gu.user_id=#{userId,jdbcType=INTEGER}
union
select gr.role
from group_roles gr
where
gr.organization_uuid=#{organizationUuid,jdbcType=VARCHAR} and
gr.group_id is null and
gr.resource_id is null
union
select ur.role
from user_roles ur
where
ur.organization_uuid=#{organizationUuid,jdbcType=VARCHAR} and
ur.user_id=#{userId,jdbcType=INTEGER}
and ur.resource_id is null
</select>
<select id="selectOrganizationPermissionsOfAnonymous" parameterType="map" resultType="string">
select gr.role
from group_roles gr
where
gr.organization_uuid=#{organizationUuid,jdbcType=VARCHAR} and
gr.resource_id is null and
gr.group_id is null
</select>
<select id="countUsersWithGlobalPermissionExcludingGroup" parameterType="map" resultType="int">
select count(1) from
(
select gu.user_id
from groups_users gu
inner join group_roles gr on gr.group_id = gu.group_id
where
gr.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
gr.role = #{permission,jdbcType=VARCHAR} and
gr.resource_id is null and
gr.group_id is not null and
gr.group_id != #{excludedGroupId,jdbcType=INTEGER}
union
select ur.user_id
from user_roles ur
where
ur.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
ur.resource_id is null and
ur.role = #{permission,jdbcType=VARCHAR}
) remaining
</select>
<select id="countUsersWithGlobalPermissionExcludingUser" parameterType="map" resultType="int">
select count(1) from
(
select gu.user_id
from groups_users gu
inner join group_roles gr on gr.group_id = gu.group_id
where
gr.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
gr.role = #{permission,jdbcType=VARCHAR} and
gr.resource_id is null and
gr.group_id is not null and
gu.user_id != #{excludedUserId,jdbcType=INTEGER}
union
select ur.user_id
from user_roles ur
where
ur.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
ur.resource_id is null and
ur.role = #{permission,jdbcType=VARCHAR} and
ur.user_id != #{excludedUserId,jdbcType=INTEGER}
) remaining
</select>
<select id="countUsersWithGlobalPermissionExcludingGroupMember" parameterType="map" resultType="int">
select count(1) from
(
select gu.user_id
from groups_users gu
inner join group_roles gr on gr.group_id = gu.group_id
where
gr.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
gr.role = #{permission,jdbcType=VARCHAR} and
gr.resource_id is null and
gr.group_id is not null and
(gu.group_id != #{groupId,jdbcType=INTEGER} or gu.user_id != #{userId,jdbcType=INTEGER})
union
select ur.user_id
from user_roles ur
where
ur.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
ur.resource_id is null and
ur.role = #{permission,jdbcType=VARCHAR}
) remaining
</select>
<select id="countUsersWithGlobalPermissionExcludingUserPermission" parameterType="map" resultType="int">
select count(1) from
(
select gu.user_id
from groups_users gu
inner join group_roles gr on gr.group_id = gu.group_id
where
gr.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
gr.role = #{permission,jdbcType=VARCHAR} and
gr.resource_id is null and
gr.group_id is not null
union
select ur.user_id
from user_roles ur
where
ur.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
ur.resource_id is null and
ur.role = #{permission,jdbcType=VARCHAR} and
ur.user_id != #{userId,jdbcType=INTEGER}
) remaining
</select>
<select id="selectOrganizationUuidsOfUserWithGlobalPermission" parameterType="map" resultType="String">
select gr.organization_uuid
from group_roles gr
inner join groups_users gu on gr.group_id = gu.group_id
where
gr.role = #{permission,jdbcType=VARCHAR} and
gr.resource_id is null and
gr.group_id is not null and
gu.user_id = #{userId,jdbcType=INTEGER}
union
select ur.organization_uuid
from user_roles ur
where
ur.resource_id is null and
ur.role = #{permission,jdbcType=VARCHAR} and
ur.user_id = #{userId,jdbcType=INTEGER}
</select>
<select id="keepAuthorizedProjectIdsForUser" parameterType="map" resultType="long">
select
gr.resource_id
from
group_roles gr
where
gr.role=#{role,jdbcType=VARCHAR}
and (
gr.group_id is null
or exists (
select
1
from
groups_users gu
where
gu.user_id = #{userId, jdbcType=INTEGER}
and gr.group_id = gu.group_id
)
)
and <foreach collection="componentIds" open="(" close=")" item="element" index="index" separator=" or ">
gr.resource_id=#{element,jdbcType=BIGINT}
</foreach>
union
select
p.id
from
user_roles ur
inner join projects p on
p.id = ur.resource_id
where
ur.role=#{role,jdbcType=VARCHAR}
and ur.user_id=#{userId,jdbcType=INTEGER}
and <foreach collection="componentIds" open="(" close=")" item="element" index="index" separator=" or ">
p.id=#{element,jdbcType=BIGINT}
</foreach>
union
<include refid="sqlSelectPublicProjectsIfRole"/>
</select>
<select id="keepAuthorizedProjectIdsForAnonymous" parameterType="map" resultType="long">
select
gr.resource_id
from
group_roles gr
where
gr.role=#{role,jdbcType=VARCHAR}
and gr.group_id is null
and <foreach collection="componentIds" open="(" close=")" item="element" index="index" separator=" or ">
gr.resource_id=#{element,jdbcType=BIGINT}
</foreach>
union
<include refid="sqlSelectPublicProjectsIfRole"/>
</select>
<sql id="sqlSelectPublicProjectsIfRole">
select
p.id
from
projects p
where
<foreach collection="componentIds" open="(" close=")" item="element" index="index" separator=" or ">
p.id=#{element,jdbcType=BIGINT}
</foreach>
and p.private = ${_false}
and #{role,jdbcType=VARCHAR} in ('user','codeviewer')
</sql>
<select id="keepAuthorizedProjectUuidsForUser" parameterType="map" resultType="String">
select p.uuid
from projects p
inner join group_roles gr on p.id = gr.resource_id
where
gr.role = #{permission,jdbcType=VARCHAR}
and (gr.group_id is null or exists (
select 1 from groups_users gu
where
gu.user_id = #{userId, jdbcType=INTEGER}
and gr.group_id = gu.group_id)
)
and p.uuid in <foreach collection="projectUuids" open="(" close=")" item="projectUuid" index="index" separator=",">#{projectUuid,jdbcType=VARCHAR}</foreach>
union
select p.uuid
from projects p
inner join user_roles ur on p.id = ur.resource_id
where
ur.role=#{permission,jdbcType=VARCHAR}
and ur.user_id=#{userId,jdbcType=INTEGER}
and p.uuid in <foreach collection="projectUuids" open="(" close=")" item="projectUuid" index="index" separator=",">#{projectUuid,jdbcType=VARCHAR}</foreach>
<if test="permission == 'user' or permission == 'codeviewer'">
union
select p.uuid
from projects p
where
p.uuid in <foreach collection="projectUuids" open="(" close=")" item="projectUuid" index="index" separator=",">#{projectUuid,jdbcType=VARCHAR}</foreach>
and p.private = ${_false}
</if>
</select>
<select id="keepAuthorizedProjectUuidsForAnonymous" parameterType="map" resultType="String">
select p.uuid
from projects p
inner join group_roles gr on p.id = gr.resource_id
where
gr.role=#{permission,jdbcType=VARCHAR}
and gr.group_id is null
and p.uuid in <foreach collection="projectUuids" open="(" close=")" item="projectUuid" index="index" separator=",">#{projectUuid,jdbcType=VARCHAR}</foreach>
<if test="permission == 'user' or permission == 'codeviewer'">
union
select p.uuid
from projects p
where
p.uuid in <foreach collection="projectUuids" open="(" close=")" item="projectUuid" index="index" separator=",">#{projectUuid,jdbcType=VARCHAR}</foreach>
and p.private = ${_false}
</if>
</select>
<select id="keepAuthorizedUsersForRoleAndProject" parameterType="map" resultType="int">
select
gu.user_id
from
groups_users gu
inner join group_roles gr on
gr.group_id=gu.group_id
where
gr.resource_id=#{componentId,jdbcType=BIGINT}
and gr.role=#{role,jdbcType=VARCHAR}
and gu.user_id in
<foreach collection="userIds" open="(" close=")" item="id" separator=",">
#{id,jdbcType=BIGINT}
</foreach>
union
select
ur.user_id
from
user_roles ur
where
ur.resource_id=#{componentId,jdbcType=BIGINT}
and ur.role=#{role,jdbcType=VARCHAR}
and ur.user_id IN
<foreach collection="userIds" open="(" close=")" item="id" separator=",">
#{id,jdbcType=BIGINT}
</foreach>
union
select
u.id
from
users u
where
u.id in
<foreach collection="userIds" open="(" close=")" item="id" separator=",">
#{id,jdbcType=BIGINT}
</foreach>
and exists (
select
1
from
projects p
where
p.id =#{componentId,jdbcType=BIGINT}
and p.private = ${_false}
and #{role,jdbcType=VARCHAR} in ('user','codeviewer')
)
</select>
<select id="selectProjectPermissions" parameterType="map" resultType="String">
select ur.role
from user_roles ur
inner join projects p on p.id = ur.resource_id
where
p.uuid = #{projectUuid,jdbcType=VARCHAR} and
p.organization_uuid = ur.organization_uuid and
ur.user_id = #{userId,jdbcType=BIGINT}
union
select gr.role
from group_roles gr
inner join groups_users gu on gr.group_id = gu.group_id
inner join projects p on p.id = gr.resource_id
where
p.uuid = #{projectUuid,jdbcType=VARCHAR} and
p.organization_uuid = gr.organization_uuid and
gu.user_id = #{userId,jdbcType=BIGINT}
union
<include refid="sql_selectProjectPermissionsOfAnonymous"/>
</select>
<select id="selectProjectPermissionsOfAnonymous" parameterType="map" resultType="String">
<include refid="sql_selectProjectPermissionsOfAnonymous"/>
</select>
<sql id="sql_selectProjectPermissionsOfAnonymous">
select
gr.role
from
group_roles gr
inner join projects p on
p.id = gr.resource_id
where
p.uuid = #{projectUuid,jdbcType=VARCHAR}
and p.organization_uuid = gr.organization_uuid
and gr.group_id is null
</sql>
<select id="selectLoginsWithGlobalPermission" parameterType="map" resultType="String">
select u.login
from user_roles ur
inner join users u on u.id=ur.user_id
where
ur.role=#{permission,jdbcType=VARCHAR}
union
select u.login
from group_roles gr
inner join groups_users gu on gr.group_id = gu.group_id
inner join users u on u.id=gu.user_id
where
gr.role = #{permission,jdbcType=VARCHAR} and
gr.group_id is not null
</select>
<select id="keepAuthorizedLoginsOnProject" parameterType="map" resultType="String">
SELECT u.login
FROM users u
INNER JOIN user_roles ur ON ur.user_id = u.id
INNER JOIN projects p ON p.kee = #{projectKey,jdbcType=VARCHAR}
WHERE
ur.organization_uuid = p.organization_uuid
AND ur.resource_id = p.id
AND ur.role = #{permission,jdbcType=VARCHAR}
AND u.login IN <foreach collection="logins" open="(" close=")" item="login" separator=",">#{login}</foreach>
UNION
SELECT u.login
FROM users u
INNER JOIN projects p ON p.kee = #{projectKey,jdbcType=VARCHAR}
INNER JOIN group_roles gr ON gr.organization_uuid = p.organization_uuid
INNER JOIN groups_users gu ON gr.group_id = gu.group_id
WHERE
gu.user_id = u.id
AND gr.role = #{permission,jdbcType=VARCHAR}
AND u.login IN <foreach collection="logins" open="(" close=")" item="login" separator=",">#{login}</foreach>
<if test="permission == 'user' or permission == 'codeviewer'">
UNION
SELECT u.login
FROM users u
INNER JOIN projects p ON p.kee = #{projectKey,jdbcType=VARCHAR}
WHERE
p.private = ${_false}
AND u.login IN <foreach collection="logins" open="(" close=")" item="login" separator=",">#{login}</foreach>
</if>
</select>
</mapper>