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 java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.apache.lenya.ac.Group;
026: import org.apache.lenya.ac.User;
027: import org.apache.lenya.cms.usecase.UsecaseException;
028:
029: /**
030: * Usecase to edit a user's group affiliation.
031: */
032: public class UserGroups extends AccessControlUsecase {
033:
034: protected static final String USER_GROUPS = "userGroups";
035: protected static final String OTHER_GROUPS = "otherGroups";
036: protected static final String ADD = "add";
037: protected static final String REMOVE = "remove";
038: protected static final String USER_GROUP = "userGroup";
039: protected static final String OTHER_GROUP = "otherGroup";
040:
041: /**
042: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
043: */
044: protected void doExecute() throws Exception {
045: super .doExecute();
046:
047: User user = getUser();
048: user.removeFromAllGroups();
049:
050: List userGroups = (List) getParameter(USER_GROUPS);
051: for (Iterator i = userGroups.iterator(); i.hasNext();) {
052: Group group = (Group) i.next();
053: group.add(user);
054: }
055: user.save();
056: }
057:
058: /**
059: * @see org.apache.lenya.cms.usecase.Usecase#advance()
060: */
061: public void advance() throws UsecaseException {
062: super .advance();
063:
064: String add = getParameterAsString(ADD);
065: String remove = getParameterAsString(REMOVE);
066: if (add != null || remove != null) {
067:
068: List userGroups = (List) getParameter(USER_GROUPS);
069: List otherGroups = (List) getParameter(OTHER_GROUPS);
070:
071: if (add != null) {
072: String groupId = getParameterAsString(OTHER_GROUP);
073: if (groupId != null) {
074: if (getLogger().isDebugEnabled()) {
075: getLogger()
076: .debug("add group [" + groupId + "]");
077: }
078: Group group = getGroupManager().getGroup(groupId);
079: userGroups.add(group);
080: otherGroups.remove(group);
081: }
082: }
083:
084: if (remove != null) {
085: String groupId = getParameterAsString(USER_GROUP);
086: if (groupId != null) {
087: if (getLogger().isDebugEnabled()) {
088: getLogger().debug(
089: "remove group [" + groupId + "]");
090: }
091: Group group = getGroupManager().getGroup(groupId);
092: otherGroups.add(group);
093: userGroups.remove(group);
094: }
095: }
096:
097: deleteParameter(ADD);
098: deleteParameter(REMOVE);
099: deleteParameter(USER_GROUP);
100: deleteParameter(OTHER_GROUP);
101: }
102:
103: }
104:
105: protected User getUser() {
106: String userId = getParameterAsString(UserProfile.USER_ID);
107: User user = getUserManager().getUser(userId);
108: if (user == null) {
109: throw new RuntimeException("User [" + userId
110: + "] not found.");
111: }
112: return user;
113: }
114:
115: protected void initParameters() {
116: super .initParameters();
117:
118: Group[] userGroupArray = getUser().getGroups();
119: List userGroups = new ArrayList(Arrays.asList(userGroupArray));
120: setParameter(USER_GROUPS, userGroups);
121:
122: Group[] allGroups = getGroupManager().getGroups();
123: List otherGroups = new ArrayList();
124: for (int i = 0; i < allGroups.length; i++) {
125: if (!userGroups.contains(allGroups[i])) {
126: otherGroups.add(allGroups[i]);
127: }
128: }
129: setParameter(OTHER_GROUPS, otherGroups);
130: }
131:
132: }
|