KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > screens > Default


1 package org.tigris.scarab.screens;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 // Java Stuff
50
import java.util.List JavaDoc;
51
52 // Turbine Stuff
53
import org.apache.turbine.RunData;
54 import org.apache.turbine.TemplateContext;
55 import org.apache.turbine.TemplateSecureScreen;
56 import org.apache.turbine.Turbine;
57
58 // Scarab Stuff
59
import org.tigris.scarab.services.security.ScarabSecurity;
60 import org.tigris.scarab.tools.ScarabRequestTool;
61 import org.tigris.scarab.tools.ScarabLocalizationTool;
62 import org.tigris.scarab.tools.localization.L10NKeySet;
63 import org.tigris.scarab.util.ScarabConstants;
64 import org.tigris.scarab.util.Log;
65 import org.tigris.scarab.om.Module;
66 import org.tigris.scarab.om.ModuleManager;
67 import org.tigris.scarab.om.ScarabUser;
68
69 /**
70  * This class is responsible for building the Context up
71  * for the Default Screen as well as validating Security information
72  * for all of the Screens. Please note that the Actions also may depend
73  * on the checkAuthorized() method in order to prevent the need for
74  * duplication of code.
75  *
76  * @author <a HREF="mailto:jon@collab.net">Jon S. Stevens</a>
77  * @version $Id: Default.java 9290 2004-12-03 15:57:52Z dep4b $
78  */

79 public class Default extends TemplateSecureScreen
80 {
81     /**
82      * Override the subclass and call doBuildTemplate. This is a hack.
83      * For some reason the doBuildTemplate is not being called in a
84      * few select cases, so lets just hack things to always get called
85      * properly.
86      */

87     public String JavaDoc doBuild(RunData data) throws Exception JavaDoc
88     {
89         super.doBuild(data);
90         return "";
91     }
92
93     /**
94      * builds up the context for display of variables on the page.
95      */

96     protected void doBuildTemplate(RunData data, TemplateContext context)
97         throws Exception JavaDoc
98     {
99         ScarabRequestTool scarabR = getScarabRequestTool(context);
100         // This may not be the best location for this, we might need to create
101
// a valve.
102
// check that the module exists, it may not have been created yet.
103
try
104         {
105             scarabR.getCurrentModule();
106         }
107         catch (Exception JavaDoc ignore)
108         {
109         }
110
111         // add the title text to the context.
112
ScarabLocalizationTool l10n =
113             (ScarabLocalizationTool) context.get("l10n");
114
115         // Determine whether this target requires issue types.
116
String JavaDoc altTarget = getTargetForNoIssueTypes(data);
117         boolean changeTarget = false;
118         if (altTarget != null)
119         {
120             List JavaDoc issueTypes = scarabR.getCurrentModule().getIssueTypes(true);
121             changeTarget = (issueTypes == null || issueTypes.isEmpty());
122         }
123
124         if (changeTarget)
125         {
126             // Pass control to the alternate target.
127
scarabR.setAlertMessage(L10NKeySet.IssueTypeUnavailable);
128             setTarget(data, altTarget);
129         }
130         else
131         {
132             // Add the title text to the context.
133
String JavaDoc title = null;
134             try
135             {
136                 title = getTitle(scarabR, l10n);
137             }
138             catch (Exception JavaDoc e)
139             {
140                 Log.get().info(
141                     "Error getting page title for Screen: " + data.getTarget());
142             }
143             if (title == null)
144             {
145                 title = "Scarab";
146             }
147             context.put("title", title);
148         }
149     }
150
151     protected String JavaDoc getTitle(ScarabRequestTool scarabR, ScarabLocalizationTool l10n)
152         throws Exception JavaDoc
153     {
154         return l10n.getTitle();
155     }
156
157     /**
158      * @return The value of the
159      * <code>template.[screen].noIssueTypesForwardsTo</code> property,
160      * or <code>null</code> if not set.
161      *
162      */

163     private String JavaDoc getTargetForNoIssueTypes(RunData data)
164     {
165         String JavaDoc property =
166             "template."
167                 + data.getTarget().replace(',', '/')
168                 + ".noIssueTypesForwardsTo";
169         return Turbine.getConfiguration().getString(property, null);
170     }
171
172     /**
173      * sets the template to Login.vm if the user hasn't logged in yet
174      * or if the user does not have the base permissions.
175      */

176     protected boolean isAuthorized(RunData data) throws Exception JavaDoc
177     {
178         return checkAuthorized(data);
179     }
180
181     /**
182      * Public static access to the isAuthorized() method so that
183      * an Action can use this same method to do authorization.
184      */

185     public static boolean checkAuthorized(RunData data) throws Exception JavaDoc
186     {
187         String JavaDoc template = data.getTarget();
188         {
189             template = template.replace(',', '.');
190             String JavaDoc perm = ScarabSecurity.getScreenPermission(template);
191             TemplateContext context = getTemplateContext(data);
192             ScarabRequestTool scarabR = getScarabRequestTool(context);
193             ScarabLocalizationTool l10n = getLocalizationTool(context);
194             Module currentModule = scarabR.getCurrentModule();
195             ScarabUser user = (ScarabUser) data.getUser();
196             if (perm != null)
197             {
198                 if (!user.hasLoggedIn()
199                     || !user.hasPermission(perm, currentModule))
200                 {
201                     scarabR.setInfoMessage(L10NKeySet.LoginToAccountWithPermissions);
202                     // it is very common to come from email to view a
203
// particular issue. Until a more general formula for
204
// deciding which requests might be ok to continue after
205
// a login, we will at least allow this one.
206
if ("ViewIssue.vm".equals(data.getTarget()))
207                     {
208                         data.getParameters().setString(
209                             "viewIssueId",
210                             data.getParameters().getString("id"));
211                     }
212
213                     setTargetLogin(data);
214                     scarabR.setCurrentModule(null);
215                     return false;
216                 }
217                 else if (currentModule == null)
218                 {
219                     Log.get().debug("Current module is null");
220                     scarabR.setInfoMessage(L10NKeySet.SelectModuleToWorkIn);
221                     setTargetSelectModule(data);
222                     return false;
223                 }
224             }
225             // does the user at least have a role in the module?
226
// we don't check user.hasLoggedIn() here because guest
227
// users could have a role in a module.
228
else if (
229                 currentModule != null && !user.hasAnyRoleIn(currentModule)
230                      && !user.hasAnyRoleIn(
231                          ModuleManager.getInstance(Module.ROOT_ID)))
232             {
233                 if (Log.get().isDebugEnabled())
234                 {
235                     Log.get().debug(
236                         "User ("
237                             + user.getUserId()
238                             + ") did not have any roles in current module"
239                             + currentModule.getName());
240                 }
241                 scarabR.setCurrentModule(null);
242                 data.getParameters().remove(ScarabConstants.CURRENT_MODULE);
243                 scarabR.setAlertMessage(L10NKeySet.NoPermissionInModule);
244                 setTargetSelectModule(data);
245                 return false;
246             }
247             /* FIXME
248                Breaks the ability to request roles because the permission is null and
249                the module is null, but we are logged in. John, we should assign default
250                permissions to each screen so that we can make it so that someone can be
251                logged in, but not select a module yet and be shown the select module
252                screen. (JSS)
253             
254                         else if (currentModule == null &&
255                                  user != null &&
256                                  user.hasLoggedIn())
257                         {
258                             setTargetSelectModule(data);
259                             return true;
260                         }
261             */

262         }
263         return true;
264     }
265
266     public static void setTargetSelectModule(RunData data)
267     {
268         getTemplateContext(data).put(
269             ScarabConstants.NEXT_TEMPLATE,
270             data.getParameters().getString(ScarabConstants.NEXT_TEMPLATE));
271
272         setTarget(
273             data,
274             Turbine.getConfiguration().getString(
275                 "scarab.CurrentModuleTemplate",
276                 "SelectModule.vm"));
277     }
278
279     public static void setTargetLogin(RunData data)
280     {
281         getTemplateContext(data).put(
282             ScarabConstants.NEXT_TEMPLATE,
283             data.getParameters().getString("template"));
284         setTarget(data, "Login.vm");
285     }
286
287     /**
288      * Helper method to retrieve the ScarabRequestTool from the Context
289      */

290     public static ScarabRequestTool getScarabRequestTool(TemplateContext context)
291     {
292         return (ScarabRequestTool) context.get(
293             ScarabConstants.SCARAB_REQUEST_TOOL);
294     }
295
296     /**
297      * Helper method to retrieve the ScarabLocalizationTool from the Context
298      */

299     public static ScarabLocalizationTool getLocalizationTool(TemplateContext context)
300     {
301         return (ScarabLocalizationTool) context.get(
302             ScarabConstants.LOCALIZATION_TOOL);
303     }
304 }
305
Popular Tags