001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: 17/03/2004 - 21:19:29
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.admin;
044:
045: import java.util.ArrayList;
046: import java.util.Arrays;
047: import java.util.Enumeration;
048: import java.util.List;
049:
050: import net.jforum.JForumExecutionContext;
051: import net.jforum.context.RequestContext;
052: import net.jforum.security.PermissionControl;
053: import net.jforum.security.Role;
054: import net.jforum.security.RoleValue;
055: import net.jforum.security.RoleValueCollection;
056:
057: /**
058: * @author Rafael Steil
059: * @version $Id: PermissionProcessHelper.java,v 1.19 2006/08/24 01:07:02 rafaelsteil Exp $
060: */
061: class PermissionProcessHelper {
062: private PermissionControl pc;
063: private int groupId;
064:
065: public PermissionProcessHelper(PermissionControl pc, int id) {
066: this .groupId = id;
067: this .pc = pc;
068:
069: this .init();
070: }
071:
072: public void processData() {
073: RequestContext request = JForumExecutionContext.getRequest();
074: Enumeration e = request.getParameterNames();
075:
076: while (e.hasMoreElements()) {
077: String paramName = (String) e.nextElement();
078:
079: if (paramName.startsWith("perm_")) {
080: if (paramName.endsWith("$single")) {
081: String paramValue = request.getParameter(paramName);
082:
083: if (paramValue.equals("deny")) {
084: continue;
085: }
086:
087: paramName = paramName.substring(0, paramName
088: .indexOf('$'));
089:
090: Role role = new Role();
091: role.setName(paramName);
092:
093: this .pc.addRole(this .groupId, role);
094: } else {
095: String[] paramValues = request
096: .getParameterValues(paramName);
097: RoleValueCollection roleValues = new RoleValueCollection();
098:
099: if ("all".equals(paramValues[0])) {
100: this .addRoleValues(roleValues, this
101: .getSplitedValues("all" + paramName));
102: } else {
103: List allowList = new ArrayList(Arrays
104: .asList(this .getSplitedValues("all"
105: + paramName)));
106: allowList.removeAll(Arrays.asList(paramValues));
107:
108: this .addRoleValues(roleValues, allowList
109: .toArray());
110: }
111:
112: Role role = new Role();
113: role.setName(paramName);
114:
115: this .pc.addRole(this .groupId, role, roleValues);
116: }
117: }
118: }
119: }
120:
121: private String[] getSplitedValues(String paramName) {
122: String[] allValues = JForumExecutionContext.getRequest()
123: .getParameter(paramName).split(";");
124: String[] returnValues = new String[allValues.length];
125:
126: for (int i = 0, counter = 0; i < allValues.length; i++) {
127: if (allValues[i].trim().equals("")) {
128: continue;
129: }
130:
131: returnValues[counter++] = allValues[i];
132: }
133:
134: return returnValues;
135: }
136:
137: private void addRoleValues(RoleValueCollection roleValues,
138: Object[] allValues) {
139: for (int i = 0; i < allValues.length; i++) {
140: String value = (String) allValues[i];
141:
142: if (value == null || value.equals("")) {
143: continue;
144: }
145:
146: roleValues.add(new RoleValue((String) allValues[i]));
147: }
148: }
149:
150: private void init() {
151: this.pc.deleteAllRoles(this.groupId);
152: }
153: }
|