001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.acl.basic;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020:
021: /**
022: * Stores some privileges typical of a domain object.
023: *
024: * @author Ben Alex
025: * @version $Id: SimpleAclEntry.java 1784 2007-02-24 21:00:24Z luke_t $
026: */
027: public class SimpleAclEntry extends AbstractBasicAclEntry {
028: //~ Static fields/initializers =====================================================================================
029:
030: private static final Log logger = LogFactory
031: .getLog(SimpleAclEntry.class);
032:
033: // Base permissions we permit
034: public static final int NOTHING = 0;
035: public static final int ADMINISTRATION = (int) Math.pow(2, 0);
036: public static final int READ = (int) Math.pow(2, 1);
037: public static final int WRITE = (int) Math.pow(2, 2);
038: public static final int CREATE = (int) Math.pow(2, 3);
039: public static final int DELETE = (int) Math.pow(2, 4);
040:
041: // Combinations of base permissions we permit
042: public static final int READ_WRITE_CREATE_DELETE = READ | WRITE
043: | CREATE | DELETE;
044: public static final int READ_WRITE_CREATE = READ | WRITE | CREATE;
045: public static final int READ_WRITE = READ | WRITE;
046: public static final int READ_WRITE_DELETE = READ | WRITE | DELETE;
047:
048: // Array required by the abstract superclass via getValidPermissions()
049: private static final int[] VALID_PERMISSIONS = { NOTHING,
050: ADMINISTRATION, READ, WRITE, CREATE, DELETE,
051: READ_WRITE_CREATE_DELETE, READ_WRITE_CREATE, READ_WRITE,
052: READ_WRITE_DELETE };
053:
054: private static final String[] VALID_PERMISSIONS_AS_STRING = {
055: "NOTHING", "ADMINISTRATION", "READ", "WRITE", "CREATE",
056: "DELETE", "READ_WRITE_CREATE_DELETE", "READ_WRITE_CREATE",
057: "READ_WRITE", "READ_WRITE_DELETE" };
058:
059: //~ Constructors ===================================================================================================
060:
061: /**
062: * Allows {@link BasicAclDao} implementations to construct this object
063: * using <code>newInstance()</code>.
064: *
065: * <P>
066: * Normal classes should <B>not</B> use this default constructor.
067: * </p>
068: */
069: public SimpleAclEntry() {
070: super ();
071: }
072:
073: public SimpleAclEntry(Object recipient,
074: AclObjectIdentity aclObjectIdentity,
075: AclObjectIdentity aclObjectParentIdentity, int mask) {
076: super (recipient, aclObjectIdentity, aclObjectParentIdentity,
077: mask);
078: }
079:
080: //~ Methods ========================================================================================================
081:
082: /**
083: * @return a copy of the permissions array, changes to the values won't affect this class.
084: */
085: public int[] getValidPermissions() {
086: return (int[]) VALID_PERMISSIONS.clone();
087: }
088:
089: public String printPermissionsBlock(int i) {
090: StringBuffer sb = new StringBuffer();
091:
092: if (isPermitted(i, ADMINISTRATION)) {
093: sb.append('A');
094: } else {
095: sb.append('-');
096: }
097:
098: if (isPermitted(i, READ)) {
099: sb.append('R');
100: } else {
101: sb.append('-');
102: }
103:
104: if (isPermitted(i, WRITE)) {
105: sb.append('W');
106: } else {
107: sb.append('-');
108: }
109:
110: if (isPermitted(i, CREATE)) {
111: sb.append('C');
112: } else {
113: sb.append('-');
114: }
115:
116: if (isPermitted(i, DELETE)) {
117: sb.append('D');
118: } else {
119: sb.append('-');
120: }
121:
122: return sb.toString();
123: }
124:
125: /**
126: * Parse a permission {@link String} literal and return associated value.
127: *
128: * @param permission one of the field names that represent a permission: <code>ADMINISTRATION</code>,
129: * <code>READ</code>, <code>WRITE</code>,...
130: * @return the value associated to that permission
131: * @throws IllegalArgumentException if argument is not a valid permission
132: */
133: public static int parsePermission(String permission) {
134: for (int i = 0; i < VALID_PERMISSIONS_AS_STRING.length; i++) {
135: if (VALID_PERMISSIONS_AS_STRING[i]
136: .equalsIgnoreCase(permission)) {
137: return VALID_PERMISSIONS[i];
138: }
139: }
140: throw new IllegalArgumentException(
141: "Permission provided does not exist: " + permission);
142: }
143:
144: /**
145: * Parse a list of permission {@link String} literals and return associated values.
146: *
147: * @param permissions array with permissions as {@link String}
148: * @see #parsePermission(String) for valid values
149: */
150: public static int[] parsePermissions(String[] permissions) {
151: int[] requirepermissionAsIntArray = new int[permissions.length];
152: for (int i = 0; i < requirepermissionAsIntArray.length; i++) {
153: requirepermissionAsIntArray[i] = parsePermission(permissions[i]);
154: }
155: return requirepermissionAsIntArray;
156: }
157: }
|