View Javadoc

1   package net.sourceforge.blogentis.utils.tools;
2   
3   import net.sourceforge.blogentis.om.Post;
4   import net.sourceforge.blogentis.turbine.BlogParameterParser;
5   import net.sourceforge.blogentis.turbine.BlogRunData;
6   import net.sourceforge.blogentis.utils.BlogConstants;
7   
8   import org.apache.turbine.om.security.User;
9   import org.apache.turbine.services.pull.RunDataApplicationTool;
10  import org.apache.turbine.util.RunData;
11  import org.apache.turbine.util.security.AccessControlList;
12  
13  /***
14   * Tool to simplify Velocity access control checks.
15   * 
16   * @author abas
17   */
18  public class SecurityTool
19          implements RunDataApplicationTool {
20      protected BlogRunData data = null;
21  
22      public void init(Object obj) {
23          if (obj instanceof BlogRunData) {
24              this.data = (BlogRunData)obj;
25          } else
26              this.data = null;
27      }
28  
29      public void setRunData(BlogRunData brdata) {
30          this.data = brdata;
31      }
32  
33      public SecurityTool() {}
34  
35      public SecurityTool(BlogRunData data) {
36          init(data);
37      }
38  
39      public void refresh(RunData data) {
40          this.data = (BlogRunData)data;
41      }
42  
43      public boolean getIsSiteAdmin() {
44          if (data == null)
45              return false;
46          AccessControlList acl = data.getACL();
47          if (acl == null)
48              return false;
49          return acl.hasPermission(BlogConstants.PERM_ADMIN_SITE);
50      }
51  
52      public boolean getIsBlogAdmin() {
53          if (data == null)
54              return false;
55          AccessControlList acl = data.getACL();
56          if (acl == null || data.getBlog() == null)
57              return false;
58          return acl.hasPermission(BlogConstants.PERM_ADMIN_BLOG, data.getBlog()
59                  .getName());
60      }
61  
62      public boolean getIsBlogAuthor() {
63          if (data == null)
64              return false;
65          AccessControlList acl = data.getACL();
66          if (acl == null || data.getBlog() == null)
67              return false;
68          return acl.hasPermission(BlogConstants.PERM_WRITE_POSTS, data.getBlog()
69                  .getName());
70      }
71  
72      public boolean getIsBlogEditor() {
73          if (data == null)
74              return false;
75          AccessControlList acl = data.getACL();
76          if (acl == null || data.getBlog() == null)
77              return false;
78          return acl.hasPermission(BlogConstants.PERM_EDIT_POSTS, data.getBlog()
79                  .getName());
80      }
81  
82      public boolean getCanEditThisPost() {
83          BlogParameterParser bpp = (BlogParameterParser)data.getParameters();
84          Post p = bpp.getPost();
85          User u = data.getUser();
86          if (p == null || u == null || !u.hasLoggedIn())
87              return false;
88          if (getIsBlogEditor())
89              return true;
90          if (!getIsBlogAuthor())
91              return false;
92          return u.getName().equals(p.getAuthorId());
93      }
94  }