2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.security
.core
;
19 import org
.argeo
.ArgeoException
;
20 import org
.argeo
.security
.ArgeoSecurity
;
21 import org
.argeo
.security
.ArgeoSecurityDao
;
22 import org
.argeo
.security
.ArgeoSecurityService
;
23 import org
.argeo
.security
.ArgeoUser
;
24 import org
.argeo
.security
.SimpleArgeoUser
;
25 import org
.springframework
.core
.task
.SimpleAsyncTaskExecutor
;
26 import org
.springframework
.core
.task
.TaskExecutor
;
27 import org
.springframework
.security
.Authentication
;
28 import org
.springframework
.security
.AuthenticationManager
;
29 import org
.springframework
.security
.context
.SecurityContext
;
30 import org
.springframework
.security
.context
.SecurityContextHolder
;
32 public class DefaultSecurityService
implements ArgeoSecurityService
{
33 private ArgeoSecurity argeoSecurity
= new DefaultArgeoSecurity();
34 private ArgeoSecurityDao securityDao
;
35 private AuthenticationManager authenticationManager
;
37 private String systemAuthenticationKey
;
39 public ArgeoUser
getCurrentUser() {
40 ArgeoUser argeoUser
= ArgeoUserDetails
.securityContextUser();
41 if (argeoUser
== null)
43 if (argeoUser
.getRoles().contains(securityDao
.getDefaultRole()))
44 argeoUser
.getRoles().remove(securityDao
.getDefaultRole());
48 public ArgeoSecurityDao
getSecurityDao() {
52 public void newRole(String role
) {
53 securityDao
.createRole(role
, argeoSecurity
.getSuperUsername());
56 public void updateUserPassword(String username
, String password
) {
57 SimpleArgeoUser user
= new SimpleArgeoUser(
58 securityDao
.getUser(username
));
59 user
.setPassword(password
);
60 securityDao
.update(user
);
63 public void updateCurrentUserPassword(String oldPassword
, String newPassword
) {
64 SimpleArgeoUser user
= new SimpleArgeoUser(getCurrentUser());
65 if (!securityDao
.isPasswordValid(user
.getPassword(), oldPassword
))
66 throw new ArgeoException("Old password is not correct.");
67 user
.setPassword(securityDao
.encodePassword(newPassword
));
68 securityDao
.update(user
);
71 public void newUser(ArgeoUser user
) {
72 user
.getUserNatures().clear();
73 argeoSecurity
.beforeCreate(user
);
74 securityDao
.create(user
);
77 public void updateUser(ArgeoUser user
) {
78 String password
= securityDao
.getUserWithPassword(user
.getUsername())
80 SimpleArgeoUser simpleArgeoUser
= new SimpleArgeoUser(user
);
81 simpleArgeoUser
.setPassword(password
);
82 securityDao
.update(simpleArgeoUser
);
85 public TaskExecutor
createSystemAuthenticatedTaskExecutor() {
86 return new SimpleAsyncTaskExecutor() {
87 private static final long serialVersionUID
= -8126773862193265020L;
90 public Thread
createThread(Runnable runnable
) {
92 .createThread(wrapWithSystemAuthentication(runnable
));
99 * Wraps another runnable, adding security context <br/>
100 * TODO: secure the call to this method with Java Security
102 public Runnable
wrapWithSystemAuthentication(final Runnable runnable
) {
103 return new Runnable() {
106 SecurityContext securityContext
= SecurityContextHolder
108 Authentication auth
= authenticationManager
109 .authenticate(new InternalAuthentication(
110 systemAuthenticationKey
));
111 securityContext
.setAuthentication(auth
);
118 public void setArgeoSecurity(ArgeoSecurity argeoSecurity
) {
119 this.argeoSecurity
= argeoSecurity
;
122 public void setSecurityDao(ArgeoSecurityDao dao
) {
123 this.securityDao
= dao
;
126 public void setAuthenticationManager(
127 AuthenticationManager authenticationManager
) {
128 this.authenticationManager
= authenticationManager
;
131 public void setSystemAuthenticationKey(String systemAuthenticationKey
) {
132 this.systemAuthenticationKey
= systemAuthenticationKey
;