You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AuthorizationMapper.xml 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd">
  3. <mapper namespace="org.sonar.db.permission.AuthorizationMapper">
  4. <select id="selectGlobalPermissions" parameterType="map" resultType="string">
  5. select gr.role
  6. from group_roles gr
  7. inner join groups_users gu on gr.group_uuid=gu.group_uuid
  8. where
  9. gr.component_uuid is null and
  10. gu.user_uuid=#{userUuid, jdbcType=VARCHAR}
  11. union
  12. select gr.role
  13. from group_roles gr
  14. where
  15. gr.group_uuid is null and
  16. gr.component_uuid is null
  17. union
  18. select ur.role
  19. from user_roles ur
  20. where
  21. ur.user_uuid=#{userUuid, jdbcType=VARCHAR}
  22. and ur.component_uuid is null
  23. </select>
  24. <select id="selectGlobalPermissionsOfAnonymous" parameterType="map" resultType="string">
  25. select gr.role
  26. from group_roles gr
  27. where
  28. gr.component_uuid is null and
  29. gr.group_uuid is null
  30. </select>
  31. <select id="countUsersWithGlobalPermissionExcludingGroup" parameterType="map" resultType="int">
  32. select count(1) from
  33. (
  34. select gu.user_uuid
  35. from groups_users gu
  36. inner join group_roles gr on gr.group_uuid = gu.group_uuid
  37. where
  38. gr.role = #{permission, jdbcType=VARCHAR} and
  39. gr.component_uuid is null and
  40. gr.group_uuid is not null and
  41. gr.group_uuid != #{excludedGroupUuid, jdbcType=VARCHAR}
  42. union
  43. select ur.user_uuid
  44. from user_roles ur
  45. where
  46. ur.component_uuid is null and
  47. ur.role = #{permission, jdbcType=VARCHAR}
  48. ) remaining
  49. </select>
  50. <select id="countUsersWithGlobalPermissionExcludingUser" parameterType="map" resultType="int">
  51. select count(1) from
  52. (
  53. select gu.user_uuid
  54. from groups_users gu
  55. inner join group_roles gr on gr.group_uuid = gu.group_uuid
  56. where
  57. gr.role = #{permission, jdbcType=VARCHAR} and
  58. gr.component_uuid is null and
  59. gr.group_uuid is not null and
  60. gu.user_uuid != #{excludedUserUuid, jdbcType=VARCHAR}
  61. union
  62. select ur.user_uuid
  63. from user_roles ur
  64. where
  65. ur.component_uuid is null and
  66. ur.role = #{permission, jdbcType=VARCHAR} and
  67. ur.user_uuid != #{excludedUserUuid, jdbcType=VARCHAR}
  68. ) remaining
  69. </select>
  70. <select id="selectUserUuidsWithGlobalPermission" parameterType="map" resultType="String">
  71. select gu.user_uuid
  72. from groups_users gu
  73. inner join group_roles gr on gr.group_uuid = gu.group_uuid
  74. where
  75. gr.role = #{permission, jdbcType=VARCHAR} and
  76. gr.component_uuid is null and
  77. gr.group_uuid is not null
  78. union
  79. select ur.user_uuid
  80. from user_roles ur
  81. where
  82. ur.component_uuid is null and
  83. ur.role = #{permission, jdbcType=VARCHAR}
  84. </select>
  85. <select id="countUsersWithGlobalPermissionExcludingGroupMember" parameterType="map" resultType="int">
  86. select count(1) from
  87. (
  88. select gu.user_uuid
  89. from groups_users gu
  90. inner join group_roles gr on gr.group_uuid = gu.group_uuid
  91. where
  92. gr.role = #{permission, jdbcType=VARCHAR} and
  93. gr.component_uuid is null and
  94. gr.group_uuid is not null and
  95. (gu.group_uuid != #{groupUuid, jdbcType=VARCHAR} or gu.user_uuid != #{userUuid, jdbcType=VARCHAR})
  96. union
  97. select ur.user_uuid
  98. from user_roles ur
  99. where
  100. ur.component_uuid is null and
  101. ur.role = #{permission, jdbcType=VARCHAR}
  102. ) remaining
  103. </select>
  104. <select id="countUsersWithGlobalPermissionExcludingUserPermission" parameterType="map" resultType="int">
  105. select count(1) from
  106. (
  107. select gu.user_uuid
  108. from groups_users gu
  109. inner join group_roles gr on gr.group_uuid = gu.group_uuid
  110. where
  111. gr.role = #{permission, jdbcType=VARCHAR} and
  112. gr.component_uuid is null and
  113. gr.group_uuid is not null
  114. union
  115. select ur.user_uuid
  116. from user_roles ur
  117. where
  118. ur.component_uuid is null and
  119. ur.role = #{permission, jdbcType=VARCHAR} and
  120. ur.user_uuid != #{userUuid, jdbcType=VARCHAR}
  121. ) remaining
  122. </select>
  123. <select id="keepAuthorizedProjectUuidsForUser" parameterType="map" resultType="String">
  124. select
  125. gr.component_uuid
  126. from
  127. group_roles gr
  128. where
  129. gr.role=#{role, jdbcType=VARCHAR}
  130. and (
  131. gr.group_uuid is null
  132. or exists (
  133. select
  134. 1
  135. from
  136. groups_users gu
  137. where
  138. gu.user_uuid = #{userUuid, jdbcType=VARCHAR}
  139. and gr.group_uuid = gu.group_uuid
  140. )
  141. )
  142. and <foreach collection="projectUuids" open="(" close=")" item="element" index="index" separator=" or ">
  143. gr.component_uuid=#{element, jdbcType=VARCHAR}
  144. </foreach>
  145. union
  146. select
  147. p.uuid
  148. from
  149. user_roles ur
  150. inner join components p on
  151. p.uuid = ur.component_uuid
  152. where
  153. ur.role=#{role, jdbcType=VARCHAR}
  154. and ur.user_uuid=#{userUuid, jdbcType=INTEGER}
  155. and <foreach collection="projectUuids" open="(" close=")" item="element" index="index" separator=" or ">
  156. p.uuid=#{element, jdbcType=VARCHAR}
  157. </foreach>
  158. union
  159. <include refid="sqlSelectPublicProjectsIfRole"/>
  160. </select>
  161. <sql id="sqlSelectPublicProjectsIfRole">
  162. select
  163. p.uuid
  164. from
  165. components p
  166. where
  167. <foreach collection="projectUuids" open="(" close=")" item="element" index="index" separator=" or ">
  168. p.uuid=#{element ,jdbcType=VARCHAR}
  169. </foreach>
  170. and p.private = ${_false}
  171. and #{role, jdbcType=VARCHAR} in ('user','codeviewer')
  172. </sql>
  173. <select id="keepAuthorizedProjectUuidsForAnonymous" parameterType="map" resultType="String">
  174. select
  175. gr.component_uuid
  176. from
  177. group_roles gr
  178. where
  179. gr.role=#{role, jdbcType=VARCHAR}
  180. and gr.group_uuid is null
  181. and <foreach collection="projectUuids" open="(" close=")" item="element" index="index" separator=" or ">
  182. gr.component_uuid=#{element, jdbcType=VARCHAR}
  183. </foreach>
  184. union
  185. <include refid="sqlSelectPublicProjectsIfRole"/>
  186. </select>
  187. <select id="keepAuthorizedUsersForRoleAndProject" parameterType="map" resultType="String">
  188. select
  189. gu.user_uuid
  190. from
  191. groups_users gu
  192. inner join group_roles gr on
  193. gr.group_uuid=gu.group_uuid
  194. where
  195. gr.component_uuid=#{componentUuid, jdbcType=VARCHAR}
  196. and gr.role=#{role, jdbcType=VARCHAR}
  197. and gu.user_uuid in
  198. <foreach collection="userUuids" open="(" close=")" item="uuid" separator=",">
  199. #{uuid, jdbcType=VARCHAR}
  200. </foreach>
  201. union
  202. select
  203. ur.user_uuid
  204. from
  205. user_roles ur
  206. where
  207. ur.component_uuid=#{componentUuid, jdbcType=VARCHAR}
  208. and ur.role=#{role, jdbcType=VARCHAR}
  209. and ur.user_uuid IN
  210. <foreach collection="userUuids" open="(" close=")" item="uuid" separator=",">
  211. #{uuid, jdbcType=VARCHAR}
  212. </foreach>
  213. union
  214. select
  215. u.uuid
  216. from
  217. users u
  218. where
  219. u.uuid in
  220. <foreach collection="userUuids" open="(" close=")" item="uuid" separator=",">
  221. #{uuid, jdbcType=VARCHAR}
  222. </foreach>
  223. and exists (
  224. select
  225. 1
  226. from
  227. components p
  228. where
  229. p.uuid =#{componentUuid, jdbcType=VARCHAR}
  230. and p.private = ${_false}
  231. and #{role, jdbcType=VARCHAR} in ('user','codeviewer')
  232. )
  233. </select>
  234. <select id="selectProjectPermissions" parameterType="map" resultType="String">
  235. select ur.role
  236. from user_roles ur
  237. inner join components p on p.uuid = ur.component_uuid
  238. where
  239. p.uuid = #{projectUuid, jdbcType=VARCHAR} and
  240. ur.user_uuid = #{userUuid, jdbcType=VARCHAR}
  241. union
  242. select gr.role
  243. from group_roles gr
  244. inner join groups_users gu on gr.group_uuid = gu.group_uuid
  245. inner join components p on p.uuid = gr.component_uuid
  246. where
  247. p.uuid = #{projectUuid, jdbcType=VARCHAR} and
  248. gu.user_uuid = #{userUuid, jdbcType=VARCHAR}
  249. union
  250. <include refid="sql_selectProjectPermissionsOfAnonymous"/>
  251. </select>
  252. <select id="selectProjectPermissionsOfAnonymous" parameterType="map" resultType="String">
  253. <include refid="sql_selectProjectPermissionsOfAnonymous"/>
  254. </select>
  255. <sql id="sql_selectProjectPermissionsOfAnonymous">
  256. select
  257. gr.role
  258. from
  259. group_roles gr
  260. inner join components p on
  261. p.uuid = gr.component_uuid
  262. where
  263. p.uuid = #{projectUuid, jdbcType=VARCHAR}
  264. and gr.group_uuid is null
  265. </sql>
  266. <select id="selectEmailSubscribersWithGlobalPermission" parameterType="map" resultType="org.sonar.db.EmailSubscriberDto">
  267. select
  268. u.login as "login",
  269. ${_true} as "global",
  270. u.email as "email"
  271. from
  272. users u
  273. inner join user_roles ur on
  274. ur.user_uuid = u.uuid
  275. and ur.role=#{permission, jdbcType=VARCHAR}
  276. and ur.component_uuid is null
  277. where
  278. u.email is not null
  279. union
  280. select
  281. u.login as "login",
  282. ${_true} as "global",
  283. u.email as "email"
  284. from users u
  285. inner join groups_users gu on
  286. u.uuid=gu.user_uuid
  287. inner join group_roles gr on
  288. gr.group_uuid = gu.group_uuid
  289. and gr.role = #{permission, jdbcType=VARCHAR}
  290. and gr.component_uuid is null
  291. where
  292. u.email is not null
  293. </select>
  294. <select id="keepAuthorizedLoginsOnProject" parameterType="map" resultType="String">
  295. select u.login
  296. from users u
  297. where
  298. u.login in <foreach collection="logins" open="(" close=")" item="login" separator=",">#{login, jdbcType=VARCHAR}</foreach>
  299. and (
  300. exists (
  301. select 1
  302. from user_roles ur
  303. inner join components p on p.uuid = ur.component_uuid
  304. where
  305. p.kee = #{projectKey, jdbcType=VARCHAR}
  306. and ur.role = #{permission, jdbcType=VARCHAR}
  307. and ur.user_uuid = u.uuid
  308. ) or exists (
  309. select 1
  310. from components p
  311. inner join group_roles gr on gr.component_uuid = p.uuid
  312. inner join groups_users gu on gu.group_uuid = gr.group_uuid
  313. where
  314. p.kee = #{projectKey, jdbcType=VARCHAR}
  315. and gu.user_uuid = u.uuid
  316. and gr.role = #{permission, jdbcType=VARCHAR}
  317. )
  318. <if test="permission == 'user' or permission == 'codeviewer'">
  319. or exists (
  320. select 1
  321. from components p
  322. where
  323. p.kee = #{projectKey, jdbcType=VARCHAR}
  324. and p.private = ${_false}
  325. )
  326. </if>
  327. )
  328. </select>
  329. </mapper>