001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.ac.usecases;
019:
020: import org.apache.avalon.framework.container.ContainerUtil;
021: import org.apache.lenya.ac.AccessControlException;
022: import org.apache.lenya.ac.ItemUtil;
023: import org.apache.lenya.ac.User;
024: import org.apache.lenya.ac.UserManager;
025: import org.apache.lenya.ac.file.FileUser;
026: import org.apache.lenya.ac.file.FileUserManager;
027: import org.apache.lenya.ac.ldap.LDAPUser;
028: import org.apache.lenya.cms.usecase.UsecaseException;
029:
030: /**
031: * Usecase to add a user.
032: *
033: * @version $Id: AddUser.java 407248 2006-05-17 13:20:01Z andreas $
034: */
035: public class AddUser extends AccessControlUsecase {
036:
037: protected static final String CLASS_NAME = "className";
038: protected static final String LDAP_ID = "ldapId";
039:
040: /**
041: * Validates the request parameters.
042: * @throws UsecaseException if an error occurs.
043: */
044: void validate() throws UsecaseException {
045:
046: String userId = getParameterAsString(UserProfile.USER_ID)
047: .toLowerCase();
048: String email = getParameterAsString(UserProfile.EMAIL);
049: String className = getParameterAsString(CLASS_NAME);
050: String ldapId = getParameterAsString(LDAP_ID);
051:
052: User existingUser = getUserManager().getUser(userId);
053:
054: if (existingUser != null) {
055: addErrorMessage("This user already exists.");
056: }
057:
058: if (!ItemUtil.isValidId(userId)) {
059: addErrorMessage("This is not a valid user ID.");
060: }
061:
062: if (!ItemUtil.isValidEmail(email)) {
063: addErrorMessage("Please enter a valid e-mail address.");
064: }
065:
066: if (className == null) {
067: addErrorMessage("Internal error - the user class name has not been provided. "
068: + "Please consult your system administrator.");
069: }
070:
071: if (className.equals(LDAPUser.class.getName())) {
072: LDAPUser ldapUser = new LDAPUser(getUserManager(),
073: getLogger());
074: ContainerUtil.enableLogging(ldapUser, getLogger());
075:
076: try {
077: if (!ldapUser.existsUser(ldapId)) {
078: addErrorMessage("ldap_no_such_user",
079: new String[] { ldapId });
080: }
081: } catch (AccessControlException e) {
082: throw new UsecaseException(e);
083: }
084: }
085:
086: else {
087: ChangePassword.checkNewPassword(this );
088: }
089:
090: }
091:
092: /**
093: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckExecutionConditions()
094: */
095: protected void doCheckExecutionConditions() throws Exception {
096: if (getLogger().isDebugEnabled())
097: getLogger().debug(
098: "AddUser.doCheckExecutionConditions() called");
099:
100: validate();
101: }
102:
103: /**
104: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
105: */
106: protected void doExecute() throws Exception {
107: UserManager userManager = (FileUserManager) getUserManager();
108:
109: String userId = getParameterAsString(UserProfile.USER_ID)
110: .toLowerCase();
111: String fullName = getParameterAsString(UserProfile.FULL_NAME);
112: String description = getParameterAsString(UserProfile.DESCRIPTION);
113: String email = getParameterAsString(UserProfile.EMAIL);
114: String className = getParameterAsString(CLASS_NAME);
115:
116: User user;
117: if (className.equals(LDAPUser.class.getName())) {
118: String ldapId = getParameterAsString(LDAP_ID);
119: user = new LDAPUser(userManager, getLogger(), userId,
120: email, ldapId, getLogger());
121: } else {
122: String password = getParameterAsString(AbstractChangePassword.NEW_PASSWORD);
123: user = new FileUser(userManager, getLogger(), userId,
124: fullName, email, "");
125: user.setName(fullName);
126: user.setPassword(password);
127: }
128: ContainerUtil.enableLogging(user, getLogger());
129: user.setDescription(description);
130: user.save();
131: getUserManager().add(user);
132:
133: setExitParameter(UserProfile.USER_ID, userId);
134: }
135: }
|