2013-02-17 21:00:08 -03:00
|
|
|
package com.wisemapping.security.ldap;
|
|
|
|
|
|
|
|
|
|
|
|
import com.wisemapping.exceptions.WiseMappingException;
|
2013-03-17 23:17:55 -03:00
|
|
|
import com.wisemapping.model.AuthenticationType;
|
2013-02-17 21:00:08 -03:00
|
|
|
import com.wisemapping.model.User;
|
|
|
|
import com.wisemapping.security.UserDetails;
|
|
|
|
import com.wisemapping.service.UserService;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import org.springframework.ldap.core.DirContextAdapter;
|
|
|
|
import org.springframework.ldap.core.DirContextOperations;
|
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
|
|
import org.springframework.security.ldap.userdetails.UserDetailsContextMapper;
|
|
|
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Collection;
|
|
|
|
|
|
|
|
public class LdapUserDetailsContextMapper implements UserDetailsContextMapper {
|
|
|
|
|
|
|
|
private UserService userService;
|
|
|
|
private String adminUser;
|
2013-02-24 17:46:05 -03:00
|
|
|
private String ldapAttributeFirstName;
|
|
|
|
private String ldapAttributeLastName;
|
2013-02-17 21:00:08 -03:00
|
|
|
|
|
|
|
|
|
|
|
public UserService getUserService() {
|
|
|
|
return userService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUserService(UserService userService) {
|
|
|
|
this.userService = userService;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private boolean isAdmin(@Nullable String email) {
|
|
|
|
return email != null && adminUser != null && email.trim().endsWith(adminUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getAdminUser() {
|
|
|
|
return adminUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAdminUser(String adminUser) {
|
|
|
|
this.adminUser = adminUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public UserDetails mapUserFromContext(@NotNull DirContextOperations userData,
|
|
|
|
String email, Collection<? extends GrantedAuthority> arg2) {
|
|
|
|
User user = userService.getUserBy(email);
|
|
|
|
if (user == null) {
|
|
|
|
// If the user was not found in the database, create a new one ...
|
|
|
|
user = new User();
|
|
|
|
user.setEmail(email);
|
|
|
|
|
2013-02-24 17:46:05 -03:00
|
|
|
final String firstName = userData.getStringAttribute(ldapAttributeFirstName);
|
2013-02-17 21:00:08 -03:00
|
|
|
user.setFirstname(firstName);
|
|
|
|
|
2013-02-24 17:46:05 -03:00
|
|
|
final String lastName = userData.getStringAttribute(ldapAttributeLastName);
|
2013-02-17 21:00:08 -03:00
|
|
|
user.setLastname(lastName);
|
|
|
|
|
|
|
|
user.setPassword(email);
|
|
|
|
final Calendar now = Calendar.getInstance();
|
|
|
|
user.setActivationDate(now);
|
|
|
|
|
|
|
|
try {
|
2013-03-17 23:17:55 -03:00
|
|
|
user.setAuthenticationType(AuthenticationType.LDAP);
|
2013-03-10 19:06:55 -03:00
|
|
|
user = userService.createUser(user, false, false);
|
2013-02-17 21:00:08 -03:00
|
|
|
} catch (WiseMappingException e) {
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new UserDetails(user, isAdmin(email));
|
|
|
|
}
|
|
|
|
|
2013-02-24 17:46:05 -03:00
|
|
|
public String getLdapAttributeLastName() {
|
|
|
|
return ldapAttributeLastName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLdapAttributeLastName(String ldapAttributLastName) {
|
|
|
|
this.ldapAttributeLastName = ldapAttributLastName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getLdapAttrbutFirstName() {
|
|
|
|
return ldapAttributeFirstName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLdapAttributeFirstName(String ldapAttributeFirstName) {
|
|
|
|
this.ldapAttributeFirstName = ldapAttributeFirstName;
|
|
|
|
}
|
|
|
|
|
2013-02-17 21:00:08 -03:00
|
|
|
@Override
|
|
|
|
public void mapUserToContext(org.springframework.security.core.userdetails.UserDetails userDetails, DirContextAdapter dirContextAdapter) {
|
|
|
|
// To be implemented ...
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|