You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2005/01/03 17:53:20 UTC

svn commit: r124007 - lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases

Author: andreas
Date: Mon Jan  3 08:53:16 2005
New Revision: 124007

URL: http://svn.apache.org/viewcvs?view=rev&rev=124007
Log:
refactor usecase packages
Added:
   lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/
   lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/AccessControl.java
   lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/AccessControlUsecase.java
   lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/AddUser.java
   lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/Login.java
   lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/Logout.java
   lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/UserGroups.java
   lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/UserPassword.java
   lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/UserProfile.java

Added: lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/AccessControl.java
Url: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/AccessControl.java?view=auto&rev=124007
==============================================================================
--- (empty file)
+++ lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/AccessControl.java	Mon Jan  3 08:53:16 2005
@@ -0,0 +1,163 @@
+/*
+ * Copyright  1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.ac.usecases;
+
+import org.apache.lenya.cms.usecase.UsecaseException;
+
+import org.apache.lenya.ac.Item;
+import org.apache.lenya.ac.Role;
+import org.apache.lenya.cms.ac.cocoon.PolicyHelper;
+
+/**
+ * Usecase to display the AccessControl tab in the site area for a document.
+ * This is a mix-in class that ideally would inherit both from
+ * AccessControlUsecase and DocumentUsecase. FIXME i just took the appropriate
+ * code from DocumentUsecase, maybe its possible to have a saner inheritance?
+ * 
+ * @version $Id: AccessControl.java 123980 2005-01-03 15:00:14Z andreas $
+ */
+
+public class AccessControl extends AccessControlUsecase {
+
+    private PolicyHelper helper = null;
+
+    private Item[] items = null;
+
+    private static String[] types = { "user", "group", "iprange", "role" };
+
+    private static String[] operations = { "add", "delete" };
+
+    /**
+     * Ctor.
+     */
+    public AccessControl() {
+        super();
+    }
+
+    /**
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doInitialize()
+     */
+    protected void doInitialize() {
+        super.doInitialize();
+        try {
+
+            Role[] roles = getRoleManager().getRoles();
+            String visitorRole = "";
+            for (int i = 0; i < roles.length; i++) {
+                if (roles[i].getId().equals("visit")) {
+                    visitorRole = roles[i].getId();
+                }
+            }
+
+            setParameter("visitorRole", visitorRole);
+
+            this.helper = new PolicyHelper(getLogger());
+            //FIXME expects the component manager
+            // helper.setup(objectModel, this.manager, area);
+
+            for (int i = 0; i < types.length; i++) {
+                Item[] items = null;
+
+                if (types[i].equals("user")) {
+                    items = getUserManager().getUsers();
+                } else if (types[i].equals("group")) {
+                    items = getGroupManager().getGroups();
+                } else if (types[i].equals("iprange")) {
+                    items = getIpRangeManager().getIPRanges();
+                } else if (types[i].equals("role")) {
+                    items = getRoleManager().getRoles();
+                }
+                for (int j = 0; j < operations.length; j++) {
+                    if (getParameterAsString(operations[j] + "_credential_" + types[i]) != null) {
+                        String roleId = getParameterAsString("role_id");
+
+                        String accreditableId = getParameterAsString("accreditable_id");
+                        Item item = null;
+                        for (int k = 0; k < items.length; k++) {
+                            if (accreditableId.equals(items[k].getId())) {
+                                item = items[k];
+                            }
+                        }
+
+                        Role role = getRoleManager().getRole(roleId);
+
+                        if (role == null) {
+                            addErrorMessage("Role [" + roleId + "] does not exist!");
+                        }
+
+                        this.helper.manipulateCredential(item, role, operations[j]);
+                    }
+                }
+            }
+
+        } catch (Exception e) {
+            addErrorMessage("Could not read a value.");
+            getLogger().error("Could not read value for AccessControl usecase. " + e.toString());
+        }
+    }
+
+    /**
+     * Validates the request parameters.
+     * @throws UsecaseException if an error occurs.
+     */
+    void validate() throws UsecaseException {
+    }
+
+    /**
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckExecutionConditions()
+     */
+    protected void doCheckExecutionConditions() throws Exception {
+        validate();
+    }
+
+    /**
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
+     */
+    protected void doExecute() throws Exception {
+        super.doExecute();
+        if (getParameterAsString("change_ssl") != null) {
+            if (getParameterAsString("ssl") != null) {
+                this.helper.setUrlSSLProtected(true);
+            } else {
+                this.helper.setUrlSSLProtected(false);
+            }
+        }
+
+    }
+
+    /**
+     * @see org.apache.lenya.cms.usecase.Usecase#setParameter(java.lang.String,
+     *      java.lang.Object)
+     */
+    public void setParameter(String name, Object value) {
+        super.setParameter(name, value);
+    }
+
+    /**
+     * @return PolicyHelper the policy helper
+     */
+    public PolicyHelper getHelper() {
+        return this.helper;
+    }
+
+    /**
+     * @return Item the item
+     */
+    public Item[] getItems() {
+        return this.items;
+    }
+}
\ No newline at end of file

Added: lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/AccessControlUsecase.java
Url: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/AccessControlUsecase.java?view=auto&rev=124007
==============================================================================
--- (empty file)
+++ lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/AccessControlUsecase.java	Mon Jan  3 08:53:16 2005
@@ -0,0 +1,121 @@
+/*
+ * Copyright  1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.ac.usecases;
+
+import org.apache.avalon.framework.service.ServiceSelector;
+import org.apache.lenya.ac.AccessController;
+import org.apache.lenya.ac.AccessControllerResolver;
+import org.apache.lenya.ac.AccreditableManager;
+import org.apache.lenya.ac.GroupManager;
+import org.apache.lenya.ac.IPRangeManager;
+import org.apache.lenya.ac.RoleManager;
+import org.apache.lenya.ac.UserManager;
+import org.apache.lenya.ac.impl.DefaultAccessController;
+import org.apache.lenya.cms.usecase.AbstractUsecase;
+
+/**
+ * Super class for access-control related usecases.
+ * 
+ * @version $Id: AccessControlUsecase.java 123981 2005-01-03 15:00:51Z andreas $
+ */
+public class AccessControlUsecase extends AbstractUsecase {
+
+    /**
+     * Ctor.
+     */
+    public AccessControlUsecase() {
+        super();
+    }
+    
+    private UserManager userManager;
+    private GroupManager groupManager;
+    private IPRangeManager ipRangeManager;
+    private RoleManager roleManager;
+    private AccessController accessController;
+
+    /**
+     * FIXME: This method resolves the AccessController, it has to be released after it is used!
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doInitialize()
+     */
+    protected void doInitialize() {
+        super.doInitialize();
+        
+        accessController = null;
+        ServiceSelector selector = null;
+        AccessControllerResolver resolver = null;
+        
+        try {
+            selector = (ServiceSelector) this.manager.lookup(AccessControllerResolver.ROLE + "Selector");
+            resolver =
+                (AccessControllerResolver) selector.select(
+                    AccessControllerResolver.DEFAULT_RESOLVER);
+
+            accessController = resolver.resolveAccessController(getSourceURL());
+
+            AccreditableManager accreditableManager =
+                ((DefaultAccessController) accessController).getAccreditableManager();
+
+            this.userManager = accreditableManager.getUserManager();
+            this.groupManager = accreditableManager.getGroupManager();
+            this.roleManager = accreditableManager.getRoleManager();
+            this.ipRangeManager = accreditableManager.getIPRangeManager();
+
+        } catch (Exception e) {
+            throw new RuntimeException("Initialization failed: ", e);
+        } finally {
+            if (selector != null) {
+                if (resolver != null) {
+                    selector.release(resolver);
+                }
+                this.manager.release(selector);
+            }
+        }
+
+    }
+    
+    
+    /**
+     * @return Returns the groupManager.
+     */
+    protected GroupManager getGroupManager() {
+        return this.groupManager;
+    }
+    /**
+     * @return Returns the ipRangeManager.
+     */
+    protected IPRangeManager getIpRangeManager() {
+        return this.ipRangeManager;
+    }
+    /**
+     * @return Returns the roleManager.
+     */
+    protected RoleManager getRoleManager() {
+        return this.roleManager;
+    }
+    /**
+     * @return Returns the userManager.
+     */
+    protected UserManager getUserManager() {
+        return this.userManager;
+    }
+    /**
+     * @return Returns the accessController.
+     */
+    protected AccessController getAccessController() {
+        return this.accessController;
+    }
+}

Added: lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/AddUser.java
Url: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/AddUser.java?view=auto&rev=124007
==============================================================================
--- (empty file)
+++ lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/AddUser.java	Mon Jan  3 08:53:16 2005
@@ -0,0 +1,123 @@
+/*
+ * Copyright  1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.ac.usecases;
+
+import java.io.File;
+
+import org.apache.lenya.ac.AccessControlException;
+import org.apache.lenya.ac.User;
+import org.apache.lenya.ac.file.FileUser;
+import org.apache.lenya.ac.file.FileUserManager;
+import org.apache.lenya.ac.impl.AbstractItem;
+import org.apache.lenya.ac.ldap.LDAPUser;
+import org.apache.lenya.cms.usecase.UsecaseException;
+
+/**
+ * Usecase to add a user.
+ * 
+ * @version $Id: AddUser.java 123348 2004-12-25 22:49:57Z gregor $
+ */
+public class AddUser extends AccessControlUsecase {
+
+    /**
+     * Ctor.
+     */
+    public AddUser() {
+        super();
+    }
+
+    protected static final String CLASS_NAME = "className";
+    protected static final String LDAP_ID = "ldapId";
+
+    /**
+     * Validates the request parameters.
+     * @throws UsecaseException if an error occurs.
+     */
+    void validate() throws UsecaseException {
+
+        String userId = getParameterAsString(UserProfile.USER_ID);
+        String email = getParameterAsString(UserProfile.EMAIL);
+        String className = getParameterAsString(CLASS_NAME);
+        String ldapId = getParameterAsString(LDAP_ID);
+
+        User existingUser = getUserManager().getUser(userId);
+
+        if (existingUser != null) {
+            addErrorMessage("This user already exists.");
+        }
+
+        if (!AbstractItem.isValidId(userId)) {
+            addErrorMessage("This is not a valid user ID.");
+        }
+
+        if (email.length() == 0) {
+            addErrorMessage("Please enter an e-mail address.");
+        }
+
+        if (className.equals(LDAPUser.class.getName())) {
+            LDAPUser ldapUser = new LDAPUser(((FileUserManager) getUserManager())
+                    .getConfigurationDirectory());
+            try {
+                if (!ldapUser.existsUser(ldapId)) {
+                    addErrorMessage("This LDAP user ID does not exist.");
+                }
+            } catch (AccessControlException e) {
+                throw new UsecaseException(e);
+            }
+        }
+
+        else {
+            UserPassword.checkNewPassword(this);
+        }
+
+    }
+
+    /**
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckExecutionConditions()
+     */
+    protected void doCheckExecutionConditions() throws Exception {
+        validate();
+    }
+
+    /**
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
+     */
+    protected void doExecute() throws Exception {
+        File configDir = ((FileUserManager) getUserManager()).getConfigurationDirectory();
+
+        String userId = getParameterAsString(UserProfile.USER_ID);
+        String fullName = getParameterAsString(UserProfile.FULL_NAME);
+        String description = getParameterAsString(UserProfile.DESCRIPTION);
+        String email = getParameterAsString(UserProfile.EMAIL);
+        String className = getParameterAsString(CLASS_NAME);
+
+        User user;
+        if (className.equals(LDAPUser.class.getName())) {
+            String ldapId = getParameterAsString(LDAP_ID);
+            user = new LDAPUser(configDir, userId, email, ldapId);
+        } else {
+            String password = getParameterAsString(UserPassword.NEW_PASSWORD);
+            user = new FileUser(configDir, userId, fullName, email, "");
+            user.setName(fullName);
+            user.setPassword(password);
+        }
+
+        user.setDescription(description);
+        user.save();
+        getUserManager().add(user);
+    }
+}
\ No newline at end of file

Added: lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/Login.java
Url: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/Login.java?view=auto&rev=124007
==============================================================================
--- (empty file)
+++ lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/Login.java	Mon Jan  3 08:53:16 2005
@@ -0,0 +1,79 @@
+/*
+ * Copyright  1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.ac.usecases;
+
+import java.util.Map;
+
+import org.apache.lenya.cms.usecase.UsecaseException;
+import org.apache.cocoon.components.ContextHelper;
+import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.environment.Request;
+
+/**
+ * Usecase to login a user.
+ * 
+ * @version $Id: Login.java 124001 2005-01-03 16:27:21Z andreas $
+ */
+public class Login extends AccessControlUsecase {
+
+    /**
+     * Ctor.
+     */
+    public Login() {
+        super();
+    }
+
+    /**
+     * Validates the request parameters.
+     * @throws UsecaseException if an error occurs.
+     */
+    void validate() throws UsecaseException {
+
+        String userId = getParameterAsString("username");
+        String password = getParameterAsString("password");
+
+        if (userId.length() == 0) {
+            addErrorMessage("Please enter a user name.");
+        }
+        if (password.length() == 0) {
+            addErrorMessage("Please enter a password.");
+        }
+
+    }
+
+    /**
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckExecutionConditions()
+     */
+    protected void doCheckExecutionConditions() throws Exception {
+        validate();
+    }
+
+    /**
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
+     */
+    protected void doExecute() throws Exception {
+        Map objectModel = ContextHelper.getObjectModel(getContext());
+        Request request = ObjectModelHelper.getRequest(objectModel);
+        request.getSession(true);
+        if (getAccessController().authenticate(request)) {
+        	setTargetURL(request.getRequestURI());
+		} else {
+        	addErrorMessage("Authentication failed.");
+        }
+   }
+
+}
\ No newline at end of file

Added: lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/Logout.java
Url: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/Logout.java?view=auto&rev=124007
==============================================================================
--- (empty file)
+++ lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/Logout.java	Mon Jan  3 08:53:16 2005
@@ -0,0 +1,71 @@
+/*
+ * Copyright  1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.ac.usecases;
+
+import java.util.Map;
+import java.util.Vector;
+
+import org.apache.lenya.ac.Identity;
+import org.apache.cocoon.components.ContextHelper;
+import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.environment.Request;
+import org.apache.cocoon.environment.Session;
+
+/**
+ * Usecase to log a user out.
+ * 
+ * @version $Id: Logout.java 124002 2005-01-03 16:27:42Z andreas $
+ */
+public class Logout extends AccessControlUsecase {
+
+    /**
+     * Ctor.
+     */
+    public Logout() {
+        super();
+    }
+
+    /**
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
+     */
+    protected void initParameters() {
+        super.initParameters();
+        Map objectModel = ContextHelper.getObjectModel(getContext());
+        Request request = ObjectModelHelper.getRequest(objectModel);
+        Session session = request.getSession(false);
+
+        if (session != null) {
+            Vector history = (Vector) session
+                    .getAttribute("org.apache.lenya.cms.cocoon.acting.History");
+            setParameter("history", history.toArray());
+        }
+    }
+    
+    /**
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
+     */
+    protected void doExecute() throws Exception {
+
+        Map objectModel = ContextHelper.getObjectModel(getContext());
+        Request request = ObjectModelHelper.getRequest(objectModel);
+        Session session = request.getSession(false);
+
+        if (session != null) {
+            session.removeAttribute(Identity.class.getName());
+        }
+    }
+}
\ No newline at end of file

Added: lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/UserGroups.java
Url: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/UserGroups.java?view=auto&rev=124007
==============================================================================
--- (empty file)
+++ lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/UserGroups.java	Mon Jan  3 08:53:16 2005
@@ -0,0 +1,134 @@
+/*
+ * Copyright  1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.ac.usecases;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.lenya.ac.Group;
+import org.apache.lenya.ac.User;
+
+/**
+ * Usecase to edit a user's group affiliation.
+ */
+public class UserGroups extends AccessControlUsecase {
+
+    private User user;
+
+    protected static final String USER_GROUPS = "userGroups";
+    protected static final String OTHER_GROUPS = "otherGroups";
+    protected static final String ADD = "add";
+    protected static final String REMOVE = "remove";
+    protected static final String USER_GROUP = "userGroup";
+    protected static final String OTHER_GROUP = "otherGroup";
+
+    /**
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
+     */
+    protected void doExecute() throws Exception {
+        super.doExecute();
+        
+        this.user.removeFromAllGroups();
+        
+        List userGroups = (List) getParameter(USER_GROUPS);
+        for (Iterator i = userGroups.iterator(); i.hasNext(); ) {
+            Group group = (Group) i.next();
+            group.add(this.user);
+        }
+    }
+
+    /**
+     * @see org.apache.lenya.cms.usecase.Usecase#advance()
+     */
+    public void advance() {
+        super.advance();
+
+        String add = getParameterAsString(ADD);
+        String remove = getParameterAsString(REMOVE);
+        if (add != null || remove != null) {
+
+            List userGroups = (List) getParameter(USER_GROUPS);
+            List otherGroups = (List) getParameter(OTHER_GROUPS);
+            
+            if (add != null) {
+                String groupId = getParameterAsString(OTHER_GROUP);
+                if (groupId != null) {
+                    if (getLogger().isDebugEnabled()) {
+                        getLogger().debug("add group [" + groupId + "]");
+                    }
+                    Group group = getGroupManager().getGroup(groupId);
+                    userGroups.add(group);
+                    otherGroups.remove(group);
+                }
+            }
+
+            if (remove != null) {
+                String groupId = getParameterAsString(USER_GROUP);
+                if (groupId != null) {
+                    if (getLogger().isDebugEnabled()) {
+                        getLogger().debug("remove group [" + groupId + "]");
+                    }
+                    Group group = getGroupManager().getGroup(groupId);
+                    otherGroups.add(group);
+                    userGroups.remove(group);
+                }
+            }
+            
+            deleteParameter(ADD);
+            deleteParameter(REMOVE);
+            deleteParameter(USER_GROUP);
+            deleteParameter(OTHER_GROUP);
+        }
+
+    }
+
+    /**
+     * @see org.apache.lenya.cms.usecase.Usecase#setParameter(java.lang.String,
+     *      java.lang.Object)
+     */
+    public void setParameter(String name, Object value) {
+        super.setParameter(name, value);
+        if (name.equals(UserProfile.USER_ID)) {
+            String userId = (String) value;
+            this.user = getUserManager().getUser(userId);
+            if (this.user == null) {
+                throw new RuntimeException("User [" + userId + "] not found.");
+            }
+
+            Group[] userGroupArray = this.user.getGroups();
+            List userGroups = new ArrayList(Arrays.asList(userGroupArray));
+
+            setParameter(USER_GROUPS, userGroups);
+
+            Group[] allGroups = getGroupManager().getGroups();
+
+            List otherGroups = new ArrayList();
+
+            for (int i = 0; i < allGroups.length; i++) {
+                if (!userGroups.contains(allGroups[i])) {
+                    otherGroups.add(allGroups[i]);
+                }
+            }
+
+            setParameter(OTHER_GROUPS, otherGroups);
+        }
+
+    }
+
+}
\ No newline at end of file

Added: lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/UserPassword.java
Url: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/UserPassword.java?view=auto&rev=124007
==============================================================================
--- (empty file)
+++ lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/UserPassword.java	Mon Jan  3 08:53:16 2005
@@ -0,0 +1,105 @@
+/*
+ * Copyright  1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.ac.usecases;
+
+import org.apache.lenya.ac.User;
+
+/**
+ * Usecase to change a user's password.
+ */
+public class UserPassword extends AccessControlUsecase {
+
+    protected static final String OLD_PASSWORD = "oldPassword";
+    protected static final String NEW_PASSWORD = "password";
+    protected static final String CONFIRM_PASSWORD = "confirmPassword";
+    
+    protected static final String CHECK_PASSWORD = "checkPassword";
+
+    /**
+     * Ctor.
+     */
+    public UserPassword() {
+        super();
+    }
+
+    /**
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckExecutionConditions()
+     */
+    protected void doCheckExecutionConditions() throws Exception {
+        super.doCheckExecutionConditions();
+        
+        String checkOldPassword = getParameterAsString(CHECK_PASSWORD);
+        if (checkOldPassword != null && checkOldPassword.equals(Boolean.toString(true))) {
+            String oldPassword = getParameterAsString(OLD_PASSWORD);
+            boolean authenticated = this.user.authenticate(oldPassword);
+            if (!authenticated) {
+                addErrorMessage("The old password is not correct.");
+            }
+        }
+        
+        checkNewPassword(this);
+    }
+
+    /**
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
+     */
+    protected void doExecute() throws Exception {
+        super.doExecute();
+        this.user.setPassword(getParameterAsString(NEW_PASSWORD));
+    }
+
+    private User user;
+
+    /**
+     * @see org.apache.lenya.cms.usecase.Usecase#setParameter(java.lang.String, java.lang.Object)
+     */
+    public void setParameter(String name, Object value) {
+        super.setParameter(name, value);
+
+        if (name.equals(UserProfile.USER_ID)) {
+            String userId = (String) value;
+            this.user = getUserManager().getUser(userId);
+            if (this.user == null) {
+                throw new RuntimeException("User [" + userId + "] not found.");
+            }
+
+        }
+    }
+
+    /**
+     * Checks a password and a confirmed password.
+     * @param usecase The usecase.
+     */
+    protected static void checkNewPassword(AccessControlUsecase usecase) {
+        String password = usecase.getParameterAsString(UserPassword.NEW_PASSWORD);
+        String confirmPassword = usecase.getParameterAsString(UserPassword.CONFIRM_PASSWORD);
+
+        if (!password.equals(confirmPassword)) {
+            usecase.addErrorMessage("Password and confirmed password are not equal.");
+        }
+
+        if (password.length() < 6) {
+            usecase.addErrorMessage("The password must be at least six characters long.");
+        }
+
+        if (!password.matches(".*\\d.*")) {
+            usecase.addErrorMessage("The password must contain at least one number.");
+        }
+    }
+
+
+}
\ No newline at end of file

Added: lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/UserProfile.java
Url: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/UserProfile.java?view=auto&rev=124007
==============================================================================
--- (empty file)
+++ lenya/trunk/src/java/org/apache/lenya/cms/ac/usecases/UserProfile.java	Mon Jan  3 08:53:16 2005
@@ -0,0 +1,95 @@
+/*
+ * Copyright  1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.lenya.cms.ac.usecases;
+
+import org.apache.lenya.ac.User;
+
+/**
+ * Usecase to edit a user's profile.
+ */
+public class UserProfile extends AccessControlUsecase {
+
+    protected static final String USER_ID = "userId";
+    protected static final String FULL_NAME = "fullName";
+    protected static final String EMAIL = "email";
+    protected static final String DESCRIPTION = "description";
+    
+    /**
+     * Ctor.
+     */
+    public UserProfile() {
+        super();
+    }
+
+    /**
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckExecutionConditions()
+     */
+    protected void doCheckExecutionConditions() throws Exception {
+        
+        String email = getParameterAsString(UserProfile.EMAIL);
+        if (email.length() == 0) {
+            addErrorMessage("Please enter an e-mail address.");
+        }
+    }
+    
+    /**
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
+     */
+    protected void doExecute() throws Exception {
+        super.doExecute();
+        
+        String fullName = getParameterAsString(UserProfile.FULL_NAME);
+        String description = getParameterAsString(UserProfile.DESCRIPTION);
+        String email = getParameterAsString(UserProfile.EMAIL);
+        
+        getUser().setEmail(email);
+        getUser().setName(fullName);
+        getUser().setDescription(description);
+        getUser().save();
+        
+    }
+    
+    private User user;
+    
+    /**
+     * Returns the currently edited user.
+     * @return A user.
+     */
+    protected User getUser() {
+        return this.user;
+    }
+    
+    /**
+     * @see org.apache.lenya.cms.usecase.Usecase#setParameter(java.lang.String, java.lang.Object)
+     */
+    public void setParameter(String name, Object value) {
+        super.setParameter(name, value);
+        
+        if (name.equals(USER_ID)) {
+            String userId = (String) value;
+            this.user = getUserManager().getUser(userId);
+            if (this.user == null) {
+                throw new RuntimeException("User [" + userId + "] not found.");
+            }
+            
+            setParameter(EMAIL, this.user.getEmail());
+            setParameter(DESCRIPTION, this.user.getDescription());
+            setParameter(FULL_NAME, this.user.getName());
+        }
+    }
+
+}

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org