KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > api > securitymgr > session > SecurityManager


1 /*
2  * SecurityManager.java
3  *
4  * Created on May 12, 2003, 2:54 PM
5  */

6
7 package com.raptus.owxv3.api.securitymgr.session;
8
9 import java.util.*;
10
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12
13 import org.apache.struts.Globals;
14
15 import com.raptus.owxv3.*;
16 import com.raptus.owxv3.api.securitymgr.SecurityMgrIF;
17
18 /**
19  *
20  * @author root
21  */

22 public class SecurityManager implements SecurityMgrIF
23 {
24     protected HttpServletRequest JavaDoc req = null;
25         
26     public SecurityManager()
27     {
28     }
29     
30     /**
31      * Creates a new instance of SecurityManager, based on the request
32      *
33      * @param req the Request containing all security info
34      */

35     public SecurityManager(HttpServletRequest JavaDoc req)
36     {
37         this.req = req;
38     }
39     
40     /**
41      * Update the requst object on an already existing security manager
42      *
43      * @param req the request containing all security info
44      */

45     public void setRequest(HttpServletRequest JavaDoc req)
46     {
47         this.req = req;
48     }
49     
50     /** Check to see if user has access to this element.
51      * The user and the required info should be taken from the object passed to
52      * factory, when this SecurityManager was created.
53      *
54      * @param element the element for wich we are checking access rights
55      * @return true if the user has access to this element
56      * false if user does not have access to this element
57      *
58      */

59     public boolean hasAccess(VModuleSectionElement element)
60     {
61         // first check if user has acces in section containing this element
62
// this will also check if user has access inside the vmodule
63
VModuleSection section = element.getSection();
64         if(!this.hasAccess(section))
65         {
66             return false;
67         }
68
69 // LoggingManager.log("Checking access for element "+element.getIdentification(),this);
70

71         String JavaDoc roles[] = element.getRoles();
72         if(roles != null)
73         {
74             LoggingManager.log("element roles:",this);
75             for(int i=0;i<roles.length;i++)
76             {
77                 LoggingManager.log("\t->"+roles[i],this);
78             }
79         }
80         
81         if(roles == null)
82         {
83             // this section has no defined roles, so the implicit access roles
84
// are the ones for the vmodule
85
// LoggingManager.log("No roles defined for element "+element.getIdentification()+
86
// " granted access through section's roles!",this);
87
return true;
88         }
89         else
90         {
91             return checkAccess(roles);
92         }
93     }
94     
95     /** Check to see if user has access to this section.
96      * The user and the required info should be taken from the object passed to
97      * factory, when this SecurityManager was created.
98      *
99      * @param section the section for wich we are checking access rights
100      * @return true if the user has access to this section
101      * false if user does not have access to this section
102      *
103      */

104     public boolean hasAccess(VModuleSection section)
105     {
106         // first check if user has access to vmodule containing this
107
// section
108
VModule vm = section.getVModule();
109         if(!this.hasAccess(vm))
110         {
111             return false;
112         }
113
114 // LoggingManager.log("Checking access for section "+section.getIdentification(),this);
115
// check if user has access inside the section
116
String JavaDoc[] roles = section.getRoles();
117         if(roles == null)
118         {
119             // this section has no defined roles, so the implicit access roles
120
// are the ones for the vmodule
121
// LoggingManager.log("No roles defined for section "+section.getIdentification()+
122
// " granted access thrugh vmodule's roles!",this);
123
return true;
124         }
125         else
126         {
127             return checkAccess(roles);
128         }
129     }
130
131     /**
132      * check if current user has access according to roles specified
133      *
134      * @roles the roles to check if user has access
135      */

136     protected boolean checkAccess(String JavaDoc[] roles)
137     {
138         // get the current user
139
com.raptus.owxv3.api.usermgr.User user = (com.raptus.owxv3.api.usermgr.User)
140             req.getSession().getAttribute(Constants.SESSIONKEY_USER);
141 /*
142         if(user == null)
143         {
144             LoggingManager.log("no user logged in!",this);
145         }
146         else
147         {
148             LoggingManager.log("User name='"+user.getUsername()+"'",this);
149         }
150 */

151         // if no roles defined, by default does not have access
152
if(roles == null)
153         {
154 // LoggingManager.log("Access denied, no roles defined when checking access,user=!"+user,this);
155
return false;
156         }
157         
158         // if user is not authenticated, check if guest role was specified
159
if(user == null)
160         {
161 // LoggingManager.log("User is not logged in, checking for guest role",this);
162
for(int i=0;i<roles.length;i++)
163             {
164 // LoggingManager.log("\tguest <-> "+roles[i],this);
165
if("guest".equals(roles[i]))
166                 {
167 // LoggingManager.log("Allowing access, based on guest role",this);
168
return true;
169                 }
170             }
171          
172             // user not logged in and guest role was not specified.
173
// LoggingManager.log("Access denied, user not logged in and no guest role was found!",this);
174
return false;
175         }
176         
177 // LoggingManager.log("User is logged in, checking for roles matching the user's role",this);
178
Iterator it = com.raptus.owxv3.api.usermgr.UserMgrFactory.getInstance().createUserManager(null).getRolesAsStringForUser(user);
179         Vector vuroles = new Vector();
180         while(it.hasNext())
181         {
182             vuroles.add(it.next());
183         }
184         
185         String JavaDoc uroles[] = new String JavaDoc[vuroles.size()];
186         vuroles.toArray(uroles);
187         for(int i=0;i<roles.length;i++)
188         {
189             for(int j=0;j<uroles.length;j++)
190             {
191                 LoggingManager.log("\t"+uroles[j]+" <-> "+roles[i]);
192             
193                 // verify if vmodule has specified a role in wich the current user
194
// is member
195
if(uroles[j].equals(roles[i]))
196                 {
197                     // got a role
198
// LoggingManager.log("Allowing access to "+user.getName()+" based on role "+roles[i],this);
199
return true;
200                 }
201                 if("guest".equals(roles[i]))
202                 {
203 // LoggingManager.log("Allowing access to "+user.getName()+" based on role "+roles[i],this);
204
return true;
205                 }
206             }
207         }
208         
209 // LoggingManager.log("Access denied, no matching role for "+user.getName()+"!",this);
210
return false;
211     }
212     
213     /** Check to see if user has access to this vmodule.
214      * The user and the required info should be taken from the object passed to
215      * factory, when this SecurityManager was created.
216      *
217      * @param vm the vmodule for wich we are checking access rights
218      * @return true if the user has access to this vmodule
219      * false if user does not have access to this vmodule
220      *
221      */

222     public boolean hasAccess(VModule vm)
223     {
224         String JavaDoc[] roles = vm.getRoles();
225 // LoggingManager.log("Checking access for vmodule "+vm.getIdentification(),this);
226
return checkAccess(roles);
227     }
228     
229     /**
230      * Return the current user's locale
231      *
232      * @return the Locale of the curent user
233      */

234     public Locale getLocale()
235     {
236         // load user's default locale, actually the locale defined to be default
237
// in config file, if this is not defined in session
238
// another implemenmtations could implement this in a different way
239
Locale l=null;
240         if(req == null)
241         {
242             // we don;t have a request, so no session, load data from config file
243
XMLConfigManager cm = XMLConfigManager.getInstance();
244             String JavaDoc slocale=cm.getPropertyByTree("/virtualhost/globalconfig/defaultlocale","value");
245             if(slocale == null)
246             {
247                 LoggingManager.log("Default locale not defined! Defaulting to en_GB",this);
248                 slocale = "en_GB";
249             }
250             
251             StringTokenizer st = new StringTokenizer(slocale);
252             String JavaDoc s1 = st.nextToken();
253             
254             if(st.hasMoreTokens())
255             {
256                 l = new Locale(s1, st.nextToken());
257             }
258             else
259             {
260                 l = new Locale(s1);
261             }
262             LoggingManager.log("Loaded locale from config file",this);
263         }
264         else
265         {
266             // we have request object.
267
// first check if is defined as request param. if not,
268
// check if we have locale defined in session. If not,
269
// then load it from config file
270
if(req.getParameter(Constants.HTTPGET_PARAM_LOCALE) != null &&
271             req.getParameter(Constants.HTTPGET_PARAM_LOCALE).length()>0)
272             {
273                 // load locale from parameters
274
String JavaDoc sl = (String JavaDoc)req.getParameter(Constants.HTTPGET_PARAM_LOCALE);
275                 LoggingManager.log("Locale:"+sl,this);
276                 String JavaDoc sres[] = split(sl, "_");
277                 if(sres.length==1)
278                 {
279                     l = new Locale(sres[0]);
280                 }
281                 else
282                 {
283                     l = new Locale(sres[0],sres[1]);
284                 }
285                 LoggingManager.log("Loaded locale from url params",this);
286             }
287             else
288             {
289                 // we don't have it as parameter, try session
290
l = (Locale)req.getSession().getAttribute(Globals.LOCALE_KEY);
291                 if(l == null)
292                 {
293                     // not in session either, load from config file
294
XMLConfigManager cm = XMLConfigManager.getInstance();
295                     String JavaDoc slocale=cm.getPropertyByTree("/virtualhost/globalconfig/defaultlocale","value");
296                     if(slocale == null)
297                     {
298                         LoggingManager.log("Default locale not defined! Defaulting to en_GB",this);
299                         slocale = "en_GB";
300                     }
301                     LoggingManager.log("Locale in cfgfile is "+slocale,this);
302                     StringTokenizer st = new StringTokenizer(slocale,"_");
303                     String JavaDoc s1 = st.nextToken();
304
305                     if(st.hasMoreTokens())
306                     {
307                         l = new Locale(s1, st.nextToken());
308                     }
309                     else
310                     {
311                         l = new Locale(s1);
312                     }
313                     LoggingManager.log("Loaded locale from config file",this);
314                 }
315                 else
316                 {
317                     LoggingManager.log("Loaded locale from session"+l.toString(),this);
318                 }
319             }
320         }
321         
322         LoggingManager.log("Final locale:"+l.toString(),this);
323         // if we have request, then save the locale in session
324
if(req != null)
325         {
326             req.getSession().setAttribute(Globals.LOCALE_KEY, l);
327         }
328         return l;
329     }
330     
331     public String JavaDoc[] split(String JavaDoc str, String JavaDoc delim)
332     {
333         StringTokenizer st = new StringTokenizer(str, delim);
334         String JavaDoc[] res = new String JavaDoc[st.countTokens()];
335         int i=0;
336         while(st.hasMoreTokens())
337         {
338             res[i++] = st.nextToken();
339         }
340         
341         return res;
342     }
343     
344     /** return true if user needs to log in
345      *
346      * The user has to log in if:
347      * - does not have access, and is not loged in
348      *
349      * The user should not log in if:
350      * - it does have access
351      * - if is does not have access, and is loged in
352      *
353      * @return true if user has to log in
354      *
355      */

356     public boolean needLoginForRoles(String JavaDoc[] roles)
357     {
358         com.raptus.owxv3.api.usermgr.User user = (com.raptus.owxv3.api.usermgr.User)
359             req.getSession().getAttribute(Constants.SESSIONKEY_USER);
360                 
361         if(checkAccess(roles))
362         {
363             // already have access for roles, no need to login
364
return false;
365         }
366         
367         // if does not have access, and not logged in
368
// then should log in
369
if(user == null)
370         {
371             return true;
372         }
373         else
374         {
375             // is logged in, and does not have access
376
// does not need to login
377
return false;
378         }
379     }
380     
381     /** return true if user needs to log in
382      *
383      * The user has to log in if:
384      * - does not have access, and is not loged in
385      *
386      * The user should not log in if:
387      * - it does have access
388      * - if is does not have access, and is loged in
389      *
390      * @return true if user has to log in
391      *
392      */

393     public boolean needLogin(VModuleSectionElement element)
394     {
395         VModuleSection section = element.getSection();
396         VModule vm = section.getVModule();
397         if(needLoginForRoles(vm.getRoles()))
398         {
399 // LoggingManager.log("Need login, based on vmodule's roles",this);
400
return true;
401         }
402         
403         if(needLoginForRoles(section.getRoles()))
404         {
405 // LoggingManager.log("Need login, based on section's roles",this);
406
return true;
407         }
408         
409         if(needLoginForRoles(element.getRoles()))
410         {
411 // LoggingManager.log("Need login, based on element's roles",this);
412
return true;
413         }
414
415         return false;
416     }
417     
418     /**
419      * Return the vmodules to which currently logged in (or guest, if not) user
420      * has access
421      */

422     public String JavaDoc[] getAllowedVModules()
423     {
424         Vector result=new Vector();
425         String JavaDoc[] vmodules=VModuleManager.getInstance().getVModules();
426         
427         for(int i=0;i<vmodules.length;i++)
428         {
429             VModule vm = VModuleManager.getInstance().getVModule(vmodules[i]);
430             if(hasAccess(vm))
431             {
432                 result.add(vm);
433             }
434         }
435         
436         String JavaDoc[] res = new String JavaDoc[result.size()];
437         for(int i=0;i<result.size();i++)
438         {
439             VModule vm = (VModule)result.get(i);
440             res[i] = vm.getIdentification();
441         }
442         
443         return res;
444     }
445 }
446
Popular Tags