forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserSession.cs
More file actions
174 lines (147 loc) · 4.88 KB
/
UserSession.cs
File metadata and controls
174 lines (147 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
// $Id: UserSession.cs 13369 2010-03-08 19:09:44Z DDNGLOBAL\Demis $
//
// Revision : $Revision: 13369 $
// Modified Date : $LastChangedDate: 2010-03-08 19:09:44 +0000 (Mon, 08 Mar 2010) $
// Modified By : $LastChangedBy: DDNGLOBAL\Demis $
//
// (c) Copyright 2010 Liquidbit Ltd
*/
using System;
using System.Collections.Generic;
using ServiceStack.Common;
namespace ServiceStack.ServiceInterface.Deprecated.Session
{
/// <summary>
/// Holds all the data required for a User Session
/// </summary>
public class UserSession
{
//Empty constructor required for TypeSerializer
public UserSession()
{
this.PublicClientSessions = new Dictionary<Guid, UserClientSession>();
}
public Guid UserId { get; private set; }
public string UserName { get; private set; }
public string ShardId { get; private set; }
public Dictionary<Guid, UserClientSession> PublicClientSessions { get; private set; }
public UserSession(Guid userId, string userName, string shardId)
: this()
{
this.UserId = userId;
this.UserName = userName;
this.ShardId = shardId;
}
/// <summary>
/// Gets the max expiry date of all the users client sessions.
/// If the user has no more active client sessions we can remove them from the cache.
/// </summary>
/// <value>The expiry date.</value>
public DateTime? ExpiryDate
{
get
{
DateTime? maxExpiryDate = null;
foreach (var session in this.PublicClientSessions.Values)
{
if (maxExpiryDate == null || session.ExpiryDate > maxExpiryDate)
{
maxExpiryDate = session.ExpiryDate;
}
}
return maxExpiryDate;
}
}
/// <summary>
/// Creates a new client session for the user.
/// </summary>
/// <param name="ipAddress">The ip address.</param>
/// <param name="userAgent">The user agent.</param>
/// <param name="base64ClientModulus">The base64 client modulus.</param>
/// <param name="userClientGlobalId">The user client global id.</param>
/// <returns></returns>
public UserClientSession CreateNewClientSession(string ipAddress, string userAgent, string base64ClientModulus, Guid userClientGlobalId)
{
return this.CreateClientSession(Guid.NewGuid(), ipAddress, userAgent, base64ClientModulus, userClientGlobalId);
}
public UserClientSession CreateClientSession(Guid sessionId, string ipAddress, string userAgent, string base64ClientModulus, Guid userClientGlobalId)
{
var clientSession = new UserClientSession(
sessionId, this.UserId, ipAddress, userAgent, base64ClientModulus, userClientGlobalId);
this.PublicClientSessions[clientSession.Id] = clientSession;
return clientSession;
}
/// <summary>
/// Removes the client session.
/// </summary>
/// <param name="clientSessionId">The client session id.</param>
public void RemoveClientSession(Guid clientSessionId)
{
if (this.PublicClientSessions.ContainsKey(clientSessionId))
{
this.PublicClientSessions.Remove(clientSessionId);
}
}
public UserClientSession GetClientSessionWithClientId(Guid userClientId)
{
foreach (var entry in this.PublicClientSessions)
{
if (entry.Value.UserClientGlobalId == userClientId)
{
return entry.Value;
}
}
return null;
}
/// <summary>
/// Verifies this UserSession, removing any expired sessions.
/// Returns true to keep the UserSession in the cache.
/// </summary>
/// <returns>
/// <c>true</c> if this session has any active client sessions; otherwise, <c>false</c>.
/// </returns>
public bool HasExpired()
{
RemoveExpiredSessions(this.PublicClientSessions);
//If there are no more active client sessions we can remove the entire UserSession
var sessionHasExpired =
this.ExpiryDate == null //There are no UserClientSessions
|| this.ExpiryDate.Value <= DateTime.UtcNow; //The max UserClientSession ExpiryDate has expired
return sessionHasExpired;
}
private static void RemoveExpiredSessions(IDictionary<Guid, UserClientSession> clientSessions)
{
var expiredSessionKeys = new List<Guid>();
foreach (var clientSession in clientSessions)
{
if (clientSession.Value.ExpiryDate < DateTime.UtcNow)
{
expiredSessionKeys.Add(clientSession.Key);
}
}
foreach (var sessionKey in expiredSessionKeys)
{
clientSessions.Remove(sessionKey);
}
}
public void RemoveAllSessions()
{
this.PublicClientSessions.Clear();
}
public UserClientSession GetClientSession(Guid clientSessionId)
{
UserClientSession session;
return this.PublicClientSessions.TryGetValue(clientSessionId, out session)
? session : null;
}
public string ToCacheKey()
{
return ToCacheKey(this.UserId);
}
public static string ToCacheKey(Guid userId)
{
return UrnId.Create<UserSession>(userId.ToString());
}
}
}