KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > reg > RolesAction


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.reg;
22
23 import org.apache.struts.action.Action;
24 import org.apache.struts.action.ActionMapping;
25 import org.apache.struts.action.ActionForm;
26 import org.apache.struts.action.DynaActionForm;
27 import org.apache.struts.action.ActionForward;
28 import org.apache.commons.lang.StringUtils;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import com.methodhead.auth.AuthUtil;
34 import com.methodhead.auth.AuthUser;
35 import com.methodhead.auth.AuthAction;
36 import com.methodhead.util.OperationContext;
37 import com.methodhead.util.StrutsUtil;
38 import com.methodhead.aikp.IntKey;
39 import com.methodhead.sitecontext.SiteContext;
40 import com.methodhead.event.Event;
41 import java.util.List JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Iterator JavaDoc;
44
45 public class RolesAction
46 extends
47   AuthAction {
48
49   // constructors /////////////////////////////////////////////////////////////
50

51   // constants ////////////////////////////////////////////////////////////////
52

53   // classes //////////////////////////////////////////////////////////////////
54

55   // methods //////////////////////////////////////////////////////////////////
56

57   protected ActionForward doRolesForm(
58     OperationContext op,
59     RegPolicy policy ) {
60
61     //
62
// authorized?
63
//
64
String JavaDoc msg = policy.isRolesFormAuthorized( op );
65     if ( msg != null ) {
66       StrutsUtil.addMessage( op.request, msg, null, null, null );
67       return op.mapping.findForward( "accessDenied" );
68     }
69
70     return new ActionForward( op.mapping.getInput() );
71   }
72
73   protected ActionForward doRoles(
74     OperationContext op,
75     RegPolicy policy ) {
76
77     //
78
// authorized?
79
//
80
String JavaDoc msg = policy.isRolesAuthorized( op );
81     if ( msg != null ) {
82       StrutsUtil.addMessage( op.request, msg, null, null, null );
83       return op.mapping.findForward( "accessDenied" );
84     }
85
86     //
87
// are we cancelling?
88
//
89
if ( StringUtils.isNotBlank( ( String JavaDoc )op.form.get( "cancel" ) ) ) {
90
91       //
92
// forward to input
93
//
94
return new ActionForward( "/user.do?action=edit&id=" + op.form.get( "userid" ) );
95     }
96     
97     //
98
// load the user
99
//
100
User user = policy.newRegUser();
101     user.load( new IntKey( op.form.get( "userid" ) ) );
102
103     //
104
// load the site context
105
//
106
SiteContext siteContext = new SiteContext();
107     siteContext.load( new IntKey( op.form.get( "siteid" ) ) );
108
109     //
110
// is another site being selected?
111
//
112
if ( StringUtils.isNotBlank( ( String JavaDoc )op.form.get( "select" ) ) ) {
113
114       //
115
// populate the role options
116
//
117
List JavaDoc roles = new ArrayList JavaDoc();
118       for ( Iterator JavaDoc iter = user.getRoles().iterator(); iter.hasNext(); ) {
119         Role role = ( Role )iter.next();
120
121         if ( siteContext.equals( role.getSiteContext() ) )
122           roles.add( role.getName() );
123       }
124
125       op.form.set( "roles", roles.toArray( new String JavaDoc[ roles.size() ] ) );
126
127       //
128
// forward to input
129
//
130
return new ActionForward( op.mapping.getInput() );
131     }
132
133     //
134
// remove any roles for the specified site
135
//
136
for ( Iterator JavaDoc iter = user.getRoles().iterator(); iter.hasNext(); ) {
137       Role role = ( Role )iter.next();
138
139       if ( role.getSiteContext().equals( siteContext ) )
140         iter.remove();
141     }
142
143     //
144
// add roles
145
//
146
String JavaDoc[] roles = ( String JavaDoc[] )op.form.get( "roles" );
147     for ( int i = 0; i < roles.length; i++ ) {
148       Role role = new Role();
149       role.setSiteContext( siteContext );
150       role.setName( roles[ i ] );
151
152       user.getRoles().add( role );
153     }
154
155     //
156
// save the user
157
//
158
user.save();
159
160     //
161
// log the event
162
//
163
Event.log(
164       SiteContext.getDefaultContext(),
165       op.user.getLogin(),
166       "reg",
167       "Updated roles for " + user.getLogin() + " on " + siteContext + "." );
168
169     //
170
// forward back to edit
171
//
172
return new ActionForward(
173       "/user.do?action=edit&id=" + user.getInt( "id" ) );
174   }
175
176   public ActionForward doExecute(
177     ActionMapping mapping,
178     ActionForm form,
179     HttpServletRequest JavaDoc request,
180     HttpServletResponse JavaDoc response )
181   throws
182     Exception JavaDoc {
183
184     //
185
// get some things we'll need
186
//
187
DynaActionForm dynaForm = ( DynaActionForm )form;
188     RegPolicy policy = ( RegPolicy )StrutsUtil.getPolicy( mapping );
189     AuthUser user = AuthUtil.getUser( request );
190
191     OperationContext op =
192       new OperationContext( mapping, dynaForm, request, response, user );
193
194     //
195
// execute the appopriate method
196
//
197
if ( mapping.getPath().equals( "/rolesForm" ) ) {
198       return doRolesForm( op, policy );
199     }
200     if ( mapping.getPath().equals( "/roles" ) ) {
201       return doRoles( op, policy );
202     }
203
204     throw
205       new Exception JavaDoc( "Unexpected mapping path \"" + mapping.getPath() + "\"" );
206   }
207
208   // properties ///////////////////////////////////////////////////////////////
209

210   // attributes ///////////////////////////////////////////////////////////////
211
}
212
Popular Tags