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