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

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

<?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.template.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="selectUserLoginsByQueryAndTemplate" parameterType="map" resultType="string">
    SELECT u.login FROM
      (SELECT DISTINCT u.login AS login, u.name AS name
      <include refid="userLoginsByQueryAndTemplate"/>
      ) u
    ORDER BY u.name
  </select>

  <select id="countUserLoginsByQueryAndTemplate" parameterType="map" resultType="int">
    SELECT count(1)
      FROM (
      SELECT DISTINCT u.login AS login, u.name AS name
      <include refid="userLoginsByQueryAndTemplate"/>) u
  </select>

  <sql id="userLoginsByQueryAndTemplate">
    FROM users u
    LEFT JOIN perm_templates_users ptu ON ptu.user_id=u.id AND ptu.template_id=#{templateId}
    <where>
      u.active = ${_true}
      <if test="query.getSearchQueryToSql() != null">
        AND lower(u.name) like #{query.searchQueryToSql} ESCAPE '/'
      </if>
      <if test="query.withPermissionOnly()">
        and ptu.permission_reference is not null
        <if test="query.getPermission()!=null">
          and ptu.permission_reference=#{query.permission}
        </if>
      </if>
    </where>
  </sql>

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

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

  <select id="selectGroupNamesByQueryAndTemplate" parameterType="map" resultType="string">
    SELECT DISTINCT groups.name, LOWER(groups.name), groups.group_id
    <include refid="groupNamesByQueryAndTemplate" />
    ORDER BY LOWER(groups.name), groups.name, groups.group_id
  </select>

  <select id="countGroupNamesByQueryAndTemplate" parameterType="map" resultType="int">
    SELECT COUNT(1)
    FROM (
      SELECT DISTINCT group_id
      <include refid="groupNamesByQueryAndTemplate" />) g
  </select>

  <sql id="groupNamesByQueryAndTemplate">
    FROM
    (SELECT g.id AS group_id, g.name AS name, ptg.permission_reference AS permission, ptg.template_id AS templateId
    FROM groups g
    LEFT JOIN perm_templates_groups ptg ON ptg.group_id=g.id
    UNION ALL
    SELECT 0 AS group_id, 'Anyone' AS name, ptg.permission_reference AS permission, ptg.template_id AS templateId
    FROM perm_templates_groups ptg
    <where>
      <if test="query.withPermissionOnly()">
        AND ptg.group_id IS NULL
      </if>
    </where>
    ) groups
    <where>
      <if test="query.searchQueryToSql != null">
        AND LOWER(groups.name) LIKE #{query.searchQueryToSql} ESCAPE '/'
      </if>
      <if test="query.withPermissionOnly()">
        AND groups.permission IS NOT NULL
        AND groups.templateId=#{templateId}
        <if test="query.permission != null">
          AND groups.permission=#{query.permission}
        </if>
      </if>
    </where>
  </sql>

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

  <sql id="permissionTemplateUserColumns">
    ptu.id,
    ptu.template_id as templateId,
    ptu.permission_reference AS permission,
    ptu.user_id AS userId,
    u.name AS userName,
    u.login AS userLogin,
    ptu.created_at AS createdAt,
    ptu.updated_at AS updatedAt
  </sql>

  <select id="selectUserPermissionsByTemplateIdAndUserLogins" parameterType="Long" resultType="PermissionTemplateUser">
    SELECT
    <include refid="permissionTemplateUserColumns"/>
    FROM perm_templates_users ptu
    INNER JOIN users u ON u.id = ptu.user_id AND u.active = ${_true}
    <where>
      AND ptu.template_id = #{templateId}
      <if test="!logins.isEmpty()">
        AND u.login IN <foreach collection="logins" open="(" close=")" item="login" separator=",">
          #{login}
        </foreach>
      </if>
    </where>
  </select>

  <select id="selectGroupPermissionsByTemplateIdAndGroupNames" parameterType="Long" resultType="PermissionTemplateGroup">
    SELECT
      sub.id,
      sub.templateId,
      sub.permission,
      sub.groupId,
      sub.groupName,
      sub.createdAt,
      sub.updatedAt
    FROM  (
      SELECT
        ptg.id,
        ptg.template_id as templateId,
        ptg.permission_reference AS permission,
        ptg.group_id AS groupId,
        g.name AS groupName,
        ptg.created_at as createdAt,
        ptg.updated_at as updatedAt
        FROM perm_templates_groups ptg
      INNER JOIN groups g ON g.id=ptg.group_id
      UNION ALL
        SELECT
          ptg.id,
          ptg.template_id as templateId,
          ptg.permission_reference AS permission,
          0 AS groupId,
          'Anyone' AS groupName,
          ptg.created_at as createdAt,
          ptg.updated_at as updatedAt
        FROM perm_templates_groups ptg
        WHERE ptg.group_id IS NULL
    ) sub
    <where>
      sub.templateId=#{templateId}
      <if test="!groups.isEmpty()">
        AND sub.groupName IN <foreach collection="groups" open="(" close=")" item="group" separator=",">
        #{group}
        </foreach>
      </if>
    </where>
  </select>

  <select id="selectPotentialPermissionsByUserIdAndTemplateId" parameterType="map" resultType="String">
    <if test="userId!=null">
      -- from template users
      select ptu.permission_reference as permission_key
      from perm_templates_users ptu
      <where>
        and ptu.user_id=#{userId}
        and ptu.template_id=#{templateId}
      </where>
      UNION
      -- from template groups except anyone group
      select ptg.permission_reference as permission_key
      from perm_templates_groups ptg
      inner join groups_users gu on ptg.group_id = gu.group_id
      <where>
        and gu.user_id=#{userId}
        and ptg.template_id=#{templateId}
      </where>
      UNION
      -- from template characteristics
      select ptc.permission_key as permission_key
      from perm_tpl_characteristics ptc
      <where>
        and with_project_creator = ${_true}
        and ptc.template_id = #{templateId}
      </where>
      UNION
    </if>
    -- from anyone group
    select ptg.permission_reference as permission_key
    from perm_templates_groups ptg
    where ptg.template_id=#{templateId}
    and ptg.group_id IS NULL
  </select>

  <select id="usersCountByTemplateIdAndPermission" parameterType="map"
          resultType="org.sonar.db.permission.template.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.template.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>
</mapper>




© 2015 - 2025 Weber Informatics LLC | Privacy Policy