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

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

The newest version!
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd">

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

  <select id="selectUserPermissionsByQueryAndUserIds" parameterType="map" resultType="org.sonar.db.permission.UserPermissionDto">
    select
      u.id as userId,
      ur.organization_uuid as organizationUuid,
      ur.resource_id as componentId,
      ur.role as permission
    <include refid="sqlQueryJoins" />
    <where>
      u.id in <foreach collection="userIds" open="(" close=")" item="userId" separator=",">#{userId,jdbcType=INTEGER}</foreach>
      <include refid="sqlQueryFilters" />
    </where>
  </select>

  <select id="selectUserIdsByQuery" parameterType="map" resultType="int">
    select
      distinct u.id, lower(u.name) as lowerName
      <include refid="sqlQueryJoins" />
      <where>
        <include refid="sqlQueryFilters" />
      </where>
    order by lowerName asc
  </select>

  <select id="countUsersByQuery" parameterType="map" resultType="int">
    select count(distinct(u.id))
    <include refid="sqlQueryJoins" />
    <where>
      <include refid="sqlQueryFilters" />
    </where>
  </select>

  <sql id="sqlQueryJoins">
    from users u
    left join user_roles ur on ur.user_id = u.id
    left join projects p on ur.resource_id = p.id
    inner join organization_members om on u.id=om.user_id and om.organization_uuid=#{query.organizationUuid,jdbcType=VARCHAR}
  </sql>

  <sql id="sqlQueryFilters">
    and u.active = ${_true}
    <if test="query.searchQueryToSql != null">
      and (
      lower(u.name) like #{query.searchQueryToSqlLowercase,jdbcType=VARCHAR} ESCAPE '/'
      or u.email like #{query.searchQueryToSql,jdbcType=VARCHAR} ESCAPE '/'
      or u.login like #{query.searchQueryToSql,jdbcType=VARCHAR} ESCAPE '/')
    </if>
    <!-- filter rows with user permissions -->
    <if test="query.withAtLeastOnePermission()">
      and ur.organization_uuid = om.organization_uuid
      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,jdbcType=VARCHAR}
      </if>
      <if test="query.permission!=null">
        and ur.role = #{query.permission,jdbcType=VARCHAR}
      </if>
    </if>
  </sql>

  <select id="selectGlobalPermissionsOfUser" parameterType="map" resultType="string">
    select ur.role
    from user_roles ur
    where
    ur.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
    ur.user_id = #{userId,jdbcType=INTEGER} and
    ur.resource_id is null
  </select>

  <select id="selectProjectPermissionsOfUser" parameterType="map" resultType="string">
    select ur.role
    from user_roles ur
    where
    ur.user_id = #{userId,jdbcType=INTEGER} and
    ur.resource_id = #{projectId,jdbcType=BIGINT}
  </select>

  <select id="countUsersByProjectPermission" resultType="org.sonar.db.permission.CountPerProjectPermission">
    select ur.resource_id as componentId, ur.role as permission, count(u.login) as count
    from users u
    inner join user_roles ur on ur.user_id = u.id
    inner join projects p on p.id = ur.resource_id
    where u.active = ${_true}
    and p.id in <foreach collection="projectIds" open="(" close=")" item="projectId" separator=",">#{projectId}</foreach>
    group by ur.resource_id, ur.role
  </select>

  <select id="selectUserIdsWithPermissionOnProjectBut" resultType="Integer">
    select
      distinct ur1.user_id
    from
      user_roles ur1
    where
      ur1.resource_id = #{projectId,jdbcType=BIGINT}
      and role &lt;&gt; #{permission,jdbcType=VARCHAR}
      and not exists (
        select
          1
        from
          user_roles ur2
        where
          ur2.resource_id = ur1.resource_id
          and ur2.user_id = ur1.user_id
          and role = #{permission,jdbcType=VARCHAR}
      )
  </select>

  <insert id="insert" parameterType="org.sonar.db.permission.UserPermissionDto" useGeneratedKeys="false">
    insert into user_roles (
    organization_uuid,
    user_id,
    resource_id,
    role
    ) values (
    #{organizationUuid,jdbcType=VARCHAR},
    #{userId,jdbcType=INTEGER},
    #{componentId,jdbcType=BIGINT},
    #{permission,jdbcType=VARCHAR}
    )
  </insert>

  <delete id="deleteGlobalPermission" parameterType="map">
    delete from user_roles
    where
    role = #{permission,jdbcType=VARCHAR} and
    user_id = #{userId,jdbcType=INTEGER} and
    organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
    resource_id is null
  </delete>

  <delete id="deleteProjectPermission" parameterType="map">
    delete from user_roles
    where
    role = #{permission,jdbcType=VARCHAR} and
    user_id = #{userId,jdbcType=INTEGER} and
    resource_id = #{projectId,jdbcType=BIGINT}
  </delete>

  <delete id="deleteProjectPermissions" parameterType="map">
    delete from user_roles
    where
    resource_id = #{projectId,jdbcType=BIGINT}
  </delete>

  <delete id="deleteProjectPermissionOfAnyUser" parameterType="map">
    delete from
      user_roles
    where
      resource_id = #{projectId,jdbcType=BIGINT}
      and role = #{permission,jdbcType=VARCHAR}
  </delete>

  <delete id="deleteByOrganization" parameterType="String">
    delete from
      user_roles
    where
      organization_uuid = #{organizationUuid,jdbcType=VARCHAR}
  </delete>

  <delete id="deleteOrganizationMemberPermissions" parameterType="map">
    delete from
      user_roles
    where
      organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
      user_id = #{userId,jdbcType=INTEGER}
  </delete>

  <delete id="deleteByUserId" parameterType="int">
    DELETE FROM user_roles WHERE user_id=#{userId,jdbcType=INTEGER}
  </delete>
</mapper>




© 2015 - 2025 Weber Informatics LLC | Privacy Policy