1
22
23 package com.liferay.portal.security.ldap;
24
25 import com.liferay.portal.kernel.util.Validator;
26 import com.liferay.portal.model.User;
27 import com.liferay.portal.util.PropsUtil;
28 import com.liferay.util.ldap.DummyDirContext;
29
30 import java.util.Properties;
31
32 import javax.naming.Name;
33 import javax.naming.NameNotFoundException;
34 import javax.naming.NamingException;
35 import javax.naming.directory.Attribute;
36 import javax.naming.directory.Attributes;
37 import javax.naming.directory.BasicAttribute;
38 import javax.naming.directory.BasicAttributes;
39
40
47 public class LDAPUser extends DummyDirContext {
48
49 public LDAPUser() {
50 super();
51 }
52
53 public User getUser() {
54 return _user;
55 }
56
57 public void setUser(User user) throws Exception {
58 _user = user;
59
60 Properties userMappings = PortalLDAPUtil.getUserMappings(
61 _user.getCompanyId());
62
63 _attrs = new BasicAttributes(true);
64
65 Attribute objectClass = new BasicAttribute("objectclass");
66
67 String[] defaultObjectClasses = PropsUtil.getArray(
68 PropsUtil.LDAP_USER_DEFAULT_OBJECT_CLASSES);
69
70 for (int i = 0; i < defaultObjectClasses.length; i++) {
71 objectClass.add(defaultObjectClasses[i]);
72 }
73
74 _attrs.put(objectClass);
75
76 _attrs.put(userMappings.getProperty("firstName"), _user.getFirstName());
77 _attrs.put(userMappings.getProperty("lastName"), _user.getLastName());
78
79 if (Validator.isNotNull(user.getPasswordUnencrypted())) {
80 _attrs.put(
81 userMappings.getProperty("password"),
82 _user.getPasswordUnencrypted());
83 }
84
85 _attrs.put(
86 userMappings.getProperty("emailAddress"), _user.getEmailAddress());
87 }
88
89 public Attributes getAttributes() {
90 return _attrs;
91 }
92
93 public Attributes getAttributes(String name) throws NamingException {
94 if (Validator.isNotNull(name)) {
95 throw new NameNotFoundException();
96 }
97
98 return (Attributes)_attrs.clone();
99 }
100
101 public Attributes getAttributes(Name name) throws NamingException {
102 return getAttributes(name.toString());
103 }
104
105 public Attributes getAttributes(String name, String[] ids)
106 throws NamingException {
107
108 if (Validator.isNotNull(name)) {
109 throw new NameNotFoundException();
110 }
111
112 Attributes attrs = new BasicAttributes(true);
113
114 for (int i = 0; i < ids.length; i++) {
115 Attribute attr = _attrs.get(ids[i]);
116
117 if (attr != null) {
118 attrs.put(attr);
119 }
120 }
121
122 return attrs;
123 }
124
125 public Attributes getAttributes(Name name, String[] ids)
126 throws NamingException {
127
128 return getAttributes(name.toString(), ids);
129 }
130
131 private User _user;
132 private Attributes _attrs;
133
134 }