"Fossies" - the Fresh Open Source Software Archive

Member "icinga-web-1.14.1/app/modules/Api/lib/datastore/AbstractDataStoreModel.class.php" (19 Dec 2017, 10668 Bytes) of package /linux/misc/old/icinga-web-1.14.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 // {{{ICINGA_LICENSE_CODE}}}
    3 // -----------------------------------------------------------------------------
    4 // This file is part of icinga-web.
    5 //
    6 // Copyright (c) 2009-2015 Icinga Developer Team.
    7 // All rights reserved.
    8 //
    9 // icinga-web is free software: you can redistribute it and/or modify
   10 // it under the terms of the GNU General Public License as published by
   11 // the Free Software Foundation, either version 3 of the License, or
   12 // (at your option) any later version.
   13 //
   14 // icinga-web is distributed in the hope that it will be useful,
   15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
   16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17 // GNU General Public License for more details.
   18 //
   19 // You should have received a copy of the GNU General Public License
   20 // along with icinga-web.  If not, see <http://www.gnu.org/licenses/>.
   21 // -----------------------------------------------------------------------------
   22 // {{{ICINGA_LICENSE_CODE}}}
   23 
   24 /**
   25 * Thrown when an datastore invalid datastore was createdd
   26 *
   27 * @package Icinga_Api
   28 * @category DataStore
   29 *
   30 * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
   31 **/
   32 class DataStoreValidationException extends AppKitException {};
   33 
   34 /**
   35 * Thrown when an datastore actions without permission will be called
   36 *
   37 * @package Icinga_Api
   38 * @category DataStore
   39 *
   40 * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
   41 **/
   42 class DataStorePermissionException extends AppKitException {};
   43 
   44 /**
   45 * Abstract base class for all datastores, which handles method decoration via
   46 * modifiers, permission/credential validation for each action.
   47 * @abstract
   48 * @package Icinga_Api
   49 * @category DataStore
   50 *
   51 *
   52 * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
   53 **/
   54 
   55 abstract class AbstractDataStoreModel extends IcingaBaseModel {
   56     protected $modifiers = array();
   57     protected $requestParameters = array();
   58 
   59     /**
   60     * Base read function for reading from the dataSource
   61     * @return mixed     The request result
   62     * @access private
   63     *
   64     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
   65     **/
   66     public function doRead() {
   67         if (!$this->hasPermission($this->canRead())) {
   68             throw new DataStorePermissionException("Store ".get_class($this)." is not readable");
   69         }
   70 
   71         return $this->execRead();
   72     }
   73 
   74     /**
   75     * Base insert function for the dataSource
   76     * @return mixed     The request result
   77     * @access private
   78     *
   79     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
   80     **/
   81     public function doInsert($valueDescriptor) {
   82         if (!$this->hasPermission($this->canInsert())) {
   83             throw new DataStorePermissionException("Store ".get_class($this)." is not insertable");
   84         }
   85 
   86         return $this->execWrite($valueDescriptor);
   87     }
   88 
   89     /**
   90     * Base update function for the dataSource
   91     * @return mixed     The request result
   92     * @access private
   93     *
   94     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
   95     **/
   96     public function doUpdate($idDescriptor) {
   97         if (!$this->hasPermission($this->canUpdate())) {
   98             throw new DataStorePermissionException("Store ".get_class($this)." is not updateable");
   99         }
  100 
  101         return $this->execUpdate($idDescriptor);
  102     }
  103 
  104     /**
  105     * Base delete function for the dataSource
  106     * @return mixed     The request result
  107     * @access private
  108     *
  109     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
  110     **/
  111     public function doDelete($valueDescriptor) {
  112         if (!$this->hasPermission($this->canDelete())) {
  113             throw new DataStorePermissionException("Store ".get_class($this)." is not deleteable");
  114         }
  115 
  116         return $this->execDelete($valueDescriptor);
  117     }
  118 
  119     /**
  120     * Inserts data to this data source
  121     * Overwrite this function with your custom insert function
  122     *
  123     * @param mixed  An object describing the new dataset
  124     * @return mixed Result or null
  125     *
  126     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
  127     **/
  128     protected function execInsert($valueDescriptor) {}
  129 
  130     /**
  131     * Reads data from this data source
  132     * Overwrite this function with your custom read function
  133     *
  134     * @return mixed Result or null
  135     *
  136     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
  137     **/
  138     protected function execRead() {}
  139 
  140     /**
  141     * Deletes data from this data source
  142     * Overwrite this function with your custom delete function
  143     * @param mixed      An object/array/id for the source to delete
  144     * @return mixed     Result or null
  145     *
  146     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
  147     **/
  148     protected function execDelete($idDescriptor) {}
  149 
  150     /**
  151     * Updates data in this data source
  152     * Overwrite this function with your custom read function
  153     * @param mixed  An object/array that describes what to update
  154     * @return mixed Result or null
  155     *
  156     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
  157     **/
  158     protected function execUpdate($updateDescriptor) {}
  159 
  160     /**
  161     * Register a new modifier for this store. Should be done statically on @see setupModifier
  162     * @param    String  The modifier to add (module path)
  163     * @param    String  The module where the modifier lies
  164     *
  165     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
  166     **/
  167     protected function registerStoreModifier($modifier, $module= null) {
  168         if ($modifier instanceof IDataStoreModifier) {
  169             $this->modifiers[] = $modifier;
  170         } else {
  171             $this->modifiers[] = $this->context->getModel($modifier,$module);
  172         }
  173     }
  174 
  175     /**
  176     * Store modifiers must be defined and added here
  177     * via the @see registerStoreModifier method.
  178     * Example:
  179     * <code>
  180     *   protected function setupModifiers() {
  181     *       $this->registerStoreModifier('Store.Modifiers.StorePaginationModifier','Api');
  182     *       $this->registerStoreModifier('Store.Modifiers.StoreSortModifier','Api');
  183     *       parent::setupModifiers();
  184     *   }
  185     * </code>
  186     *
  187     *
  188     *
  189     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
  190     **/
  191     protected function setupModifiers() {
  192 
  193         foreach($this->requestParameters as $parameter=>$value) {
  194             foreach($this->modifiers as $modifier) {
  195                 $modifier->handleArgument($parameter,$value);
  196             }
  197         }
  198     }
  199 
  200     public function getModifiers() {
  201         return $this->modifiers;
  202     }
  203     /**
  204     * Initializes the module and calls setupModifers if parameters doesn't contain "noStoreModifierSetup"
  205     * @param    AgaviContext    The context of the agavi instance
  206     * @param    Array           An array of submitted parameters
  207     *                               - AgaviRequestDataHolder request : the request information
  208     *                               - noStoreModifierSetup : Don't setup modifiers (add when callin parent::initialize)
  209     *
  210     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
  211     **/
  212     public function initialize(AgaviContext $context,array $parameters = array()) {
  213         parent::initialize($context,$parameters);
  214         $this->defaultInitialize($context,$parameters);
  215     }
  216 
  217     protected function defaultInitialize(AgaviContext $context,array $parameters = array()) {
  218         if (isset($parameters["request"])) {
  219             $this->requestParameters = $parameters["request"]->getParameters();
  220         }
  221 
  222         if (!isset($parameters["noStoreModifierSetup"])) {
  223             $this->setupModifiers();
  224         }
  225 
  226     }
  227 
  228     /**
  229     * Applies all modifiers to the passed object (may be a Doctrine_Query, a PDO
  230     * Statement, a sql string.
  231     * @param $object
  232     *
  233     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
  234     **/
  235     protected function applyModifiers(&$object) {
  236         foreach($this->getModifiers() as $mod) {
  237             $mod->modify($object);
  238         }
  239     }
  240 
  241     /**
  242     * Checks if the current user has this permission
  243     * @param true|false|String|Array True/False to directly set permission, or an array of agavi credentials to check the user against
  244     * @return boolean
  245     *
  246     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
  247     **/
  248     protected function hasPermission($perm) {
  249         if ($perm === true) {
  250             return true;
  251         }
  252 
  253         if ($perm === false) {
  254             return false;
  255         }
  256 
  257         $ctx = AgaviContext::getInstance();
  258         $user = $ctx->getUser();
  259 
  260         if (!$user) {
  261             return false;
  262         }
  263 
  264         if (!is_array($perm)) {
  265             $perm = array($perm);
  266         }
  267 
  268         foreach($perm as $p) {
  269             if ($user->hasCredential($p)) {
  270                 return true;
  271             }
  272         }
  273         return false;
  274     }
  275 
  276     /**
  277     * Returns true or false whether read can be performed. Override with a custom
  278     * credential check if you need to.
  279     *
  280     * @return boolean or Array with credentials
  281     *
  282     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
  283     **/
  284     public function canRead() {
  285         return true;
  286     }
  287 
  288     /**
  289     * Returns true or false whether insert can be performed. Override with a custom
  290     * credential check if you need to.
  291     *
  292     * @return boolean  or Array with credentials
  293     *
  294     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
  295     **/
  296     public function canInsert() {
  297         return false;
  298     }
  299 
  300     /**
  301     * Returns true or false whether update can be performed. Override with a custom
  302     * credential check if you need to.
  303     *
  304     * @return boolean  or Array with credentials
  305     *
  306     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
  307     **/
  308     public function canUpdate() {
  309         return false;
  310     }
  311 
  312     /**
  313     * Returns true or false whether delete can be performed. Override with a custom
  314     * credential check if you need to.
  315     *
  316     * @return boolean  or Array with credentials
  317     *
  318     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
  319     **/
  320     public function canDelete() {
  321         return false;
  322     }
  323 
  324     /**
  325     * Delegates unknown method calls to the modifiers and thereby extends the
  326     * store by the modifiers methods
  327     *
  328     * @author Jannis Moßhammer <jannis.mosshammer@netways.de>
  329     **/
  330     public function __call($method,$argument) {
  331         $found = false;
  332         foreach($this->getModifiers() as $mod) {
  333             if (method_exists($mod,$method)) {
  334                 $found = true;
  335                 return call_user_func_array(array($mod,$method),$argument);
  336             }
  337         }
  338 
  339         if (!$found) {
  340             throw new BadMethodCallException("Call to unknown method $method");
  341         }
  342     }
  343 }
  344 ?>