View Javadoc

1   package net.sourceforge.blogentis.plugins.base;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: AbstractNavigationExtension.java,v 1.4 2004/11/02 00:58:50 tassos Exp $
23  //
24  
25  import java.util.List;
26  
27  import org.apache.turbine.util.security.AccessControlList;
28  
29  import net.sourceforge.blogentis.om.Blog;
30  import net.sourceforge.blogentis.plugins.AbstractBlogExtension;
31  import net.sourceforge.blogentis.plugins.IPlugin;
32  import net.sourceforge.blogentis.turbine.BlogRunData;
33  import net.sourceforge.blogentis.utils.AbsoluteLinkURL;
34  import net.sourceforge.blogentis.utils.BlogConstants;
35  
36  /***
37   * @author abas
38   */
39  public abstract class AbstractNavigationExtension
40          extends AbstractBlogExtension
41          implements INavigationExtension {
42      public AbstractNavigationExtension(IPlugin plugin, Blog blog) {
43          super(plugin, blog);
44      }
45  
46      public void addNavigations(BlogRunData data, String navName, List links) {}
47  
48      protected static interface Authenticator {
49          public boolean isVisible(BlogRunData data);
50      }
51  
52      protected static class TargetAuthenticator
53              implements Authenticator {
54          private String[] permissions;
55  
56          public TargetAuthenticator(String permission) {
57              permissions = new String[] {permission};
58          }
59  
60          public TargetAuthenticator(String permission, String otherPermission) {
61              permissions = new String[] {permission, otherPermission};
62          }
63  
64          public boolean isVisible(BlogRunData data) {
65              AccessControlList acl = data.getACL();
66              if(acl==null)
67                  return false;
68              String group = data.getBlog().getName();
69              for(int i = 0; i < permissions.length; i++) {
70                  if (acl.hasPermission(permissions[i], group)) {
71                      return true;
72                  }
73              }
74              return false;
75          }
76      }
77  
78      protected static final Authenticator SiteAdminAuthenticator = new TargetAuthenticator(
79          BlogConstants.PERM_ADMIN_SITE);
80      protected static final Authenticator BlogAdminAuthenticator = new TargetAuthenticator(
81          BlogConstants.PERM_ADMIN_BLOG);
82      protected static final Authenticator EditorAuthenticator = new TargetAuthenticator(
83          BlogConstants.PERM_EDIT_POSTS);
84      protected static final Authenticator AuthorAuthenticator = new TargetAuthenticator(
85          BlogConstants.PERM_WRITE_POSTS);
86  
87      protected static abstract class AbstractLink
88              implements ILinkTo {
89          protected Authenticator au = null;
90          protected String label;
91  
92          public AbstractLink(String name, Authenticator a) {
93              this.label = name;
94              this.au = a;
95          }
96  
97          public String getLabel() {
98              return label;
99          }
100 
101         public boolean isAuthorized(BlogRunData data) {
102             if (au == null)
103                 return true;
104             return au.isVisible(data);
105         }
106     }
107 
108     protected static class LinkToTemplate
109             extends AbstractLink {
110 
111         protected String page;
112 
113         public AbsoluteLinkURL getLink(BlogRunData data, AbsoluteLinkURL link) {
114             return link.setBlog(data.getBlog()).setPage(page);
115         }
116 
117         public LinkToTemplate(String page, String name) {
118             super(name, null);
119             this.page = page;
120         }
121 
122         public LinkToTemplate(String page, String name, Authenticator a) {
123             super(name, a);
124             this.page = page;
125         }
126 
127         public boolean isCurrentPage(BlogRunData data) {
128             return (page + ".vm").equals(data.getParameters()
129                 .getString("template"));
130         }
131     }
132 
133     protected static class LinkToParameterPage
134             extends LinkToTemplate {
135         protected String pName, pValue;
136 
137         public LinkToParameterPage(String page, String name, String pName,
138                                    String pValue) {
139             super(page, name);
140             this.pName = pName;
141             this.pValue = pValue;
142         }
143 
144         public LinkToParameterPage(String page, String name, String pName,
145                                    String pValue, Authenticator a) {
146             super(page, name, a);
147             this.pName = pName;
148             this.pValue = pValue;
149         }
150 
151         public AbsoluteLinkURL getLink(BlogRunData data, AbsoluteLinkURL link) {
152             return super.getLink(data, link).addQueryData(pName, pValue);
153         }
154 
155         public boolean isCurrentPage(BlogRunData data) {
156             if (!super.isCurrentPage(data)) {
157                 return false;
158             }
159             return pValue.equals(data.getParameters().getString(pName, "0"));
160         }
161     }
162 
163     protected static class LinkToSplitPage
164             extends LinkToParameterPage {
165         public LinkToSplitPage(String page, String name, String pValue) {
166             super(page, name, "s", pValue);
167         }
168 
169         public LinkToSplitPage(String page, String name, String pValue,
170                                Authenticator a) {
171             super(page, name, "s", pValue, a);
172         }
173     }
174 
175     protected static class LinkToActionPage
176             extends LinkToParameterPage {
177         public LinkToActionPage(String page, String name, String pValue) {
178             super(page, name, "action", pValue);
179         }
180 
181         public LinkToActionPage(String page, String name, String pValue,
182                                 Authenticator a) {
183             super(page, name, "action", pValue, a);
184         }
185     }
186 }