Class org.as2lib.util.AccessPermission

org.as2lib.core.BasicClass
   +--org.as2lib.util.AccessPermission

Description

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.

Field Index

ALLOW_ALL, ALLOW_NOTHING, HIDE, PROTECT_DELETE, PROTECT_OVERWRITE

Method Index

get(), isDeletable(), isEnumerable(), isOverwritable(), set()

Inherited from BasicClass

toString()

Field Detail

ALLOW_ALL

static public ALLOW_ALL:Number
Allow everything to be done with the object.

HIDE

static public HIDE:Number
Hide an object from for..in loops.

PROTECT_DELETE

static public PROTECT_DELETE:Number
Protect an object from deletion.

PROTECT_OVERWRITE

static public PROTECT_OVERWRITE:Number
Protect an object from overwriting.

ALLOW_NOTHING

static public ALLOW_NOTHING:Number
Allow nothing to be done with the object.

Method Detail

set

static public 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:

HIDE Hides the reference from for-in loops.
PROTECT_DELETE Protects the reference from deletion
PROTECT_OVERWRITE Protects the reference from overwriting
ALLOW_ALL Allows everything to be done with the reference.
ALLOW_NOTHING Allows 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

targetthe object that holds references to the objects the access permissions shall be applied to
referenceNamesthe names of the references to apply the access permission to
accessthe access permissions to apply

get

static public 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

targetthe target object that holds the reference
referenceNamethe name of the reference to return the access permission for

Return

a number representing the access permission of the reference

isEnumerable

static public function isEnumerable(target, referenceName:String):Boolean

Returns whether the reference is enumerable.

Parameters

targetthe target object that holds the reference
referenceNamethe name of the reference to return whether it is enumerable

Return

true if the reference is enumerable else false

isOverwritable

static public function isOverwritable(target, referenceName:String):Boolean

Returns whether the reference is overwritable.

Parameters

targetthe target object that holds the reference
referenceNamethe name of the reference to return whether it is overwritable

Return

true if the reference is overwritable else false

isDeletable

static public function isDeletable(target, referenceName:String):Boolean

Returns whether the reference is deletable.

Parameters

targetthe target object that holds the reference
referenceNamethe name of the reference to return whether it is deletable

Return

true if the reference is deletable else false