View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.decoration;
18  
19  import java.io.Serializable;
20  import java.util.HashMap;
21  
22  import javax.portlet.PortletMode;
23  import javax.portlet.WindowState;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.jetspeed.JetspeedActions;
28  import org.apache.jetspeed.om.page.Page;
29  
30  /***
31   * PageActionAccess
32   *
33   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
34   * @version $Id: PageActionAccess.java 516448 2007-03-09 16:25:47Z ate $
35   */
36  public class PageActionAccess implements PageEditAccess, Serializable
37  {
38      protected static final Log log = LogFactory.getLog(PageActionAccess.class);
39  
40      private static final class ActionAccess implements Serializable
41      {
42          int checkedFlags;
43          int actionFlags;
44      }
45      
46      private boolean anonymous;
47      private boolean editAllowed;
48      private boolean editing;
49      private HashMap fragmentActionAccess;
50      
51      public PageActionAccess(boolean anonymous, Page page)
52      {
53          this.anonymous = anonymous;        
54          this.editAllowed = checkEditPage(page);
55          this.fragmentActionAccess = new HashMap();
56      }
57      
58      public void checkReset(boolean anonymous, Page page)
59      {
60          if (this.anonymous != anonymous)
61          {
62              this.anonymous = anonymous;
63              this.editAllowed = checkEditPage(page);
64              this.fragmentActionAccess.clear();
65              this.editing = false;
66          }
67      }
68      
69      public boolean isAnonymous()
70      {
71          return anonymous;
72      }
73      
74      public boolean isEditAllowed()
75      {
76          return editAllowed;
77      }
78      
79      public boolean isEditing()
80      {
81          return editing;
82      }
83      
84      public void setEditing(boolean editing)
85      {
86          if ( editing && ! editAllowed )
87          {
88              throw new SecurityException();
89          }
90          this.editing = editing;
91      }
92      
93      public boolean checkPortletMode(String fragmentId, String portletName, PortletMode mode)
94      {
95          return checkActionAccess(fragmentId, portletName, mode.toString());
96      }
97  
98      public boolean checkWindowState(String fragmentId, String portletName, WindowState state)
99      {
100         return checkActionAccess(fragmentId, portletName, state.toString());
101     }
102     
103     protected synchronized boolean checkActionAccess(String fragmentId, String portletName, String action)
104     {
105         try
106         {
107             int actionIndex = JetspeedActions.getContainerActionMask(action);
108             ActionAccess actionAccess = (ActionAccess)fragmentActionAccess.get(fragmentId);
109             if ( actionAccess == null )
110             {
111                 actionAccess = new ActionAccess();
112                 fragmentActionAccess.put(fragmentId, actionAccess);
113             }
114             if ( (actionAccess.checkedFlags & actionIndex) != actionIndex )
115             {
116                 // TODO: not handling PortletPermission checks yet 
117                 // boolean access = checkPermission(portletName, action);
118                 boolean access = true;
119 
120                 if ( access )
121                 {
122                     actionAccess.actionFlags |= actionIndex;
123                 }
124                 actionAccess.checkedFlags |= actionIndex;            
125             }
126             return ((actionAccess.actionFlags & actionIndex) == actionIndex);
127         }
128         catch (IndexOutOfBoundsException e)
129         {
130             log.error("Unknown action: "+action, e);
131             return false;
132         }
133     }
134         
135     protected boolean checkEditPage(Page page)
136     {
137         boolean allowed = false;
138         try
139         {
140             page.checkAccess(JetspeedActions.EDIT);
141             allowed = true;
142         }       
143         catch (SecurityException se) {}
144         return allowed;
145     }
146 }