KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > security > UserSecurityHelper


1 /*
2  * Copyright (c) 2003, 2004 Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: 19/03/2004 - 18:56:49
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.security;
44
45 import java.util.ArrayList JavaDoc;
46 import java.util.HashMap JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.List JavaDoc;
49 import java.util.Map JavaDoc;
50
51 /**
52  * @author Rafael Steil
53  * @version $Id: UserSecurityHelper.java,v 1.8 2005/07/26 03:05:06 rafaelsteil Exp $
54  */

55 public class UserSecurityHelper
56 {
57     public static void mergeUserGroupRoles(RoleCollection userRoles, List JavaDoc groupsRolesList)
58     {
59         Map JavaDoc newRolesMap = new HashMap JavaDoc();
60         
61         // First, iterate over all group roles and merge them with user roles
62
for (Iterator JavaDoc iter = groupsRolesList.iterator(); iter.hasNext(); ) {
63             RoleCollection rc = (RoleCollection)iter.next();
64             
65             for (Iterator JavaDoc rcIter = rc.values().iterator(); rcIter.hasNext(); ) {
66                 final Role role = (Role)rcIter.next();
67                 Role userRole = userRoles.get(role.getName());
68                 
69                 if (userRole == null) {
70                     if (newRolesMap.containsKey(role.getName())) {
71                         ((List JavaDoc)newRolesMap.get(role.getName())).add(role);
72                     }
73                     else {
74                         newRolesMap.put(role.getName(), new ArrayList JavaDoc() {{ add(role); }});
75                     }
76                 }
77                 else {
78                     // Merge the little bastards
79
for (Iterator JavaDoc vIter = role.getValues().iterator(); vIter.hasNext(); ) {
80                         RoleValue gRv = (RoleValue)vIter.next();
81                         RoleValue uRv = userRole.getValues().get(gRv.getValue());
82                         
83                         // We only check for nulls because the user role always
84
// has preference over the group role.
85
if (uRv == null) {
86                             userRole.getValues().add(gRv);
87                         }
88                     }
89                 }
90             }
91         }
92         
93         for (Iterator JavaDoc iter = newRolesMap.entrySet().iterator(); iter.hasNext(); ) {
94             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
95
96             Role newRole = new Role();
97             newRole.setName((String JavaDoc)entry.getKey());
98             newRole.setType(PermissionControl.ROLE_DENY);
99             
100             List JavaDoc roles = (List JavaDoc)entry.getValue();
101             for (Iterator JavaDoc rolesIter = roles.iterator(); rolesIter.hasNext(); ) {
102                 Role role = (Role)rolesIter.next();
103                 newRole.setId(role.getId());
104                 
105                 // Check if it's a single permission ( eg, no children values )
106
// We're assuming here that if the call to getValue() of the current
107
// role object returns 0, all other related roles will also return 0
108
if (role.getValues().size() == 0) {
109                     if (role.getType() == PermissionControl.ROLE_ALLOW) {
110                         newRole.setType(PermissionControl.ROLE_ALLOW);
111                         break;
112                     }
113                 }
114                 else {
115                     // Ok, we have some children ( like forums or categories ids )
116
// Iterate for all values of the current role, checking the
117
// access rights of each one
118
for (Iterator JavaDoc valuesIter = role.getValues().iterator(); valuesIter.hasNext(); ) {
119                         RoleValue rv = (RoleValue)valuesIter.next();
120                         RoleValue currentValue = newRole.getValues().get(rv.getValue());
121                         
122                         if (currentValue == null) {
123                             newRole.getValues().add(rv);
124                         }
125                         else {
126                             // We already have some value with this name in the collection
127
// Time to check for its rights
128
if (rv.getType() != currentValue.getType()
129                                 && rv.getType() == PermissionControl.ROLE_ALLOW) {
130                                 newRole.getValues().remove(currentValue);
131                                 currentValue.setType(PermissionControl.ROLE_ALLOW);
132                                 newRole.getValues().add(currentValue);
133                             }
134                         }
135                     }
136                 }
137             }
138             
139             userRoles.add(newRole);
140         }
141     }
142 }
143
Popular Tags