001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.ac.usecases;
019:
020: import java.util.Arrays;
021: import java.util.SortedSet;
022: import java.util.TreeSet;
023:
024: import org.apache.lenya.ac.Authorizer;
025: import org.apache.lenya.ac.Role;
026: import org.apache.lenya.cms.ac.usecase.UsecaseAuthorizer;
027: import org.apache.lenya.cms.publication.Publication;
028: import org.apache.lenya.cms.publication.PublicationException;
029: import org.apache.lenya.cms.publication.URLInformation;
030: import org.apache.lenya.cms.usecase.UsecaseResolver;
031:
032: /**
033: * Edit usecase policies.
034: */
035: public class Usecases extends AccessControlUsecase {
036:
037: protected void initParameters() {
038: super .initParameters();
039:
040: UsecaseResolver resolver = null;
041: try {
042: resolver = (UsecaseResolver) this .manager
043: .lookup(UsecaseResolver.ROLE);
044: String[] allUsecases = resolver.getUsecaseNames();
045: SortedSet rootUsecases = new TreeSet();
046: for (int i = 0; i < allUsecases.length; i++) {
047: if (allUsecases[i].indexOf("/") == -1) {
048: rootUsecases.add(allUsecases[i]);
049: }
050: }
051:
052: String[] usecases = (String[]) rootUsecases
053: .toArray(new String[rootUsecases.size()]);
054:
055: setParameter("usecases", usecases);
056:
057: Role[] roles = getAccessController()
058: .getAccreditableManager().getRoleManager()
059: .getRoles();
060: String[] roleNames = new String[roles.length];
061: for (int r = 0; r < roles.length; r++) {
062: roleNames[r] = roles[r].getId();
063: }
064: Arrays.sort(roleNames);
065: setParameter("roles", roleNames);
066:
067: Publication pub = getPublication();
068: setParameter("publicationId", pub.getId());
069: setParameter("templates", pub.getTemplateIds());
070:
071: for (int u = 0; u < usecases.length; u++) {
072: for (int r = 0; r < roles.length; r++) {
073: boolean value = getUsecaseAuthorizer().isPermitted(
074: usecases[u], pub, roles[r]);
075: setParameter(usecases[u] + ":" + roles[r], Boolean
076: .valueOf(value));
077: }
078: }
079:
080: } catch (Exception e) {
081: throw new RuntimeException(e);
082: } finally {
083: if (resolver != null) {
084: this .manager.release(resolver);
085: }
086: }
087:
088: }
089:
090: protected Publication getPublication() throws PublicationException {
091: String pubId = new URLInformation(getSourceURL())
092: .getPublicationId();
093: Publication pub = getDocumentFactory().getPublication(pubId);
094: return pub;
095: }
096:
097: protected void doExecute() throws Exception {
098: super .doExecute();
099:
100: String[] usecases = (String[]) getParameter("usecases");
101: String[] roleNames = (String[]) getParameter("roles");
102:
103: Publication pub = getPublication();
104: for (int u = 0; u < usecases.length; u++) {
105: for (int r = 0; r < roleNames.length; r++) {
106: String key = usecases[u] + ":" + roleNames[r];
107: String stringValue = getBooleanCheckboxParameter(key);
108: boolean value = Boolean.valueOf(stringValue)
109: .booleanValue();
110: Role role = getAccessController()
111: .getAccreditableManager().getRoleManager()
112: .getRole(roleNames[r]);
113: getUsecaseAuthorizer().setPermission(usecases[u], pub,
114: role, value);
115: }
116: }
117:
118: }
119:
120: private UsecaseAuthorizer authorizer;
121:
122: protected UsecaseAuthorizer getUsecaseAuthorizer() {
123: if (this .authorizer == null) {
124: Authorizer[] authorizers = getAccessController()
125: .getAuthorizers();
126: for (int i = 0; i < authorizers.length; i++) {
127: if (authorizers[i] instanceof UsecaseAuthorizer) {
128: this .authorizer = (UsecaseAuthorizer) authorizers[i];
129: }
130: }
131: }
132: return this.authorizer;
133: }
134:
135: }
|