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.view;
16
17 import org.figure8.join.core.InvalidParameterException;
18 import org.figure8.join.businessobjects.security.User;
19 import org.figure8.join.businessobjects.security.Role;
20 import org.figure8.join.businessobjects.security.Permission;
21
22 import java.io.Serializable;
23 import java.util.List;
24 import java.util.HashMap;
25 import java.util.Collection;
26 import java.util.ArrayList;
27 /**
28 * This is a JavaBean encapsulating a User domain model object and its
29 * permission. This is necessary because in our model, a User is decoupled
30 * from its security permission.<br/>
31 * Instance of <code>UserView</code> class are intended to be stored within
32 * a HttpSession.
33 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
34 * @version $Revision: 1.2 $
35 */
36 public class UserView implements Serializable{
37
38
39
40 /** The encapsulated user domain object */
41 protected User user = null;
42 /**
43 * Map containing user's permission. Keys are security roles,
44 * values are permissions on different resources for this role.
45 */
46 protected HashMap permissions = new HashMap();
47 /**
48 * Non encrypted password of user (the one provided through authentication form)
49 * This is necessary in case of user calling a remote Join web-service that expect
50 * clear password for doing its own authentication. Not so unsafe because clear
51 * password will only be kept in VM memory...
52 */
53 protected String clearPassword = null;
54
55
56
57
58 /**
59 * Creates a new instance of UserView
60 * @param user The encasuplated user business object
61 */
62 public UserView(User user){
63 this.user = user;
64 }
65
66 /**
67 * Creates a new instance of UserView with a set of <code>Permission</code>s
68 * @param user The encasuplated user business object
69 * @param permissions A list of <code>org.figure8.join.businessobjects.security.Permission</code>s
70 */
71 public UserView(User user, List permissions){
72 this.user = user;
73 if (permissions != null)
74 for (int i=0; i < permissions.size(); i++)
75 addPermission((Permission)permissions.get(i));
76 }
77
78
79
80
81 /** @return The encapsulated user business object */
82 public User getUser(){
83 return user;
84 }
85
86 /**
87 * Add a security permission to this user.
88 * @param permission Permission to add
89 */
90 public void addPermission(Permission permission){
91
92 List permissionsForRole = null;
93 Object obj = permissions.get(permission.getRole());
94
95 if (obj != null)
96 permissionsForRole = (List)obj;
97 else
98 permissionsForRole = new ArrayList();
99
100
101 permissionsForRole.add(permission);
102 permissions.put(permission.getRole(), permissionsForRole);
103 }
104
105 /** @return All the permissions acquired by this user */
106 public Collection getPermissions(){
107 return permissions.values();
108 }
109 /**
110 * @param role The security role to filter permissions for
111 * @return All the permissions correspondinf to role acquired by this user
112 */
113 public Collection getPermissions(Role role){
114 return (Collection)permissions.get(role);
115 }
116
117 /**
118 * Tells if this user has permission to endorse this security role
119 * @param role Security role that has to be endorsed by user
120 * @return true is user as permission corresponding to role, false otherwise
121 */
122 public boolean hasPermission(Role role){
123 if (permissions.get(role) != null)
124 return true;
125 return false;
126 }
127
128 /**
129 * Tells if this user has permission to endorse this security role for this resource
130 * @param resource Entity for whom user should have permission
131 * @param role Security role that has to be endorsed by user
132 * @return true is user as permission corresponding to role for resource, false otherwise
133 */
134 public boolean hasPermissionForResource(Object resource, Role role){
135
136 String resourceId = null;
137 try {resourceId = role.getPermissionResourceResolver().getResourceId(resource);}
138 catch (InvalidParameterException ipe) {return false;}
139
140 if (hasPermission(role)){
141 List permissionsForRole = (List)permissions.get(role);
142
143 for (int i=0; i < permissionsForRole.size(); i++){
144 Permission permission = (Permission)permissionsForRole.get(i);
145 if (permission.isResourcePermission() && permission.getResourceId().equals(resourceId))
146 return true;
147 }
148 }
149 return false;
150 }
151
152 /** @return The non encrypted password of wrapped user */
153 public String getClearPassword(){
154 return clearPassword;
155 }
156 /** @param clearPassword The non encrypted password of wrapped user */
157 public void setClearPassword(String clearPassword){
158 this.clearPassword = clearPassword;
159 }
160 }