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.books.frontend;
017:
018: import org.outerj.daisy.frontend.util.AbstractDaisyApple;
019: import org.outerj.daisy.frontend.util.FormHelper;
020: import org.outerj.daisy.frontend.util.HttpMethodNotAllowedException;
021: import org.outerj.daisy.frontend.util.EncodingUtil;
022: import org.outerj.daisy.frontend.FrontEndContext;
023: import org.outerj.daisy.books.store.*;
024: import org.outerj.daisy.books.store.impl.AclResult;
025: import org.outerj.daisy.books.store.impl.BookAclEvaluator;
026: import org.outerj.daisy.repository.Repository;
027: import org.outerj.daisy.repository.user.UserManager;
028: import org.apache.cocoon.components.flow.apples.AppleRequest;
029: import org.apache.cocoon.components.flow.apples.AppleResponse;
030: import org.apache.cocoon.forms.formmodel.*;
031: import org.apache.cocoon.forms.validation.WidgetValidator;
032: import org.apache.cocoon.forms.util.I18nMessage;
033: import org.apache.cocoon.forms.FormContext;
034: import org.apache.avalon.framework.service.Serviceable;
035: import org.apache.avalon.framework.service.ServiceManager;
036: import org.apache.avalon.framework.service.ServiceException;
037:
038: import java.util.Map;
039: import java.util.HashMap;
040: import java.util.Locale;
041:
042: public class BookAclEditorApple extends AbstractDaisyApple implements
043: Serviceable {
044: private ServiceManager serviceManager;
045: private boolean init = false;
046: private Form form;
047: private BookInstance bookInstance;
048: private Locale locale;
049:
050: public void service(ServiceManager serviceManager)
051: throws ServiceException {
052: this .serviceManager = serviceManager;
053: }
054:
055: protected void processRequest(AppleRequest appleRequest,
056: AppleResponse appleResponse) throws Exception {
057: if (!init) {
058: if (!request.getMethod().equals("POST"))
059: throw new HttpMethodNotAllowedException(request
060: .getMethod());
061:
062: String bookInstanceName = appleRequest
063: .getSitemapParameter("bookInstanceName");
064: Repository repository = frontEndContext.getRepository();
065: BookStore bookStore = (BookStore) repository
066: .getExtension("BookStore");
067: bookInstance = bookStore.getBookInstance(bookInstanceName);
068: BookAcl bookAcl = bookInstance.getAcl();
069:
070: form = FormHelper.createForm(serviceManager,
071: "resources/form/bookacl_definition.xml");
072: form.getChild("editmode").setValue("gui");
073: form.getChild("editmode").setState(WidgetState.INVISIBLE);
074: initForm(form, repository);
075: load(form, bookAcl);
076: annotateAclSubjectValues(form, repository);
077:
078: locale = frontEndContext.getLocale();
079:
080: init = true;
081: appleResponse.redirectTo(EncodingUtil
082: .encodePath(getMountPoint() + "/books/"
083: + bookInstanceName + "/acl/"
084: + getContinuationId()));
085: return;
086: }
087:
088: if (request.getMethod().equals("GET")) {
089: showForm(frontEndContext, appleResponse);
090: } else if (request.getMethod().equals("POST")) {
091: boolean finished = form.process(new FormContext(request,
092: locale));
093: if (finished) {
094: BookAcl bookAcl = getBookAcl(form);
095: bookInstance.lock();
096: try {
097: bookInstance.setAcl(bookAcl);
098: } finally {
099: bookInstance.unlock();
100: }
101: appleResponse.redirectTo(EncodingUtil
102: .encodePath(getMountPoint() + "/books"));
103: } else {
104: showForm(frontEndContext, appleResponse);
105: }
106: } else {
107: throw new HttpMethodNotAllowedException(request.getMethod());
108: }
109:
110: }
111:
112: private void showForm(FrontEndContext frontEndContext,
113: AppleResponse appleResponse) throws Exception {
114: Map<String, Object> viewData = new HashMap<String, Object>();
115: viewData.put("CocoonFormsInstance", form);
116: viewData.put("locale", locale);
117: viewData.put("pageContext", frontEndContext.getPageContext());
118: appleResponse.sendPage("Form-bookacl-Pipe", viewData);
119: }
120:
121: public static void load(Form form, BookAcl bookAcl) {
122: Repeater entriesRepeater = (Repeater) form
123: .lookupWidget("editors/gui/entries");
124: entriesRepeater.clear(); // in case load is called on an already loaded form
125: BookAclEntry[] entries = bookAcl.getEntries();
126: for (BookAclEntry entry : entries) {
127: Repeater.RepeaterRow row = entriesRepeater.addRow();
128: row.getChild("subjectType")
129: .setValue(entry.getSubjectType());
130: row.getChild("subjectValue").setValue(
131: new Long(entry.getSubjectValue()));
132: row.getChild("readPerm")
133: .setValue(entry.getReadPermission());
134: row.getChild("managePerm").setValue(
135: entry.getManagePermission());
136: }
137: }
138:
139: public static BookAcl getBookAcl(Form form) {
140: Repeater entriesRepeater = (Repeater) form
141: .lookupWidget("editors/gui/entries");
142: BookAclEntry[] entries = new BookAclEntry[entriesRepeater
143: .getSize()];
144: for (int i = 0; i < entries.length; i++) {
145: Repeater.RepeaterRow row = entriesRepeater.getRow(i);
146: BookAclSubjectType subjectType = (BookAclSubjectType) row
147: .getChild("subjectType").getValue();
148: long subjectValue = ((Long) row.getChild("subjectValue")
149: .getValue()).longValue();
150: BookAclActionType readPerm = (BookAclActionType) row
151: .getChild("readPerm").getValue();
152: BookAclActionType managePerm = (BookAclActionType) row
153: .getChild("managePerm").getValue();
154: entries[i] = new BookAclEntry(subjectType, subjectValue,
155: readPerm, managePerm);
156: }
157: return new BookAcl(entries);
158: }
159:
160: public static void annotateAclSubjectValues(Form form,
161: Repository repository) {
162: UserManager userManager = repository.getUserManager();
163: Repeater entriesRepeater = (Repeater) form
164: .lookupWidget("editors/gui/entries");
165: for (int k = 0; k < entriesRepeater.getSize(); k++) {
166: Repeater.RepeaterRow entry = entriesRepeater.getRow(k);
167: BookAclSubjectType subjectType = (BookAclSubjectType) entry
168: .getChild("subjectType").getValue();
169: long subjectValue;
170: subjectValue = ((Long) entry.getChild("subjectValue")
171: .getValue()).longValue();
172: if (subjectType == BookAclSubjectType.ROLE) {
173: String roleName;
174: try {
175: roleName = userManager.getRole(subjectValue, false)
176: .getName();
177: } catch (Exception e) {
178: roleName = "(error)";
179: }
180: entry.getChild("subjectValueLabel").setValue(roleName);
181: } else if (subjectType == BookAclSubjectType.USER) {
182: String userName;
183: try {
184: userName = userManager.getUserLogin(subjectValue);
185: } catch (Exception e) {
186: userName = "(error)";
187: }
188: entry.getChild("subjectValueLabel").setValue(userName);
189: }
190: }
191: }
192:
193: public static void initForm(Form form, Repository repository) {
194: form.lookupWidget("editors/gui/entries").addValidator(
195: new AclFormValidator(repository));
196: }
197:
198: public static class AclFormValidator implements WidgetValidator {
199: private Repository repository;
200:
201: public AclFormValidator(Repository repository) {
202: this .repository = repository;
203: }
204:
205: public boolean validate(Widget widget) {
206: Form form = widget.getForm();
207: AclResult result = BookAclEvaluator.evaluate(
208: getBookAcl(form), repository.getUserId(),
209: repository.getActiveRoleIds());
210: if (!result.canManage()) {
211: ((Messages) form.lookupWidget("editors/gui/messages"))
212: .addMessage(new I18nMessage(
213: "bookacl.error-cannot-exclude-yourself"));
214: return false;
215: } else {
216: return true;
217: }
218: }
219: }
220:
221: }
|