Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Peter Harrison
gravity
Commits
26d299fc
Commit
26d299fc
authored
Mar 06, 2016
by
Peter Harrison
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Working primarily on getting unit tests working again.
May have resolved issue with Cache and Cached Lists.
parent
6818ebd4
Changes
30
Hide whitespace changes
Inline
Side-by-side
Showing
30 changed files
with
1117 additions
and
653 deletions
+1117
-653
pom.xml
pom.xml
+1
-1
src/main/java/nz/net/orcon/kanban/automation/ClusterManager.java
...n/java/nz/net/orcon/kanban/automation/ClusterManager.java
+8
-164
src/main/java/nz/net/orcon/kanban/automation/ClusterManagerImpl.java
...va/nz/net/orcon/kanban/automation/ClusterManagerImpl.java
+173
-0
src/main/java/nz/net/orcon/kanban/automation/JcrObserver.java
...main/java/nz/net/orcon/kanban/automation/JcrObserver.java
+2
-6
src/main/java/nz/net/orcon/kanban/controllers/BoardsCache.java
...ain/java/nz/net/orcon/kanban/controllers/BoardsCache.java
+3
-3
src/main/java/nz/net/orcon/kanban/controllers/CacheImpl.java
src/main/java/nz/net/orcon/kanban/controllers/CacheImpl.java
+1
-2
src/main/java/nz/net/orcon/kanban/controllers/CardController.java
.../java/nz/net/orcon/kanban/controllers/CardController.java
+5
-4
src/main/java/nz/net/orcon/kanban/controllers/NotificationController.java
.../net/orcon/kanban/controllers/NotificationController.java
+1
-1
src/main/java/nz/net/orcon/kanban/security/SecurityTool.java
src/main/java/nz/net/orcon/kanban/security/SecurityTool.java
+14
-136
src/main/java/nz/net/orcon/kanban/security/SecurityToolImpl.java
...n/java/nz/net/orcon/kanban/security/SecurityToolImpl.java
+164
-0
src/main/java/nz/net/orcon/kanban/tools/MemoryRepositoryFactory.java
...va/nz/net/orcon/kanban/tools/MemoryRepositoryFactory.java
+14
-0
src/main/java/nz/net/orcon/kanban/tools/MongoRepositoryFactory.java
...ava/nz/net/orcon/kanban/tools/MongoRepositoryFactory.java
+53
-0
src/main/java/nz/net/orcon/kanban/tools/OcmMapperFactory.java
...main/java/nz/net/orcon/kanban/tools/OcmMapperFactory.java
+10
-35
src/main/java/nz/net/orcon/kanban/tools/RepositoryFactory.java
...ain/java/nz/net/orcon/kanban/tools/RepositoryFactory.java
+9
-0
src/main/webapp/WEB-INF/spring/app/repository.xml
src/main/webapp/WEB-INF/spring/app/repository.xml
+5
-1
src/main/webapp/WEB-INF/spring/app/security.xml
src/main/webapp/WEB-INF/spring/app/security.xml
+1
-1
src/test/java/nz/net/orcon/kanban/automation/MockClusterManager.java
...va/nz/net/orcon/kanban/automation/MockClusterManager.java
+79
-0
src/test/java/nz/net/orcon/kanban/automation/plugin/ScriptPluginTest.java
.../net/orcon/kanban/automation/plugin/ScriptPluginTest.java
+2
-1
src/test/java/nz/net/orcon/kanban/controllers/BoardControllerTest.java
.../nz/net/orcon/kanban/controllers/BoardControllerTest.java
+7
-162
src/test/java/nz/net/orcon/kanban/controllers/CacheImplTest.java
...t/java/nz/net/orcon/kanban/controllers/CacheImplTest.java
+1
-0
src/test/java/nz/net/orcon/kanban/controllers/CardControllerTest.java
...a/nz/net/orcon/kanban/controllers/CardControllerTest.java
+83
-98
src/test/java/nz/net/orcon/kanban/controllers/FilterControllerTest.java
...nz/net/orcon/kanban/controllers/FilterControllerTest.java
+29
-26
src/test/java/nz/net/orcon/kanban/controllers/RuleControllerTest.java
...a/nz/net/orcon/kanban/controllers/RuleControllerTest.java
+12
-8
src/test/java/nz/net/orcon/kanban/controllers/TestBoardTool.java
...t/java/nz/net/orcon/kanban/controllers/TestBoardTool.java
+180
-0
src/test/java/nz/net/orcon/kanban/controllers/TestCache.java
src/test/java/nz/net/orcon/kanban/controllers/TestCache.java
+18
-3
src/test/java/nz/net/orcon/kanban/security/MockSecurityTool.java
...t/java/nz/net/orcon/kanban/security/MockSecurityTool.java
+35
-0
src/test/resources/test-cache.xml
src/test/resources/test-cache.xml
+39
-0
src/test/resources/test-controllers.xml
src/test/resources/test-controllers.xml
+12
-1
src/test/resources/test-event-config.xml
src/test/resources/test-event-config.xml
+151
-0
src/test/resources/test-repository.xml
src/test/resources/test-repository.xml
+5
-0
No files found.
pom.xml
View file @
26d299fc
...
...
@@ -4,7 +4,7 @@
<groupId>
nz.net.gravity
</groupId>
<artifactId>
gravity
</artifactId>
<packaging>
war
</packaging>
<version>
3.0.
0
</version>
<version>
3.0.
1-SNAPSHOT
</version>
<name>
Gravity Workflow Automation
</name>
<properties>
...
...
src/main/java/nz/net/orcon/kanban/automation/ClusterManager.java
View file @
26d299fc
/**
* GRAVITY WORKFLOW AUTOMATION
* (C) Copyright 2015 Orcon Limited
*
* This file is part of Gravity Workflow Automation.
*
* Gravity Workflow Automation is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Gravity Workflow Automation is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Gravity Workflow Automation.
* If not, see <http://www.gnu.org/licenses/>.
*/
package
nz.net.orcon.kanban.automation
;
import
javax.jms.Message
;
import
javax.jms.ObjectMessage
;
import
nz.net.orcon.kanban.model.Coup
;
import
nz.net.orcon.kanban.model.IdRequest
;
import
nz.net.orcon.kanban.model.IdResponse
;
import
org.apache.commons.lang.RandomStringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.jms.core.JmsTemplate
;
import
org.springframework.scheduling.annotation.Scheduled
;
/**
* The purpose of the Cluster manager is to manage all the functions that need to be
* coordinated across the Cluster.
*
* - Invalidation of Caches.
* - Phase Updates
* - Getting Identifiers.
* - Master Server Protocol
*
* @author peter
*
*/
public
class
ClusterManager
{
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
ClusterManager
.
class
);
private
boolean
leader
;
private
long
startTime
=
System
.
currentTimeMillis
();
private
String
serverId
=
RandomStringUtils
.
random
(
10
,
true
,
true
);
@Autowired
@Qualifier
(
"idJmsTemplate"
)
private
JmsTemplate
idTemplate
;
@Autowired
@Qualifier
(
"idSyncReceiver"
)
private
SyncReceive
idSyncReceiever
;
@Autowired
JcrObserver
jcrObserver
;
@Autowired
TimerManager
timerManager
;
public
Long
getId
(
String
path
,
String
field
)
throws
Exception
{
if
(
LOG
.
isDebugEnabled
()){
LOG
.
debug
(
"Generating ID from "
+
path
+
"."
+
field
);
}
Message
message
=
requestId
(
path
,
field
);
if
(
message
==
null
){
executeChallenge
();
message
=
requestId
(
path
,
field
);
}
ObjectMessage
objectMessage
=
(
ObjectMessage
)
message
;
IdResponse
idResponse
=
(
IdResponse
)
objectMessage
.
getObject
();
return
idResponse
.
getId
();
}
public
interface
ClusterManager
{
public
Message
requestId
(
String
path
,
String
field
){
String
requestId
=
RandomStringUtils
.
random
(
10
,
true
,
true
);
SyncMessage
idSync
=
idSyncReceiever
.
register
(
requestId
);
IdRequest
request
=
new
IdRequest
();
request
.
setField
(
field
);
request
.
setPath
(
path
);
request
.
setRequestId
(
requestId
);
idTemplate
.
convertAndSend
(
request
);
return
idSync
.
receiveMessage
(
10000
l
);
}
public
Long
getId
(
String
path
,
String
field
)
throws
Exception
;
public
String
getIdString
(
String
path
,
String
field
,
String
prefix
)
throws
Exception
{
Long
id
=
getId
(
path
,
field
);
return
prefix
+
id
.
toString
();
}
/**
* Note that this method is simply a wrapper to eat the return value of executeChallenge.
* This is needed to be able to schedule the task.
*
* @throws Exception
*/
@Scheduled
(
fixedDelay
=
30000
l
)
public
void
pollLeader
()
throws
Exception
{
if
(!
isLeader
()){
this
.
executeChallenge
();
}
}
public
synchronized
boolean
executeChallenge
()
throws
Exception
{
SyncMessage
coupSync
=
idSyncReceiever
.
register
(
this
.
serverId
);
Coup
coup
=
new
Coup
();
coup
.
setRequestId
(
this
.
serverId
);
coup
.
setCoupTime
(
this
.
startTime
);
idTemplate
.
convertAndSend
(
coup
);
if
(
LOG
.
isDebugEnabled
()){
LOG
.
debug
(
"Sending Heatbeat: "
+
this
.
serverId
+
", Time: "
+
coup
.
getCoupTime
()
+
", ID: "
+
coup
.
getRequestId
());
}
Message
coupMessage
=
coupSync
.
receiveMessage
(
10000
l
);
boolean
newLeader
=
(
coupMessage
==
null
);
if
(
isLeader
()!=
newLeader
){
if
(
newLeader
){
LOG
.
warn
(
"FAILOVER: Node promoted to MASTER."
);
setLeader
(
true
);
this
.
jcrObserver
.
start
();
this
.
timerManager
.
startup
();
}
else
{
LOG
.
warn
(
"FAILOVER: Node demoted to worker."
);
setLeader
(
false
);
this
.
jcrObserver
.
stop
();
this
.
timerManager
.
stopAll
();
}
}
return
isLeader
();
}
public
String
getIdString
(
String
path
,
String
field
,
String
prefix
)
throws
Exception
;
public
synchronized
void
setLeader
(
boolean
leader
)
{
this
.
leader
=
leader
;
}
public
void
setLeader
(
boolean
leader
);
public
synchronized
boolean
isLeader
()
{
return
leader
;
}
public
boolean
isLeader
();
public
long
getStartTime
()
{
return
startTime
;
}
public
String
getServerId
()
{
return
serverId
;
}
public
long
getStartTime
();
public
String
getServerId
();
}
src/main/java/nz/net/orcon/kanban/automation/ClusterManagerImpl.java
0 → 100644
View file @
26d299fc
/**
* GRAVITY WORKFLOW AUTOMATION
* (C) Copyright 2015 Orcon Limited
*
* This file is part of Gravity Workflow Automation.
*
* Gravity Workflow Automation is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Gravity Workflow Automation is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Gravity Workflow Automation.
* If not, see <http://www.gnu.org/licenses/>.
*/
package
nz.net.orcon.kanban.automation
;
import
javax.jms.Message
;
import
javax.jms.ObjectMessage
;
import
nz.net.orcon.kanban.model.Coup
;
import
nz.net.orcon.kanban.model.IdRequest
;
import
nz.net.orcon.kanban.model.IdResponse
;
import
org.apache.commons.lang.RandomStringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.jms.core.JmsTemplate
;
import
org.springframework.scheduling.annotation.Scheduled
;
/**
* The purpose of the Cluster manager is to manage all the functions that need to be
* coordinated across the Cluster.
*
* - Invalidation of Caches.
* - Phase Updates
* - Getting Identifiers.
* - Master Server Protocol
*
* @author peter
*
*/
public
class
ClusterManagerImpl
implements
ClusterManager
{
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
ClusterManagerImpl
.
class
);
private
boolean
leader
;
private
long
startTime
=
System
.
currentTimeMillis
();
private
String
serverId
=
RandomStringUtils
.
random
(
10
,
true
,
true
);
@Autowired
@Qualifier
(
"idJmsTemplate"
)
private
JmsTemplate
idTemplate
;
@Autowired
@Qualifier
(
"idSyncReceiver"
)
private
SyncReceive
idSyncReceiever
;
@Autowired
JcrObserver
jcrObserver
;
@Autowired
TimerManager
timerManager
;
public
Long
getId
(
String
path
,
String
field
)
throws
Exception
{
if
(
LOG
.
isDebugEnabled
()){
LOG
.
debug
(
"Generating ID from "
+
path
+
"."
+
field
);
}
Message
message
=
requestId
(
path
,
field
);
if
(
message
==
null
){
executeChallenge
();
message
=
requestId
(
path
,
field
);
}
ObjectMessage
objectMessage
=
(
ObjectMessage
)
message
;
IdResponse
idResponse
=
(
IdResponse
)
objectMessage
.
getObject
();
return
idResponse
.
getId
();
}
private
Message
requestId
(
String
path
,
String
field
){
String
requestId
=
RandomStringUtils
.
random
(
10
,
true
,
true
);
SyncMessage
idSync
=
idSyncReceiever
.
register
(
requestId
);
IdRequest
request
=
new
IdRequest
();
request
.
setField
(
field
);
request
.
setPath
(
path
);
request
.
setRequestId
(
requestId
);
idTemplate
.
convertAndSend
(
request
);
return
idSync
.
receiveMessage
(
10000
l
);
}
public
String
getIdString
(
String
path
,
String
field
,
String
prefix
)
throws
Exception
{
Long
id
=
getId
(
path
,
field
);
return
prefix
+
id
.
toString
();
}
/**
* Note that this method is simply a wrapper to eat the return value of executeChallenge.
* This is needed to be able to schedule the task.
*
* @throws Exception
*/
@Scheduled
(
fixedDelay
=
30000
l
)
public
void
pollLeader
()
throws
Exception
{
if
(!
isLeader
()){
this
.
executeChallenge
();
}
}
public
synchronized
boolean
executeChallenge
()
throws
Exception
{
SyncMessage
coupSync
=
idSyncReceiever
.
register
(
this
.
serverId
);
Coup
coup
=
new
Coup
();
coup
.
setRequestId
(
this
.
serverId
);
coup
.
setCoupTime
(
this
.
startTime
);
idTemplate
.
convertAndSend
(
coup
);
if
(
LOG
.
isDebugEnabled
()){
LOG
.
debug
(
"Sending Heatbeat: "
+
this
.
serverId
+
", Time: "
+
coup
.
getCoupTime
()
+
", ID: "
+
coup
.
getRequestId
());
}
Message
coupMessage
=
coupSync
.
receiveMessage
(
10000
l
);
boolean
newLeader
=
(
coupMessage
==
null
);
if
(
isLeader
()!=
newLeader
){
if
(
newLeader
){
LOG
.
warn
(
"FAILOVER: Node promoted to MASTER."
);
setLeader
(
true
);
this
.
jcrObserver
.
start
();
this
.
timerManager
.
startup
();
}
else
{
LOG
.
warn
(
"FAILOVER: Node demoted to worker."
);
setLeader
(
false
);
this
.
jcrObserver
.
stop
();
this
.
timerManager
.
stopAll
();
}
}
return
isLeader
();
}
public
synchronized
void
setLeader
(
boolean
leader
)
{
this
.
leader
=
leader
;
}
public
synchronized
boolean
isLeader
()
{
return
leader
;
}
public
long
getStartTime
()
{
return
startTime
;
}
public
String
getServerId
()
{
return
serverId
;
}
}
src/main/java/nz/net/orcon/kanban/automation/JcrObserver.java
View file @
26d299fc
/**
* GRAVITY WORKFLOW AUTOMATION
* (C) Copyright 2015 Orcon Limited
* (C) Copyright 2016 Peter Harrison
*
* This file is part of Gravity Workflow Automation.
*
...
...
@@ -21,8 +22,6 @@
package
nz.net.orcon.kanban.automation
;
import
java.net.UnknownHostException
;
import
javax.annotation.PreDestroy
;
import
nz.net.orcon.kanban.tools.OcmMapperFactory
;
...
...
@@ -33,9 +32,6 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
javax.jcr.LoginException
;
import
javax.jcr.RepositoryException
;
import
javax.jcr.UnsupportedRepositoryOperationException
;
import
javax.jcr.observation.ObservationManager
;
public
class
JcrObserver
{
...
...
@@ -50,7 +46,7 @@ public class JcrObserver {
ObjectContentManager
ocm
;
public
void
start
()
throws
UnsupportedRepositoryOperationException
,
LoginException
,
RepositoryException
,
ClassNotFoundException
,
UnknownHost
Exception
{
public
void
start
()
throws
Exception
{
this
.
ocm
=
ocmFactory
.
getOcm
();
ObservationManager
observationManager
=
ocm
.
getSession
().
getWorkspace
().
getObservationManager
();
final
String
[]
types
=
{
"nt:unstructured"
};
...
...
src/main/java/nz/net/orcon/kanban/controllers/BoardsCache.java
View file @
26d299fc
...
...
@@ -49,15 +49,15 @@ public class BoardsCache extends CacheImpl<Board> implements MessageListener {
@Autowired
@Qualifier
(
"phaseChangeJmsTemplate"
)
JmsTemplate
jmsTemplate
;
private
JmsTemplate
jmsTemplate
;
private
final
Object
lock
=
new
Object
();
@Resource
(
name
=
"ocmFactory"
)
OcmMapperFactory
ocmFactory
;
private
OcmMapperFactory
ocmFactory
;
@Autowired
ListTools
listTools
;
private
ListTools
listTools
;
public
void
incrementCardCount
(
String
boardId
,
String
phaseId
,
int
increment
)
{
PhaseChange
change
=
new
PhaseChange
(
boardId
,
phaseId
,
increment
);
...
...
src/main/java/nz/net/orcon/kanban/controllers/CacheImpl.java
View file @
26d299fc
...
...
@@ -41,12 +41,11 @@ abstract public class CacheImpl<T> implements Cache<T> {
if
(
itemIds
.
length
>
1
){
String
[]
listId
=
new
String
[
itemIds
.
length
-
1
];
for
(
int
a
=
0
;
a
<
itemIds
.
length
;
a
++){
for
(
int
a
=
0
;
a
<
itemIds
.
length
-
1
;
a
++){
listId
[
a
]
=
itemIds
[
a
];
}
this
.
cacheList
.
remove
(
getCacheId
(
listId
));
}
}
@Override
...
...
src/main/java/nz/net/orcon/kanban/controllers/CardController.java
View file @
26d299fc
...
...
@@ -108,8 +108,8 @@ public class CardController {
@Autowired
CardListener
cardListener
;
@Autowired
AutomationEngine
automationEngine
;
//
@Autowired
//
AutomationEngine automationEngine;
@Autowired
private
NotificationController
notificationController
;
...
...
@@ -177,8 +177,9 @@ public class CardController {
CardHolder
cardHolder
=
new
CardHolder
();
cardHolder
.
setBoardId
(
boardId
);
cardHolder
.
setCardId
(
cardId
);
Map
<
String
,
Map
<
String
,
Boolean
>>
explain
=
automationEngine
.
explain
(
cardHolder
);
return
explain
;
//Map<String, Map<String,Boolean>> explain = automationEngine.explain(cardHolder);
//return explain;
return
null
;
}
@PreAuthorize
(
"hasPermission(#boardId, 'BOARD', 'READ,WRITE,ADMIN')"
)
...
...
src/main/java/nz/net/orcon/kanban/controllers/NotificationController.java
View file @
26d299fc
...
...
@@ -138,7 +138,7 @@ public class NotificationController {
}
@RequestMapping
(
value
=
"/{notificationType}"
,
method
=
RequestMethod
.
GET
)
public
@ResponseBody
NotificationTypeMapping
getNotificationTypeMapping
(
@PathVariable
String
notificationType
)
throws
LoginException
,
RepositoryException
,
ClassNotFoundException
,
UnknownHost
Exception
{
public
@ResponseBody
NotificationTypeMapping
getNotificationTypeMapping
(
@PathVariable
String
notificationType
)
throws
Exception
{
ObjectContentManager
ocm
=
ocmFactory
.
getOcm
();
NotificationTypeMapping
notificationTypeMapping
=
null
;
try
{
...
...
src/main/java/nz/net/orcon/kanban/security/SecurityTool.java
View file @
26d299fc
/**
* GRAVITY WORKFLOW AUTOMATION
* (C) Copyright 2015 Orcon Limited
*
* This file is part of Gravity Workflow Automation.
*
* Gravity Workflow Automation is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Gravity Workflow Automation is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Gravity Workflow Automation.
* If not, see <http://www.gnu.org/licenses/>.
*/
package
nz.net.orcon.kanban.security
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
nz.net.orcon.kanban.controllers.TeamCache
;
import
nz.net.orcon.kanban.model.Team
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.GrantedAuthority
;
import
org.springframework.security.core.authority.SimpleGrantedAuthority
;
import
org.springframework.security.core.context.SecurityContext
;
import
org.springframework.security.core.context.SecurityContextHolder
;
public
class
SecurityTool
{
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
SecurityTool
.
class
);
public
interface
SecurityTool
{
public
static
final
String
SYSTEM
=
"system"
;
@Autowired
TeamCache
teamCache
;
public
String
getCurrentUser
()
{
SecurityContext
context
=
SecurityContextHolder
.
getContext
();
Authentication
authentication
=
context
.
getAuthentication
();
Object
principal
=
SYSTEM
;
if
(
authentication
!=
null
){
principal
=
authentication
.
getPrincipal
();
}
return
(
String
)
principal
;
}
public
void
iAmSystem
()
throws
Exception
{
SecurityContext
context
=
SecurityContextHolder
.
getContext
();
Collection
<?
extends
GrantedAuthority
>
authorities
=
this
.
getRoles
(
SYSTEM
);
UsernamePasswordAuthenticationToken
authentication
=
new
UsernamePasswordAuthenticationToken
(
SYSTEM
,
""
,
authorities
);
context
.
setAuthentication
(
authentication
);
}
public
abstract
String
getCurrentUser
();
public
abstract
void
iAmSystem
()
throws
Exception
;
/**
* Determine if the user is authorised based on the supplied roles.
* @param roles
* @param filter
* @return boolean - is the user authorised?
*/
public
boolean
isAuthorised
(
Map
<
String
,
String
>
roles
,
String
filter
){
if
(
roles
==
null
){
return
false
;
}
SecurityContext
context
=
SecurityContextHolder
.
getContext
();
if
(
context
==
null
||
context
.
getAuthentication
()==
null
){
return
false
;
}
String
username
=
(
String
)
context
.
getAuthentication
().
getPrincipal
();
Set
<
String
>
teams
=
new
HashSet
<
String
>();
for
(
GrantedAuthority
authority
:
context
.
getAuthentication
().
getAuthorities
()){
teams
.
add
(
authority
.
getAuthority
());
}
for
(
String
authorised
:
roles
.
keySet
()){
if
(
filter
==
null
||
filter
.
contains
((
roles
.
get
(
authorised
)))){
if
(
username
.
equals
(
authorised
)){
return
true
;
}
if
(
teams
.
contains
(
authorised
)){
return
true
;
}
}
}
LOG
.
warn
(
"Unauthorized: "
+
username
);