Access keys

AccessPermission

Kind of class: class
Inherits from: BasicClass
Author: Simon Wacker
Classpath: org.as2lib.util.AccessPermission
File last modified: Saturday, 02 July 2005, 22:43:52
AccessPermission adjusts the access permissions of members like methods
and properties in a specific context.

You can hide methods from for..in loops and protect them from deletion and
from being overwritten.

Note that no matter what access permissions you set they can be overwritten.

Also note that the access permissions are not applied to the object but to
the reference to the object. That means that the object can for example be
enumerable in one reference but not in another.

Example:

var object:Object = new Object();
object.myProperty = new Object();
object.mySecondReference = object.myProperty;
trace("myProperty:          Value: " + object.myProperty);
trace("mySecondReference:   Value: " + object.mySecondReference);
AccessPermission.set(object, ["myProperty"], AccessPermission.PROTECT_DELETE);
trace("myProperty:          Permission: " + AccessPermission.get(object, "myProperty"));
trace("mySecondReference:   Permission: " + AccessPermission.get(object, "mySecondReference"));
delete object.myProperty;
delete object.mySecondReference;
trace("myProperty:          Value: " + object.myProperty);
trace("mySecondReference:   Value: " + object.mySecondReference);

Output:

myProperty:          Value: [object Object]
mySecondReference:   Value: [object Object]
myProperty:          Permission: 2
mySecondReference:   Permission: 0
myProperty:          Value: [object Object]
mySecondReference:   Value: undefined

As you can see, the above statement holds true. We have two references that
reference the same object. We set the access permission of one reference. We can
then not delete the reference the access permission was applied to, but the other
reference.

Following is another example with a property in its normal state and another
protected property we applied the ALLOW_NOTHING access permission to.

Example:

var object:Object = new Object();
object.myNormalProperty = "myNormalPropertyValue";
object.myProtectedProperty = "myProtectedPropertyValue";
trace("myNormalProperty:      Default Permission: " + AccessPermission.get(object, "myNormalProperty"));
trace("myProtectedProperty:   Default Permission: " + AccessPermission.get(object, "myProtectedProperty"));
AccessPermission.set(object, ["myProtectedProperty"], AccessPermission.ALLOW_NOTHING);
trace("myProtectedProperty:   New Permission: " + AccessPermission.get(object, "myProtectedProperty"));
object.myNormalProperty = "newMyNormalPropertyValue";
object.myProtectedProperty = "newMyProtectedPropertyValue";
trace("myNormalProperty:      Value After Overwriting: " + object.myNormalProperty);
trace("myProtectedProperty:   Value After Overwriting: " + object.myProtectedProperty);
for (var i:String in object) {
  trace(i + ":      Found In For..In Loop, Value: " + object[i]);
}
delete object.myNormalProperty;
delete object.myProtectedProperty;
trace("myNormalProperty:      Value After Deletion: " + object.myNormalProperty);
trace("myProtectedProperty:   Value After Deletion: " + object.myProtectedProperty);

Output:

myNormalProperty:      Default Permission: 0
myProtectedProperty:   Default Permission: 0
myProtectedProperty:   New Permission: 7
myNormalProperty:      Value After Overwriting: newMyNormalPropertyValue
myProtectedProperty:   Value After Overwriting: myProtectedPropertyValue
myNormalProperty:      Found In For..In Loop, Value: newMyNormalPropertyValue
myNormalProperty:      Value After Deletion: undefined
myProtectedProperty:   Value After Deletion: myProtectedPropertyValue

As you can see the protected property cannot be deleted, overwritten and is
hidden from for..in loops, while the non-protected property can be deleted, can
be overwritten and can be enumerated.

Besides the get method you can check up on properties for specific
access permissions using the isEnumerable, isDeletable and
isOverwritable methods.

Summary

Instance methods
Instance methods inherited from BasicClass

Class properties

ALLOW_ALL

static ALLOW_ALL:Number = 0
(read,write)
Allow everything to be done with the object.

ALLOW_NOTHING

static ALLOW_NOTHING:Number = 7
(read,write)
Allow nothing to be done with the object.

HIDE

static HIDE:Number = 1
(read,write)
Hide an object from for..in loops.

PROTECT_DELETE

static PROTECT_DELETE:Number = 2
(read,write)
Protect an object from deletion.

PROTECT_OVERWRITE

static PROTECT_OVERWRITE:Number = 4
(read,write)
Protect an object from overwriting.

Class methods

get

static function get (
target, referenceName:String) : Number
Returns the current access permission of the reference.

The permission is represented by a Number. This number is a bitwise
combination of the three access specifier HIDE, PROTECT_DELETE
and PROTECT_OVERWRITE. You can find out what the returned access
permission number means using these constants.

Parameters:
target :
the target object that holds the reference
referenceName:
the name of the reference to return the access permission for
Returns:
a number representing the access permission of the reference

isDeletable

static function isDeletable (
target, referenceName:String) : Boolean
Returns whether the reference is deletable.
Parameters:
target :
the target object that holds the reference
referenceName:
the name of the reference to return whether it is deletable
Returns:
true if the reference is deletable else false

isEnumerable

static function isEnumerable (
target, referenceName:String) : Boolean
Returns whether the reference is enumerable.
Parameters:
target :
the target object that holds the reference
referenceName:
the name of the reference to return whether it is enumerable
Returns:
true if the reference is enumerable else false

isOverwritable

static function isOverwritable (
target, referenceName:String) : Boolean
Returns whether the reference is overwritable.
Parameters:
target :
the target object that holds the reference
referenceName:
the name of the reference to return whether it is overwritable
Returns:
true if the reference is overwritable else false

set

static function set (
target, referenceNames:Array, access:Number) : Void
Sets the access permission of a reference by an access code.

The following access codes are applicable:

HIDEHides the reference from for-in loops.
PROTECT_DELETEProtects the reference from deletion
PROTECT_OVERWRITEProtects the reference from overwriting
ALLOW_ALLAllows everything to be done with the reference.
ALLOW_NOTHINGAllows nothing to be done with the reference.

These access codes can be combined as follows to apply multiple access
permissions.

AccessPermission.PROTECT_DELETE | AccessPermission.PROTECT_OVERWRITE

Note that every new invocation of this method simply overwrites the old access
permissions of the reference.

Parameters:
target :
the object that holds references to the objects the access permissions
shall be applied to
referenceNames:
the names of the references to apply the access permission to
access :
the access permissions to apply