Update logged in user.
authorMathieu Baudier <mbaudier@argeo.org>
Mon, 8 Nov 2010 15:22:11 +0000 (15:22 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Mon, 8 Nov 2010 15:22:11 +0000 (15:22 +0000)
Add PosixAccount nature

git-svn-id: https://svn.argeo.org/commons/trunk@3858 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

security/modules/org.argeo.security.manager.ldap/META-INF/spring/natures-osgi.xml
security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/ArgeoSecurityDao.java
security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/ArgeoSecurityService.java
security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/ArgeoUserDetails.java
security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/DefaultSecurityService.java
security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/ldap/ArgeoSecurityDaoLdap.java
security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/ldap/nature/SimpleUserNatureMapper.java
security/runtime/org.argeo.security.mvc/pom.xml
security/runtime/org.argeo.security.mvc/src/main/java/org/argeo/security/mvc/ArgeoUserInterceptor.java
security/runtime/org.argeo.security.mvc/src/main/java/org/argeo/security/mvc/UsersRolesController.java

index 9a3cc743d67ff55fd4954e7a45d5df13b50f4e1e..6f7c1e5cb7ed4762ba3a22d1e9a46a1312aa69d9 100644 (file)
@@ -7,11 +7,11 @@
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">\r
 \r
        <service interface="org.argeo.security.ldap.UserNatureMapper">\r
-               <beans:bean class="org.argeo.security.ldap.nature.SimpleUserNatureMapper" />\r
+               <beans:bean name="simpleUser" class="org.argeo.security.ldap.nature.SimpleUserNatureMapper" />\r
        </service>\r
 \r
        <service interface="org.argeo.security.ldap.UserNatureMapper">\r
-               <beans:bean class="org.argeo.security.ldap.nature.CoworkerUserNatureMapper" />\r
+               <beans:bean name="coworker" class="org.argeo.security.ldap.nature.CoworkerUserNatureMapper" />\r
        </service>\r
 \r
 </beans:beans>
\ No newline at end of file
index d8412da8e002e7290fc40a1655e1143a9f1dc1bc..7156a93eaccd3a25a11dc0a0bb51a03ea186ce1e 100644 (file)
@@ -19,7 +19,7 @@ package org.argeo.security;
 import java.util.List;
 
 public interface ArgeoSecurityDao {
-       public ArgeoUser getCurrentUser();
+//     public ArgeoUser getCurrentUser();
 
        public List<ArgeoUser> listUsers();
 
@@ -40,4 +40,6 @@ public interface ArgeoSecurityDao {
        public ArgeoUser getUser(String username);
 
        public ArgeoUser getUserWithPassword(String username);
+       
+       public String getDefaultRole();
 }
index 75f849704014bfc91741ba5d6a2011ea9513a40e..046c689fe040f269051f04e5fb9b66eb61aa7eb5 100644 (file)
@@ -17,6 +17,8 @@
 package org.argeo.security;
 
 public interface ArgeoSecurityService {
+       public ArgeoUser getCurrentUser();
+
        public void newUser(ArgeoUser argeoUser);
 
        public void updateUser(ArgeoUser user);
index 1948d1252ae24a8d92cb6d81106d524e875bd507..df16008e0da61607dd92c73d1d6dcb793aad2efa 100644 (file)
@@ -69,7 +69,7 @@ public class ArgeoUserDetails extends User implements ArgeoUser {
        }
 
        /** The provided list, for chaining using {@link Collections} */
-       protected static List<String> addAuthoritiesToRoles(
+       public static List<String> addAuthoritiesToRoles(
                        GrantedAuthority[] authorities, List<String> roles) {
                for (GrantedAuthority authority : authorities) {
                        roles.add(authority.getAuthority());
@@ -77,7 +77,7 @@ public class ArgeoUserDetails extends User implements ArgeoUser {
                return roles;
        }
 
-       protected static GrantedAuthority[] rolesToAuthorities(List<String> roles) {
+       public static GrantedAuthority[] rolesToAuthorities(List<String> roles) {
                GrantedAuthority[] arr = new GrantedAuthority[roles.size()];
                for (int i = 0; i < roles.size(); i++) {
                        String role = roles.get(i);
index 28f399f5a240078f2d8ef4531926cab390380909..23e2372c8a5b1adb171c7e5a462d3ddad0e62be3 100644 (file)
@@ -36,6 +36,15 @@ public class DefaultSecurityService implements ArgeoSecurityService {
 
        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;
        }
@@ -45,14 +54,14 @@ 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(securityDao.getCurrentUser());
+               SimpleArgeoUser user = new SimpleArgeoUser(getCurrentUser());
                if (!user.getPassword().equals(oldPassword))
                        throw new ArgeoException("Old password is not correct.");
                user.setPassword(newPassword);
@@ -122,5 +131,4 @@ public class DefaultSecurityService implements ArgeoSecurityService {
        public void setSystemAuthenticationKey(String systemAuthenticationKey) {
                this.systemAuthenticationKey = systemAuthenticationKey;
        }
-
 }
index f9628c3b2984e2ecc1711000220c4fd9b1509304..3d6d8ff9949dd9c22664abb6788ae508e5a55a73 100644 (file)
@@ -36,7 +36,6 @@ import org.springframework.ldap.core.DirContextAdapter;
 import org.springframework.ldap.core.DistinguishedName;
 import org.springframework.ldap.core.LdapTemplate;
 import org.springframework.ldap.core.support.BaseLdapPathContextSource;
-import org.springframework.security.Authentication;
 import org.springframework.security.context.SecurityContextHolder;
 import org.springframework.security.ldap.DefaultLdapUsernameToDnMapper;
 import org.springframework.security.ldap.LdapAuthoritiesPopulator;
@@ -44,6 +43,7 @@ import org.springframework.security.ldap.LdapUsernameToDnMapper;
 import org.springframework.security.ldap.LdapUtils;
 import org.springframework.security.ldap.populator.DefaultLdapAuthoritiesPopulator;
 import org.springframework.security.ldap.search.FilterBasedLdapUserSearch;
+import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
 import org.springframework.security.userdetails.UserDetails;
 import org.springframework.security.userdetails.UserDetailsManager;
 import org.springframework.security.userdetails.UserDetailsService;
@@ -118,31 +118,31 @@ public class ArgeoSecurityDaoLdap implements ArgeoSecurityDao, InitializingBean
                ldapTemplate = new LdapTemplate(this.contextSource);
        }
 
-       public void create(ArgeoUser user) {
+       public synchronized void create(ArgeoUser user) {
                userDetailsManager.createUser(new ArgeoUserDetails(user));
        }
 
-       public ArgeoUser getUser(String uname) {
+       public synchronized ArgeoUser getUser(String uname) {
                SimpleArgeoUser user = createSimpleArgeoUser(getDetails(uname));
                user.setPassword(null);
                return user;
        }
 
-       public ArgeoUser getUserWithPassword(String uname) {
+       public synchronized ArgeoUser getUserWithPassword(String uname) {
                return createSimpleArgeoUser(getDetails(uname));
        }
 
-       public ArgeoUser getCurrentUser() {
-               ArgeoUser argeoUser = ArgeoUserDetails.securityContextUser();
-               if (argeoUser == null)
-                       return null;
-               if (argeoUser.getRoles().contains(defaultRole))
-                       argeoUser.getRoles().remove(defaultRole);
-               return argeoUser;
-       }
+//     public ArgeoUser getCurrentUser() {
+//             ArgeoUser argeoUser = ArgeoUserDetails.securityContextUser();
+//             if (argeoUser == null)
+//                     return null;
+//             if (argeoUser.getRoles().contains(defaultRole))
+//                     argeoUser.getRoles().remove(defaultRole);
+//             return argeoUser;
+//     }
 
        @SuppressWarnings("unchecked")
-       public List<ArgeoUser> listUsers() {
+       public synchronized List<ArgeoUser> listUsers() {
                List<String> usernames = (List<String>) ldapTemplate.listBindings(
                                new DistinguishedName(userBase), new ContextMapper() {
                                        public Object mapFromContext(Object ctxArg) {
@@ -171,15 +171,23 @@ public class ArgeoSecurityDaoLdap implements ArgeoSecurityDao, InitializingBean
                                });
        }
 
-       public void update(ArgeoUser user) {
+       public synchronized void update(ArgeoUser user) {
+               ArgeoUserDetails argeoUserDetails = new ArgeoUserDetails(user);
                userDetailsManager.updateUser(new ArgeoUserDetails(user));
+               // refresh logged in user
+               if (ArgeoUserDetails.securityContextUser().getUsername()
+                               .equals(argeoUserDetails.getUsername())) {
+                       SecurityContextHolder.getContext().setAuthentication(
+                                       new UsernamePasswordAuthenticationToken(argeoUserDetails,
+                                                       null, argeoUserDetails.getAuthorities()));
+               }
        }
 
-       public void delete(String username) {
+       public synchronized void delete(String username) {
                userDetailsManager.deleteUser(username);
        }
 
-       public Boolean userExists(String username) {
+       public synchronized Boolean userExists(String username) {
                return userDetailsManager.userExists(username);
        }
 
index 60ccafbb9bb5336e4805fa82d2a8b27bc94bd228..56da470056de67ba5184548140fac8ce4f709bb2 100644 (file)
@@ -24,7 +24,7 @@ import org.springframework.ldap.core.DirContextOperations;
 
 public class SimpleUserNatureMapper implements UserNatureMapper {
        public String getName() {
-               return "simple";
+               return "simpleUser";
        }
 
        public UserNature mapUserInfoFromContext(DirContextOperations ctx) {
index e2a9bcd28cd5c7262f33a05e9b3b87ac5913f7bd..e38aac5cbbde3d4f92a9379e758109761e9eae2b 100644 (file)
@@ -1,4 +1,5 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
                <groupId>org.argeo.commons.security</groupId>
@@ -31,6 +32,7 @@
                                                <Export-Package>
                                                        org.argeo.security.mvc.*
                                                </Export-Package>
+                                               <Import-Package>*,javax.servlet</Import-Package>
                                        </instructions>
                                </configuration>
                        </plugin>
                        <artifactId>org.argeo.server.core</artifactId>
                        <version>0.2.1-SNAPSHOT</version>
                </dependency>
-       
+
                <!-- Argeo Security -->
                <dependency>
                        <groupId>org.argeo.commons.security</groupId>
                        <artifactId>org.argeo.security.core</artifactId>
                        <version>0.2.1-SNAPSHOT</version>
                </dependency>
-       
+
                <!-- Spring -->
                <dependency>
                        <groupId>org.springframework</groupId>
index 74e97a53a27b470a696bf95d36b6d79f059c24ef..d79a70360f992c740569ac7a9fd9f198c7e6171a 100644 (file)
@@ -13,8 +13,7 @@ public class ArgeoUserInterceptor extends HandlerInterceptorAdapter {
        @Override
        public boolean preHandle(HttpServletRequest request,
                        HttpServletResponse response, Object handler) throws Exception {
-               request.setAttribute("argeoUser", securityService.getSecurityDao()
-                               .getCurrentUser());
+               request.setAttribute("argeoUser", securityService.getCurrentUser());
                return super.preHandle(request, response, handler);
        }
 
index 4d59fc5007710925f6ef9ea5a592ca0660a1e9c8..cd954644aecff4ddb4e266efcf85fe2aad3173b2 100644 (file)
@@ -50,7 +50,7 @@ public class UsersRolesController implements MvcConstants {
        @RequestMapping("/getCredentials.*")
        @ModelAttribute("user")
        public ArgeoUser getCredentials() {
-               ArgeoUser argeoUser = securityService.getSecurityDao().getCurrentUser();
+               ArgeoUser argeoUser = securityService.getCurrentUser();
                if (argeoUser == null)
                        return new SimpleArgeoUser();
                else
@@ -92,7 +92,7 @@ public class UsersRolesController implements MvcConstants {
        @ModelAttribute("user")
        /** Will only update the user natures.*/
        public ArgeoUser updateUserSelf(Reader reader) {
-               ArgeoUser user = securityService.getSecurityDao().getCurrentUser();
+               ArgeoUser user = securityService.getCurrentUser();
                ArgeoUser userForNatures = userDeserializer.deserialize(reader,
                                SimpleArgeoUser.class);
                user.updateUserNatures(userForNatures.getUserNatures());