<commit_before><?php

/*
 * This file is part of the 'org.octris.core' package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace org\octris\core\auth\acl {
    /**
     * ACL role.
     *
     * @octdoc      c:acl/role
     * @copyright   copyright (c) 2011 by <NAME>
     * @author      <NAME> <<EMAIL>>
     */
    class role {
        /**
         * Name of role.
         *
         * @octdoc  v:role/$name
         * @var     string
         */
        protected $name;
        /**/

        /**
         * Privileges assigned to role.
         *
         * @octdoc  v:role/$privileges
         * @var     array
         */
        protected $privileges = array();
        /**/

        /**
         * Parent role.
         *
         * @octdoc  v:role/$parent
         * @var     \org\octris\core\auth\acl\role|null
         */
        protected $parent = null;
        /**/

        /**
         * Constructor.
         *
         * @octdoc  m:role/__construct
         * @param   string                          $name       The name of the role.
         * @param   \org\octris\core\auth\acl\role  $parent     Optional role to inherit.
         */
        public function __construct($name, \org\octris\core\auth\acl\role $parent = null)
        /**/
        {
            $this->name   = $name;
            $this->parent = $parent;
        }

        /**
         * Add a privilege to the role.
         *
         * @octdoc  m:role/addPrivilege
         * @param   \org\octris\core\auth\acl\resource  $resource       Resource to grant privilege to.
         * @param   array                               $actions        Allowed actions.
         */
        public function addPrivilege(\org\octris\core\auth\acl\resource $resource, array $actions);
        /**/
        {
            $this->privileges[$resource->getName()] = array(
                'resource' => $resource,
                'actions'  => $actions
            );
        }

        /**
         * Test whether a role may perform an specified action on a specified resource.
         *
         * @octdoc  m:role/isPrivileged
         * @param   \org\octris\core\auth\acl\resource  $resource       Resource to test.
         * @param   string                              $action         Action to test.
         * @return  bool                                                Returns true, if privilege is given.
         */
        public function isPrivileged(\org\octris\core\auth\acl\resource $resource, $action)
        /**/
        {
            $name = $resource->getName();

            return (isset($this->privileges[$name]) && in_array($action, $this->privileges[$name]['actions']));
        }
    }
}<commit_msg>- added support for multiple parents - renamed 'privilege' to 'policy' - finished implementation to checking authorization<commit_after><?php

/*
 * This file is part of the 'org.octris.core' package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace org\octris\core\auth\acl {
    /**
     * ACL role.
     *
     * @octdoc      c:acl/role
     * @copyright   copyright (c) 2011 by <NAME>
     * @author      <NAME> <<EMAIL>>
     */
    class role {
        /**
         * Name of role.
         *
         * @octdoc  v:role/$name
         * @var     string
         */
        protected $name;
        /**/

        /**
         * Additional policies assigned to role.
         *
         * @octdoc  v:role/$policies
         * @var     array
         */
        protected $policies = array();
        /**/

        /**
         * Parent roles.
         *
         * @octdoc  v:role/$parents
         * @var     array
         */
        protected $parents = array();
        /**/

        /**
         * Constructor.
         *
         * @octdoc  m:role/__construct
         * @param   string                          $name       The name of the role.
         */
        public function __construct($name)
        /**/
        {
            $this->name = $name;
        }

        /**
         * Add a parent role.
         *
         * @octdoc  m:role/addParent
         * @param   \org\octris\core\auth\acl\role  $parent     Role to inherit.
         */
        public function addParent(\org\octris\core\auth\acl\role $parent)
        /**/
        {
            $this->parents[] = $parent;
        }

        /**
         * Add the policy for a resource to the role.
         *
         * @octdoc  m:role/addPolicy
         * @param   \org\octris\core\auth\acl\resource  $resource       Resource to add policy for.
         * @param   string                              $action         Action to add policy for.
         * @param   int                                 $policy         Policy to set.
         */
        public function addPolicy(\org\octris\core\auth\acl\resource $resource, $action, $policy)
        /**/
        {
            $name = $resource->getName();

            if (!isset($this->policies[$name])) {
                $this->policies[$name] = array(
                    'resource' => $resource,
                    'actions'  => array()
                );
            }

            $this->policies[$name]['actions'][$action] = $policy;
        }

        /**
         * Return the role-specific policy for a specified resource and action. This method will not
         * return the default policy of a resource, instead -- if no policy for the resource is
         * specified in the role -- the method will return 'null'.
         *
         * @octdoc  m:role/getPolicy
         * @param   \org\octris\core\auth\acl\resource  $resource       Resource to get policy of.
         * @param   string                              $action         Action to get policy of.
         * @return  int|null                                            Policy, if available
         */
        public function getPolicy(\org\octris\core\auth\acl\resource $resource, $action)
        /**/
        {
            $name = $resource->getName();

            return (isset($this->policies[$name]) &&
                    isset($this->policies[$name]['actions'][$action])
                    ? $this->policies[$name]['actions'][$action]
                    : null);
        }

        /**
         * Thie method will calculate the policy for a specified resource and action by
         * including the parent roles, if any, to determine the final policy.
         *
         * @octdoc  m:role/calcPolicy
         * @param   \org\octris\core\auth\acl\resource  $resource       Resource to get policy of.
         * @param   string                              $action         Action to get policy of.
         * @param   int                                 $default        Optional default policy.
         * @return  int|null                                            Policy, if available
         */
        public function calcPolicy(\org\octris\core\auth\acl\resource $resource, $action, $default = null)
        /**/
        {
            $name   = $resource->getName();
            $result = (is_null($default) ? $resource->getPolicy() : $default);

            if (isset($this->policies[$name]) && isset($this->policies[$name]['actions'][$action])) {
                $result = self::$this->policies[$name]['actions'][$action];
            } else {
                foreach ($this->parents as $parent) {
                    $result = $parent->calcPolicy($resource, $action, $default);
                }
            }

            return $result;
        }

        /**
         * Test whether a role may perform an specified action on a specified resource.
         *
         * @octdoc  m:role/isAuthorized
         * @param   \org\octris\core\auth\acl\resource  $resource       Resource to test.
         * @param   string                              $action         Action to test.
         * @return  bool                                                Returns true, if role is authorized.
         */
        public function isAuthorized(\org\octris\core\auth\acl\resource $resource, $action)
        /**/
        {
            $policy = $this->calcPolicy($resource, $action);

            return ($policy == \org\octris\core\auth\acl::T_ALLOW);
        }
    }
}
{'fraction_non_alphanumeric': 0.11886120996441281, 'fraction_numerical': 0.0009489916963226571, 'mean_word_length': 1.2731194392019411, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7122205', 'n_tokens_mistral': 2236, 'n_tokens_neox': 2205, 'n_words': 562}