Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Acl_Resource Class Reference

Extends acl resources to support associated privileges. More...

List of all members.

Public Member Functions

 __construct ($resourceId, array $privileges=null, $label=null)
 Sets the resource identifier, and optionally the privileges and label.
 addPrivilege ($privilege)
 Add a privilege to this resource.
 getId ()
 Get the id of this resource.
 getLabel ()
 Get the label set for this resource.
 getPrivilege ($id)
 Get a specific privilege associated with this resource.
 getPrivileges ()
 Get the privileges associated with this resource.
 hasPrivilege ($id)
 Check if this resource has the named privilege.
 hasPrivileges ()
 Check if this resource has any privileges.
 normalizePrivilege ($privilege, $id=null)
 Normalize mixed input to a identifiable privilege.
 removePrivilege ($id)
 Remove a privilege from this resource.
 setLabel ($label)
 Set the human-friendly label for this resource.
 setPrivileges (array $privileges=null)
 Set the privileges associated with this resource.

Protected Attributes

 $_label = null
 $_privileges = array()
 $_published = false

Detailed Description

Extends acl resources to support associated privileges.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Constructor & Destructor Documentation

P4Cms_Acl_Resource::__construct ( resourceId,
array $  privileges = null,
label = null 
)

Sets the resource identifier, and optionally the privileges and label.

Parameters:
string$resourceIdthe string-based id of this resource
array$privilegesoptional - set of privileges for this resource
string$labeloptional - human friendly label for this resource
Returns:
void
    {
        parent::__construct($resourceId);

        $this->setPrivileges($privileges);
        $this->setLabel($label);
    }

Member Function Documentation

P4Cms_Acl_Resource::addPrivilege ( privilege)

Add a privilege to this resource.

Parameters:
P4Cms_Acl_Privilege | array | string$privilegea privilege instance, an array of privilege information or a string-based privilege id.
Returns:
P4Cms_Acl_Resource provides fluent interface.
Exceptions:
InvalidArgumentExceptionif the given privilege is invalid.
    {
        $privilege = $this->normalizePrivilege($privilege);
        $privilege->setResource($this);

        $this->_privileges[$privilege->getId()] = $privilege;

        return $this;
    }
P4Cms_Acl_Resource::getId ( )

Get the id of this resource.

Returns:
string|null the id of this resource, or null if none set.
    {
        return $this->getResourceId();
    }
P4Cms_Acl_Resource::getLabel ( )

Get the label set for this resource.

Any macros in the label will be expanded automatically.

Returns:
string the label of this resource, or the id if no label is set.
    {
        $macro = new P4Cms_Filter_Macro(array('resource' => $this));
        return $macro->filter($this->_label ?: $this->getId());
    }
P4Cms_Acl_Resource::getPrivilege ( id)

Get a specific privilege associated with this resource.

Parameters:
string$idthe associated privilege to get
Returns:
P4Cms_Acl_Privilege the specified privilege.
    {
        if (!$this->hasPrivilege($id)) {
            throw new P4Cms_Acl_Exception(
                "Cannot get privilege. Privilege $id not found."
            );
        }
        return $this->_privileges[$id];
    }
P4Cms_Acl_Resource::getPrivileges ( )

Get the privileges associated with this resource.

Each element in the privileges list is keyed on the privilege id. The element value is itself an array gauranteed to contain the id of the privilege. It may also contain a human-friendly label

Returns:
array the privileges defined for this resource.

p4cms.acl.<resource>.privileges Modify the passed resource, intended to allow subscribers to add/remove/modify the resource's privileges. The <resource> portion of the topic is the resource's ID. P4Cms_Acl_Resource $resource The resource to modify.

    {
        // publish resource to allow modification of privileges
        // only publish when privileges first accessed, this
        // also permits access to privileges from subscribers
        if (!$this->_published) {
            $this->_published = true;
            P4Cms_PubSub::publish(
                'p4cms.acl.' . $this->getId() . '.privileges',
                $this
            );
        }

        return $this->_privileges;
    }
P4Cms_Acl_Resource::hasPrivilege ( id)

Check if this resource has the named privilege.

Parameters:
string$idthe id of the privilege to check for.
Returns:
bool true if the identified privilege exists on this resource.
    {
        return array_key_exists($id, $this->_privileges);
    }
P4Cms_Acl_Resource::hasPrivileges ( )

Check if this resource has any privileges.

Returns:
bool true, if there are any privileges on this resource.
    {
        return !empty($this->_privileges);
    }
P4Cms_Acl_Resource::normalizePrivilege ( privilege,
id = null 
)

Normalize mixed input to a identifiable privilege.

If a string is given, it will be taken to be the privilege id. If an array is given, it may contain:

id - the id of the privilege label - optional, human-friendly privilege label allow - default set of roles granted this privilege

If the optional id parameter is given, it will be set as the privilege id, but only if the privilege parameter is an array without an id element.

Parameters:
mixed$privilegea privilege info array, string id, or instance.
string$idoptional - id of the privilege if not present in privilege.
Returns:
P4Cms_Acl_Privilege the new privilege instance.
Exceptions:
InvalidArgumentExceptionif we can't produce an identifable privilege.
    {
        if (!$privilege instanceof P4Cms_Acl_Privilege) {
            $privilege = P4Cms_Acl_Privilege::factory($privilege);
        }

        // last chance to identify privilege.
        if (!$privilege->getId()) {
            $privilege->setId($id);
        }

        // throw exception if privilege is not identified.
        if (!$privilege->getId()) {
            throw new InvalidArgumentException(
                "Cannot normalize input to an identifiable privilege."
            );
        }

        return $privilege;
    }
P4Cms_Acl_Resource::removePrivilege ( id)

Remove a privilege from this resource.

Parameters:
string$ida string-based privilege id.
Returns:
P4Cms_Acl_Resource provides fluent interface.
    {
        if ($this->hasPrivilege($id)) {
            unset($this->_privileges[$id]);
        }

        return $this;
    }
P4Cms_Acl_Resource::setLabel ( label)

Set the human-friendly label for this resource.

Parameters:
string$labelthe label for this resource.
Returns:
P4Cms_Acl_Resource provides fluent interface.
    {
        $this->_label = (string) $label;
    }
P4Cms_Acl_Resource::setPrivileges ( array $  privileges = null)

Set the privileges associated with this resource.

Parameters:
array$privilegesthe privileges to associate with this resource. each entry must be a privilege instance, an array of privilege information or a string-based privilege id.
Returns:
P4Cms_Acl_Resource provides fluent interface.
Exceptions:
InvalidArgumentExceptionif any of the privileges are invalid.
    {
        // clear existing privileges.
        $this->_privileges = array();

        // if given privileges are null, all done.
        if (!$privileges) {
            return $this;
        }

        // mormalize and add each privilege.
        foreach ($privileges as $key => $value) {
            $this->addPrivilege(
                $this->normalizePrivilege($value, $key)
            );
        }

        return $this;
    }

Member Data Documentation

P4Cms_Acl_Resource::$_label = null [protected]
P4Cms_Acl_Resource::$_privileges = array() [protected]
P4Cms_Acl_Resource::$_published = false [protected]

The documentation for this class was generated from the following file: