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: package org.apache.jetspeed.decoration;
018:
019: import java.io.Serializable;
020: import java.util.HashMap;
021:
022: import javax.portlet.PortletMode;
023: import javax.portlet.WindowState;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.jetspeed.JetspeedActions;
028: import org.apache.jetspeed.om.page.Page;
029:
030: /**
031: * PageActionAccess
032: *
033: * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
034: * @version $Id: PageActionAccess.java 516448 2007-03-09 16:25:47Z ate $
035: */
036: public class PageActionAccess implements PageEditAccess, Serializable {
037: protected static final Log log = LogFactory
038: .getLog(PageActionAccess.class);
039:
040: private static final class ActionAccess implements Serializable {
041: int checkedFlags;
042: int actionFlags;
043: }
044:
045: private boolean anonymous;
046: private boolean editAllowed;
047: private boolean editing;
048: private HashMap fragmentActionAccess;
049:
050: public PageActionAccess(boolean anonymous, Page page) {
051: this .anonymous = anonymous;
052: this .editAllowed = checkEditPage(page);
053: this .fragmentActionAccess = new HashMap();
054: }
055:
056: public void checkReset(boolean anonymous, Page page) {
057: if (this .anonymous != anonymous) {
058: this .anonymous = anonymous;
059: this .editAllowed = checkEditPage(page);
060: this .fragmentActionAccess.clear();
061: this .editing = false;
062: }
063: }
064:
065: public boolean isAnonymous() {
066: return anonymous;
067: }
068:
069: public boolean isEditAllowed() {
070: return editAllowed;
071: }
072:
073: public boolean isEditing() {
074: return editing;
075: }
076:
077: public void setEditing(boolean editing) {
078: if (editing && !editAllowed) {
079: throw new SecurityException();
080: }
081: this .editing = editing;
082: }
083:
084: public boolean checkPortletMode(String fragmentId,
085: String portletName, PortletMode mode) {
086: return checkActionAccess(fragmentId, portletName, mode
087: .toString());
088: }
089:
090: public boolean checkWindowState(String fragmentId,
091: String portletName, WindowState state) {
092: return checkActionAccess(fragmentId, portletName, state
093: .toString());
094: }
095:
096: protected synchronized boolean checkActionAccess(String fragmentId,
097: String portletName, String action) {
098: try {
099: int actionIndex = JetspeedActions
100: .getContainerActionMask(action);
101: ActionAccess actionAccess = (ActionAccess) fragmentActionAccess
102: .get(fragmentId);
103: if (actionAccess == null) {
104: actionAccess = new ActionAccess();
105: fragmentActionAccess.put(fragmentId, actionAccess);
106: }
107: if ((actionAccess.checkedFlags & actionIndex) != actionIndex) {
108: // TODO: not handling PortletPermission checks yet
109: // boolean access = checkPermission(portletName, action);
110: boolean access = true;
111:
112: if (access) {
113: actionAccess.actionFlags |= actionIndex;
114: }
115: actionAccess.checkedFlags |= actionIndex;
116: }
117: return ((actionAccess.actionFlags & actionIndex) == actionIndex);
118: } catch (IndexOutOfBoundsException e) {
119: log.error("Unknown action: " + action, e);
120: return false;
121: }
122: }
123:
124: protected boolean checkEditPage(Page page) {
125: boolean allowed = false;
126: try {
127: page.checkAccess(JetspeedActions.EDIT);
128: allowed = true;
129: } catch (SecurityException se) {
130: }
131: return allowed;
132: }
133: }
|