Skip to content

Commit

Permalink
Merge pull request #186 from GEBIT/ldap_jpegPhoto_as_user_avatar
Browse files Browse the repository at this point in the history
Fetch jpegPhoto or thumbnailPhoto as avatar image from LDAP
  • Loading branch information
csokol committed Aug 13, 2015
2 parents 84db250 + b425403 commit 4454302
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/main/java/org/mamute/auth/LDAPApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import static org.apache.commons.lang.StringUtils.isNotEmpty;
import static org.mamute.model.SanitizedText.fromTrustedText;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.naming.directory.InvalidAttributeValueException;

import org.apache.directory.api.ldap.model.cursor.CursorException;
import org.apache.directory.api.ldap.model.cursor.EntryCursor;
Expand All @@ -23,12 +25,16 @@
import org.apache.directory.ldap.client.api.LdapNetworkConnection;
import org.mamute.dao.LoginMethodDAO;
import org.mamute.dao.UserDAO;
import org.mamute.filesystem.ImageStore;
import org.mamute.infra.ClientIp;
import org.mamute.model.Attachment;
import org.mamute.model.LoginMethod;
import org.mamute.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.caelum.vraptor.environment.Environment;
import br.com.caelum.vraptor.observer.upload.DefaultUploadedFile;

/**
* LDAP authentication API
Expand All @@ -50,10 +56,14 @@ public class LDAPApi {
public static final String LDAP_MODERATOR_GROUP = "ldap.moderatorGroup";
public static final String PLACHOLDER_PASSWORD = "ldap-password-ignore-me";
public static final String LDAP_USE_SSL = "ldap.useSSL";
public static final String LDAP_AVATAR_IMAGE = "ldap.avatarImageAttr";

@Inject private Environment env;
@Inject private UserDAO users;
@Inject private LoginMethodDAO loginMethods;
@Inject private ImageStore imageStore;
@Inject private ClientIp clientIp;


private String host;
private Integer port;
Expand All @@ -67,6 +77,7 @@ public class LDAPApi {
private String[] lookupAttrs;
private String moderatorGroup;
private Boolean useSsl;
private String avatarImageAttr;

/**
* Ensure that required variables are set if LDAP auth
Expand All @@ -90,6 +101,7 @@ public void init() {
moderatorGroup = env.get(LDAP_MODERATOR_GROUP, "");
lookupAttrs = env.get(LDAP_LOOKUP, "").split(",");
useSsl = env.supports(LDAP_USE_SSL);
avatarImageAttr = env.get(LDAP_AVATAR_IMAGE, "");
}
}

Expand Down Expand Up @@ -153,6 +165,36 @@ private String userCn(String username) {
return cn;
}

private void updateAvatarImage(LDAPResource ldap, Entry entry, User user) {
try {
byte[] jpegBytes = getAvatarImage(ldap, entry);
if (jpegBytes != null) {
String fileName = user.getEmail() + ".jpg";
DefaultUploadedFile avatar = new DefaultUploadedFile(new ByteArrayInputStream(jpegBytes), fileName, "image/jpeg", jpegBytes.length);
Attachment attachment = imageStore.processAndStore(avatar, user, clientIp);
Attachment old = user.getAvatar();
if (old != null) {
imageStore.delete(old);
}
user.setAvatar(attachment);
}
} catch (LdapException | IOException e) {
// problems with avatar processing are non-fatal
logger.warn("Error updating user avatar from LDAP: " + user.getName(), e);
}
}

private byte[] getAvatarImage(LDAPResource ldap, Entry entry) throws LdapException {
if (avatarImageAttr != null && avatarImageAttr.length() > 0) {
try {
return ldap.getByteAttribute(entry, avatarImageAttr);
} catch (InvalidAttributeValueException ex) {
throw new LdapException("Invalid attribute value while looking up " + avatarImageAttr, ex);
}
}
return null;
}

private void createUserIfNeeded(LDAPResource ldap, String cn) throws LdapException {
Entry ldapUser = ldap.getUser(cn);
String email = ldap.getAttribute(ldapUser, emailAttr);
Expand All @@ -179,6 +221,7 @@ private void createUserIfNeeded(LDAPResource ldap, String cn) throws LdapExcepti
} else {
user.removeModerator();
}
updateAvatarImage(ldap, ldapUser, user);

users.save(user);
}
Expand Down Expand Up @@ -271,6 +314,14 @@ private String getAttribute(Entry entry, String attribute) throws LdapException
return entry.get(attribute).getString();
}

private byte[] getByteAttribute(Entry entry, String attribute) throws LdapException, InvalidAttributeValueException {
Attribute value = entry.get(attribute);
if (value != null) {
return value.getBytes();
}
return null;
}

@Override
public void close() throws IOException {
connection.close();
Expand Down

0 comments on commit 4454302

Please sign in to comment.