]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/KeyBasedSystemExecutionService.java
GIS does not expose perspectives and views
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / core / KeyBasedSystemExecutionService.java
1 package org.argeo.security.core;
2
3 import org.argeo.security.SystemExecutionService;
4 import org.springframework.core.task.SimpleAsyncTaskExecutor;
5 import org.springframework.core.task.TaskExecutor;
6 import org.springframework.security.Authentication;
7 import org.springframework.security.AuthenticationManager;
8 import org.springframework.security.context.SecurityContext;
9 import org.springframework.security.context.SecurityContextHolder;
10
11 public class KeyBasedSystemExecutionService implements SystemExecutionService {
12 private AuthenticationManager authenticationManager;
13 private String systemAuthenticationKey;
14
15 public void executeAsSystem(Runnable runnable) {
16 wrapWithSystemAuthentication(runnable).run();
17 }
18
19 public TaskExecutor createSystemAuthenticatedTaskExecutor() {
20 return new SimpleAsyncTaskExecutor() {
21 private static final long serialVersionUID = -8126773862193265020L;
22
23 @Override
24 public Thread createThread(Runnable runnable) {
25 return super
26 .createThread(wrapWithSystemAuthentication(runnable));
27 }
28
29 };
30 }
31
32 protected Runnable wrapWithSystemAuthentication(final Runnable runnable) {
33 return new Runnable() {
34
35 public void run() {
36 SecurityContext securityContext = SecurityContextHolder
37 .getContext();
38 Authentication auth = authenticationManager
39 .authenticate(new InternalAuthentication(
40 systemAuthenticationKey));
41 securityContext.setAuthentication(auth);
42
43 runnable.run();
44 }
45 };
46 }
47
48 public void setAuthenticationManager(
49 AuthenticationManager authenticationManager) {
50 this.authenticationManager = authenticationManager;
51 }
52
53 public void setSystemAuthenticationKey(String systemAuthenticationKey) {
54 this.systemAuthenticationKey = systemAuthenticationKey;
55 }
56
57 }