Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezpolicy.php
Go to the documentation of this file.
1 <?php
18 class eZPolicy extends eZPersistentObject
19 {
20 public function __construct( $row )
21 {
22 parent::__construct( $row );
23 $this->NodeID = 0;
24 }
25
26 static function definition()
27 {
28 static $definition = array( 'fields' => array( 'id' => array( 'name' => 'ID',
29 'datatype' => 'integer',
30 'default' => 0,
31 'required' => true ),
32 'role_id' => array( 'name' => 'RoleID',
33 'datatype' => 'integer',
34 'default' => 0,
35 'required' => true,
36 'foreign_class' => 'eZRole',
37 'foreign_attribute' => 'id',
38 'multiplicity' => '1..*' ),
39 'module_name' => array( 'name' => 'ModuleName',
40 'datatype' => 'string',
41 'default' => '',
42 'required' => true ),
43 'function_name' => array( 'name' => 'FunctionName',
44 'datatype' => 'string',
45 'default' => '',
46 'required' => true ),
47 'original_id' => array( 'name' => 'OriginalID',
48 'datatype' => 'integer',
49 'default' => null,
50 'required' => false ) ),
51 'keys' => array( 'id' ),
52 'function_attributes' => array( 'limitations' => 'limitationList',
53 'role' => 'role',
54 'limit_identifier' => 'limitIdentifier',
55 'limit_value' => 'limitValue',
56 'user_role_id' => 'userRoleID' ),
57 'increment_key' => 'id',
58 'sort' => array( 'id' => 'asc' ),
59 'class_name' => 'eZPolicy',
60 'name' => 'ezpolicy' );
61 return $definition;
62 }
63
64 function limitIdentifier()
65 {
66 return $this->LimitIdentifier;
67 }
68
69 function limitValue()
70 {
71 return $this->LimitValue;
72 }
73
74 function userRoleID()
75 {
76 return $this->UserRoleID;
77 }
78
79 function setAttribute( $attr, $val )
80 {
81 switch( $attr )
82 {
83 case 'limit_identifier':
84 {
85 if ( !$this->LimitIdentifier )
86 {
87 $this->LimitIdentifier = $val;
88 }
89 } break;
90
91 case 'limit_value':
92 {
93 if ( !$this->LimitValue )
94 {
95 $this->LimitValue = $val;
96 }
97 } break;
98 case 'user_role_id':
99 {
100 if ( !$this->UserRoleID )
101 {
102 $this->UserRoleID = $val;
103 }
104 } break;
105
106 default:
107 {
108 parent::setAttribute( $attr, $val );
109 } break;
110 }
111 }
112
117 static function createNew( $roleID , $params = array() )
118 {
119 $policy = new eZPolicy( array( 'id' => null ) );
120 $policy->setAttribute( 'role_id', $roleID );
121 if ( array_key_exists( 'ModuleName', $params ))
122 {
123 $policy->setAttribute( 'module_name', $params['ModuleName'] );
124 }
125 if ( array_key_exists( 'FunctionName', $params ))
126 {
127 $policy->setAttribute( 'function_name', $params['FunctionName'] );
128 }
129 $policy->store();
130
131 return $policy;
132 }
133
142 static function create( $roleID, $module, $function )
143 {
144 if ( $module === true )
145 $module = '*';
146 if ( $function === true )
147 $function = '*';
148 $row = array( 'id' => null,
149 'role_id' => $roleID,
150 'module_name' => $module,
151 'function_name' => $function );
152 $policy = new eZPolicy( $row );
153 return $policy;
154 }
155
164 function appendLimitation( $identifier, $values )
165 {
166 $limitation = eZPolicyLimitation::create( $this->ID, $identifier );
167
168 $db = eZDB::instance();
169 $db->begin();
170 $limitation->store();
171 $limitationID = $limitation->attribute( 'id' );
172 $limitations = array();
173 foreach ( $values as $value )
174 {
175 $limitationValue = eZPolicyLimitationValue::create( $limitationID, $value );
176 $limitationValue->store();
177 if ( isset( $limitation->Values ) )
178 {
179 $limitation->Values[] = $limitationValue;
180 }
181 }
182 $db->commit();
183
184 if ( isset( $this->Limitations ) )
185 {
186 $this->Limitations[] = $limitation;
187 }
188 return $limitation;
189 }
190
197 function copy( $roleID )
198 {
199 $params = array();
200 $params['ModuleName'] = $this->attribute( 'module_name' );
201 $params['FunctionName'] = $this->attribute( 'function_name' );
202
203 $db = eZDB::instance();
204 $db->begin();
205 $newPolicy = eZPolicy::createNew( $roleID, $params );
206 foreach ( $this->attribute( 'limitations' ) as $limitation )
207 {
208 $limitation->copy( $newPolicy->attribute( 'id' ) );
209 }
210 $db->commit();
211
212 return $newPolicy;
213 }
214
218 static function removeByID( $id )
219 {
220 $policy = eZPolicy::fetch( $id );
221 if ( !$policy )
222 {
223 return null;
224 }
225 $policy->removeThis();
226 }
227
232 function removeThis( $id = false )
233 {
234 $db = eZDB::instance();
235 $db->begin();
236 foreach ( $this->attribute( 'limitations' ) as $limitation )
237 {
238 $limitation->removeThis();
239 }
240 $db->query( "DELETE FROM ezpolicy
241 WHERE id='" . $db->escapeString( $this->attribute( 'id' ) ) . "'" );
242 $db->commit();
243 }
244
250 function accessArray( $ignoreLimitIdentifier = false )
251 {
252 $limitations = $this->limitationList( true, $ignoreLimitIdentifier );
253 if ( $this->Disabled === true )
254 {
255 return array();
256 }
257
258 if ( !$limitations )
259 {
260 return array( $this->attribute( 'module_name' ) => array ( $this->attribute( 'function_name' ) => array( '*' => '*' ) ) );
261 }
262
263 $limitArray = array();
264
265 foreach( array_keys( $limitations ) as $limitKey )
266 {
267 $limitArray = array_merge_recursive( $limitArray, $limitations[$limitKey]->limitArray() );
268 }
269
270 $policyName = 'p_' . $this->attribute( 'id' ) . ( isset($this->UserRoleID) ? ( '_' . $this->UserRoleID ) : '' );
271
272 return array( $this->attribute( 'module_name' ) => array ( $this->attribute( 'function_name' ) => array( $policyName => $limitArray ) ) );
273 }
274
280 function limitationList( $useCache = true, $ignoreLimitIdentifier = false )
281 {
282 if ( !isset( $this->Limitations ) || !$useCache )
283 {
284
285 $limitations = eZPersistentObject::fetchObjectList( eZPolicyLimitation::definition(),
286 null, array( 'policy_id' => $this->attribute( 'id') ), null, null,
287 true );
288
289 eZDebugSetting::writeDebug( 'kernel-policy-limitation', $limitations, "before policy limitations " . $this->ID );
290 eZDebugSetting::writeDebug( 'kernel-policy-limitation', $this, "policy itself before before limitations check" );
291
292 if ( $ignoreLimitIdentifier === false && isset( $this->LimitIdentifier ) && $this->LimitIdentifier )
293 {
294 $limitIdentifier = $this->attribute( 'limit_identifier' );
295 $limitValue = $this->attribute( 'limit_value' );
296 $limitationTouched = false;
297 $checkEmptyLimitation = true;
298 foreach ( $limitations as $limitation )
299 {
300 if ( $limitation->attribute( 'identifier' ) == $limitIdentifier )
301 {
302 if ( $limitIdentifier == 'Subtree' )
303 {
304 $limitationTouched = true;
305
306 $values = $limitation->attribute( 'values' );
307
308 foreach ( $values as $limitationValue )
309 {
310 $value = $limitationValue->attribute( 'value' );
311 if ( strpos( $value, $limitValue ) === 0 )
312 {
313 $checkEmptyLimitation = false;
314 eZDebugSetting::writeDebug( 'kernel-policy-limitation', $value,
315 "Limitationvalue has been left in the limitation [limitValue=$limitValue]" );
316 }
317 else if ( strpos( $limitValue, $value ) === 0 )
318 {
319 $checkEmptyLimitation = false;
320 $limitationValue->setAttribute( 'value', $limitValue );
321 eZDebugSetting::writeDebug( 'kernel-policy-limitation',
322 $value,
323 "Limitationvalue has been exchanged to the value from cond assignment [limitValue=$limitValue]" );
324 }
325 else
326 {
327 eZDebugSetting::writeDebug( 'kernel-policy-limitation', $value,
328 "Limitationvalue has been removed from limitation [limitValue=$limitValue]" );
329 //exlude limitation value from limitation..
330 unset( $limitationValue );
331 }
332 }
333 if ( $checkEmptyLimitation )
334 {
335 eZDebugSetting::writeDebug( 'kernel-policy-limitation', $this, 'The policy has been disabled' );
336 $this->Disabled = true;
337 $this->Limitations = array();
338 return $this->Limitations;
339 }
340 }
341 }
342 }
343
344 if ( !$limitationTouched )
345 {
346 $policyLimitation = new eZPolicyLimitation( array ( 'id' => -1,
347 'policy_id' => $this->attribute( 'id' ),
348 'identifier' => $this->attribute( 'limit_identifier' ) ) );
349 $policyLimitation->setAttribute( 'limit_value', $this->attribute( 'limit_value' ) );
350
351 $limitations[] = $policyLimitation;
352 }
353 }
354 eZDebugSetting::writeDebug( 'kernel-policy-limitation', $limitations, "policy limitations " . $this->ID );
355
356 $this->Limitations = $limitations;
357 }
358 return $this->Limitations;
359 }
360
361 function role()
362 {
363 if ( $this->ID )
364 {
365 return eZPersistentObject::fetchObject( eZRole::definition(),
366 null, array( 'id' => $this->RoleID ), true );
367 }
368
369 return false;
370 }
371
377 static function fetch( $policyID )
378 {
379 return eZPersistentObject::fetchObject( self::definition(),
380 null, array( 'id' => $policyID ), true );
381 }
382
388 public static function fetchTemporaryCopy( $policyID )
389 {
390 $policy = eZPersistentObject::fetchObject( self::definition(),
391 null, array( 'original_id' => $policyID ), true );
392
393 if ( $policy instanceof eZPolicy )
394 {
395 return $policy;
396 }
397 // The temporary copy does not exist yet, create it
398 else
399 {
400 $policy = self::fetch( $policyID );
401 if ( $policy === null )
402 return false;
403 else
404 {
405 return $policy->createTemporaryCopy();
406 }
407 }
408 }
409
415 public function createTemporaryCopy()
416 {
417 $db = eZDB::instance();
418
419 if( $db->DatabaseName() != 'sqlite' )
420 {
421 if ( $this->attribute( 'original_id' ) === 0 )
422 throw new Exception( 'eZPolicy #' . $this->attribute( 'id' ) . ' is already a temporary item (original: #'. $this->attribute( 'original_id' ) . ')' );
423 }
424
425 $policyCopy = self::copy( $this->attribute( 'role_id' ) );
426 $policyCopy->setAttribute( 'original_id', $this->attribute( 'id' ) );
427 $policyCopy->store();
428
429 return $policyCopy;
430 }
431
438 public function saveTemporary()
439 {
440 if ( $this->attribute( 'original_id' ) === 0 )
441 throw new Exception( __METHOD__ . ' can only be used on a temporary policy' );
442
443 // 1. Remove the original policy
444 $originalPolicy = eZPolicy::fetch( $this->attribute( 'original_id' ) );
445 $originalPolicy->removeThis();
446
447 // 2. Remove the original ID in the temporary policy (make it final)
448 $this->setAttribute( 'original_id', 0 );
449 $this->store();
450
451 return $this;
452 }
453
454 // Used for assign based limitations.
455 public $Disabled = false;
456 public $LimitValue;
457 public $LimitIdentifier;
458 public $UserRoleID;
459 public $Limitations;
460 public $RoleID;
461 public $ModuleName;
462 public $FunctionName;
463 public $OriginalID;
464
465 }
466
467 ?>
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZDebugSetting\writeDebug
static writeDebug( $conditionName, $string, $label="")
Definition: ezdebugsetting.php:127
eZPersistentObject
Definition: ezpersistentobject.php:33
eZPersistentObject\fetchObject
static fetchObject( $def, $field_filters, $conds, $asObject=true, $grouping=null, $custom_fields=null)
Definition: ezpersistentobject.php:213
eZPersistentObject\store
store( $fieldFilters=null)
Definition: ezpersistentobject.php:304
eZPersistentObject\attribute
attribute( $attr, $noFunction=false)
Definition: ezpersistentobject.php:1329
eZPersistentObject\fetchObjectList
static fetchObjectList( $def, $field_filters=null, $conds=null, $sorts=null, $limit=null, $asObject=true, $grouping=false, $custom_fields=null, $custom_tables=null, $custom_conds=null)
Definition: ezpersistentobject.php:806
eZPolicyLimitationValue\create
static create( $limitationID, $value)
Definition: ezpolicylimitationvalue.php:68
eZPolicyLimitation
Defines a limitation for a policy in the permission system.
Definition: ezpolicylimitation.php:18
eZPolicyLimitation\create
static create( $policyID, $identifier)
Definition: ezpolicylimitation.php:115
eZPolicyLimitation\definition
static definition()
Definition: ezpolicylimitation.php:36
eZPolicy
Defines a policy in the permission system.
Definition: ezpolicy.php:19
eZPolicy\role
role()
Definition: ezpolicy.php:361
eZPolicy\appendLimitation
appendLimitation( $identifier, $values)
Definition: ezpolicy.php:164
eZPolicy\$FunctionName
$FunctionName
Definition: ezpolicy.php:462
eZPolicy\removeByID
static removeByID( $id)
Definition: ezpolicy.php:218
eZPolicy\createNew
static createNew( $roleID, $params=array())
Definition: ezpolicy.php:117
eZPolicy\__construct
__construct( $row)
Definition: ezpolicy.php:20
eZPolicy\$Limitations
$Limitations
Definition: ezpolicy.php:459
eZPolicy\create
static create( $roleID, $module, $function)
Definition: ezpolicy.php:142
eZPolicy\removeThis
removeThis( $id=false)
Definition: ezpolicy.php:232
eZPolicy\fetch
static fetch( $policyID)
Definition: ezpolicy.php:377
eZPolicy\createTemporaryCopy
createTemporaryCopy()
Definition: ezpolicy.php:415
eZPolicy\copy
copy( $roleID)
Definition: ezpolicy.php:197
eZPolicy\limitIdentifier
limitIdentifier()
Definition: ezpolicy.php:64
eZPolicy\limitValue
limitValue()
Definition: ezpolicy.php:69
eZPolicy\saveTemporary
saveTemporary()
Definition: ezpolicy.php:438
eZPolicy\definition
static definition()
Definition: ezpolicy.php:26
eZPolicy\$RoleID
$RoleID
Definition: ezpolicy.php:460
eZPolicy\$ModuleName
$ModuleName
Definition: ezpolicy.php:461
eZPolicy\fetchTemporaryCopy
static fetchTemporaryCopy( $policyID)
Definition: ezpolicy.php:388
eZPolicy\userRoleID
userRoleID()
Definition: ezpolicy.php:74
eZPolicy\accessArray
accessArray( $ignoreLimitIdentifier=false)
Definition: ezpolicy.php:250
eZPolicy\$UserRoleID
$UserRoleID
Definition: ezpolicy.php:458
eZPolicy\$OriginalID
$OriginalID
Definition: ezpolicy.php:463
eZPolicy\$LimitIdentifier
$LimitIdentifier
Definition: ezpolicy.php:457
eZPolicy\setAttribute
setAttribute( $attr, $val)
Definition: ezpolicy.php:79
eZPolicy\$Disabled
$Disabled
Definition: ezpolicy.php:455
eZPolicy\$LimitValue
$LimitValue
Definition: ezpolicy.php:456
eZPolicy\limitationList
limitationList( $useCache=true, $ignoreLimitIdentifier=false)
Definition: ezpolicy.php:280
eZRole\definition
static definition()
Definition: ezrole.php:50
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.