3 changed files with 676 additions and 672 deletions
File diff suppressed because it is too large
Load Diff
@ -1,102 +1,106 @@ |
|||||||
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited |
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited |
||||||
* |
* |
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
* you may not use this file except in compliance with the License. |
* you may not use this file except in compliance with the License. |
||||||
* You may obtain a copy of the License at |
* You may obtain a copy of the License at |
||||||
* |
* |
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* |
* |
||||||
* Unless required by applicable law or agreed to in writing, software |
* Unless required by applicable law or agreed to in writing, software |
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
* See the License for the specific language governing permissions and |
* See the License for the specific language governing permissions and |
||||||
* limitations under the License. |
* limitations under the License. |
||||||
*/ |
*/ |
||||||
|
|
||||||
package org.acegisecurity.acls.domain; |
package org.acegisecurity.acls.domain; |
||||||
|
|
||||||
import org.acegisecurity.acls.AclFormattingUtils; |
import org.acegisecurity.acls.AclFormattingUtils; |
||||||
import org.acegisecurity.acls.Permission; |
import org.acegisecurity.acls.Permission; |
||||||
|
|
||||||
|
/** |
||||||
/** |
* DOCUMENT ME! |
||||||
* DOCUMENT ME! |
* |
||||||
* |
* @author $author$ |
||||||
* @author $author$ |
* @version $Revision$ |
||||||
* @version $Revision$ |
*/ |
||||||
*/ |
public class BasePermission implements Permission { |
||||||
public class BasePermission implements Permission { |
//~ Static fields/initializers =====================================================================================
|
||||||
//~ Static fields/initializers =====================================================================================
|
|
||||||
|
public static final Permission READ = new BasePermission(1 << 0, 'R'); // 1
|
||||||
public static final Permission READ = new BasePermission(1 << 0, 'R'); // 1
|
public static final Permission WRITE = new BasePermission(1 << 1, 'W'); // 2
|
||||||
public static final Permission WRITE = new BasePermission(1 << 1, 'W'); // 2
|
public static final Permission CREATE = new BasePermission(1 << 2, 'C'); // 4
|
||||||
public static final Permission CREATE = new BasePermission(1 << 2, 'C'); // 4
|
public static final Permission DELETE = new BasePermission(1 << 3, 'D'); // 8
|
||||||
public static final Permission ADMINISTRATION = new BasePermission(1 << 3, 'A'); // 8
|
public static final Permission ADMINISTRATION = new BasePermission(1 << 4, 'A'); // 16
|
||||||
|
|
||||||
//~ Instance fields ================================================================================================
|
//~ Instance fields ================================================================================================
|
||||||
|
|
||||||
private char code; |
private char code; |
||||||
private int mask; |
private int mask; |
||||||
|
|
||||||
//~ Constructors ===================================================================================================
|
//~ Constructors ===================================================================================================
|
||||||
|
|
||||||
private BasePermission(int mask, char code) { |
private BasePermission(int mask, char code) { |
||||||
this.mask = mask; |
this.mask = mask; |
||||||
this.code = code; |
this.code = code; |
||||||
} |
} |
||||||
|
|
||||||
//~ Methods ========================================================================================================
|
//~ Methods ========================================================================================================
|
||||||
|
|
||||||
/** |
/** |
||||||
* Dynamically creates a <code>CumulativePermission</code> representing the active bits in the passed mask. |
* Dynamically creates a <code>CumulativePermission</code> representing the active bits in the passed mask. |
||||||
* NB: Only uses <code>BasePermission</code>! |
* NB: Only uses <code>BasePermission</code>! |
||||||
* |
* |
||||||
* @param mask to review |
* @param mask to review |
||||||
* |
* |
||||||
* @return DOCUMENT ME! |
* @return DOCUMENT ME! |
||||||
*/ |
*/ |
||||||
public static Permission buildFromMask(int mask) { |
public static Permission buildFromMask(int mask) { |
||||||
CumulativePermission permission = new CumulativePermission(); |
CumulativePermission permission = new CumulativePermission(); |
||||||
|
|
||||||
// TODO: Write the rest of it to iterate through the 32 bits and instantiate BasePermissions
|
// TODO: Write the rest of it to iterate through the 32 bits and instantiate BasePermissions
|
||||||
if (mask == 1) { |
if (mask == 1) { |
||||||
permission.set(READ); |
permission.set(READ); |
||||||
} |
} |
||||||
|
|
||||||
if (mask == 2) { |
if (mask == 2) { |
||||||
permission.set(WRITE); |
permission.set(WRITE); |
||||||
} |
} |
||||||
|
|
||||||
if (mask == 4) { |
if (mask == 4) { |
||||||
permission.set(CREATE); |
permission.set(CREATE); |
||||||
} |
} |
||||||
|
|
||||||
if (mask == 8) { |
if (mask == 8) { |
||||||
permission.set(ADMINISTRATION); |
permission.set(DELETE); |
||||||
} |
} |
||||||
|
|
||||||
return permission; |
if (mask == 16) { |
||||||
} |
permission.set(ADMINISTRATION); |
||||||
|
} |
||||||
public boolean equals(Object arg0) { |
|
||||||
if (!(arg0 instanceof BasePermission)) { |
return permission; |
||||||
return false; |
} |
||||||
} |
|
||||||
|
public boolean equals(Object arg0) { |
||||||
BasePermission rhs = (BasePermission) arg0; |
if (!(arg0 instanceof BasePermission)) { |
||||||
|
return false; |
||||||
return (this.mask == rhs.getMask()); |
} |
||||||
} |
|
||||||
|
BasePermission rhs = (BasePermission) arg0; |
||||||
public int getMask() { |
|
||||||
return mask; |
return (this.mask == rhs.getMask()); |
||||||
} |
} |
||||||
|
|
||||||
public String getPattern() { |
public int getMask() { |
||||||
return AclFormattingUtils.printBinary(mask, code); |
return mask; |
||||||
} |
} |
||||||
|
|
||||||
public String toString() { |
public String getPattern() { |
||||||
return "BasePermission[" + getPattern() + "=" + mask + "]"; |
return AclFormattingUtils.printBinary(mask, code); |
||||||
} |
} |
||||||
} |
|
||||||
|
public String toString() { |
||||||
|
return "BasePermission[" + getPattern() + "=" + mask + "]"; |
||||||
|
} |
||||||
|
} |
||||||
|
|||||||
@ -1,70 +1,70 @@ |
|||||||
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited |
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited |
||||||
* |
* |
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
* you may not use this file except in compliance with the License. |
* you may not use this file except in compliance with the License. |
||||||
* You may obtain a copy of the License at |
* You may obtain a copy of the License at |
||||||
* |
* |
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* |
* |
||||||
* Unless required by applicable law or agreed to in writing, software |
* Unless required by applicable law or agreed to in writing, software |
||||||
* distributed under the License is distributed on an "AS IS" BASIS, |
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
* See the License for the specific language governing permissions and |
* See the License for the specific language governing permissions and |
||||||
* limitations under the License. |
* limitations under the License. |
||||||
*/ |
*/ |
||||||
|
|
||||||
package org.acegisecurity.acls.domain; |
package org.acegisecurity.acls.domain; |
||||||
|
|
||||||
import junit.framework.TestCase; |
import junit.framework.TestCase; |
||||||
|
|
||||||
|
|
||||||
/** |
/** |
||||||
* Tests BasePermission and CumulativePermission. |
* Tests BasePermission and CumulativePermission. |
||||||
* |
* |
||||||
* @author Ben Alex |
* @author Ben Alex |
||||||
* @version $Id${date} |
* @version $Id${date} |
||||||
*/ |
*/ |
||||||
public class PermissionTests extends TestCase { |
public class PermissionTests extends TestCase { |
||||||
//~ Methods ========================================================================================================
|
//~ Methods ========================================================================================================
|
||||||
|
|
||||||
public void testExpectedIntegerValues() { |
public void testExpectedIntegerValues() { |
||||||
assertEquals(1, BasePermission.READ.getMask()); |
assertEquals(1, BasePermission.READ.getMask()); |
||||||
assertEquals(8, BasePermission.ADMINISTRATION.getMask()); |
assertEquals(16, BasePermission.ADMINISTRATION.getMask()); |
||||||
assertEquals(9, new CumulativePermission().set(BasePermission.READ).set(BasePermission.ADMINISTRATION).getMask()); |
assertEquals(17, new CumulativePermission().set(BasePermission.READ).set(BasePermission.ADMINISTRATION).getMask()); |
||||||
} |
} |
||||||
|
|
||||||
public void testStringConversion() { |
public void testStringConversion() { |
||||||
System.out.println("R = " + BasePermission.READ.toString()); |
System.out.println("R = " + BasePermission.READ.toString()); |
||||||
assertEquals("BasePermission[...............................R=1]", BasePermission.READ.toString()); |
assertEquals("BasePermission[...............................R=1]", BasePermission.READ.toString()); |
||||||
|
|
||||||
System.out.println("A = " + BasePermission.ADMINISTRATION.toString()); |
System.out.println("A = " + BasePermission.ADMINISTRATION.toString()); |
||||||
assertEquals("BasePermission[............................A...=8]", BasePermission.ADMINISTRATION.toString()); |
assertEquals("BasePermission[...........................A....=16]", BasePermission.ADMINISTRATION.toString()); |
||||||
|
|
||||||
System.out.println("R = " + new CumulativePermission().set(BasePermission.READ).toString()); |
System.out.println("R = " + new CumulativePermission().set(BasePermission.READ).toString()); |
||||||
assertEquals("CumulativePermission[...............................R=1]", |
assertEquals("CumulativePermission[...............................R=1]", |
||||||
new CumulativePermission().set(BasePermission.READ).toString()); |
new CumulativePermission().set(BasePermission.READ).toString()); |
||||||
|
|
||||||
System.out.println("A = " + new CumulativePermission().set(BasePermission.ADMINISTRATION).toString()); |
System.out.println("A = " + new CumulativePermission().set(BasePermission.ADMINISTRATION).toString()); |
||||||
assertEquals("CumulativePermission[............................A...=8]", |
assertEquals("CumulativePermission[...........................A....=16]", |
||||||
new CumulativePermission().set(BasePermission.ADMINISTRATION).toString()); |
new CumulativePermission().set(BasePermission.ADMINISTRATION).toString()); |
||||||
|
|
||||||
System.out.println("RA = " |
System.out.println("RA = " |
||||||
+ new CumulativePermission().set(BasePermission.ADMINISTRATION).set(BasePermission.READ).toString()); |
+ new CumulativePermission().set(BasePermission.ADMINISTRATION).set(BasePermission.READ).toString()); |
||||||
assertEquals("CumulativePermission[............................A..R=9]", |
assertEquals("CumulativePermission[...........................A...R=17]", |
||||||
new CumulativePermission().set(BasePermission.ADMINISTRATION).set(BasePermission.READ).toString()); |
new CumulativePermission().set(BasePermission.ADMINISTRATION).set(BasePermission.READ).toString()); |
||||||
|
|
||||||
System.out.println("R = " |
System.out.println("R = " |
||||||
+ new CumulativePermission().set(BasePermission.ADMINISTRATION).set(BasePermission.READ) |
+ new CumulativePermission().set(BasePermission.ADMINISTRATION).set(BasePermission.READ) |
||||||
.clear(BasePermission.ADMINISTRATION).toString()); |
.clear(BasePermission.ADMINISTRATION).toString()); |
||||||
assertEquals("CumulativePermission[...............................R=1]", |
assertEquals("CumulativePermission[...............................R=1]", |
||||||
new CumulativePermission().set(BasePermission.ADMINISTRATION).set(BasePermission.READ) |
new CumulativePermission().set(BasePermission.ADMINISTRATION).set(BasePermission.READ) |
||||||
.clear(BasePermission.ADMINISTRATION).toString()); |
.clear(BasePermission.ADMINISTRATION).toString()); |
||||||
|
|
||||||
System.out.println("0 = " |
System.out.println("0 = " |
||||||
+ new CumulativePermission().set(BasePermission.ADMINISTRATION).set(BasePermission.READ) |
+ new CumulativePermission().set(BasePermission.ADMINISTRATION).set(BasePermission.READ) |
||||||
.clear(BasePermission.ADMINISTRATION).clear(BasePermission.READ).toString()); |
.clear(BasePermission.ADMINISTRATION).clear(BasePermission.READ).toString()); |
||||||
assertEquals("CumulativePermission[................................=0]", |
assertEquals("CumulativePermission[................................=0]", |
||||||
new CumulativePermission().set(BasePermission.ADMINISTRATION).set(BasePermission.READ) |
new CumulativePermission().set(BasePermission.ADMINISTRATION).set(BasePermission.READ) |
||||||
.clear(BasePermission.ADMINISTRATION).clear(BasePermission.READ).toString()); |
.clear(BasePermission.ADMINISTRATION).clear(BasePermission.READ).toString()); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue