001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.identity.services.impl;
023:
024: import java.util.ArrayList;
025: import java.util.HashSet;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.Map;
029: import java.util.Set;
030:
031: import org.jboss.portal.core.identity.services.IdentityConstants;
032: import org.jboss.portal.core.identity.services.IdentityUserManagementService;
033: import org.jboss.portal.core.identity.services.metadata.IdentityUIConfigurationService;
034: import org.jboss.portal.identity.IdentityContext;
035: import org.jboss.portal.identity.IdentityException;
036: import org.jboss.portal.identity.IdentityServiceController;
037: import org.jboss.portal.identity.MembershipModule;
038: import org.jboss.portal.identity.Role;
039: import org.jboss.portal.identity.RoleModule;
040: import org.jboss.portal.identity.User;
041: import org.jboss.portal.identity.UserModule;
042: import org.jboss.portal.identity.UserProfileModule;
043: import org.jboss.portal.jems.as.JNDI;
044: import org.jboss.portal.jems.as.system.AbstractJBossService;
045:
046: /**
047: * @author <a href="mailto:emuckenh@redhat.com">Emanuel Muckenhuber</a>
048: * @version $Revision$
049: */
050: public class IdentityUserManagementServiceImpl extends
051: AbstractJBossService implements IdentityUserManagementService {
052: /** The identity user module */
053: private UserModule userModule;
054:
055: /** The identity role module */
056: private RoleModule roleModule;
057:
058: /** The identity user profile module */
059: private UserProfileModule userProfileModule;
060:
061: /** The identity membership module */
062: private MembershipModule membershipModule;
063:
064: /** The identity service controller */
065: private IdentityServiceController identityServiceController;
066:
067: /** The core-identity configuration service */
068: private IdentityUIConfigurationService identityUIConfigurationService;
069:
070: /** The JNDI binding */
071: private JNDI.Binding jndiBinding;
072:
073: /** The jndi name */
074: private String jndiName = null;
075:
076: public void startService() throws Exception {
077: super .startService();
078:
079: try {
080: // Loading required modules
081: userModule = (UserModule) identityServiceController
082: .getIdentityContext().getObject(
083: IdentityContext.TYPE_USER_MODULE);
084: roleModule = (RoleModule) identityServiceController
085: .getIdentityContext().getObject(
086: IdentityContext.TYPE_ROLE_MODULE);
087: userProfileModule = (UserProfileModule) identityServiceController
088: .getIdentityContext().getObject(
089: IdentityContext.TYPE_USER_PROFILE_MODULE);
090: membershipModule = (MembershipModule) identityServiceController
091: .getIdentityContext().getObject(
092: IdentityContext.TYPE_MEMBERSHIP_MODULE);
093: } catch (IdentityException e) {
094: super .stopService();
095: throw new RuntimeException(e);
096: }
097:
098: if (this .jndiName != null) {
099: jndiBinding = new JNDI.Binding(jndiName, this );
100: jndiBinding.bind();
101: }
102: }
103:
104: public void stopService() throws Exception {
105: super .stopService();
106:
107: if (jndiBinding != null) {
108: jndiBinding.unbind();
109: jndiBinding = null;
110: }
111: }
112:
113: public String getJNDIName() {
114: return this .jndiName;
115: }
116:
117: public void setJNDIName(String jndiName) {
118: this .jndiName = jndiName;
119: }
120:
121: public IdentityServiceController getIdentityServiceController() {
122: return identityServiceController;
123: }
124:
125: public void setIdentityServiceController(
126: IdentityServiceController identityServiceController) {
127: this .identityServiceController = identityServiceController;
128: }
129:
130: public UserModule getUserModule() {
131: return userModule;
132: }
133:
134: public void setUserModule(UserModule userModule) {
135: this .userModule = userModule;
136: }
137:
138: public RoleModule getRoleModule() {
139: return roleModule;
140: }
141:
142: public void setRoleModule(RoleModule roleModule) {
143: this .roleModule = roleModule;
144: }
145:
146: public UserProfileModule getUserProfileModule() {
147: return userProfileModule;
148: }
149:
150: public void setUserProfileModule(UserProfileModule userProfileModule) {
151: this .userProfileModule = userProfileModule;
152: }
153:
154: public MembershipModule getMembershipModule() {
155: return membershipModule;
156: }
157:
158: public void setMembershipModule(MembershipModule membershipModule) {
159: this .membershipModule = membershipModule;
160: }
161:
162: public IdentityUIConfigurationService getIdentityUIConfigurationService() {
163: return identityUIConfigurationService;
164: }
165:
166: public void setIdentityUIConfigurationService(
167: IdentityUIConfigurationService identityUIConfigurationService) {
168: this .identityUIConfigurationService = identityUIConfigurationService;
169: }
170:
171: public boolean createUser(String username, String password,
172: Map profileMap, List roles) {
173: try {
174: User user = this .getUserModule().createUser(username,
175: password);
176: Set roleSet = this .checkRoles(roles);
177:
178: // Enable the user
179: profileMap.put(User.INFO_USER_ENABLED, Boolean.TRUE);
180:
181: Iterator i = profileMap.keySet().iterator();
182: while (i.hasNext()) {
183: String key = (String) i.next();
184: Object value = profileMap.get(key);
185: this .getUserProfileModule().setProperty(user, key,
186: value);
187: }
188:
189: this .getMembershipModule().assignRoles(user, roleSet);
190: return true;
191: } catch (Exception e) {
192: log.error("Error when creating user", e);
193: }
194:
195: return false;
196: }
197:
198: public String getCurrentEmail(String username) {
199: try {
200: User user = this .getUserModule().findUserByUserName(
201: username);
202: return (String) this .getUserProfileModule().getProperty(
203: user, User.INFO_USER_EMAIL_REAL);
204: } catch (Exception e) {
205: log.error("failed to retrieve current email address", e);
206: }
207: return null;
208: }
209:
210: public boolean updateEmail(String username, String email) {
211: try {
212: User user = this .getUserModule().findUserByUserName(
213: username);
214: this .getUserProfileModule().setProperty(user,
215: User.INFO_USER_EMAIL_REAL, email);
216: return true;
217: } catch (Exception e) {
218: log.error("failed to update email", e);
219: }
220: return false;
221: }
222:
223: private Set checkRoles(List roles) throws IllegalArgumentException,
224: IdentityException {
225: Set roleSet = new HashSet();
226: // Set default roles if required
227: if (roles == null || (roles != null && roles.size() == 0)) {
228: roles = this .identityUIConfigurationService
229: .getConfiguration().getDefaultRoles();
230: }
231: // If roles are still not available
232: if (roles == null || (roles != null && roles.size() == 0)) {
233: roles = new ArrayList();
234: roles.add(IdentityConstants.DEFAULT_ROLE);
235: log
236: .error("no default roles spezified - please check your configuration");
237: }
238:
239: if (roles != null && roles.size() > 0) { // Checking existing roles
240: Iterator i = roles.iterator();
241:
242: while (i.hasNext()) {
243: String roleName = (String) i.next();
244: Role role = this .getRoleModule().findRoleByName(
245: roleName);
246:
247: if (role == null) {
248: // Create new role ?
249: } else {
250: roleSet.add(role);
251: }
252: }
253: }
254: return roleSet;
255: }
256:
257: }
|