eZ Publish  [4.0]
ezpolicylimitation.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZPolicyLimitation class
00004 //
00005 // Created on: <19-Aug-2002 10:57:01 sp>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezpolicylimitation.php
00032 */
00033 
00034 /*!
00035   \class eZPolicyLimitation ezpolicylimitation.php
00036   \ingroup eZRole
00037   \brief Defines a limitation for a policy in the permission system
00038 
00039 */
00040 //include_once( "lib/ezdb/classes/ezdb.php" );
00041 //include_once( "kernel/classes/ezpolicylimitationvalue.php" );
00042 //include_once( "kernel/classes/ezpersistentobject.php" );
00043 
00044 class eZPolicyLimitation extends eZPersistentObject
00045 {
00046     /*!
00047      Constructor
00048     */
00049     function eZPolicyLimitation( $row )
00050     {
00051           $this->eZPersistentObject( $row );
00052           $this->NodeID = 0;
00053     }
00054 
00055     static function definition()
00056     {
00057         return array( "fields" => array( "id" => array( 'name' => 'ID',
00058                                                         'datatype' => 'integer',
00059                                                         'default' => 0,
00060                                                         'required' => true ),
00061                                          'policy_id' => array( 'name' => 'PolicyID',
00062                                                                'datatype' => 'integer',
00063                                                                'default' => 0,
00064                                                                'required' => true,
00065                                                                'foreign_class' => 'eZPolicy',
00066                                                                'foreign_attribute' => 'id',
00067                                                                'multiplicity' => '1..*' ),
00068                                          'identifier' => array( 'name' => 'Identifier',
00069                                                                 'datatype' => 'string',
00070                                                                 'default' => '',
00071                                                                 'required' => true ) ),
00072                       "keys" => array( "id" ),
00073                       "function_attributes" => array( 'policy' => 'policy',
00074                                                       'values' => 'valueList',
00075                                                       'values_as_array' => 'allValues',
00076                                                       'values_as_string' => 'allValuesAsString',
00077                                                       'values_as_array_with_names' => 'allValuesAsArrayWithNames',
00078                                                       'limit_value' => 'limitValue' ),
00079                       "increment_key" => "id",
00080                       "sort" => array( "id" => "asc" ),
00081                       "class_name" => "eZPolicyLimitation",
00082                       "name" => "ezpolicy_limitation" );
00083     }
00084 
00085     function limitValue()
00086     {
00087         return $this->LimitValue;
00088     }
00089 
00090     /*!
00091      Get policy object of this policy limitation
00092     */
00093     function policy()
00094     {
00095         //include_once( 'kernel/classes/ezpolicy.php' );
00096         return eZPolicy::fetch( $this->attribute( 'policy_id' ) );
00097     }
00098 
00099     /*!
00100      \reimp
00101     */
00102     function setAttribute( $attr, $val )
00103     {
00104         switch( $attr )
00105         {
00106             case 'limit_value':
00107             {
00108                 $this->LimitValue = $val;
00109             } break;
00110 
00111             default:
00112             {
00113                 eZPersistentObject::setAttribute( $attr, $val );
00114             } break;
00115         }
00116     }
00117 
00118     /*!
00119      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00120      the calls within a db transaction; thus within db->begin and db->commit.
00121      */
00122     static function createNew( $policyID, $identifier )
00123     {
00124         $policyParameter = new eZPolicyLimitation( array() );
00125         $policyParameter->setAttribute( 'policy_id', $policyID );
00126         $policyParameter->setAttribute( 'identifier', $identifier );
00127         $policyParameter->store();
00128 
00129         return $policyParameter;
00130     }
00131 
00132     /*!
00133      \static
00134      Create a new policy limitation for the policy \a $policyID with the identifier \a $identifier.
00135      \note The limitation is not stored.
00136     */
00137     static function create( $policyID, $identifier )
00138     {
00139         $row = array( 'id' => null,
00140                       'policy_id' => $policyID,
00141                       'identifier' => $identifier );
00142         return new eZPolicyLimitation( $row );
00143     }
00144 
00145     /*!
00146      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00147      the calls within a db transaction; thus within db->begin and db->commit.
00148      */
00149     static function removeSelected( $ID )
00150     {
00151         eZPersistentObject::removeObject( eZPolicyLimitation::definition(),
00152                                           array( "id" => $ID ) );
00153     }
00154 
00155     static function fetchByIdentifier( $policyID, $identifier, $asObject = true )
00156     {
00157         return eZPersistentObject::fetchObject( eZPolicyLimitation::definition(),
00158                                                 null,
00159                                                 array( "policy_id" => $policyID,
00160                                                        "identifier" => $identifier ),
00161                                                 $asObject );
00162     }
00163 
00164     static function fetchByPolicyID( $policyID, $asObject = true )
00165     {
00166         return eZPersistentObject::fetchObjectList( eZPolicyLimitation::definition(),
00167                                                     null,
00168                                                     array( "policy_id" => $policyID ),
00169                                                     null,
00170                                                     null,
00171                                                     $asObject );
00172     }
00173 
00174     /*!
00175      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00176      the calls within a db transaction; thus within db->begin and db->commit.
00177      */
00178     function copy( $policyID )
00179     {
00180         $newParameter = eZPolicyLimitation::createNew( $policyID, $this->attribute( 'identifier' ) );
00181         foreach( $this->attribute( 'values' ) as $value )
00182         {
00183             $value->copy( $newParameter->attribute( 'id' ) );
00184         }
00185     }
00186 
00187     /*!
00188      \sa removeThis
00189     */
00190     static function removeByID( $id )
00191     {
00192         $db = eZDB::instance();
00193 
00194         $idString = $db->escapeString( $id );
00195         $db->begin();
00196 
00197         $db->query( "DELETE FROM ezpolicy_limitation_value
00198                      WHERE ezpolicy_limitation_value.limitation_id = '$idString'" );
00199 
00200         $db->query( "DELETE FROM ezpolicy_limitation
00201                      WHERE ezpolicy_limitation.id = '$idString' " );
00202         $db->commit();
00203     }
00204 
00205     /*!
00206      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00207      the calls within a db transaction; thus within db->begin and db->commit.
00208      */
00209     function removeThis()
00210     {
00211         eZPolicyLimitation::removeByID( $this->attribute( 'id' ) );
00212     }
00213 
00214     function allValuesAsString()
00215     {
00216         $str='';
00217         foreach ( $this->attribute( 'values' ) as $value )
00218         {
00219             if ( $str == '' )
00220             {
00221                 $str .= $value->attribute( 'value' );
00222             }else
00223             {
00224                 $str .= ',' . $value->attribute( 'value' );
00225             }
00226         }
00227         return $str;
00228     }
00229 
00230     function allValuesAsArrayWithNames()
00231     {
00232         $returnValue = null;
00233         $valueList   = $this->attribute( 'values_as_array' );
00234         $names       = array();
00235         $policy      = $this->attribute( 'policy' );
00236         if ( !$policy )
00237         {
00238             return $returnValue;
00239         }
00240 
00241         $currentModule = $policy->attribute( 'module_name' );
00242         $mod = eZModule::exists( $currentModule );
00243         if ( !is_object( $mod ) )
00244         {
00245             eZDebug::writeError( 'Failed to fetch instance for module ' . $currentModule );
00246             return $returnValue;
00247         }
00248         $functions = $mod->attribute( 'available_functions' );
00249         $functionNames = array_keys( $functions );
00250 
00251         $currentFunction = $policy->attribute( 'function_name' );
00252         $limitationValueArray = array();
00253 
00254         $limitation = $functions[$currentFunction ][$this->attribute( 'identifier' )];
00255 
00256         if ( $limitation &&
00257              count( $limitation[ 'values' ] == 0 ) &&
00258              array_key_exists( 'class', $limitation ) )
00259         {
00260             $basePath = 'kernel/'; //set default basepath for limitationValueClasses
00261             if( array_key_exists( 'extension', $limitation ) && $limitation['extension'] )
00262             {
00263                 $basePath = 'extension/' . $limitation['extension'] . '/';
00264             }
00265             include_once( $basePath . $limitation['path'] . $limitation['file']  );
00266             $obj = new $limitation['class']( array() );
00267             $limitationValueList = call_user_func_array ( array( $obj , $limitation['function']) , $limitation['parameter'] );
00268             foreach( $limitationValueList as $limitationValue )
00269             {
00270                 $limitationValuePair = array();
00271                 $limitationValuePair['Name'] = $limitationValue[ 'name' ];
00272                 $limitationValuePair['value'] = $limitationValue[ 'id' ];
00273                 $limitationValueArray[] = $limitationValuePair;
00274             }
00275         }
00276         else if ( $limitation['name'] == "Node" )
00277         {
00278             //include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
00279             foreach ( $valueList as $value )
00280             {
00281                 $node = eZContentObjectTreeNode::fetch( $value, false, false );
00282                 if ( $node == null )
00283                     continue;
00284                 $limitationValuePair = array();
00285                 $limitationValuePair['Name'] = $node['name'];
00286                 $limitationValuePair['value'] = $value;
00287                 $limitationValueArray[] = $limitationValuePair;
00288             }
00289         }
00290         else if ( $limitation['name'] == "Subtree" )
00291         {
00292             //include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
00293             foreach ( $valueList as $value )
00294             {
00295                 $subtreeObject = eZContentObjectTreeNode::fetchByPath( $value, false );
00296                 if ( $subtreeObject != null )
00297                 {
00298                     $limitationValuePair = array();
00299                     $limitationValuePair['Name'] = $subtreeObject['name'];
00300                     $limitationValuePair['value'] = $value;
00301                     $limitationValueArray[] = $limitationValuePair;
00302                 }
00303             }
00304         }
00305         else
00306         {
00307             $limitationValueArray = $limitation[ 'values' ];
00308         }
00309         $limitationValuesWithNames = array();
00310         foreach ( array_keys( $valueList ) as $key )
00311         {
00312             $value = $valueList[$key];
00313             if ( isset( $limitationValueArray ) )
00314             {
00315                 reset( $limitationValueArray );
00316                 foreach ( array_keys( $limitationValueArray ) as $ckey )
00317                 {
00318                     if ( $value == $limitationValueArray[$ckey]['value'] )
00319                     {
00320                         $limitationValuesWithNames[] = $limitationValueArray[$ckey];
00321                     }
00322                 }
00323             }
00324         }
00325 
00326         return $limitationValuesWithNames;
00327     }
00328 
00329     /*!
00330      Get limitation array
00331 
00332      \return access limitation array
00333     */
00334     function limitArray()
00335     {
00336         $limitValues = $this->attribute( 'values' );
00337 
00338         $valueArray = array();
00339 
00340         foreach ( array_keys( $limitValues ) as $valueKey )
00341         {
00342             $valueArray[] = $limitValues[$valueKey]->attribute( 'value' );
00343         }
00344 
00345         return array( $this->attribute( 'identifier' ) => $valueArray );
00346     }
00347 
00348     function allValues()
00349     {
00350         $values = array();
00351         foreach ( $this->attribute( 'values' ) as $value )
00352         {
00353                 $values[] = $value->attribute( 'value' );
00354         }
00355 
00356         return $values;
00357     }
00358 
00359     function valueList()
00360     {
00361         if ( !isset( $this->Values ) )
00362         {
00363             $values = eZPersistentObject::fetchObjectList( eZPolicyLimitationValue::definition(),
00364                                                            null, array( 'limitation_id' => $this->attribute( 'id') ), null, null,
00365                                                            true);
00366 
00367             if ( $this->LimitValue )
00368             {
00369                 $values[] = new eZPolicyLimitationValue( array ( 'id' => -1,
00370                                                                  'value' => $this->LimitValue ) );
00371             }
00372 
00373             $this->Values = $values;
00374         }
00375 
00376         return $this->Values;
00377     }
00378 
00379     static function findByType( $type, $value, $asObject = true, $useLike = true )
00380     {
00381         $cond = '';
00382         $db = eZDB::instance();
00383         $value = $db->escapeString( $value );
00384         $type = $db->escapeString( $type );
00385         if ( $useLike === true )
00386         {
00387             $cond = "ezpolicy_limitation_value.value like '$value%' ";
00388         }
00389         else
00390         {
00391             $cond = "ezpolicy_limitation_value.value = '$value' ";
00392         }
00393 
00394         $query = "SELECT DISTINCT ezpolicy_limitation.*
00395                   FROM ezpolicy_limitation,
00396                        ezpolicy_limitation_value
00397                   WHERE
00398                        ezpolicy_limitation.identifier = '$type' AND
00399                        $cond AND
00400                        ezpolicy_limitation_value.limitation_id =  ezpolicy_limitation.id";
00401 
00402         $dbResult = $db->arrayQuery( $query );
00403         $resultArray = array();
00404         $resultCount = count( $dbResult );
00405         for( $i = 0; $i < $resultCount; $i++ )
00406         {
00407             if ( $asObject )
00408             {
00409                 $resultArray[] = new eZPolicyLimitation( $dbResult[$i] );
00410             }
00411             else
00412             {
00413                 $resultArray[] = $dbResult[$i]['id'];
00414             }
00415         }
00416         return $resultArray;
00417     }
00418 
00419     // Used for assign subtree matching
00420     public $LimitValue;
00421 
00422 }
00423 
00424 ?>