001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.reg;
022:
023: import org.apache.struts.action.Action;
024: import org.apache.struts.action.ActionMapping;
025: import org.apache.struts.action.ActionForm;
026: import org.apache.struts.action.DynaActionForm;
027: import org.apache.struts.action.ActionForward;
028: import org.apache.commons.lang.StringUtils;
029:
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: import com.methodhead.auth.AuthUtil;
034: import com.methodhead.auth.AuthUser;
035: import com.methodhead.auth.AuthAction;
036: import com.methodhead.util.OperationContext;
037: import com.methodhead.util.StrutsUtil;
038: import com.methodhead.aikp.IntKey;
039: import com.methodhead.sitecontext.SiteContext;
040: import com.methodhead.event.Event;
041: import java.util.List;
042: import java.util.ArrayList;
043: import java.util.Iterator;
044:
045: public class RolesAction extends AuthAction {
046:
047: // constructors /////////////////////////////////////////////////////////////
048:
049: // constants ////////////////////////////////////////////////////////////////
050:
051: // classes //////////////////////////////////////////////////////////////////
052:
053: // methods //////////////////////////////////////////////////////////////////
054:
055: protected ActionForward doRolesForm(OperationContext op,
056: RegPolicy policy) {
057:
058: //
059: // authorized?
060: //
061: String msg = policy.isRolesFormAuthorized(op);
062: if (msg != null) {
063: StrutsUtil.addMessage(op.request, msg, null, null, null);
064: return op.mapping.findForward("accessDenied");
065: }
066:
067: return new ActionForward(op.mapping.getInput());
068: }
069:
070: protected ActionForward doRoles(OperationContext op,
071: RegPolicy policy) {
072:
073: //
074: // authorized?
075: //
076: String msg = policy.isRolesAuthorized(op);
077: if (msg != null) {
078: StrutsUtil.addMessage(op.request, msg, null, null, null);
079: return op.mapping.findForward("accessDenied");
080: }
081:
082: //
083: // are we cancelling?
084: //
085: if (StringUtils.isNotBlank((String) op.form.get("cancel"))) {
086:
087: //
088: // forward to input
089: //
090: return new ActionForward("/user.do?action=edit&id="
091: + op.form.get("userid"));
092: }
093:
094: //
095: // load the user
096: //
097: User user = policy.newRegUser();
098: user.load(new IntKey(op.form.get("userid")));
099:
100: //
101: // load the site context
102: //
103: SiteContext siteContext = new SiteContext();
104: siteContext.load(new IntKey(op.form.get("siteid")));
105:
106: //
107: // is another site being selected?
108: //
109: if (StringUtils.isNotBlank((String) op.form.get("select"))) {
110:
111: //
112: // populate the role options
113: //
114: List roles = new ArrayList();
115: for (Iterator iter = user.getRoles().iterator(); iter
116: .hasNext();) {
117: Role role = (Role) iter.next();
118:
119: if (siteContext.equals(role.getSiteContext()))
120: roles.add(role.getName());
121: }
122:
123: op.form.set("roles", roles
124: .toArray(new String[roles.size()]));
125:
126: //
127: // forward to input
128: //
129: return new ActionForward(op.mapping.getInput());
130: }
131:
132: //
133: // remove any roles for the specified site
134: //
135: for (Iterator iter = user.getRoles().iterator(); iter.hasNext();) {
136: Role role = (Role) iter.next();
137:
138: if (role.getSiteContext().equals(siteContext))
139: iter.remove();
140: }
141:
142: //
143: // add roles
144: //
145: String[] roles = (String[]) op.form.get("roles");
146: for (int i = 0; i < roles.length; i++) {
147: Role role = new Role();
148: role.setSiteContext(siteContext);
149: role.setName(roles[i]);
150:
151: user.getRoles().add(role);
152: }
153:
154: //
155: // save the user
156: //
157: user.save();
158:
159: //
160: // log the event
161: //
162: Event.log(SiteContext.getDefaultContext(), op.user.getLogin(),
163: "reg", "Updated roles for " + user.getLogin() + " on "
164: + siteContext + ".");
165:
166: //
167: // forward back to edit
168: //
169: return new ActionForward("/user.do?action=edit&id="
170: + user.getInt("id"));
171: }
172:
173: public ActionForward doExecute(ActionMapping mapping,
174: ActionForm form, HttpServletRequest request,
175: HttpServletResponse response) throws Exception {
176:
177: //
178: // get some things we'll need
179: //
180: DynaActionForm dynaForm = (DynaActionForm) form;
181: RegPolicy policy = (RegPolicy) StrutsUtil.getPolicy(mapping);
182: AuthUser user = AuthUtil.getUser(request);
183:
184: OperationContext op = new OperationContext(mapping, dynaForm,
185: request, response, user);
186:
187: //
188: // execute the appopriate method
189: //
190: if (mapping.getPath().equals("/rolesForm")) {
191: return doRolesForm(op, policy);
192: }
193: if (mapping.getPath().equals("/roles")) {
194: return doRoles(op, policy);
195: }
196:
197: throw new Exception("Unexpected mapping path \""
198: + mapping.getPath() + "\"");
199: }
200:
201: // properties ///////////////////////////////////////////////////////////////
202:
203: // attributes ///////////////////////////////////////////////////////////////
204: }
|