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.businessfacades.security;
16
17 import org.figure8.join.core.DuplicateEntityException;
18 import org.figure8.join.businessobjects.security.Role;
19 import org.figure8.join.businessobjects.security.Permission;
20 import org.figure8.join.businessobjects.security.User;
21 import org.figure8.join.businessobjects.security.persistence.RoleDao;
22 import org.figure8.join.businessobjects.security.persistence.PermissionDao;
23 import org.figure8.join.util.LogUtil;
24
25 import org.apache.commons.logging.Log;
26
27 import java.util.List;
28 /**
29 * Default implementation of <code>PermissionManager</code>
30 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
31 * @version $Revision: 1.1 $
32 */
33 public class DefaultPermissionManager implements PermissionManager{
34
35
36
37 /** Get a commons logger */
38 private static Log log = LogUtil.getLog(DefaultPermissionManager.class);
39
40
41
42
43 /** Role data access object */
44 protected RoleDao roleDao = null;
45 /** Permission data access object */
46 protected PermissionDao dao = null;
47
48
49
50
51 /** Creates a new instance of DefaultPermissionManager */
52 public DefaultPermissionManager(){
53 }
54
55
56
57
58 /** @param dao A Role data access object */
59 public void setRoleDao(RoleDao dao){
60 this.roleDao = dao;
61 }
62 /** @param dao A Permission data access object */
63 public void setPermissionDao(PermissionDao dao){
64 this.dao = dao;
65 }
66
67
68
69
70 /**
71 * Save or update a givean security <b>role</b> into datastore. This is
72 * indeed a create or update method.
73 * @param role The security role to save
74 * @throws DuplicateEntityException if a role with same name already exists
75 */
76 public void saveRole(Role role) throws DuplicateEntityException{
77 if (log.isInfoEnabled())
78 log.info("Saving security role: " + role);
79
80 if (role.isTransient()){
81 Role other = roleDao.getRole(role.getName());
82 if (other != null){
83 log.error("A Role with same name already exists: " + other.getDescription());
84 throw new DuplicateEntityException("A Role with same name already exists", other);
85 }
86 }
87
88 roleDao.save(role);
89 }
90
91 /**
92 * Remove a given security <b>role</b> from datastore
93 * @param role The role to remove
94 */
95 public void removeRole(Role role){
96 log.info("Removing security role: " + role);
97 roleDao.remove(role);
98 }
99
100 /**
101 * Retrieve the available security roles within application
102 * @return A List of {@code org.figure8.join.businessobjects.security.Role}
103 */
104 public List getRoles(){
105
106 List result = roleDao.findAll();
107 if (log.isDebugEnabled())
108 log.debug("Found " + result.size() + " security roles in application");
109 return result;
110 }
111
112 /**
113 * Retrieve an application security Role using its unique identifier
114 * @param id Unique identifier of role within datastore
115 * @return The corresponding role or null if no role has this id
116 */
117 public Role getRole(long id){
118 return roleDao.getRole(id);
119 }
120
121 /**
122 * Retrieve an application security Role using its name
123 * @param name The name of security role to retrieve
124 * @return The corresponding role or null if no role has this name
125 */
126 public Role getRole(String name){
127 return roleDao.getRole(name);
128 }
129
130 /**
131 * Save or update a given <b>permission</b> into datastore
132 * @param permission The permission to save
133 */
134 public void savePermission(Permission permission){
135 if (log.isDebugEnabled())
136 log.debug("Saving permission: " + permission);
137 dao.save(permission);
138 }
139
140 /**
141 * Remove a given <b>permission</b> from datastore
142 * @param permission The permission to remove
143 */
144 public void removePermission(Permission permission){
145 if (log.isDebugEnabled())
146 log.debug("Removing permission: " + permission);
147 dao.remove(permission);
148 }
149
150 /**
151 * Remove all permissions assigned to a specific user
152 * @param user The user to remove permissions for
153 */
154 public void removeAllUserPermissions(User user){
155 if (log.isDebugEnabled())
156 log.debug("Removing all permissions for user: " + user.getLogin());
157
158 List permissions = getUserPermissions(user);
159 for (int i=0; i<permissions.size(); i++)
160 removePermission((Permission)permissions.get(i));
161 }
162
163 /**
164 * Tells if a user has permission to endorse this security role
165 * @param role Security role that has to be endorsed by user
166 * @param userId Id of user for whom permission test is done
167 * @return true is user as permission corresponding to role, false otherwise
168 */
169 public boolean hasPermission(Role role, String userId){
170 return false;
171 }
172
173 /**
174 * Tells if a user has permission to endorse this security role for this resource
175 * @param resource Entity for whom user should have permission
176 * @param role Security role that has to be endorsed by user
177 * @param userId Id of user for whom permission test is done
178 * @return true is user as permission corresponding to role for resource, false otherwise
179 */
180 public boolean hasPermissionForResource(Object resource, Role role, String userId){
181 return false;
182 }
183
184 /**
185 * Retrieves all the permission acquired by a specified User
186 * @param user The user to retrieve permissions for
187 * @return A list of <code>org.figure8.join.businessobjects.security.Permission</code>
188 */
189 public List getUserPermissions(User user){
190 if (log.isDebugEnabled())
191 log.debug("Retrieving permissions for user: " + user.getLogin());
192 return dao.getPermissionsByUser(user);
193 }
194 }