/src/main/java/org/randombits/filtering/confluence/criteria/user/SpacePermissionCriterion.java

https://bitbucket.org/ianchiu/filtering-confluence · Java · 241 lines · 128 code · 44 blank · 69 comment · 28 complexity · 8217333f506c1e86b62f2c3106320973 MD5 · raw file

  1. /*
  2. * Copyright (c) 2006, David Peterson
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of "randombits.org" nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. package org.randombits.filtering.confluence.criteria.user;
  30. import com.atlassian.confluence.pages.BlogPost;
  31. import com.atlassian.confluence.pages.Comment;
  32. import com.atlassian.confluence.pages.Page;
  33. import com.atlassian.confluence.security.Permission;
  34. import com.atlassian.confluence.security.PermissionManager;
  35. import com.atlassian.confluence.spaces.Space;
  36. import com.atlassian.confluence.spaces.SpaceManager;
  37. import com.atlassian.confluence.user.UserAccessor;
  38. import com.atlassian.user.User;
  39. import org.randombits.filtering.confluence.criteria.AutowiredCriterion;
  40. import org.randombits.filtering.core.criteria.CriteriaException;
  41. import org.randombits.filtering.core.criteria.Criterion;
  42. import org.randombits.filtering.core.criteria.CriterionInterpreter;
  43. import java.util.Map;
  44. /**
  45. * Checks if the object is a User and it has the specified space permission.
  46. */
  47. public class SpacePermissionCriterion extends AutowiredCriterion {
  48. public static class Interpreter implements CriterionInterpreter {
  49. /**
  50. * A post-fix for specifying a user must have 'edit' permissions for a
  51. * spaces.
  52. */
  53. public static final String EDIT_PAGE = "edit";
  54. /**
  55. * A post-fix for specifying a user must have 'admin' permissions for a
  56. * space.
  57. */
  58. public static final String SPACE_ADMIN = "admin";
  59. // This is private because blog has been deprecated in favour of news.
  60. private static final String SPACE_BLOG = "blog";
  61. /**
  62. * A post-fix for specifying a user must have 'news' permissions for a
  63. * space.
  64. */
  65. public static final String SPACE_NEWS = "news";
  66. /**
  67. * A post-fix for specifying a user must have 'view' permissions for a
  68. * space.
  69. */
  70. public static final String SPACE_VIEW = "view";
  71. /**
  72. * A post-fix for specifying a user must have 'comment' permissions for
  73. * a space.
  74. */
  75. public static final String SPACE_COMMENT = "comment";
  76. private static final String SELF_SPACE = "@space";
  77. private static final Map<String, Object> ACTIONS;
  78. static {
  79. ACTIONS = new java.util.HashMap<String, Object>( 4 );
  80. ACTIONS.put( SPACE_VIEW, Permission.VIEW );
  81. ACTIONS.put( SPACE_ADMIN, Permission.ADMINISTER );
  82. ACTIONS.put( EDIT_PAGE, Page.class );
  83. ACTIONS.put( SPACE_BLOG, BlogPost.class );
  84. ACTIONS.put( SPACE_NEWS, BlogPost.class );
  85. ACTIONS.put( SPACE_COMMENT, Comment.class );
  86. }
  87. private String spaceKey;
  88. public Interpreter( Space space ) {
  89. this( space == null ? null : space.getKey() );
  90. }
  91. public Interpreter( String spaceKey ) {
  92. this.spaceKey = spaceKey;
  93. }
  94. public String getSpaceKey() {
  95. return spaceKey;
  96. }
  97. public Criterion createCriterion( String value ) throws CriteriaException {
  98. Object action;
  99. String spaceKey = value;
  100. // Find the separator.
  101. int splitAt = spaceKey.indexOf( '>' );
  102. if ( splitAt == -1 )
  103. splitAt = spaceKey.indexOf( ':' );
  104. if ( splitAt != -1 ) {
  105. action = ACTIONS.get( spaceKey.substring( splitAt + 1 ).toLowerCase().trim() );
  106. spaceKey = spaceKey.substring( 0, splitAt ).trim();
  107. if ( action == null )
  108. throw new IllegalArgumentException( "The permission requested is not recognised: " + value );
  109. } else {
  110. action = Permission.VIEW;
  111. }
  112. if ( SELF_SPACE.equals( spaceKey ) ) {
  113. if ( this.spaceKey != null )
  114. spaceKey = this.spaceKey;
  115. else
  116. throw new CriteriaException( SELF_SPACE + " cannot be used in this context." );
  117. }
  118. if ( action instanceof Permission ) {
  119. return new SpacePermissionCriterion( spaceKey, (Permission) action );
  120. } else if ( action instanceof Class ) {
  121. return new SpacePermissionCriterion( spaceKey, (Class<?>) action );
  122. } else {
  123. throw new CriteriaException( "Unsupported space permission: " + value );
  124. }
  125. }
  126. }
  127. private PermissionManager permissionManager;
  128. private String spaceKey;
  129. private Permission permission;
  130. private Class<?> createClass;
  131. private SpaceManager spaceManager;
  132. private UserAccessor userAccessor;
  133. /**
  134. * Constructs a new criterion which will check that the object is a User who
  135. * can create the specified class in the specified space.
  136. *
  137. * @param spaceKey The space to check.
  138. * @param createClass The class to create.
  139. */
  140. public SpacePermissionCriterion( String spaceKey, Class<?> createClass ) {
  141. this.spaceKey = spaceKey;
  142. this.createClass = createClass;
  143. }
  144. /**
  145. * Constructs a new criterion with the specified space key and space
  146. * permission type (e.g. {@link com.atlassian.confluence.security.Permission#VIEW})
  147. *
  148. * @param spaceKey The space key.
  149. * @param permission The space permission type.
  150. */
  151. public SpacePermissionCriterion( String spaceKey, Permission permission ) {
  152. this.spaceKey = spaceKey;
  153. this.permission = permission;
  154. }
  155. public boolean matches( Object object ) {
  156. User user = null;
  157. if ( object instanceof User )
  158. user = (User) object;
  159. else if ( object instanceof String )
  160. user = userAccessor.getUser( (String) object );
  161. else if ( object != null )
  162. return false;
  163. // Admins have all permissions on spaces, but are often
  164. // not given them explicitly.
  165. if ( permissionManager.hasPermission( user, Permission.ADMINISTER, PermissionManager.TARGET_APPLICATION ) )
  166. return true;
  167. Space space = spaceManager.getSpace( spaceKey );
  168. if ( space != null ) {
  169. if ( permission != null )
  170. return permissionManager.hasPermission( user, permission, space );
  171. else
  172. return permissionManager.hasCreatePermission( user, space, createClass );
  173. }
  174. return false;
  175. }
  176. public void setPermissionManager( PermissionManager permissionManager ) {
  177. this.permissionManager = permissionManager;
  178. }
  179. public void setSpaceManager( SpaceManager spaceManager ) {
  180. this.spaceManager = spaceManager;
  181. }
  182. public void setUserAccessor( UserAccessor userAccessor ) {
  183. this.userAccessor = userAccessor;
  184. }
  185. public String getSpaceKey() {
  186. return spaceKey;
  187. }
  188. public Permission getPermission() {
  189. return permission;
  190. }
  191. @Override
  192. public String toString() {
  193. return "{space: " + spaceKey
  194. + ( createClass == null ? "" : "; create class: " + createClass.getSimpleName() )
  195. + ( permission == null ? "" : "; permission: " + permission ) + "}";
  196. }
  197. }