GUACAMOLE-394: Automatically update the end time of user history records upon logout.

This commit is contained in:
Michael Jumper
2017-09-12 18:03:22 -07:00
parent 3ccb4d4ac1
commit 28e7d215ac
6 changed files with 87 additions and 4 deletions

View File

@@ -27,6 +27,11 @@ import java.util.Date;
*/ */
public class ActivityRecordModel { public class ActivityRecordModel {
/**
* The ID of this object in the database, if any.
*/
private Integer recordID;
/** /**
* The database ID of the user associated with this activity record. * The database ID of the user associated with this activity record.
*/ */
@@ -53,6 +58,27 @@ public class ActivityRecordModel {
*/ */
private Date endDate; private Date endDate;
/**
* Returns the ID of this record in the database, if it exists.
*
* @return
* The ID of this record in the database, or null if this record was
* not retrieved from the database.
*/
public Integer getRecordID() {
return recordID;
}
/**
* Sets the database ID of this record to the given value.
*
* @param recordID
* The ID to assign to this object.
*/
public void setRecordID(Integer recordID) {
this.recordID = recordID;
}
/** /**
* Returns the database ID of the user associated with this activity * Returns the database ID of the user associated with this activity
* record. * record.

View File

@@ -231,7 +231,11 @@ public class ModeledUserContext extends RestrictedObject
@Override @Override
public void invalidate() { public void invalidate() {
// Nothing to invalidate
// Record logout time
userRecord.setEndDate(new Date());
userRecordMapper.update(userRecord);
} }
} }

View File

@@ -56,6 +56,17 @@ public interface UserRecordMapper {
*/ */
int insert(@Param("record") ActivityRecordModel record); int insert(@Param("record") ActivityRecordModel record);
/**
* Updates the given user login record.
*
* @param record
* The user login record to update.
*
* @return
* The number of rows updated.
*/
int update(@Param("record") ActivityRecordModel record);
/** /**
* Searches for up to <code>limit</code> user login records that contain * Searches for up to <code>limit</code> user login records that contain
* the given terms, sorted by the given predicates, regardless of whether * the given terms, sorted by the given predicates, regardless of whether

View File

@@ -25,6 +25,7 @@
<!-- Result mapper for system permissions --> <!-- Result mapper for system permissions -->
<resultMap id="UserRecordResultMap" type="org.apache.guacamole.auth.jdbc.base.ActivityRecordModel"> <resultMap id="UserRecordResultMap" type="org.apache.guacamole.auth.jdbc.base.ActivityRecordModel">
<id column="history_id" property="recordID" jdbcType="INTEGER"/>
<result column="remote_host" property="remoteHost" jdbcType="VARCHAR"/> <result column="remote_host" property="remoteHost" jdbcType="VARCHAR"/>
<result column="user_id" property="userID" jdbcType="INTEGER"/> <result column="user_id" property="userID" jdbcType="INTEGER"/>
<result column="username" property="username" jdbcType="VARCHAR"/> <result column="username" property="username" jdbcType="VARCHAR"/>
@@ -52,7 +53,8 @@
</select> </select>
<!-- Insert the given user record --> <!-- Insert the given user record -->
<insert id="insert" parameterType="org.apache.guacamole.auth.jdbc.base.ActivityRecordModel"> <insert id="insert" useGeneratedKeys="true" keyProperty="record.recordID"
parameterType="org.apache.guacamole.auth.jdbc.base.ActivityRecordModel">
INSERT INTO guacamole_user_history ( INSERT INTO guacamole_user_history (
remote_host, remote_host,
@@ -72,6 +74,18 @@
</insert> </insert>
<!-- Update the given user record -->
<update id="update" parameterType="org.apache.guacamole.auth.jdbc.base.ActivityRecordModel">
UPDATE guacamole_user_history
SET remote_host = #{record.remoteHost,jdbcType=VARCHAR},
user_id = (SELECT user_id FROM guacamole_user
WHERE username = #{record.username,jdbcType=VARCHAR}),
username = #{record.username,jdbcType=VARCHAR},
start_date = #{record.startDate,jdbcType=TIMESTAMP},
end_date = #{record.endDate,jdbcType=TIMESTAMP}
WHERE history_id = #{record.recordID,jdbcType=INTEGER}
</update>
<!-- Search for specific user records --> <!-- Search for specific user records -->
<select id="search" resultMap="UserRecordResultMap"> <select id="search" resultMap="UserRecordResultMap">

View File

@@ -25,6 +25,7 @@
<!-- Result mapper for system permissions --> <!-- Result mapper for system permissions -->
<resultMap id="UserRecordResultMap" type="org.apache.guacamole.auth.jdbc.base.ActivityRecordModel"> <resultMap id="UserRecordResultMap" type="org.apache.guacamole.auth.jdbc.base.ActivityRecordModel">
<id column="history_id" property="recordID" jdbcType="INTEGER"/>
<result column="remote_host" property="remoteHost" jdbcType="VARCHAR"/> <result column="remote_host" property="remoteHost" jdbcType="VARCHAR"/>
<result column="user_id" property="userID" jdbcType="INTEGER"/> <result column="user_id" property="userID" jdbcType="INTEGER"/>
<result column="username" property="username" jdbcType="VARCHAR"/> <result column="username" property="username" jdbcType="VARCHAR"/>
@@ -52,7 +53,8 @@
</select> </select>
<!-- Insert the given user record --> <!-- Insert the given user record -->
<insert id="insert" parameterType="org.apache.guacamole.auth.jdbc.base.ActivityRecordModel"> <insert id="insert" useGeneratedKeys="true" keyProperty="record.recordID"
parameterType="org.apache.guacamole.auth.jdbc.base.ActivityRecordModel">
INSERT INTO guacamole_user_history ( INSERT INTO guacamole_user_history (
remote_host, remote_host,
@@ -72,6 +74,18 @@
</insert> </insert>
<!-- Update the given user record -->
<update id="update" parameterType="org.apache.guacamole.auth.jdbc.base.ActivityRecordModel">
UPDATE guacamole_user_history
SET remote_host = #{record.remoteHost,jdbcType=VARCHAR},
user_id = (SELECT user_id FROM guacamole_user
WHERE username = #{record.username,jdbcType=VARCHAR}),
username = #{record.username,jdbcType=VARCHAR},
start_date = #{record.startDate,jdbcType=TIMESTAMP},
end_date = #{record.endDate,jdbcType=TIMESTAMP}
WHERE history_id = #{record.recordID,jdbcType=INTEGER}::integer
</update>
<!-- Search for specific user records --> <!-- Search for specific user records -->
<select id="search" resultMap="UserRecordResultMap"> <select id="search" resultMap="UserRecordResultMap">

View File

@@ -25,6 +25,7 @@
<!-- Result mapper for system permissions --> <!-- Result mapper for system permissions -->
<resultMap id="UserRecordResultMap" type="org.apache.guacamole.auth.jdbc.base.ActivityRecordModel"> <resultMap id="UserRecordResultMap" type="org.apache.guacamole.auth.jdbc.base.ActivityRecordModel">
<id column="history_id" property="recordID" jdbcType="INTEGER"/>
<result column="remote_host" property="remoteHost" jdbcType="VARCHAR"/> <result column="remote_host" property="remoteHost" jdbcType="VARCHAR"/>
<result column="user_id" property="userID" jdbcType="INTEGER"/> <result column="user_id" property="userID" jdbcType="INTEGER"/>
<result column="username" property="username" jdbcType="VARCHAR"/> <result column="username" property="username" jdbcType="VARCHAR"/>
@@ -52,7 +53,8 @@
</select> </select>
<!-- Insert the given user record --> <!-- Insert the given user record -->
<insert id="insert" parameterType="org.apache.guacamole.auth.jdbc.base.ActivityRecordModel"> <insert id="insert" useGeneratedKeys="true" keyProperty="record.recordID"
parameterType="org.apache.guacamole.auth.jdbc.base.ActivityRecordModel">
INSERT INTO [guacamole_user_history] ( INSERT INTO [guacamole_user_history] (
remote_host, remote_host,
@@ -72,6 +74,18 @@
</insert> </insert>
<!-- Update the given user record -->
<update id="update" parameterType="org.apache.guacamole.auth.jdbc.base.ActivityRecordModel">
UPDATE [guacamole_user_history]
SET remote_host = #{record.remoteHost,jdbcType=VARCHAR},
user_id = (SELECT user_id FROM [guacamole_user]
WHERE username = #{record.username,jdbcType=VARCHAR}),
username = #{record.username,jdbcType=VARCHAR},
start_date = #{record.startDate,jdbcType=TIMESTAMP},
end_date = #{record.endDate,jdbcType=TIMESTAMP}
WHERE history_id = #{record.recordID,jdbcType=INTEGER}
</update>
<!-- Search for specific user records --> <!-- Search for specific user records -->
<select id="search" resultMap="UserRecordResultMap"> <select id="search" resultMap="UserRecordResultMap">