KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > view > admin > PermissionProcessHelper


1 /*
2  * Copyright (c) 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: 17/03/2004 - 21:19:29
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.view.admin;
44
45 import java.util.ArrayList JavaDoc;
46 import java.util.Arrays JavaDoc;
47 import java.util.Enumeration JavaDoc;
48 import java.util.List JavaDoc;
49
50 import net.jforum.ActionServletRequest;
51 import net.jforum.JForumExecutionContext;
52 import net.jforum.security.PermissionControl;
53 import net.jforum.security.Role;
54 import net.jforum.security.RoleValue;
55 import net.jforum.security.RoleValueCollection;
56
57 /**
58  * @author Rafael Steil
59  * @version $Id: PermissionProcessHelper.java,v 1.13 2006/01/29 15:07:13 rafaelsteil Exp $
60  */

61 class PermissionProcessHelper
62 {
63     private PermissionControl pc;
64     private int id;
65     
66     public PermissionProcessHelper(PermissionControl pc, int id, boolean isGroup) throws Exception JavaDoc
67     {
68         this.id = id;
69         this.pc = pc;
70         
71         this.init();
72     }
73     
74     public PermissionProcessHelper(PermissionControl pc, int id) throws Exception JavaDoc
75     {
76         this(pc, id, false);
77     }
78     
79     public void processData() throws Exception JavaDoc
80     {
81         ActionServletRequest request = JForumExecutionContext.getRequest();
82         Enumeration JavaDoc e = request.getParameterNames();
83         
84         while (e.hasMoreElements()) {
85             String JavaDoc paramName = (String JavaDoc)e.nextElement();
86             
87             if (paramName.startsWith("perm_")) {
88                 if (paramName.endsWith("$single")) {
89                     String JavaDoc paramValue = request.getParameter(paramName);
90
91                     paramName = paramName.substring(0, paramName.indexOf('$'));
92                     
93                     Role role = new Role();
94                     role.setName(paramName);
95                     
96                     if (paramValue.equals("deny")) {
97                         role.setType(PermissionControl.ROLE_DENY);
98                     }
99                     else {
100                         role.setType(PermissionControl.ROLE_ALLOW);
101                     }
102                     
103                     this.pc.addRole(this.id, role);
104                 }
105                 else {
106                     String JavaDoc[] paramValues = request.getParameterValues(paramName);
107                     
108                     RoleValueCollection roleValues = new RoleValueCollection();
109                     if (!"all".equals(paramValues[0])) {
110                         // Deny
111
for (int i = 0; i < paramValues.length; i++) {
112                             roleValues.add(this.createRoleValue(paramValues[i], PermissionControl.ROLE_DENY));
113                         }
114                         
115                         // Allow
116
List JavaDoc allowList = new ArrayList JavaDoc(Arrays.asList(this.getSplitedValues("all" + paramName)));
117                         allowList.removeAll(Arrays.asList(paramValues));
118                         
119                         this.addRoleValues(roleValues, allowList.toArray(), PermissionControl.ROLE_ALLOW);
120                     }
121                     else {
122                         this.addRoleValues(roleValues, this.getSplitedValues("all" + paramName),
123                                 PermissionControl.ROLE_ALLOW);
124                     }
125                     
126                     Role role = new Role();
127                     role.setName(paramName);
128                     this.pc.addRole(this.id, role, roleValues);
129                 }
130             }
131         }
132     }
133     
134     private String JavaDoc[] getSplitedValues(String JavaDoc paramName)
135     {
136         String JavaDoc[] allValues = JForumExecutionContext.getRequest().getParameter(paramName).split(";");
137         String JavaDoc[] returnValues = new String JavaDoc[allValues.length];
138         
139         for (int i = 0, counter = 0; i < allValues.length; i++) {
140             if (allValues[i].trim().equals("")) {
141                 continue;
142             }
143             
144             returnValues[counter++] = allValues[i];
145         }
146         
147         return returnValues;
148     }
149     
150     private void addRoleValues(RoleValueCollection roleValues, Object JavaDoc[] allValues, int permissionType)
151     {
152         for (int i = 0; i < allValues.length; i++) {
153             String JavaDoc value = (String JavaDoc)allValues[i];
154             if (value == null || value.equals("")) {
155                 continue;
156             }
157
158             roleValues.add(this.createRoleValue((String JavaDoc)allValues[i], permissionType));
159         }
160     }
161     
162     private RoleValue createRoleValue(String JavaDoc value, int type)
163     {
164         RoleValue rv = new RoleValue();
165         rv.setType(type);
166         rv.setValue(value);
167         
168         return rv;
169     }
170     
171     private void init() throws Exception JavaDoc
172     {
173         this.pc.deleteAllRoles(this.id);
174     }
175 }
176
Popular Tags