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

<!--
   Copyright (C) 2015 Glyptodon LLC

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
-->

<mapper namespace="org.glyptodon.guacamole.auth.jdbc.permission.UserPermissionMapper" >

    <!-- Result mapper for user permissions -->
    <resultMap id="UserPermissionResultMap" type="org.glyptodon.guacamole.auth.jdbc.permission.ObjectPermissionModel">
        <result column="user_id"           property="userID"           jdbcType="INTEGER"/>
        <result column="username"          property="username"         jdbcType="VARCHAR"/>
        <result column="permission"        property="type"             jdbcType="VARCHAR"
                javaType="org.glyptodon.guacamole.net.auth.permission.ObjectPermission$Type"/>
        <result column="affected_username" property="objectIdentifier" jdbcType="INTEGER"/>
    </resultMap>

    <!-- Select all permissions for a given user -->
    <select id="select" resultMap="UserPermissionResultMap">

        SELECT
            guacamole_user_permission.user_id,
            guacamole_user.username,
            permission,
            affected.username AS affected_username
        FROM guacamole_user_permission
        JOIN guacamole_user          ON guacamole_user_permission.user_id          = guacamole_user.user_id
        JOIN guacamole_user affected ON guacamole_user_permission.affected_user_id = affected.user_id
        WHERE guacamole_user_permission.user_id = #{user.objectID,jdbcType=INTEGER}

    </select>

    <!-- Select the single permission matching the given criteria -->
    <select id="selectOne" resultMap="UserPermissionResultMap">

        SELECT
            guacamole_user_permission.user_id,
            guacamole_user.username,
            permission,
            affected.username AS affected_username
        FROM guacamole_user_permission
        JOIN guacamole_user          ON guacamole_user_permission.user_id          = guacamole_user.user_id
        JOIN guacamole_user affected ON guacamole_user_permission.affected_user_id = affected.user_id
        WHERE
            guacamole_user_permission.user_id = #{user.objectID,jdbcType=INTEGER}
            AND permission = #{type,jdbcType=VARCHAR}::guacamole_object_permission_type
            AND affected.username = #{identifier,jdbcType=INTEGER}

    </select>

    <!-- Select identifiers accessible by the given user for the given permissions -->
    <select id="selectAccessibleIdentifiers" resultType="string">

        SELECT DISTINCT username
        FROM guacamole_user_permission
        JOIN guacamole_user ON guacamole_user_permission.affected_user_id = guacamole_user.user_id
        WHERE
            guacamole_user_permission.user_id = #{user.objectID,jdbcType=INTEGER}
            AND username IN
                <foreach collection="identifiers" item="identifier"
                         open="(" separator="," close=")">
                    #{identifier,jdbcType=INTEGER}
                </foreach>
            AND permission IN
                <foreach collection="permissions" item="permission"
                         open="(" separator="," close=")">
                    #{permission,jdbcType=VARCHAR}::guacamole_object_permission_type
                </foreach>

    </select>

    <!-- Delete all given permissions -->
    <delete id="delete" parameterType="org.glyptodon.guacamole.auth.jdbc.permission.ObjectPermissionModel">

        DELETE FROM guacamole_user_permission
        USING guacamole_user affected
        WHERE
            guacamole_user_permission.affected_user_id = affected.user_id
            AND (guacamole_user_permission.user_id, permission, affected.username) IN
                <foreach collection="permissions" item="permission"
                         open="(" separator="," close=")">
                    (#{permission.userID,jdbcType=INTEGER},
                     #{permission.type,jdbcType=VARCHAR}::guacamole_object_permission_type,
                     #{permission.objectIdentifier,jdbcType=INTEGER})
                </foreach>

    </delete>

    <!-- Insert all given permissions -->
    <insert id="insert" parameterType="org.glyptodon.guacamole.auth.jdbc.permission.ObjectPermissionModel">

        INSERT INTO guacamole_user_permission (
            user_id,
            permission,
            affected_user_id
        )
        SELECT permissions.user_id, permissions.permission, guacamole_user.user_id FROM
            <foreach collection="permissions" item="permission"
                     open="(" separator="UNION ALL" close=")">
                SELECT #{permission.userID,jdbcType=INTEGER}                                 AS user_id,
                       #{permission.type,jdbcType=VARCHAR}::guacamole_object_permission_type AS permission,
                       #{permission.objectIdentifier,jdbcType=INTEGER}                       AS username
            </foreach>
        AS permissions
        JOIN guacamole_user ON guacamole_user.username = permissions.username; 

    </insert>

</mapper>