SEC-169: Add SessionRegistry.getAllPrincipals() method.
This commit is contained in:
parent
3a01e48b17
commit
8c0ce12332
|
@ -1,4 +1,4 @@
|
||||||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -24,6 +24,14 @@ package org.acegisecurity.concurrent;
|
||||||
public interface SessionRegistry {
|
public interface SessionRegistry {
|
||||||
//~ Methods ================================================================
|
//~ Methods ================================================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtains all the known principals in the <code>SessionRegistry</code>.
|
||||||
|
*
|
||||||
|
* @return each of the unique principals, which can then be presented to
|
||||||
|
* {@link #getAllSessions(Object)}.
|
||||||
|
*/
|
||||||
|
public Object[] getAllPrincipals();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtains all the known sessions for the specified principal. Sessions
|
* Obtains all the known sessions for the specified principal. Sessions
|
||||||
* that have expired or destroyed are not returned.
|
* that have expired or destroyed are not returned.
|
||||||
|
@ -50,7 +58,8 @@ public interface SessionRegistry {
|
||||||
/**
|
/**
|
||||||
* Updates the given <code>sessionId</code> so its last request time is
|
* Updates the given <code>sessionId</code> so its last request time is
|
||||||
* equal to the present date and time. Silently returns if the given
|
* equal to the present date and time. Silently returns if the given
|
||||||
* <code>sessionId</code> cannot be found or the session is marked to expire.
|
* <code>sessionId</code> cannot be found or the session is marked to
|
||||||
|
* expire.
|
||||||
*
|
*
|
||||||
* @param sessionId for which to update the date and time of the last
|
* @param sessionId for which to update the date and time of the last
|
||||||
* request (should never be <code>null</code>)
|
* request (should never be <code>null</code>)
|
||||||
|
|
|
@ -144,4 +144,8 @@ public class SessionRegistryImpl implements SessionRegistry,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Object[] getAllPrincipals() {
|
||||||
|
return principals.keySet().toArray();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -52,6 +52,23 @@ public class SessionRegistryImplTests extends TestCase {
|
||||||
assertNull(sessionRegistry.getSessionInformation(sessionId));
|
assertNull(sessionRegistry.getSessionInformation(sessionId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testMultiplePrincipals() throws Exception {
|
||||||
|
Object principal1 = "principal_1";
|
||||||
|
Object principal2 = "principal_2";
|
||||||
|
String sessionId1 = "1234567890";
|
||||||
|
String sessionId2 = "9876543210";
|
||||||
|
String sessionId3 = "5432109876";
|
||||||
|
|
||||||
|
SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
|
||||||
|
|
||||||
|
sessionRegistry.registerNewSession(sessionId1, principal1);
|
||||||
|
sessionRegistry.registerNewSession(sessionId2, principal1);
|
||||||
|
sessionRegistry.registerNewSession(sessionId3, principal2);
|
||||||
|
|
||||||
|
assertEquals(principal1, sessionRegistry.getAllPrincipals()[0]);
|
||||||
|
assertEquals(principal2, sessionRegistry.getAllPrincipals()[1]);
|
||||||
|
}
|
||||||
|
|
||||||
public void testSessionInformationLifecycle() throws Exception {
|
public void testSessionInformationLifecycle() throws Exception {
|
||||||
Object principal = "Some principal object";
|
Object principal = "Some principal object";
|
||||||
String sessionId = "1234567890";
|
String sessionId = "1234567890";
|
||||||
|
@ -95,6 +112,33 @@ public class SessionRegistryImplTests extends TestCase {
|
||||||
assertNull(sessionRegistry.getAllSessions(principal));
|
assertNull(sessionRegistry.getAllSessions(principal));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testTwoSessionsOnePrincipalExpiring() throws Exception {
|
||||||
|
Object principal = "Some principal object";
|
||||||
|
String sessionId1 = "1234567890";
|
||||||
|
String sessionId2 = "9876543210";
|
||||||
|
SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
|
||||||
|
|
||||||
|
// Register new Session
|
||||||
|
sessionRegistry.registerNewSession(sessionId1, principal);
|
||||||
|
assertEquals(1, sessionRegistry.getAllSessions(principal).length);
|
||||||
|
assertEquals(sessionId1,
|
||||||
|
sessionRegistry.getAllSessions(principal)[0].getSessionId());
|
||||||
|
|
||||||
|
// Register new Session
|
||||||
|
sessionRegistry.registerNewSession(sessionId2, principal);
|
||||||
|
assertEquals(2, sessionRegistry.getAllSessions(principal).length);
|
||||||
|
assertEquals(sessionId2,
|
||||||
|
sessionRegistry.getAllSessions(principal)[1].getSessionId());
|
||||||
|
|
||||||
|
// Expire one session
|
||||||
|
SessionInformation session = sessionRegistry.getSessionInformation(sessionId2);
|
||||||
|
session.expireNow();
|
||||||
|
|
||||||
|
// Check retrieval still correct
|
||||||
|
assertTrue(sessionRegistry.getSessionInformation(sessionId2).isExpired());
|
||||||
|
assertFalse(sessionRegistry.getSessionInformation(sessionId1).isExpired());
|
||||||
|
}
|
||||||
|
|
||||||
public void testTwoSessionsOnePrincipalHandling() throws Exception {
|
public void testTwoSessionsOnePrincipalHandling() throws Exception {
|
||||||
Object principal = "Some principal object";
|
Object principal = "Some principal object";
|
||||||
String sessionId1 = "1234567890";
|
String sessionId1 = "1234567890";
|
||||||
|
@ -124,32 +168,4 @@ public class SessionRegistryImplTests extends TestCase {
|
||||||
assertNull(sessionRegistry.getSessionInformation(sessionId2));
|
assertNull(sessionRegistry.getSessionInformation(sessionId2));
|
||||||
assertNull(sessionRegistry.getAllSessions(principal));
|
assertNull(sessionRegistry.getAllSessions(principal));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testTwoSessionsOnePrincipalExpiring() throws Exception {
|
|
||||||
Object principal = "Some principal object";
|
|
||||||
String sessionId1 = "1234567890";
|
|
||||||
String sessionId2 = "9876543210";
|
|
||||||
SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
|
|
||||||
|
|
||||||
// Register new Session
|
|
||||||
sessionRegistry.registerNewSession(sessionId1, principal);
|
|
||||||
assertEquals(1, sessionRegistry.getAllSessions(principal).length);
|
|
||||||
assertEquals(sessionId1,
|
|
||||||
sessionRegistry.getAllSessions(principal)[0].getSessionId());
|
|
||||||
|
|
||||||
// Register new Session
|
|
||||||
sessionRegistry.registerNewSession(sessionId2, principal);
|
|
||||||
assertEquals(2, sessionRegistry.getAllSessions(principal).length);
|
|
||||||
assertEquals(sessionId2,
|
|
||||||
sessionRegistry.getAllSessions(principal)[1].getSessionId());
|
|
||||||
|
|
||||||
// Expire one session
|
|
||||||
SessionInformation session = sessionRegistry.getSessionInformation(sessionId2);
|
|
||||||
session.expireNow();
|
|
||||||
|
|
||||||
// Check retrieval still correct
|
|
||||||
assertTrue(sessionRegistry.getSessionInformation(sessionId2).isExpired());
|
|
||||||
assertFalse(sessionRegistry.getSessionInformation(sessionId1).isExpired());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue