]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/DefaultSecurityService.java
Make security UI more robust
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / core / DefaultSecurityService.java
index 74aa57cd50b8759f047890b51f324be21c13e93f..d6ff69cc23b549632e26edfbe4f93a7ac081b0ad 100644 (file)
@@ -1,14 +1,52 @@
+/*
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package org.argeo.security.core;
 
+import java.util.Iterator;
+import java.util.List;
+
+import org.argeo.ArgeoException;
 import org.argeo.security.ArgeoSecurity;
 import org.argeo.security.ArgeoSecurityDao;
 import org.argeo.security.ArgeoSecurityService;
 import org.argeo.security.ArgeoUser;
 import org.argeo.security.SimpleArgeoUser;
+import org.springframework.core.task.SimpleAsyncTaskExecutor;
+import org.springframework.core.task.TaskExecutor;
+import org.springframework.security.Authentication;
+import org.springframework.security.AuthenticationManager;
+import org.springframework.security.context.SecurityContext;
+import org.springframework.security.context.SecurityContextHolder;
 
 public class DefaultSecurityService implements ArgeoSecurityService {
        private ArgeoSecurity argeoSecurity = new DefaultArgeoSecurity();
        private ArgeoSecurityDao securityDao;
+       private AuthenticationManager authenticationManager;
+
+       private String systemAuthenticationKey;
+
+       public ArgeoUser getCurrentUser() {
+               ArgeoUser argeoUser = ArgeoUserDetails.securityContextUser();
+               if (argeoUser == null)
+                       return null;
+               if (argeoUser.getRoles().contains(securityDao.getDefaultRole()))
+                       argeoUser.getRoles().remove(securityDao.getDefaultRole());
+               return argeoUser;
+       }
 
        public ArgeoSecurityDao getSecurityDao() {
                return securityDao;
@@ -19,14 +57,22 @@ public class DefaultSecurityService implements ArgeoSecurityService {
        }
 
        public void updateUserPassword(String username, String password) {
-               SimpleArgeoUser user = new SimpleArgeoUser(securityDao
-                               .getUser(username));
+               SimpleArgeoUser user = new SimpleArgeoUser(
+                               securityDao.getUser(username));
                user.setPassword(password);
                securityDao.update(user);
        }
 
+       public void updateCurrentUserPassword(String oldPassword, String newPassword) {
+               SimpleArgeoUser user = new SimpleArgeoUser(getCurrentUser());
+               if (!securityDao.isPasswordValid(user.getPassword(), oldPassword))
+                       throw new ArgeoException("Old password is not correct.");
+               user.setPassword(securityDao.encodePassword(newPassword));
+               securityDao.update(user);
+       }
+
        public void newUser(ArgeoUser user) {
-               user.getUserNatures().clear();
+//             user.getUserNatures().clear();
                argeoSecurity.beforeCreate(user);
                securityDao.create(user);
        }
@@ -36,7 +82,53 @@ public class DefaultSecurityService implements ArgeoSecurityService {
                                .getPassword();
                SimpleArgeoUser simpleArgeoUser = new SimpleArgeoUser(user);
                simpleArgeoUser.setPassword(password);
-               securityDao.update(user);
+               securityDao.update(simpleArgeoUser);
+       }
+
+       public TaskExecutor createSystemAuthenticatedTaskExecutor() {
+               return new SimpleAsyncTaskExecutor() {
+                       private static final long serialVersionUID = -8126773862193265020L;
+
+                       @Override
+                       public Thread createThread(Runnable runnable) {
+                               return super
+                                               .createThread(wrapWithSystemAuthentication(runnable));
+                       }
+
+               };
+       }
+
+       /**
+        * Wraps another runnable, adding security context <br/>
+        * TODO: secure the call to this method with Java Security
+        */
+       public Runnable wrapWithSystemAuthentication(final Runnable runnable) {
+               return new Runnable() {
+
+                       public void run() {
+                               SecurityContext securityContext = SecurityContextHolder
+                                               .getContext();
+                               Authentication auth = authenticationManager
+                                               .authenticate(new InternalAuthentication(
+                                                               systemAuthenticationKey));
+                               securityContext.setAuthentication(auth);
+
+                               runnable.run();
+                       }
+               };
+       }
+
+       public List<ArgeoUser> listUsersInRole(String role) {
+               List<ArgeoUser> lst = securityDao.listUsersInRole(role);
+               Iterator<ArgeoUser> it = lst.iterator();
+               while (it.hasNext()) {
+                       if (it.next().getUsername()
+                                       .equals(argeoSecurity.getSuperUsername())) {
+                               it.remove();
+                               break;
+                       }
+               }
+               return lst;
        }
 
        public void setArgeoSecurity(ArgeoSecurity argeoSecurity) {
@@ -47,4 +139,12 @@ public class DefaultSecurityService implements ArgeoSecurityService {
                this.securityDao = dao;
        }
 
+       public void setAuthenticationManager(
+                       AuthenticationManager authenticationManager) {
+               this.authenticationManager = authenticationManager;
+       }
+
+       public void setSystemAuthenticationKey(String systemAuthenticationKey) {
+               this.systemAuthenticationKey = systemAuthenticationKey;
+       }
 }