]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/DefaultSecurityService.java
Improve Security
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / core / DefaultSecurityService.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.security.core;
18
19 import java.util.Iterator;
20 import java.util.List;
21
22 import org.argeo.ArgeoException;
23 import org.argeo.security.ArgeoSecurity;
24 import org.argeo.security.ArgeoSecurityDao;
25 import org.argeo.security.ArgeoSecurityService;
26 import org.argeo.security.ArgeoUser;
27 import org.argeo.security.SimpleArgeoUser;
28 import org.springframework.core.task.SimpleAsyncTaskExecutor;
29 import org.springframework.core.task.TaskExecutor;
30 import org.springframework.security.Authentication;
31 import org.springframework.security.AuthenticationManager;
32 import org.springframework.security.context.SecurityContext;
33 import org.springframework.security.context.SecurityContextHolder;
34
35 public class DefaultSecurityService implements ArgeoSecurityService {
36 private ArgeoSecurity argeoSecurity = new DefaultArgeoSecurity();
37 private ArgeoSecurityDao securityDao;
38 private AuthenticationManager authenticationManager;
39
40 private String systemAuthenticationKey;
41
42 public ArgeoUser getCurrentUser() {
43 ArgeoUser argeoUser = ArgeoUserDetails.securityContextUser();
44 if (argeoUser == null)
45 return null;
46 if (argeoUser.getRoles().contains(securityDao.getDefaultRole()))
47 argeoUser.getRoles().remove(securityDao.getDefaultRole());
48 return argeoUser;
49 }
50
51 public ArgeoSecurityDao getSecurityDao() {
52 return securityDao;
53 }
54
55 public void newRole(String role) {
56 securityDao.createRole(role, argeoSecurity.getSuperUsername());
57 }
58
59 public void updateUserPassword(String username, String password) {
60 SimpleArgeoUser user = new SimpleArgeoUser(
61 securityDao.getUser(username));
62 user.setPassword(password);
63 securityDao.update(user);
64 }
65
66 public void updateCurrentUserPassword(String oldPassword, String newPassword) {
67 SimpleArgeoUser user = new SimpleArgeoUser(getCurrentUser());
68 if (!securityDao.isPasswordValid(user.getPassword(), oldPassword))
69 throw new ArgeoException("Old password is not correct.");
70 user.setPassword(securityDao.encodePassword(newPassword));
71 securityDao.update(user);
72 }
73
74 public void newUser(ArgeoUser user) {
75 user.getUserNatures().clear();
76 argeoSecurity.beforeCreate(user);
77 securityDao.create(user);
78 }
79
80 public void updateUser(ArgeoUser user) {
81 String password = securityDao.getUserWithPassword(user.getUsername())
82 .getPassword();
83 SimpleArgeoUser simpleArgeoUser = new SimpleArgeoUser(user);
84 simpleArgeoUser.setPassword(password);
85 securityDao.update(simpleArgeoUser);
86 }
87
88 public TaskExecutor createSystemAuthenticatedTaskExecutor() {
89 return new SimpleAsyncTaskExecutor() {
90 private static final long serialVersionUID = -8126773862193265020L;
91
92 @Override
93 public Thread createThread(Runnable runnable) {
94 return super
95 .createThread(wrapWithSystemAuthentication(runnable));
96 }
97
98 };
99 }
100
101 /**
102 * Wraps another runnable, adding security context <br/>
103 * TODO: secure the call to this method with Java Security
104 */
105 public Runnable wrapWithSystemAuthentication(final Runnable runnable) {
106 return new Runnable() {
107
108 public void run() {
109 SecurityContext securityContext = SecurityContextHolder
110 .getContext();
111 Authentication auth = authenticationManager
112 .authenticate(new InternalAuthentication(
113 systemAuthenticationKey));
114 securityContext.setAuthentication(auth);
115
116 runnable.run();
117 }
118 };
119 }
120
121 public List<ArgeoUser> listUsersInRole(String role) {
122 List<ArgeoUser> lst = securityDao.listUsersInRole(role);
123 Iterator<ArgeoUser> it = lst.iterator();
124 while (it.hasNext()) {
125 if (it.next().getUsername()
126 .equals(argeoSecurity.getSuperUsername())) {
127 it.remove();
128 break;
129 }
130 }
131 return lst;
132 }
133
134 public void setArgeoSecurity(ArgeoSecurity argeoSecurity) {
135 this.argeoSecurity = argeoSecurity;
136 }
137
138 public void setSecurityDao(ArgeoSecurityDao dao) {
139 this.securityDao = dao;
140 }
141
142 public void setAuthenticationManager(
143 AuthenticationManager authenticationManager) {
144 this.authenticationManager = authenticationManager;
145 }
146
147 public void setSystemAuthenticationKey(String systemAuthenticationKey) {
148 this.systemAuthenticationKey = systemAuthenticationKey;
149 }
150 }