Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
X
xnat-web
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dhcp
xnat-web
Commits
2d4e8479
Commit
2d4e8479
authored
8 years ago
by
Rick Herrick
Browse files
Options
Downloads
Patches
Plain Diff
XNAT-4495 Added delete method to invalidate sessions for a particular user.
parent
2f4e7b4d
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/org/nrg/xnat/restlet/actions/UserSessionId.java
+51
-8
51 additions, 8 deletions
...main/java/org/nrg/xnat/restlet/actions/UserSessionId.java
with
51 additions
and
8 deletions
src/main/java/org/nrg/xnat/restlet/actions/UserSessionId.java
+
51
−
8
View file @
2d4e8479
...
...
@@ -12,6 +12,7 @@ package org.nrg.xnat.restlet.actions;
import
java.util.List
;
import
org.apache.commons.lang3.StringUtils
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
org.nrg.xdat.XDAT
;
...
...
@@ -34,24 +35,29 @@ import org.springframework.security.core.session.SessionRegistryImpl;
public
class
UserSessionId
extends
SecureResource
{
final
private
String
userI
D
;
final
private
String
_
userI
d
;
public
UserSessionId
(
Context
context
,
Request
request
,
Response
response
)
throws
Exception
{
super
(
context
,
request
,
response
);
final
UserI
user
=
getUser
();
userI
D
=
(
String
)
getParameter
(
request
,
"USER_ID"
);
if
(!
Roles
.
isSiteAdmin
(
user
)
&&
!
user
.
getLogin
().
equals
(
userI
D
))
{
_log
.
error
(
"User "
+
user
.
getLogin
()
+
" attempted to access session list for user "
+
userI
D
);
_
userI
d
=
(
String
)
getParameter
(
request
,
"USER_ID"
);
if
(!
Roles
.
isSiteAdmin
(
user
)
&&
!
user
.
getLogin
().
equals
(
_
userI
d
))
{
_log
.
error
(
"User "
+
user
.
getLogin
()
+
" attempted to access session list for user "
+
_
userI
d
);
response
.
setStatus
(
Status
.
CLIENT_ERROR_FORBIDDEN
,
"Only administrators can get the session list for users other than themselves."
);
}
else
{
if
(
_log
.
isDebugEnabled
())
{
_log
.
debug
(
user
.
getLogin
()
+
" is retrieving active sessions for user "
+
userI
D
);
_log
.
debug
(
user
.
getLogin
()
+
" is retrieving active sessions for user "
+
_
userI
d
);
}
getVariants
().
add
(
new
Variant
(
MediaType
.
ALL
));
}
}
@Override
public
boolean
allowDelete
()
{
return
true
;
}
@Override
public
Representation
represent
(
Variant
variant
)
{
SessionRegistry
sessionRegistry
=
XDAT
.
getContextService
().
getBean
(
"sessionRegistry"
,
SessionRegistryImpl
.
class
);
...
...
@@ -59,7 +65,7 @@ public class UserSessionId extends SecureResource {
List
<
SessionInformation
>
l
=
null
;
for
(
Object
p
:
allPrincipals
)
{
if
(
p
instanceof
UserI
)
{
if
(((
UserI
)
p
).
getLogin
().
equalsIgnoreCase
(
userI
D
))
{
if
(((
UserI
)
p
).
getLogin
().
equalsIgnoreCase
(
_
userI
d
))
{
l
=
sessionRegistry
.
getAllSessions
(
p
,
false
);
}
}
...
...
@@ -67,11 +73,11 @@ public class UserSessionId extends SecureResource {
try
{
if
(
l
==
null
)
{
JSONObject
json
=
new
JSONObject
();
json
.
put
(
userI
D
,
0
);
json
.
put
(
_
userI
d
,
0
);
return
new
JSONObjectRepresentation
(
null
,
json
);
}
else
{
JSONObject
json
=
new
JSONObject
();
json
.
put
(
userI
D
,
l
.
size
());
json
.
put
(
_
userI
d
,
l
.
size
());
return
new
JSONObjectRepresentation
(
null
,
json
);
}
}
catch
(
JSONException
e
)
{
...
...
@@ -82,5 +88,42 @@ public class UserSessionId extends SecureResource {
}
@Override
public
void
handleDelete
()
{
final
SessionRegistry
sessionRegistry
=
XDAT
.
getContextService
().
getBean
(
"sessionRegistry"
,
SessionRegistryImpl
.
class
);
Object
principal
=
null
;
for
(
final
Object
candidate
:
sessionRegistry
.
getAllPrincipals
())
{
if
(
candidate
instanceof
UserI
)
{
if
(
StringUtils
.
equals
(
_userId
,
((
UserI
)
candidate
).
getUsername
()))
{
principal
=
candidate
;
break
;
}
}
else
if
(
candidate
instanceof
String
)
{
if
(
StringUtils
.
equals
(
_userId
,
(
String
)
candidate
))
{
principal
=
candidate
;
break
;
}
}
}
if
(
principal
==
null
)
{
getResponse
().
setStatus
(
Status
.
CLIENT_ERROR_NOT_FOUND
);
return
;
}
if
(
_log
.
isDebugEnabled
())
{
_log
.
debug
(
"Found principal for user {}"
,
_userId
);
}
final
List
<
SessionInformation
>
sessions
=
sessionRegistry
.
getAllSessions
(
principal
,
false
);
if
(
_log
.
isDebugEnabled
())
{
_log
.
debug
(
"Found {} sessions for user {}"
,
sessions
.
size
(),
_userId
);
}
for
(
final
SessionInformation
session
:
sessions
)
{
if
(
_log
.
isDebugEnabled
())
{
_log
.
debug
(
"Expiring user {} session with ID {}"
,
_userId
,
session
.
getSessionId
());
}
session
.
expireNow
();
}
getResponse
().
setStatus
(
Status
.
SUCCESS_OK
);
}
private
static
final
Logger
_log
=
LoggerFactory
.
getLogger
(
UserSessionId
.
class
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment