aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db/src/main/resources/org/sonar/db/permission/PermissionTemplateMapper.xml
blob: 95567f2e5c215664fea38c22a619d7001086f90e (plain)
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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="org.sonar.db.permission.PermissionTemplateMapper">

  <insert id="insert" parameterType="PermissionTemplate" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO permission_templates (name, kee, description, key_pattern, created_at, updated_at)
    VALUES (#{name}, #{kee}, #{description}, #{keyPattern}, #{createdAt}, #{updatedAt})
  </insert>

  <update id="update" parameterType="PermissionTemplate">
    UPDATE permission_templates
    SET name = #{name}, description = #{description}, key_pattern = #{keyPattern}, updated_at = #{updatedAt}
    WHERE id = #{id}
  </update>

  <delete id="delete" parameterType="long">
    DELETE FROM permission_templates
    WHERE id = #{templateId}
  </delete>

  <delete id="deleteUsersPermissions" parameterType="long">
    DELETE FROM perm_templates_users
    WHERE template_id = #{templateId}
  </delete>

  <delete id="deleteGroupsPermissions" parameterType="long">
    DELETE FROM perm_templates_groups
    WHERE template_id = #{templateId}
  </delete>

  <insert id="insertUserPermission" parameterType="PermissionTemplateUser">
    INSERT INTO perm_templates_users (template_id, user_id, permission_reference, created_at, updated_at)
    VALUES (#{templateId}, #{userId}, #{permission}, #{createdAt}, #{updatedAt})
  </insert>

  <delete id="deleteUserPermission" parameterType="PermissionTemplateUser">
    DELETE FROM perm_templates_users
    WHERE template_id = #{templateId}
    AND user_id = #{userId}
    AND permission_reference = #{permission}
  </delete>

  <insert id="insertGroupPermission" parameterType="PermissionTemplateGroup">
    INSERT INTO perm_templates_groups (template_id, group_id, permission_reference, created_at, updated_at)
    VALUES (#{templateId}, #{groupId}, #{permission}, #{createdAt}, #{updatedAt})
  </insert>

  <delete id="deleteGroupPermission" parameterType="PermissionTemplateGroup">
    DELETE FROM perm_templates_groups
    WHERE template_id = #{templateId}
    AND permission_reference = #{permission}
    AND
    <choose>
      <when test="groupId != null">
        group_id = #{groupId}
      </when>
      <otherwise>
        group_id IS NULL
      </otherwise>
    </choose>
  </delete>

  <delete id="deleteByGroupId" parameterType="long">
    DELETE FROM perm_templates_groups
    WHERE group_id = #{groupId}
  </delete>

  <select id="selectUsers" parameterType="map" resultType="UserWithPermission">
    SELECT u.login as login, u.name as name, ptu.permission_reference as permission
    FROM users u
    LEFT JOIN perm_templates_users ptu ON ptu.user_id=u.id
    AND ptu.permission_reference=#{query.permission}
    AND ptu.template_id=#{templateId}
    <where>
      u.active = ${_true}
      <choose>
        <when test="query.membership() == 'IN'">
          AND ptu.permission_reference IS NOT NULL
        </when>
        <when test="query.membership() == 'OUT'">
          AND ptu.permission_reference IS NULL
        </when>
      </choose>
      <if test="query.search() != null">
        AND (UPPER(u.name) LIKE #{query.searchSql} ESCAPE '/')
      </if>
    </where>
    ORDER BY u.name
  </select>

  <select id="selectGroups" parameterType="map" resultType="GroupWithPermission">
    SELECT name, description, permission FROM
    (SELECT g.name as name, g.description as description, ptg.permission_reference as permission
    FROM groups g
    LEFT JOIN perm_templates_groups ptg ON ptg.group_id=g.id
    AND ptg.permission_reference=#{query.permission}
    AND ptg.template_id=#{templateId}
    UNION
    -- Add Anyone group permission
    SELECT #{anyoneGroup} as name, NULL as description, ptg.permission_reference as permission
    FROM perm_templates_groups ptg
    <where>
      AND ptg.permission_reference=#{query.permission}
      AND ptg.template_id=#{templateId}
      AND ptg.group_id IS NULL
    </where>
    ) groups
    <where>
      <if test="query.search() != null">
        AND (UPPER(groups.name) LIKE #{query.searchSql} ESCAPE '/')
      </if>
    </where>
    ORDER BY groups.name
  </select>

  <select id="selectByKey" parameterType="String" resultType="PermissionTemplate">
    SELECT id, name, kee, description, key_pattern AS keyPattern, created_at AS createdAt, updated_at AS updatedAt
    FROM permission_templates
    WHERE kee = #{kee}
  </select>

  <select id="selectAllPermissionTemplates" resultType="PermissionTemplate">
    SELECT id, name, kee, description, key_pattern AS keyPattern, created_at AS createdAt, updated_at AS updatedAt
    FROM permission_templates
  </select>

  <select id="selectTemplateUsersPermissions" parameterType="String" resultMap="fullPermissionsTemplateResult">
    SELECT pt.id AS template_id,
    pt.name AS template_name,
    pt.description AS template_description,
    pt.key_pattern AS template_key_pattern,
    pt.created_at AS template_created_at,
    pt.updated_at AS template_updated_at,
    ptu.id AS permission_template_user_id,
    ptu.permission_reference AS user_permission,
    ptu.user_id AS user_id,
    u.name AS user_name,
    u.login AS user_login
    FROM permission_templates pt
    INNER JOIN perm_templates_users ptu ON ptu.template_id = pt.id
    INNER JOIN users u ON u.id = ptu.user_id AND u.active = ${_true}
    WHERE pt.kee = #{templateKey}
  </select>

  <select id="selectTemplateGroupsPermissions" parameterType="String" resultMap="fullPermissionsTemplateResult">
    SELECT pt.id AS template_id,
    pt.name AS template_name,
    pt.description AS template_description,
    pt.key_pattern AS template_key_pattern,
    pt.created_at AS template_created_at,
    pt.updated_at AS template_updated_at,
    ptg.id AS permission_template_group_id,
    ptg.permission_reference AS group_permission,
    ptg.group_id AS group_id,
    g.name AS group_name
    FROM permission_templates pt
    INNER JOIN perm_templates_groups ptg ON ptg.template_id = pt.id
    LEFT OUTER JOIN groups g ON g.id = ptg.group_id
    WHERE pt.kee = #{templateKey}
    AND (g.name IS NOT NULL OR ptg.group_id IS NULL)
  </select>

  <resultMap id="fullPermissionsTemplateResult" type="PermissionTemplate">
    <id property="id" column="template_id"/>
    <result property="name" column="template_name"/>
    <result property="description" column="template_description"/>
    <result property="keyPattern" column="template_key_pattern"/>
    <result property="createdAt" column="template_created_at"/>
    <result property="updatedAt" column="template_updated_at"/>
    <collection property="usersPermissions" ofType="PermissionTemplateUser">
      <id property="id" column="permission_template_user_id"/>
      <result property="userId" column="user_id"/>
      <result property="permission" column="user_permission"/>
      <result property="userName" column="user_name"/>
      <result property="userLogin" column="user_login"/>
    </collection>
    <collection property="groupsPermissions" ofType="PermissionTemplateGroup">
      <id property="id" column="permission_template_group_id"/>
      <result property="groupId" column="group_id"/>
      <result property="permission" column="group_permission"/>
      <result property="groupName" column="group_name"/>
    </collection>
  </resultMap>

</mapper>