Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Acl_Privilege Class Reference

A class to encapsulate resource privilege information. More...

Inheritance diagram for P4Cms_Acl_Privilege:
P4Cms_ModelInterface

List of all members.

Public Member Functions

 __construct ($id, $label=null, $allow=null, array $options=null)
 Sets the privilege id and optionally the label and default allowed roles.
 getDefaultAllowed ()
 Get the default set of roles that are allowed this privilege.
 getFields ()
 Return array with all privilege fields.
 getId ()
 Get the id of this privilege.
 getLabel ()
 Get the human-friendly label of this privilege.
 getOption ($name)
 Get a custom option by name.
 getOptions ()
 Get custom options stored with this privilege.
 getResource ()
 Get the resource this privilege is associated with.
 getResourceId ()
 Get the id of the resource this privilege is associated with.
 getValue ($field)
 Return value of given field of the privilege.
 hasField ($field)
 Check if given field is a valid privilege field.
 hasResource ()
 Determine if this privilege is associated with a resource.
 setDefaultAllowed ($roles)
 Set the default default set of roles to be granted this privilege.
 setId ($id)
 Set the id of this privilege.
 setLabel ($label)
 Set the friendly label of this privilege.
 setOption ($name, $value)
 Set a custom option to be stored with this privilege.
 setOptions (array $options=null)
 Set custom options to be stored with this privilege.
 setResource (P4Cms_Acl_Resource $resource)
 Set the resource this privilege is associated with.
 toArray ()
 Convert this privilege to array form.

Static Public Member Functions

static factory ($privilege)
 Generate a acl privilege instance from string or array input.

Protected Attributes

 $_defaultAllow
 $_id
 $_label
 $_options
 $_resource

Detailed Description

A class to encapsulate resource privilege information.

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_Privilege::__construct ( id,
label = null,
allow = null,
array $  options = null 
)

Sets the privilege id and optionally the label and default allowed roles.

Parameters:
string$idthe string-based id of this privilege
string$labeloptional - human friendly label for this resource
string | array$allowoptional - default set of roles granted this privilege. string input is assumed to be comma-delimited.
array$optionsoptional - custom options to store with the privilege.
Returns:
void
    {
        $this->setId($id)
             ->setLabel($label)
             ->setDefaultAllowed($allow)
             ->setOptions($options);
    }

Member Function Documentation

static P4Cms_Acl_Privilege::factory ( privilege) [static]

Generate a acl privilege instance from string or array input.

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 to be granted this privilege options - optional custom properties to store on privilege

Parameters:
array | string$privilegean array of privilege information or a string-based privilege id.
Returns:
P4Cms_Acl_Privilege the new privilege instance.
    {
        // expected elements.
        $info = array(
            'id'        => null,
            'label'     => null,
            'allow'     => null,
            'options'   => array()
        );

        // if privilege itself is a string, take it as the id.
        if (is_string($privilege)) {
            $info['id'] = $privilege;
        }

        // if privilege is an array, it's properties win.
        // any non-standard properties add to the options element.
        if (is_array($privilege)) {
            $options         = array_diff_key($privilege, $info);
            $info            = array_merge($info, array_intersect_key($privilege, $info));
            $info['options'] = array_merge($options, $info['options']);
        }

        // generate privilege instance.
        $privilege = new P4Cms_Acl_Privilege(
            $info['id'],
            $info['label'],
            $info['allow'],
            $info['options']
        );

        return $privilege;
    }
P4Cms_Acl_Privilege::getDefaultAllowed ( )

Get the default set of roles that are allowed this privilege.

Returns:
array the default allowed roles.
    {
        return is_array($this->_defaultAllow) ? $this->_defaultAllow : array();
    }
P4Cms_Acl_Privilege::getFields ( )

Return array with all privilege fields.

Returns:
array all privilege fields.

Implements P4Cms_ModelInterface.

    {
        return array_keys($this->toArray());
    }
P4Cms_Acl_Privilege::getId ( )

Get the id of this privilege.

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

Get the human-friendly label of this privilege.

Any macros in the label will be expanded automatically.

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

Get a custom option by name.

Parameters:
string$namethe name of the option to get
Returns:
mixed the value of the custom option (null if unset).
    {
        return isset($this->_options[$name])
            ? $this->_options[$name]
            : null;
    }
P4Cms_Acl_Privilege::getOptions ( )

Get custom options stored with this privilege.

Returns:
array key/value pairs.
    {
        return is_array($this->_options) ? $this->_options : array();
    }
P4Cms_Acl_Privilege::getResource ( )

Get the resource this privilege is associated with.

Returns:
P4Cms_Acl_Resource the resource this privilege is associated with
Exceptions:
P4Cms_Acl_Exceptionif there is no associated resource.
    {
        if (!$this->_resource instanceof P4Cms_Acl_Resource) {
            throw new P4Cms_Acl_Exception(
                "Cannot get resource. No resource associated with this privilege."
            );
        }

        return $this->_resource;
    }
P4Cms_Acl_Privilege::getResourceId ( )

Get the id of the resource this privilege is associated with.

Returns:
string the id of the resource this privilege is associated with
Exceptions:
P4Cms_Acl_Exceptionif there is no associated resource.
    {
        return $this->getResource()->getResourceId();
    }
P4Cms_Acl_Privilege::getValue ( field)

Return value of given field of the privilege.

Parameters:
string$fieldmodel field to retrieve
Returns:
mixed the value of the named field.

Implements P4Cms_ModelInterface.

    {
        $fields = $this->toArray();
        return isset($fields[$field]) ? $fields[$field] : null;
    }
P4Cms_Acl_Privilege::hasField ( field)

Check if given field is a valid privilege field.

Parameters:
string$fieldprivilege field to check
Returns:
boolean true if the field exists.

Implements P4Cms_ModelInterface.

    {
        return in_array($field, $this->getFields());
    }
P4Cms_Acl_Privilege::hasResource ( )

Determine if this privilege is associated with a resource.

Returns:
bool true if this resource is associated with a resource; false otherwise.
    {
        try {
            $this->getResource();
            return true;
        } catch (P4Cms_Acl_Exception $e) {
            return false;
        }
    }
P4Cms_Acl_Privilege::setDefaultAllowed ( roles)

Set the default default set of roles to be granted this privilege.

Parameters:
array | string | null$rolesthe list of roles, or null to clear. string input is assumed to be comma-delimited.
Returns:
P4Cms_Acl_Privilege provides fluent interface.
    {
        // explode string on comma.
        if (is_string($roles)) {
            $roles = explode(',', $roles);
            $roles = array_map('trim', $roles);
        }

        if (!is_array($roles) && !is_null($roles)) {
            throw new InvalidArgumentException(
                "Cannot set default allowed roles. Roles must be an array, string or null"
            );
        }

        $this->_defaultAllow = $roles;

        return $this;
    }
P4Cms_Acl_Privilege::setId ( id)

Set the id of this privilege.

Parameters:
string | null$idthe id of this privilege, or null to clear.
Returns:
P4Cms_Acl_Privilege provides fluent interface.
    {
        if (!is_string($id) && !is_null($id)) {
            throw new InvalidArgumentException("Cannot set id. Id must be a string or null");
        }

        $this->_id = $id;

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

Set the friendly label of this privilege.

Parameters:
string | null$labelthe label of this privilege, or null to clear.
Returns:
P4Cms_Acl_Privilege provides fluent interface.
    {
        if (!is_string($label) && !is_null($label)) {
            throw new InvalidArgumentException("Cannot set label. Label must be a string or null");
        }

        $this->_label = $label;

        return $this;
    }
P4Cms_Acl_Privilege::setOption ( name,
value 
)

Set a custom option to be stored with this privilege.

Parameters:
string$namethe option name
mixed$valuethe option value
Returns:
P4Cms_Acl_Privilege provides fluent interface.
    {
        $this->_options[$name] = $value;

        return $this;
    }
P4Cms_Acl_Privilege::setOptions ( array $  options = null)

Set custom options to be stored with this privilege.

Parameters:
array | null$optionsa list of key/value pairs to store.
Returns:
P4Cms_Acl_Privilege provides fluent interface.
    {
        $this->_options = $options;

        return $this;
    }
P4Cms_Acl_Privilege::setResource ( P4Cms_Acl_Resource resource)

Set the resource this privilege is associated with.

Parameters:
P4Cms_Acl_Resource$resourcethe resource this privilege is associated with
Returns:
P4Cms_Acl_Privilege provides fluent interface.
    {
        $this->_resource = $resource;
    }
P4Cms_Acl_Privilege::toArray ( )

Convert this privilege to array form.

Returns:
array this privilege as an array.

Implements P4Cms_ModelInterface.

    {
        return array(
            'id'        => $this->getId(),
            'label'     => $this->getLabel(),
            'allow'     => $this->getDefaultAllowed(),
            'resource'  => $this->getResourceId(),
            'options'   => $this->getOptions()
        );
    }

Member Data Documentation

P4Cms_Acl_Privilege::$_defaultAllow [protected]
P4Cms_Acl_Privilege::$_id [protected]
P4Cms_Acl_Privilege::$_label [protected]
P4Cms_Acl_Privilege::$_options [protected]
P4Cms_Acl_Privilege::$_resource [protected]

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