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