Introduce security core
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / ldap / ArgeoUserDetailsContextMapper.java
diff --git a/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/ldap/ArgeoUserDetailsContextMapper.java b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/ldap/ArgeoUserDetailsContextMapper.java
new file mode 100644 (file)
index 0000000..25b5c6e
--- /dev/null
@@ -0,0 +1,55 @@
+package org.argeo.security.ldap;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.argeo.security.ArgeoUser;
+import org.argeo.security.UserNature;
+import org.argeo.security.core.ArgeoUserDetails;
+import org.springframework.ldap.core.DirContextAdapter;
+import org.springframework.ldap.core.DirContextOperations;
+import org.springframework.security.GrantedAuthority;
+import org.springframework.security.userdetails.UserDetails;
+import org.springframework.security.userdetails.ldap.UserDetailsContextMapper;
+
+public class ArgeoUserDetailsContextMapper implements UserDetailsContextMapper {
+       private List<UserNatureMapper> userInfoMappers = new ArrayList<UserNatureMapper>();
+
+       public UserDetails mapUserFromContext(DirContextOperations ctx,
+                       String username, GrantedAuthority[] authorities) {
+               byte[] arr = (byte[]) ctx.getAttributeSortedStringSet("userPassword")
+                               .first();
+               String password = new String(arr);
+
+               List<UserNature> userInfos = new ArrayList<UserNature>();
+               for (UserNatureMapper userInfoMapper : userInfoMappers) {
+                       userInfos.add(userInfoMapper.mapUserInfoFromContext(ctx));
+               }
+
+               return new ArgeoUserDetails(username, Collections
+                               .unmodifiableList(userInfos), password, authorities);
+       }
+
+       public void mapUserToContext(UserDetails user, DirContextAdapter ctx) {
+               ctx.setAttributeValues("objectClass", new String[] { "inetOrgPerson" });
+               ctx.setAttributeValue("uid", user.getUsername());
+               ctx.setAttributeValue("userPassword", user.getPassword());
+               if (user instanceof ArgeoUser) {
+                       ArgeoUser argeoUser = (ArgeoUser) user;
+                       for (UserNature userInfo : argeoUser.getUserNatures()) {
+                               for (UserNatureMapper userInfoMapper : userInfoMappers) {
+                                       if (userInfoMapper.supports(userInfo)) {
+                                               userInfoMapper.mapUserInfoToContext(userInfo, ctx);
+                                               break;// use the first mapper found an no others
+                                       }
+                               }
+                       }
+               }
+       }
+
+       public void setUserInfoMappers(List<UserNatureMapper> userInfoMappers) {
+               this.userInfoMappers = userInfoMappers;
+       }
+
+}