phpPoA2
[ class tree: phpPoA2 ] [ index: phpPoA2 ] [ all elements ]

Source for file AttributeFilterAuthzEngine.php

Documentation is available at AttributeFilterAuthzEngine.php

  1. <?php
  2. /**
  3.  * @copyright Copyright 2005-2010 RedIRIS, http://www.rediris.es/
  4.  *
  5.  *  This file is part of phpPoA2.
  6.  *
  7.  *  phpPoA2 is free software: you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation, either version 3 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  phpPoA2 is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with phpPoA2. If not, see <http://www.gnu.org/licenses/>.
  19.  *
  20.  * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
  21.  * @version 2.0
  22.  * @author Jaime Perez <jaime.perez@rediris.es>
  23.  * @filesource
  24.  */
  25.  
  26. /**
  27.  * This hook is executed right after retrieving the arrays of allowed and denied attributes that
  28.  * will be checked inmediately.
  29.  * It can be used to configure the filters on runtime and modify the user's attributes.
  30.  * The hook receives the attributes, and the allowed and denied attribute arrays.
  31.  * Functions for this hook must be defined like this:
  32.  *
  33.  * function attributeBeforeFilterHook(&$attrs, &$allowed, &$denied);
  34.  *
  35.  * Please bear in mind that hooks must return TRUE or they'll keep other hooks from executing.
  36.  */
  37. define("ATTRIBUTE_BEFORE_FILTERS""ATTRIBUTE_BEFORE_FILTERS");
  38.  
  39. /**
  40.  * Authorization engine that works by checking the attributes of the user. The first match of an
  41.  * attribute against one of the filters will trigger the authorization result, no matter if it's
  42.  * positive or negative.
  43.  * @package phpPoA2
  44.  * @subpackage AttributeFilterAuthorizationEngine
  45.  */
  46.  
  47.     protected $valid_hooks = array(ATTRIBUTE_BEFORE_FILTERS);
  48.  
  49.     public function isAuthorized($user$attrs{
  50.         $default $this->cfg->getDefaultBehaviour();
  51.         $allowed $this->cfg->getAllowedAttributes();
  52.         $denied  $this->cfg->getDeniedAttributes();
  53.  
  54.         // run hook before checking patterns
  55.         $args array($attrs$allowed$denied);
  56.         $this->runHooks(ATTRIBUTE_BEFORE_FILTERS$args);
  57.         $attrs $args[0];
  58.         $allowed $args[1];
  59.         $denied $args[2];
  60.  
  61.         $allowed_match $this->matches($attrs$allowed);
  62.         $denied_match  $this->matches($attrs$denied);
  63.  
  64.         // check matches giving priority to the default setting
  65.         $order array($default!$default);
  66.         foreach ($order as $option{
  67.             if ($option// check allowed attributes
  68.                 if ($allowed_match{
  69.                     trigger_error(PoAUtils::msg('allowed-attr-match'array($allowed_match))E_USER_WARNING);
  70.                     return true;
  71.                 }
  72.             else // check denied attributes
  73.                 if ($denied_match{
  74.                     trigger_error(PoAUtils::msg('denied-attr-match'array($denied_match))E_USER_WARNING);
  75.                     return false;
  76.                 }
  77.             }
  78.         }
  79.  
  80.         // default response
  81.         trigger_error(PoAUtils::msg('authz-default-fallback')E_USER_NOTICE);
  82.         return $default;
  83.     }
  84.  
  85.     public function getAuthorizedList({
  86.         $this->registerHandler();
  87.         $list $this->cfg->getAllowedAttributes();
  88.         $this->clean();
  89.         return $list;
  90.     }
  91.  
  92.     public function authorize($user$attrs$ref$expires 0{
  93.         return false;
  94.     }
  95.  
  96.     public function revoke($mail{
  97.         return false;
  98.     }
  99.  
  100.     /**
  101.      * Returns the attribute (or attributes) that matched a list of patterns.
  102.      * @param attrs An associative array of attributes to check.
  103.      * @param patterns An associative array of attributes and their patterns.
  104.      * @return The names of the attributes matched, comma separated if more than one.
  105.      *  False otherwise.
  106.      */
  107.     private function matches($attrs$patterns{
  108.         $match false;
  109.         foreach ($patterns as $key => $value{
  110.             if (is_numeric($key&& is_array($value)) // must match a bunch of options
  111.                 $partial_match true;
  112.                 $matches array();
  113.                 foreach ($value as $name => $pattern{
  114.                     if (!isset($attrs[$name])) // attribute not set, skip this option
  115.                         $partial_match false;
  116.                         break;
  117.                     }
  118.  
  119.                     // convert attribute to array for easy handling
  120.                     $attr $attrs[$name];
  121.                     if (!is_array($attrs[$name])) {
  122.                         $attr array($attrs[$name]);
  123.                     }
  124.  
  125.                     // convert pattern to array for easy handling
  126.                     $pats $pattern;
  127.                     if (!is_array($pattern)) {
  128.                         $pats array($pattern);
  129.                     }
  130.  
  131.                     // check if any of the possible values match
  132.                     $some_val_matches false;
  133.                     foreach ($attr as $item{
  134.                         foreach ($pats as $pat{
  135.                             if (preg_match('/^'.$pat.'$/'$item)) {
  136.                                 // attribute matches, continue
  137.                                 $some_val_matches true;
  138.                                 break;
  139.                             }
  140.                         }
  141.                         if ($some_val_matchesbreak;
  142.                     }
  143.                     if (!$some_val_matches{
  144.                         $partial_match false;
  145.                         break;
  146.                     }
  147.  
  148.                     $matches[$name;
  149.                 }
  150.                 if ($partial_match{
  151.                     $match implode(","$matches);
  152.                 }
  153.  
  154.             else // match just one attribute
  155.                 if (!isset($attrs[$key])) // attribute not set, skip this option
  156.                     continue;
  157.  
  158.                 // convert attribute to array for easy handling
  159.                 $attr $attrs[$key];
  160.                 if (!is_array($attrs[$key])) {
  161.                     $attr array($attrs[$key]);
  162.                 }
  163.  
  164.                 // convert pattern to array for easy handling
  165.                 $pats $value;
  166.                 if (!is_array($value)) {
  167.                     $pats array($value);
  168.                 }
  169.  
  170.                 // check if any of the possible values match
  171.                 foreach ($attr as $item{
  172.                     foreach ($pats as $pattern{
  173.                         if (preg_match('/^'.$pattern.'$/'$item)) {
  174.                             // attribute matches, stop searching
  175.                             $match $key;
  176.                             break;
  177.                         }
  178.                     }
  179.                     if ($matchbreak;
  180.                 }
  181.             }
  182.         }
  183.         return $match;
  184.     }
  185.  
  186. }
  187.  
  188. ?>

Documentation generated on Tue, 14 Jun 2011 12:22:05 +0200 by phpDocumentor 1.4.3