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

org.sonar.db.permission.PermissionMapper.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.PermissionMapper">

  <select id="selectUsers" parameterType="map" resultType="UserWithPermission">
    SELECT u.login as login, u.name as name, u.email as email, user_role.role 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 user_roles user_role ON user_role.user_id=u.id
    AND user_role.role=#{query.permission}
    <if test="componentId != null">
      AND user_role.resource_id=#{componentId}
    </if>
    <if test="componentId == null">
      AND user_role.resource_id IS NULL
    </if>
    <where>
      u.active = ${_true}
      <choose>
        <when test="query.membership() == 'IN'">
          AND user_role.role IS NOT NULL
        </when>
        <when test="query.membership() == 'OUT'">
          AND user_role.role IS NULL
        </when>
      </choose>
      <if test="query.search() != null">
        AND (UPPER(u.name) LIKE #{query.searchSql} ESCAPE '/')
      </if>
    </where>
  </sql>

  <sql id="userColumns">
    <!-- u.name and u.id are present to order by with select distinct -->
    u.login as login, u.name as name, u.id
  </sql>

  <select id="selectLoginsByPermissionQuery" parameterType="map" resultType="string">
    select u.login
    from (
      select distinct <include refid="userColumns" />
      <include refid="usersByQuery"/>
    ) u
    order by lower(u.name), u.name, u.id
  </select>

  <select id="countUsersByPermissionQuery" parameterType="map" resultType="int">
    select count(1)
    from (
      select distinct <include refid="userColumns" />
      <include refid="usersByQuery"/>) u
  </select>

  <sql id="usersByQuery">
    from users u
      left join user_roles ur ON ur.user_id=u.id
        left join projects p on ur.resource_id = p.id
    <where>
      and u.active = ${_true}
      <if test="query.searchQueryToSql != null">
        and lower(u.name) like #{query.searchQueryToSql} ESCAPE '/'
      </if>
      <!-- filter rows with user permissions -->
      <if test="query.withPermissionOnly()">
        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}
        </if>
        <if test="query.permission!=null">
          and ur.role=#{query.permission}
        </if>
      </if>
    </where>
  </sql>

  <select id="selectUserPermissionsByLogins" parameterType="map" resultType="UserRole">
    SELECT ur.user_id AS userId, ur.resource_id AS componentId, ur.role AS permission
    FROM user_roles ur
    INNER JOIN users u ON u.id = ur.user_id AND u.active = ${_true}
    <where>
      u.login IN
      <foreach collection="logins" open="(" close=")" item="login" separator=",">
        #{login}
      </foreach>
      <if test="projectId!=null">
        AND ur.resource_id=#{projectId}
      </if>
      <if test="projectId==null">
        AND ur.resource_id IS NULL
      </if>
    </where>
  </select>

  <select id="usersCountByProjectIdAndPermission" parameterType="map"
          resultType="org.sonar.db.permission.CountByProjectAndPermissionDto">
    SELECT user_role.resource_id as componentId, user_role.role as permission, count(u.login) as count
    FROM users u
    INNER JOIN user_roles user_role ON user_role.user_id=u.id
    AND user_role.resource_id in
    <foreach collection="componentIds" open="(" close=")" item="id" separator=",">
      #{id}
    </foreach>
    <where>
      u.active = ${_true}
    </where>
    GROUP BY user_role.resource_id, user_role.role
  </select>

  <select id="groupsCountByProjectIdAndPermission" parameterType="map"
          resultType="org.sonar.db.permission.CountByProjectAndPermissionDto">
    SELECT count(1) as count, permission, componentId
    FROM
    (SELECT g.name as name, group_role.role as permission, group_role.resource_id as componentId
    FROM groups g
    INNER JOIN group_roles group_role ON group_role.group_id=g.id
    UNION
    -- Add Anyone group permission
    SELECT #{anyoneGroup} as name, group_role.role as permission, group_role.resource_id as componentId
    FROM group_roles group_role
    <where>
      AND group_role.group_id IS NULL
    </where>
    ) groups
    <where>
      groups.componentId in
      <foreach collection="componentIds" open="(" close=")" item="id" separator=",">
        #{id}
      </foreach>
    </where>
    GROUP BY groups.permission, groups.componentId
  </select>

  <select id="countGroups" parameterType="map" resultType="int">
    SELECT count(1)
    FROM
    (SELECT g.name as name
    FROM groups g
    INNER JOIN group_roles group_role ON group_role.group_id=g.id
    AND group_role.role=#{permission}
    <if test="componentId != null">
      AND group_role.resource_id=#{componentId}
    </if>
    <if test="componentId == null">
      AND group_role.resource_id IS NULL
    </if>
    UNION
    -- Add Anyone group permission
    SELECT #{anyoneGroup} as name
    FROM group_roles group_role
    <where>
      AND group_role.role=#{permission}
      AND group_role.group_id IS NULL
      <if test="componentId != null">
        AND group_role.resource_id=#{componentId}
      </if>
      <if test="componentId == null">
        AND group_role.resource_id IS NULL
      </if>
    </where>
    ) groups
  </select>

  <select id="selectGroups" parameterType="map" resultType="GroupWithPermission">
    SELECT id, name, description, permission FROM
    (SELECT g.id as id, g.name as name, g.description as description, group_role.role as permission
    FROM groups g
    LEFT JOIN group_roles group_role ON group_role.group_id=g.id
    AND group_role.role=#{query.permission}
    <if test="componentId != null">
      AND group_role.resource_id=#{componentId}
    </if>
    <if test="componentId == null">
      AND group_role.resource_id IS NULL
    </if>
    UNION
    -- Add Anyone group permission
    SELECT 0 as id, #{anyoneGroup} as name, NULL as description, group_role.role as permission
    FROM group_roles group_role
    <where>
      AND group_role.role=#{query.permission}
      AND group_role.group_id IS NULL
      <if test="componentId != null">
        AND group_role.resource_id=#{componentId}
      </if>
      <if test="componentId == null">
        AND group_role.resource_id IS NULL
      </if>
    </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="selectGroupNamesByPermissionQuery" parameterType="map" resultType="string">
    select distinct gr.name, lower(gr.name), gr.group_id
    <include refid="groupsByQuery" />
    order by lower(gr.name), gr.name, gr.group_id
  </select>

  <select id="countGroupsByPermissionQuery" parameterType="map" resultType="int">
    select count(1)
    from (
      select distinct gr.group_id
      <include refid="groupsByQuery" />) g
  </select>

  <sql id="groupsByQuery">
    from (
    select g.id as group_id, g.name as name, gr.role as permission, gr.resource_id as resource_id, gr.id as id
    from groups g
      left join group_roles gr on g.id = gr.group_id
    UNION ALL
    select 0 as group_id, 'Anyone' as name, gr.role as permission, gr.resource_id as resource_id, gr.id as id
    from group_roles gr
    <where>
      <if test="query.withPermissionOnly()">
        and gr.group_id is null
      </if>
    </where>
    ) gr
    left join projects p on gr.resource_id = p.id
    <where>
      <if test="query.searchQueryToSql != null">
        and lower(gr.name) like #{query.searchQueryToSql} ESCAPE '/'
      </if>
      <!-- filter rows with group permissions -->
      <if test="query.withPermissionOnly()">
        and gr.permission is not null
        <if test="query.componentUuid==null">
          and gr.resource_id is null
        </if>
        <if test="query.componentUuid!=null">
          and p.uuid=#{query.componentUuid}
        </if>
        <if test="query.permission!=null">
          and gr.permission=#{query.permission}
        </if>
      </if>
    </where>
  </sql>

  <select id="selectGroupPermissionByGroupNames" parameterType="map" resultType="GroupRole">
    SELECT sub.id, sub.groupId, sub.resourceId, sub.role
    FROM
      (
      SELECT gr.id AS id, gr.group_id AS groupId, gr.resource_id AS resourceId, gr.role AS role, g.name AS name
      FROM group_roles gr
      INNER JOIN groups g ON g.id=gr.group_id
      UNION ALL
      SELECT gr.id as id, 0 AS group_id, gr.resource_id AS resource_id, gr.role AS role, 'Anyone' AS name
      FROM group_roles gr
      WHERE gr.group_id is null
      ) sub
    <where>
      sub.name IN
      <foreach collection="groupNames" open="(" close=")" item="name" separator=",">
        #{name}
      </foreach>
      <if test="projectId!=null">
        AND sub.resourceId=#{projectId}
      </if>
      <if test="projectId==null">
        AND sub.resourceId IS NULL
      </if>
    </where>
  </select>

</mapper>




© 2015 - 2025 Weber Informatics LLC | Privacy Policy