Coverage Report - org.acegisecurity.acl.basic.SimpleAclEntry
 
Classes in this File Line Coverage Branch Coverage Complexity
SimpleAclEntry
100% 
100% 
2.667
 
 1  
 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 2  
  *
 3  
  * Licensed under the Apache License, Version 2.0 (the "License");
 4  
  * you may not use this file except in compliance with the License.
 5  
  * You may obtain a copy of the License at
 6  
  *
 7  
  *     http://www.apache.org/licenses/LICENSE-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing, software
 10  
  * distributed under the License is distributed on an "AS IS" BASIS,
 11  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
  * See the License for the specific language governing permissions and
 13  
  * limitations under the License.
 14  
  */
 15  
 
 16  
 package org.acegisecurity.acl.basic;
 17  
 
 18  
 import org.apache.commons.logging.Log;
 19  
 import org.apache.commons.logging.LogFactory;
 20  
 
 21  
 /**
 22  
  * Stores some privileges typical of a domain object.
 23  
  *
 24  
  * @author Ben Alex
 25  
  * @version $Id: SimpleAclEntry.java 1784 2007-02-24 21:00:24Z luke_t $
 26  
  */
 27  
 public class SimpleAclEntry extends AbstractBasicAclEntry {
 28  
     //~ Static fields/initializers =====================================================================================
 29  
 
 30  2
     private static final Log logger = LogFactory.getLog(SimpleAclEntry.class);
 31  
 
 32  
     // Base permissions we permit
 33  
     public static final int NOTHING = 0;
 34  1
     public static final int ADMINISTRATION = (int) Math.pow(2, 0);
 35  1
     public static final int READ = (int) Math.pow(2, 1);
 36  1
     public static final int WRITE = (int) Math.pow(2, 2);
 37  1
     public static final int CREATE = (int) Math.pow(2, 3);
 38  1
     public static final int DELETE = (int) Math.pow(2, 4);
 39  
 
 40  
     // Combinations of base permissions we permit
 41  1
     public static final int READ_WRITE_CREATE_DELETE = READ | WRITE | CREATE | DELETE;
 42  1
     public static final int READ_WRITE_CREATE = READ | WRITE | CREATE;
 43  1
     public static final int READ_WRITE = READ | WRITE;
 44  1
     public static final int READ_WRITE_DELETE = READ | WRITE | DELETE;
 45  
 
 46  
     // Array required by the abstract superclass via getValidPermissions()
 47  1
     private static final int[] VALID_PERMISSIONS = {
 48  
             NOTHING, ADMINISTRATION, READ, WRITE, CREATE, DELETE, READ_WRITE_CREATE_DELETE, READ_WRITE_CREATE,
 49  
             READ_WRITE, READ_WRITE_DELETE
 50  
         };
 51  
 
 52  1
     private static final String[] VALID_PERMISSIONS_AS_STRING = {
 53  
             "NOTHING", "ADMINISTRATION", "READ", "WRITE", "CREATE", "DELETE", "READ_WRITE_CREATE_DELETE",
 54  
             "READ_WRITE_CREATE", "READ_WRITE", "READ_WRITE_DELETE" };
 55  
 
 56  
     //~ Constructors ===================================================================================================
 57  
 
 58  
     /**
 59  
      * Allows {@link BasicAclDao} implementations to construct this object
 60  
      * using <code>newInstance()</code>.
 61  
      *
 62  
      * <P>
 63  
      * Normal classes should <B>not</B> use this default constructor.
 64  
      * </p>
 65  
      */
 66  
     public SimpleAclEntry() {
 67  30
         super();
 68  30
     }
 69  
 
 70  
     public SimpleAclEntry(Object recipient, AclObjectIdentity aclObjectIdentity,
 71  
         AclObjectIdentity aclObjectParentIdentity, int mask) {
 72  166
         super(recipient, aclObjectIdentity, aclObjectParentIdentity, mask);
 73  164
     }
 74  
 
 75  
     //~ Methods ========================================================================================================
 76  
 
 77  
     /**
 78  
      * @return a copy of the permissions array, changes to the values won't affect this class.
 79  
      */
 80  
     public int[] getValidPermissions() {
 81  196
         return (int[]) VALID_PERMISSIONS.clone();
 82  
     }
 83  
 
 84  
     public String printPermissionsBlock(int i) {
 85  6
         StringBuffer sb = new StringBuffer();
 86  
 
 87  6
         if (isPermitted(i, ADMINISTRATION)) {
 88  1
             sb.append('A');
 89  
         } else {
 90  5
             sb.append('-');
 91  
         }
 92  
 
 93  6
         if (isPermitted(i, READ)) {
 94  2
             sb.append('R');
 95  
         } else {
 96  4
             sb.append('-');
 97  
         }
 98  
 
 99  6
         if (isPermitted(i, WRITE)) {
 100  2
             sb.append('W');
 101  
         } else {
 102  4
             sb.append('-');
 103  
         }
 104  
 
 105  6
         if (isPermitted(i, CREATE)) {
 106  2
             sb.append('C');
 107  
         } else {
 108  4
             sb.append('-');
 109  
         }
 110  
 
 111  6
         if (isPermitted(i, DELETE)) {
 112  1
             sb.append('D');
 113  
         } else {
 114  5
             sb.append('-');
 115  
         }
 116  
 
 117  6
         return sb.toString();
 118  
     }
 119  
 
 120  
     /**
 121  
      * Parse a permission {@link String} literal and return associated value.
 122  
      *
 123  
      * @param permission one of the field names that represent a permission: <code>ADMINISTRATION</code>,
 124  
      * <code>READ</code>, <code>WRITE</code>,...
 125  
      * @return the value associated to that permission
 126  
      * @throws IllegalArgumentException if argument is not a valid permission
 127  
      */
 128  
     public static int parsePermission(String permission) {
 129  83
         for (int i = 0; i < VALID_PERMISSIONS_AS_STRING.length; i++) {
 130  81
             if (VALID_PERMISSIONS_AS_STRING[i].equalsIgnoreCase(permission)) {
 131  15
                 return VALID_PERMISSIONS[i];
 132  
             }
 133  
         }
 134  2
         throw new IllegalArgumentException("Permission provided does not exist: " + permission);
 135  
     }
 136  
 
 137  
     /**
 138  
      * Parse a list of permission {@link String} literals and return associated values.
 139  
      *
 140  
      * @param permissions array with permissions as {@link String}
 141  
      * @see #parsePermission(String) for valid values
 142  
      */
 143  
     public static int[] parsePermissions(String[] permissions) {
 144  8
         int[] requirepermissionAsIntArray = new int[permissions.length];
 145  16
         for (int i = 0; i < requirepermissionAsIntArray.length; i++) {
 146  9
             requirepermissionAsIntArray[i] = parsePermission(permissions[i]);
 147  
         }
 148  7
         return requirepermissionAsIntArray;
 149  
     }
 150  
 }