001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.frontend.admin;
017:
018: import org.apache.cocoon.environment.Request;
019: import org.apache.cocoon.components.flow.apples.AppleRequest;
020: import org.apache.cocoon.components.flow.apples.AppleResponse;
021: import org.apache.cocoon.components.flow.apples.StatelessAppleController;
022: import org.apache.xmlbeans.XmlCursor;
023: import org.outerj.daisy.repository.Repository;
024: import org.outerj.daisy.repository.user.UserManager;
025: import org.outerj.daisy.repository.acl.AccessManager;
026: import org.outerj.daisy.frontend.util.XmlObjectXMLizable;
027: import org.outerj.daisy.frontend.util.AbstractDaisyApple;
028: import org.outerx.daisy.x10.AclDocument;
029: import org.outerx.daisy.x10.AclObjectDocument;
030: import org.outerx.daisy.x10.AclEntryDocument;
031:
032: import java.util.Map;
033: import java.util.HashMap;
034:
035: public class ManageAclApple extends AbstractDaisyApple implements
036: StatelessAppleController {
037:
038: protected void processRequest(AppleRequest appleRequest,
039: AppleResponse appleResponse) throws Exception {
040: String aclName = appleRequest.getSitemapParameter("name");
041:
042: if (!"live".equals(aclName) && !"staging".equals(aclName))
043: throw new org.apache.cocoon.ResourceNotFoundException(
044: "ACL does not exist: " + aclName);
045:
046: Repository repository = frontEndContext.getRepository();
047: AccessManager accessManager = repository.getAccessManager();
048:
049: String action = request.getParameter("action");
050: if (action == null) {
051: // show the ACL
052: AclDocument aclXml;
053: if (aclName.equals("live"))
054: aclXml = accessManager.getLiveAcl().getXml();
055: else
056: aclXml = accessManager.getStagingAcl().getXml();
057:
058: annotateAcl(aclXml, repository);
059:
060: Map<String, Object> viewData = new HashMap<String, Object>();
061: viewData.put("pageXml", new XmlObjectXMLizable(aclXml));
062: viewData.put("pageContext", frontEndContext
063: .getPageContext());
064:
065: appleResponse.sendPage("ShowAclPipe", viewData);
066: } else if (action.equals("putLive")
067: && aclName.equals("staging")) {
068: if (isConfirmed(request)) {
069: accessManager.copyStagingToLive();
070:
071: Map<String, Object> viewData = new HashMap<String, Object>();
072: viewData.put("title", "Done");
073: viewData.put("message",
074: "The staging ACL has been put live.");
075: viewData.put("linkTitle", "Administration Home");
076: viewData.put("link", getMountPoint() + "/admin");
077: viewData.put("pageContext", frontEndContext
078: .getPageContext());
079:
080: appleResponse.sendPage("MessagePagePipe", viewData);
081: } else {
082: Map<String, Object> viewData = new HashMap<String, Object>();
083: viewData.put("title", "Are you sure?");
084: viewData
085: .put("message",
086: "Are you sure you want to put the current staging ACL live?");
087: viewData
088: .put(
089: "confirmURL",
090: getMountPoint()
091: + "/admin/acl/staging?action=putLive&confirmed=true");
092: viewData.put("cancelURL", getMountPoint() + "/admin");
093: viewData.put("pageContext", frontEndContext
094: .getPageContext());
095: appleResponse
096: .sendPage("ConfirmationPagePipe", viewData);
097: }
098: } else if (action.equals("revertChanges")
099: && aclName.equals("staging")) {
100: if (isConfirmed(request)) {
101: accessManager.copyLiveToStaging();
102:
103: Map<String, Object> viewData = new HashMap<String, Object>();
104: viewData.put("title", "Done");
105: viewData
106: .put("message",
107: "The staging ACL has been overwritten with the contents of the live ACL.");
108: viewData.put("linkTitle", "Administration Home");
109: viewData.put("link", getMountPoint() + "/admin");
110: viewData.put("pageContext", frontEndContext
111: .getPageContext());
112:
113: appleResponse.sendPage("MessagePagePipe", viewData);
114: } else {
115: Map<String, Object> viewData = new HashMap<String, Object>();
116: viewData.put("title", "Are you sure?");
117: viewData
118: .put("message",
119: "Are you sure you want to overwrite the staging ACL with the live ACL?");
120: viewData
121: .put(
122: "confirmURL",
123: getMountPoint()
124: + "/admin/acl/staging?action=revertChanges&confirmed=true");
125: viewData.put("cancelURL", getMountPoint() + "/admin");
126: viewData.put("pageContext", frontEndContext
127: .getPageContext());
128: appleResponse
129: .sendPage("ConfirmationPagePipe", viewData);
130: }
131: } else {
132: throw new java.lang.Exception(
133: "Illegal request with action parameter = " + action);
134: }
135: }
136:
137: private boolean isConfirmed(Request request) {
138: String confirmed = request.getParameter("confirmed");
139: return confirmed != null && confirmed.equals("true");
140: }
141:
142: private void annotateAcl(AclDocument aclDocument,
143: Repository repository) {
144: UserManager userManager = repository.getUserManager();
145: for (AclObjectDocument.AclObject aclObject : aclDocument
146: .getAcl().getAclObjectList()) {
147: for (AclEntryDocument.AclEntry entry : aclObject
148: .getAclEntryList()) {
149: String subjectType = entry.getSubjectType().toString();
150: long subjectValue = entry.getSubjectValue();
151: String label = "";
152: if (subjectType.equals("role")) {
153: try {
154: label = userManager
155: .getRoleDisplayName(subjectValue);
156: } catch (Exception e) {
157: label = "(error)";
158: }
159: } else if (subjectType.equals("user")) {
160: try {
161: label = userManager
162: .getUserDisplayName(subjectValue);
163: } catch (Exception e) {
164: label = "(error)";
165: }
166: }
167:
168: XmlCursor cursor = entry.newCursor();
169: cursor.toNextToken();
170: cursor.insertAttributeWithValue("subjectValueLabel",
171: label);
172: cursor.dispose();
173: }
174: }
175: }
176: }
|