-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
UserPermissionMapper.xml
179 lines (161 loc) · 5.75 KB
/
UserPermissionMapper.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
<?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.UserPermissionMapper">
<select id="selectUserPermissionsByQueryAndUserIds" parameterType="map" resultType="org.sonar.db.permission.UserPermissionDto">
select
u.id as userId,
ur.organization_uuid as organizationUuid,
ur.resource_id as componentId,
ur.role as permission
<include refid="sqlQueryJoins" />
<where>
u.id in <foreach collection="userIds" open="(" close=")" item="userId" separator=",">#{userId,jdbcType=INTEGER}</foreach>
<include refid="sqlQueryFilters" />
</where>
</select>
<select id="selectUserIdsByQuery" parameterType="map" resultType="int">
select
u.id, lower(u.name) as lowerName
<include refid="sqlQueryJoins" />
<where>
<include refid="sqlQueryFilters" />
</where>
group by u.id, lowerName
order by case when (count(ur.role) > 0) then 1 else 2 end asc, lowerName asc
</select>
<select id="countUsersByQuery" parameterType="map" resultType="int">
select count(distinct(u.id))
<include refid="sqlQueryJoins" />
<where>
<include refid="sqlQueryFilters" />
</where>
</select>
<sql id="sqlQueryJoins">
from users u
left join user_roles ur on ur.user_id = u.id
left join projects p on ur.resource_id = p.id
inner join organization_members om on u.id=om.user_id and om.organization_uuid=#{query.organizationUuid,jdbcType=VARCHAR}
</sql>
<sql id="sqlQueryFilters">
and u.active = ${_true}
<if test="query.searchQueryToSql != null">
and (
lower(u.name) like #{query.searchQueryToSqlLowercase,jdbcType=VARCHAR} ESCAPE '/'
or u.email like #{query.searchQueryToSql,jdbcType=VARCHAR} ESCAPE '/'
or u.login like #{query.searchQueryToSql,jdbcType=VARCHAR} ESCAPE '/')
</if>
<!-- filter rows with user permissions -->
<if test="query.withAtLeastOnePermission()">
and ur.organization_uuid = om.organization_uuid
and ur.role is not null
<if test="query.componentUuid==null">
and ur.resource_id is null
</if>
<if test="query.componentUuid!=null">
and p.uuid = #{query.componentUuid,jdbcType=VARCHAR}
</if>
<if test="query.permission!=null">
and ur.role = #{query.permission,jdbcType=VARCHAR}
</if>
</if>
</sql>
<select id="selectGlobalPermissionsOfUser" parameterType="map" resultType="string">
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="selectProjectPermissionsOfUser" parameterType="map" resultType="string">
select ur.role
from user_roles ur
where
ur.user_id = #{userId,jdbcType=INTEGER} and
ur.resource_id = #{projectId,jdbcType=BIGINT}
</select>
<select id="countUsersByProjectPermission" resultType="org.sonar.db.permission.CountPerProjectPermission">
select ur.resource_id as componentId, ur.role as permission, count(u.login) as count
from users u
inner join user_roles ur on ur.user_id = u.id
inner join projects p on p.id = ur.resource_id
where u.active = ${_true}
and p.id in <foreach collection="projectIds" open="(" close=")" item="projectId" separator=",">#{projectId}</foreach>
group by ur.resource_id, ur.role
</select>
<select id="selectUserIdsWithPermissionOnProjectBut" resultType="Integer">
select
distinct ur1.user_id
from
user_roles ur1
where
ur1.resource_id = #{projectId,jdbcType=BIGINT}
and role <> #{permission,jdbcType=VARCHAR}
and not exists (
select
1
from
user_roles ur2
where
ur2.resource_id = ur1.resource_id
and ur2.user_id = ur1.user_id
and role = #{permission,jdbcType=VARCHAR}
)
</select>
<insert id="insert" parameterType="org.sonar.db.permission.UserPermissionDto" useGeneratedKeys="false">
insert into user_roles (
organization_uuid,
user_id,
resource_id,
role
) values (
#{organizationUuid,jdbcType=VARCHAR},
#{userId,jdbcType=INTEGER},
#{componentId,jdbcType=BIGINT},
#{permission,jdbcType=VARCHAR}
)
</insert>
<delete id="deleteGlobalPermission" parameterType="map">
delete from user_roles
where
role = #{permission,jdbcType=VARCHAR} and
user_id = #{userId,jdbcType=INTEGER} and
organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
resource_id is null
</delete>
<delete id="deleteProjectPermission" parameterType="map">
delete from user_roles
where
role = #{permission,jdbcType=VARCHAR} and
user_id = #{userId,jdbcType=INTEGER} and
resource_id = #{projectId,jdbcType=BIGINT}
</delete>
<delete id="deleteProjectPermissions" parameterType="map">
delete from user_roles
where
resource_id = #{projectId,jdbcType=BIGINT}
</delete>
<delete id="deleteProjectPermissionOfAnyUser" parameterType="map">
delete from
user_roles
where
resource_id = #{projectId,jdbcType=BIGINT}
and role = #{permission,jdbcType=VARCHAR}
</delete>
<delete id="deleteByOrganization" parameterType="String">
delete from
user_roles
where
organization_uuid = #{organizationUuid,jdbcType=VARCHAR}
</delete>
<delete id="deleteOrganizationMemberPermissions" parameterType="map">
delete from
user_roles
where
organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
user_id = #{userId,jdbcType=INTEGER}
</delete>
<delete id="deleteByUserId" parameterType="int">
DELETE FROM user_roles WHERE user_id=#{userId,jdbcType=INTEGER}
</delete>
</mapper>