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

<!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
-->

<mapper namespace="org.apache.guacamole.auth.jdbc.user.UserMapper" >

    <!-- Result mapper for user objects -->
    <resultMap id="UserResultMap" type="org.apache.guacamole.auth.jdbc.user.UserModel" >
        <id     column="user_id"             property="objectID"          jdbcType="INTEGER"/>
        <result column="username"            property="identifier"        jdbcType="VARCHAR"/>
        <result column="password_hash"       property="passwordHash"      jdbcType="BINARY"/>
        <result column="password_salt"       property="passwordSalt"      jdbcType="BINARY"/>
        <result column="disabled"            property="disabled"          jdbcType="BOOLEAN"/>
        <result column="access_window_start" property="accessWindowStart" jdbcType="TIME"/>
        <result column="access_window_end"   property="accessWindowEnd"   jdbcType="TIME"/>
        <result column="valid_from"          property="validFrom"         jdbcType="DATE"/>
        <result column="valid_until"         property="validUntil"        jdbcType="DATE"/>
        <result column="timezone"            property="timeZone"          jdbcType="VARCHAR"/>
    </resultMap>

    <!-- Select all usernames -->
    <select id="selectIdentifiers" resultType="string">
        SELECT username
        FROM guacamole_user
    </select>

    <!-- Select usernames of all readable users -->
    <select id="selectReadableIdentifiers" resultType="string">
        SELECT username
        FROM guacamole_user
        JOIN guacamole_user_permission ON affected_user_id = guacamole_user.user_id
        WHERE
            guacamole_user_permission.user_id = #{user.objectID,jdbcType=INTEGER}
            AND permission = 'READ'
    </select>

    <!-- Select multiple users by username -->
    <select id="select" resultMap="UserResultMap">

        SELECT
            user_id,
            username,
            password_hash,
            password_salt,
            disabled,
            expired,
            access_window_start,
            access_window_end,
            valid_from,
            valid_until,
            timezone
        FROM guacamole_user
        WHERE username IN
            <foreach collection="identifiers" item="identifier"
                     open="(" separator="," close=")">
                #{identifier,jdbcType=VARCHAR}
            </foreach>

    </select>

    <!-- Select multiple users by username only if readable -->
    <select id="selectReadable" resultMap="UserResultMap">

        SELECT
            guacamole_user.user_id,
            username,
            password_hash,
            password_salt,
            disabled,
            expired,
            access_window_start,
            access_window_end,
            valid_from,
            valid_until,
            timezone
        FROM guacamole_user
        JOIN guacamole_user_permission ON affected_user_id = guacamole_user.user_id
        WHERE username IN
            <foreach collection="identifiers" item="identifier"
                     open="(" separator="," close=")">
                #{identifier,jdbcType=VARCHAR}
            </foreach>
            AND guacamole_user_permission.user_id = #{user.objectID,jdbcType=INTEGER}
            AND permission = 'READ'

    </select>

    <!-- Select single user by username -->
    <select id="selectOne" resultMap="UserResultMap">

        SELECT
            user_id,
            username,
            password_hash,
            password_salt,
            disabled,
            expired,
            access_window_start,
            access_window_end,
            valid_from,
            valid_until,
            timezone
        FROM guacamole_user
        WHERE
            username = #{username,jdbcType=VARCHAR}

    </select>

    <!-- Delete single user by username -->
    <delete id="delete">
        DELETE FROM guacamole_user
        WHERE username = #{identifier,jdbcType=VARCHAR}
    </delete>

    <!-- Insert single user -->
    <insert id="insert" useGeneratedKeys="true" keyProperty="object.objectID"
            parameterType="org.apache.guacamole.auth.jdbc.user.UserModel">

        INSERT INTO guacamole_user (
            username,
            password_hash,
            password_salt,
            disabled,
            expired,
            access_window_start,
            access_window_end,
            valid_from,
            valid_until,
            timezone
        )
        VALUES (
            #{object.identifier,jdbcType=VARCHAR},
            #{object.passwordHash,jdbcType=BINARY},
            #{object.passwordSalt,jdbcType=BINARY},
            #{object.disabled,jdbcType=BOOLEAN},
            #{object.expired,jdbcType=BOOLEAN},
            #{object.accessWindowStart,jdbcType=TIME},
            #{object.accessWindowEnd,jdbcType=TIME},
            #{object.validFrom,jdbcType=DATE},
            #{object.validUntil,jdbcType=DATE},
            #{object.timeZone,jdbcType=VARCHAR}
        )

    </insert>

    <!-- Update single user -->
    <update id="update" parameterType="org.apache.guacamole.auth.jdbc.user.UserModel">
        UPDATE guacamole_user
        SET password_hash = #{object.passwordHash,jdbcType=BINARY},
            password_salt = #{object.passwordSalt,jdbcType=BINARY},
            disabled = #{object.disabled,jdbcType=BOOLEAN},
            expired = #{object.expired,jdbcType=BOOLEAN},
            access_window_start = #{object.accessWindowStart,jdbcType=TIME},
            access_window_end = #{object.accessWindowEnd,jdbcType=TIME},
            valid_from = #{object.validFrom,jdbcType=DATE},
            valid_until = #{object.validUntil,jdbcType=DATE},
            timezone = #{object.timeZone,jdbcType=VARCHAR}
        WHERE user_id = #{object.objectID,jdbcType=VARCHAR}
    </update>

</mapper>