1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.control;
16
17 import org.figure8.join.view.UserView;
18 import org.figure8.join.businessobjects.security.Role;
19
20 import javax.servlet.http.HttpSessionBindingEvent;
21 import javax.servlet.http.HttpSessionBindingListener;
22
23 import java.io.Serializable;
24 import java.util.Collection;
25 /**
26 * Used to store information about a specific user. This class is used so that
27 * the information is not scattered throughout the HttpSession. Only this object
28 * is stored in the session for the user. This class implements the HttpSession
29 * BindingListener so that it can be notified of session timeoutand perform cleanup.
30 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
31 * @version $Revision: 1.1 $
32 */
33 public class UserContainer implements Serializable, HttpSessionBindingListener{
34
35
36
37 /** A wrapped user view if container is for authenticated user */
38 private UserView view = null;
39
40
41
42
43 /** Creates a new instance of UserContainer */
44 public UserContainer(){
45 super();
46 }
47
48
49
50
51 /** @return The associated user view */
52 public UserView getView(){
53 return view;
54 }
55 /** @param view The user view to associate with container */
56 public void setView(UserView view){
57 this.view = view;
58 }
59
60 /**
61 * Check if user has the required role. Delegates call to UserView if present.
62 * @param role Security role the user should endorse
63 * @return True if user has specified role, false otherwise
64 */
65 public boolean isUserInRole(Role role){
66 if (view != null)
67 return view.hasPermission(role);
68 return false;
69 }
70
71 /**
72 * Check if user has the required role for given resource.
73 * @param role Security role the user should endorse
74 * @param resource Resource that user has to be granted to for the role
75 * @return True is user has specified role for specified resource, false otherwise
76 */
77 public boolean isUserInRoleForResource(Role role, Object resource){
78 if (view != null)
79 return view.hasPermissionForResource(resource, role);
80 return false;
81 }
82
83 /**
84 * Get the resources for this role if user has the given role.
85 * Resources can represents deliverables, environments on which user
86 * has rights, depending on the role.
87 * @param role Security role the user should endorder
88 * @return A collection of resources this user has corresponding security role
89 */
90
91
92
93
94
95
96
97
98
99
100
101 /** Cleanup any opened resources */
102 protected void cleanup(){
103 this.view = null;
104 }
105
106
107
108
109 /**
110 * The container calls this method when this object is
111 * being bound to the user's session.
112 * @param httpSessionBindingEvent The binding event raised by servlet container
113 */
114 public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent){
115 }
116
117 /**
118 * The container calls this method when this object is
119 * being unbound from the user's session.
120 * @param httpSessionBindingEvent The binding event raised by servlet container
121 */
122 public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent){
123 cleanup();
124 }
125 }