Skip to content

Commit

Permalink
People Lookup: backend class now also implements ExternalUidLookup
Browse files Browse the repository at this point in the history
- PeopleLookupBackend class can be used for external user id lookup (Instructors, Timetable Managers)
  - reusing the LDAP directory settings of the People Lookup Dialog
  - with the optional translation of the user external id to user name and back

- usage:
  # Manager lookup
  tmtbl.manager.external_id.lookup.class=org.unitime.timetable.server.lookup.PeopleLookupBackend
  tmtbl.manager.external_id.lookup.enabled=true
  # Instructor lookup
  tmtbl.instructor.external_id.lookup.class=org.unitime.timetable.server.lookup.PeopleLookupBackend
  tmtbl.instructor.external_id.lookup.enabled=true
  • Loading branch information
tomas-muller committed Apr 16, 2015
1 parent 763df6d commit cc56657
Showing 1 changed file with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.unitime.timetable.gwt.command.server.GwtRpcImplements;
import org.unitime.timetable.gwt.shared.PersonInterface;
import org.unitime.timetable.gwt.shared.PersonInterface.LookupRequest;
import org.unitime.timetable.interfaces.ExternalUidLookup;
import org.unitime.timetable.interfaces.ExternalUidTranslation;
import org.unitime.timetable.interfaces.ExternalUidTranslation.Source;
import org.unitime.timetable.model.DepartmentalInstructor;
Expand All @@ -62,6 +63,7 @@
import org.unitime.timetable.security.SessionContext;
import org.unitime.timetable.security.UserContext;
import org.unitime.timetable.security.rights.Right;
import org.unitime.timetable.spring.SpringApplicationContextHolder;
import org.unitime.timetable.util.Constants;
import org.unitime.timetable.util.NameFormat;
import org.unitime.timetable.util.NameInterface;
Expand All @@ -70,7 +72,7 @@
* @author Tomas Muller
*/
@GwtRpcImplements(PersonInterface.LookupRequest.class)
public class PeopleLookupBackend implements GwtRpcImplementation<PersonInterface.LookupRequest, GwtRpcResponseList<PersonInterface>> {
public class PeopleLookupBackend implements GwtRpcImplementation<PersonInterface.LookupRequest, GwtRpcResponseList<PersonInterface>>, ExternalUidLookup {
private static Logger sLog = Logger.getLogger(PeopleLookupBackend.class);
private ExternalUidTranslation iTranslation;
private LdapTemplate iLdapTemplate;
Expand Down Expand Up @@ -328,7 +330,11 @@ protected void findPeopleFromTimetableManagers(SearchContext context) throws Exc
protected LdapTemplate getLdapTemplate() {
if (iLdapTemplate == null) {
try {
iLdapTemplate = applicationContext.getBean("ldapPeopleLookupTemplate", LdapTemplate.class);
if (applicationContext != null) {
iLdapTemplate = applicationContext.getBean("ldapPeopleLookupTemplate", LdapTemplate.class);
} else {
iLdapTemplate = (LdapTemplate)SpringApplicationContextHolder.getBean("ldapPeopleLookupTemplate");
}
if (iLdapTemplate != null) return iLdapTemplate;
} catch (BeansException e) {}
String url = ApplicationProperty.PeopleLookupLdapUrl.value();
Expand Down Expand Up @@ -464,4 +470,57 @@ public GwtRpcResponseList<PersonInterface> response(boolean displayWithoutId) {
}

}

@Override
public UserInfo doLookup(String uid) throws Exception {
try {
if (uid == null || uid.isEmpty()) return null;
if (getLdapTemplate() == null) return null;

if (iTranslation != null)
uid = iTranslation.translate(uid, Source.User, Source.LDAP);

return (UserInfo)getLdapTemplate().lookup("uid=" + uid, new AttributesMapper() {
protected String getAttribute(Attributes attrs, String name) {
if (attrs==null) return null;
if (name == null || name.isEmpty()) return null;
for (StringTokenizer stk = new StringTokenizer(name,",");stk.hasMoreTokens();) {
Attribute a = attrs.get(stk.nextToken());
try {
if (a!=null && a.get()!=null) return a.get().toString();
} catch (NamingException e) {
}
}
return null;
}
@Override
public Object mapFromAttributes(Attributes a) throws NamingException {
UserInfo info = new UserInfo();
info.setUserName(getAttribute(a,"uid"));
if (iTranslation == null)
info.setExternalId(info.getUserName());
else
info.setExternalId(iTranslation.translate(info.getUserName(), Source.LDAP, Source.User));
info.setFirstName(Constants.toInitialCase(getAttribute(a,"givenName")));
info.setName(Constants.toInitialCase(getAttribute(a,"cn")));
info.setLastName(Constants.toInitialCase(getAttribute(a,"sn")));
info.setEmail(getAttribute(a, ApplicationProperty.PeopleLookupLdapEmailAttribute.value()));
info.setPhone(getAttribute(a, ApplicationProperty.PeopleLookupLdapPhoneAttribute.value()));
info.setAcademicTitle(getAttribute(a, ApplicationProperty.PeopleLookupLdapAcademicTitleAttribute.value()));
if (info.getName() != null) {
String middle = info.getName();
if (info.getFirstName() != null && middle.indexOf(info.getFirstName()) >= 0)
middle = middle.replaceAll(info.getFirstName() + " ?", "");
if (info.getLastName() != null && middle.indexOf(info.getLastName()) >= 0)
middle = middle.replaceAll(" ?" + info.getLastName(), "");
info.setMiddleName(middle);
}
return info;
}
});
} catch (Exception e) {
sLog.warn("Failed to lookup a person: " + e.getMessage(), e);
return null;
}
}
}

0 comments on commit cc56657

Please sign in to comment.