All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.db.permission.PermissionTemplateMapper.xml Maven / Gradle / Ivy

There is a newer version: 6.3.1
Show newest version
<?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="deleteUserPermissions" parameterType="long">
    DELETE FROM perm_templates_users
    WHERE template_id = #{templateId}
  </delete>

  <delete id="deleteGroupPermissions" 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, u.email, ptu.permission_reference as permission
    <include refid="usersSelection"/>
    ORDER BY u.name
  </select>

  <select id="countUsers" parameterType="map" resultType="int">
    SELECT count(u.login)
    <include refid="usersSelection"/>
  </select>

  <sql id="usersSelection">
    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>
  </sql>

  <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}

    <if test="!query.permission().equals(projectAdminPermission)">
    UNION
    -- Add Anyone group permission
    SELECT
      #{anyoneGroup} as name,
      NULL as description,
      (select ptg.permission_reference
      FROM perm_templates_groups ptg
      <where>
        AND ptg.permission_reference=#{query.permission}
        AND ptg.template_id=#{templateId}
        AND ptg.group_id IS NULL
      </where>) as permission
    FROM groups g
    </if>

    ) groups
    <where>
      <if test="query.search() != null">
        AND (UPPER(groups.name) LIKE #{query.searchSql} ESCAPE '/')
      </if>
      <choose>
        <when test="query.membership() == 'IN'">
          AND groups.permission IS NOT NULL
        </when>
        <when test="query.membership() == 'OUT'">
          AND groups.permission IS NULL
        </when>
      </choose>
    </where>
    ORDER BY groups.name
  </select>

  <select id="countGroups" parameterType="map" resultType="int">
    SELECT count(1) 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,
    (select ptg.permission_reference
    FROM perm_templates_groups ptg
    <where>
      AND ptg.permission_reference=#{query.permission}
      AND ptg.template_id=#{templateId}
      AND ptg.group_id IS NULL
    </where>) as permission
    FROM groups g
    ) groups
    <where>
      <if test="groupName!=null">
        AND (UPPER(groups.name) LIKE #{groupName} ESCAPE '/')
      </if>
      <if test="query.search() != null">
        AND (UPPER(groups.name) LIKE #{query.searchSql} ESCAPE '/')
      </if>
      <choose>
        <when test="query.membership() == 'IN'">
          AND groups.permission IS NOT NULL
        </when>
        <when test="query.membership() == 'OUT'">
          AND groups.permission IS NULL
        </when>
      </choose>
    </where>
  </select>

  <sql id="templateColumns">
    id, name, kee, description, key_pattern AS keyPattern, created_at AS createdAt, updated_at AS updatedAt
  </sql>

  <select id="selectByUuid" parameterType="String" resultType="PermissionTemplate">
    SELECT
    <include refid="templateColumns"/>
    FROM permission_templates
    WHERE kee=#{uuid}
  </select>

  <select id="selectAll" parameterType="map" resultType="PermissionTemplate">
    SELECT
    <include refid="templateColumns"/>
    FROM permission_templates
    <where>
      <if test="nameMatch!=null">
        AND (UPPER(name) LIKE #{nameMatch} ESCAPE '/')
      </if>
    </where>
    ORDER BY UPPER(name), name
  </select>

  <select id="countAll" parameterType="String" resultType="int">
    SELECT count(1)
    FROM permission_templates
    <where>
      <if test="nameMatch!=null">
        AND (UPPER(name) LIKE #{nameMatch} ESCAPE '/')
      </if>
    </where>
  </select>

  <select id="selectByName" parameterType="String" resultType="PermissionTemplate">
    SELECT
    <include refid="templateColumns"/>
    FROM permission_templates
    WHERE UPPER(name)=#{templateName}
  </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>

  <select id="usersCountByTemplateIdAndPermission" parameterType="map"
          resultType="org.sonar.db.permission.CountByTemplateAndPermissionDto">
    SELECT ptu.template_id as templateId, ptu.permission_reference as permission, count(u.login) as count
    FROM users u
    INNER JOIN perm_templates_users ptu ON ptu.user_id=u.id
    AND ptu.template_id in
    <foreach collection="templateIds" open="(" close=")" item="id" separator=",">
      #{id}
    </foreach>
    <where>
      AND u.active = ${_true}
    </where>
    GROUP BY ptu.template_id, ptu.permission_reference
  </select>

  <select id="groupsCountByTemplateIdAndPermission" parameterType="map"
          resultType="org.sonar.db.permission.CountByTemplateAndPermissionDto">
    SELECT count(1) as count, permission, templateId
    FROM
    (SELECT g.name as name, ptg.permission_reference as permission, ptg.template_id as templateId
    FROM groups g
    INNER JOIN perm_templates_groups ptg ON ptg.group_id=g.id
    UNION
    -- Add Anyone group permission
    SELECT #{anyoneGroup} as name, ptg.permission_reference as permission, ptg.template_id as templateId
    FROM perm_templates_groups ptg
    <where>
      AND ptg.group_id IS NULL
    </where>
    ) groups
    <where>
      AND groups.templateId in
      <foreach collection="templateIds" open="(" close=")" item="id" separator=",">
        #{id}
      </foreach>
    </where>
    GROUP BY groups.permission, groups.templateId
  </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>




© 2015 - 2025 Weber Informatics LLC | Privacy Policy