Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztemplateroleoperator.php
Go to the documentation of this file.
1 <?php
23 class eZTemplateRoleOperator
24 {
30 private $Operators;
31
35 public function __construct()
36 {
37 $this->Operators = array(
38 'has_role',
39 'has_role_id',
40 'has_any_role',
41 'has_role_by_user',
42 'has_role_by_user_id',
43 'has_policy',
44 'has_policy_by_user'
45 );
46 }
47
53 public function operatorList()
54 {
55 return $this->Operators;
56 }
57
64 public function namedParameterPerOperator()
65 {
66 return true;
67 }
68
74 public function namedParameterList()
75 {
76 return array(
77 'has_role' => array(
78 'role_name' => array(
79 'type' => 'string',
80 'required' => true,
81 'default' => ''
82 ),
83 'user_id' => array(
84 'type' => 'integer',
85 'required' => false,
86 'default' => 0
87 )
88 ),
89 'has_role_id' => array(
90 'role_id' => array(
91 'type' => 'integer',
92 'required' => true,
93 'default' => 0
94 ),
95 'user_id' => array(
96 'type' => 'integer',
97 'required' => false,
98 'default' => 0
99 )
100 ),
101 'has_any_role' => array(
102 'roles' => array(
103 'type' => 'array',
104 'required' => true,
105 'default' => array()
106 ),
107 'user_id' => array(
108 'type' => 'integer',
109 'required' => false,
110 'default' => 0
111 ),
112 'match_all' => array(
113 'type' => 'boolean',
114 'required' => false,
115 'default' => false
116 )
117 ),
118 'has_role_by_user' => array(
119 'user' => array(
120 'type' => 'object',
121 'required' => true,
122 'default' => null
123 ),
124 'role_id' => array(
125 'type' => 'integer',
126 'required' => true,
127 'default' => 0
128 )
129 ),
130 'has_role_by_user_id' => array(
131 'user_id' => array(
132 'type' => 'integer',
133 'required' => true,
134 'default' => 0
135 ),
136 'role_id' => array(
137 'type' => 'integer',
138 'required' => true,
139 'default' => 0
140 )
141 ),
142 'has_policy' => array(
143 'module' => array(
144 'type' => 'string',
145 'required' => true,
146 'default' => ''
147 ),
148 'function' => array(
149 'type' => 'string',
150 'required' => true,
151 'default' => ''
152 ),
153 'user_id' => array(
154 'type' => 'integer',
155 'required' => false,
156 'default' => 0
157 )
158 ),
159 'has_policy_by_user' => array(
160 'user' => array(
161 'type' => 'object',
162 'required' => true,
163 'default' => null
164 ),
165 'module' => array(
166 'type' => 'string',
167 'required' => true,
168 'default' => ''
169 ),
170 'function' => array(
171 'type' => 'string',
172 'required' => true,
173 'default' => ''
174 )
175 )
176 );
177 }
178
190 public function modify(
191 $tpl,
192 $operatorName,
193 $operatorParameters,
194 $rootNamespace,
195 $currentNamespace,
196 &$operatorValue,
197 $namedParameters
198 )
199 {
200 switch ( $operatorName )
201 {
202 case 'has_role':
203 {
204 $roleName = $namedParameters['role_name'];
205 $userId = $namedParameters['user_id'];
206
207 if ( $userId === 0 )
208 {
209 $userId = eZUser::currentUserID();
210 }
211
212 $operatorValue = $this->isMemberOfRole( $userId, $roleName );
213 }
214 break;
215
216 case 'has_role_id':
217 {
218 $roleId = $namedParameters['role_id'];
219 $userId = $namedParameters['user_id'];
220
221 if ( $userId === 0 )
222 {
223 $userId = eZUser::currentUserID();
224 }
225
226 $operatorValue = $this->isMemberOfRoleId( $userId, $roleId );
227 }
228 break;
229
230 case 'has_any_role':
231 {
232 $roles = $namedParameters['roles'];
233 $userId = $namedParameters['user_id'];
234 $matchAll = $namedParameters['match_all'];
235
236 if ( $userId === 0 )
237 {
238 $userId = eZUser::currentUserID();
239 }
240
241 $operatorValue = $this->isMemberOfAnyRole( $userId, $roles, $matchAll );
242 }
243 break;
244
245 case 'has_role_by_user':
246 {
247 $user = $namedParameters['user'];
248 $roleId = $namedParameters['role_id'];
249
250 $operatorValue = self::memberOfRoleByUser( $user, $roleId );
251 }
252 break;
253
254 case 'has_role_by_user_id':
255 {
256 $userId = $namedParameters['user_id'];
257 $roleId = $namedParameters['role_id'];
258
259 $operatorValue = self::memberOfRoleByUserId( $userId, $roleId );
260 }
261 break;
262
263 case 'has_policy':
264 {
265 $module = $namedParameters['module'];
266 $function = $namedParameters['function'];
267 $userId = $namedParameters['user_id'];
268
269 if ($userId === 0)
270 {
271 $userId = eZUser::currentUserID();
272 }
273
274 $operatorValue = $this->userHasPolicy($userId, $module, $function);
275 }
276 break;
277
278 case 'has_policy_by_user':
279 {
280 $user = $namedParameters['user'];
281 $module = $namedParameters['module'];
282 $function = $namedParameters['function'];
283
284 $operatorValue = self::hasPolicyByUser($user, $module, $function);
285 }
286 break;
287 }
288 }
289
297 public function isMemberOfRole( $userId, $roleName )
298 {
299 if ( empty( $roleName ) || $userId <= 0 )
300 {
301 return false;
302 }
303
304 $user = eZUser::fetch( $userId );
305 if ( !$user instanceof eZUser )
306 {
307 return false;
308 }
309
310 $roles = $user->roles();
311
312 foreach ( $roles as $role )
313 {
314 if ( $role instanceof eZRole && $role->attribute('name') == $roleName )
315 {
316 return true;
317 }
318 }
319
320 return false;
321 }
322
330 public function isMemberOfRoleId( $userId, $roleId )
331 {
332 if ( $roleId <= 0 || $userId <= 0 )
333 {
334 return false;
335 }
336
337 $user = eZUser::fetch( $userId );
338 if ( !$user instanceof eZUser )
339 {
340 return false;
341 }
342
343 $roles = $user->roles();
344
345 foreach ( $roles as $role )
346 {
347 if ( $role instanceof eZRole && $role->attribute('id') == $roleId )
348 {
349 return true;
350 }
351 }
352
353 return false;
354 }
355
364 public function isMemberOfAnyRole( $userId, $roles, $matchAll = false )
365 {
366 if ( empty( $roles ) || !is_array( $roles ) || $userId <= 0 )
367 {
368 return false;
369 }
370
371 $user = eZUser::fetch( $userId );
372 if ( !$user instanceof eZUser )
373 {
374 return false;
375 }
376
377 $userRoles = $user->roles();
378 $userRoleNames = array();
379 $userRoleIds = array();
380
381 // Build arrays of user's role names and IDs
382 foreach ( $userRoles as $role )
383 {
384 if ( $role instanceof eZRole )
385 {
386 $userRoleNames[] = $role->attribute('name');
387 $userRoleIds[] = (int)$role->attribute('id');
388 }
389 }
390
391 if ( $matchAll )
392 {
393 // User must have all specified roles
394 foreach ( $roles as $role )
395 {
396 $matched = false;
397
398 // Check if it's a role ID (integer) or role name (string)
399 if ( is_int( $role ) || ( is_string( $role ) && ctype_digit( $role ) ) )
400 {
401 // Match by role ID
402 if ( in_array( (int)$role, $userRoleIds ) )
403 {
404 $matched = true;
405 }
406 }
407 else
408 {
409 // Match by role name
410 if ( in_array( $role, $userRoleNames ) )
411 {
412 $matched = true;
413 }
414 }
415
416 // If any role doesn't match, return false
417 if ( !$matched )
418 {
419 return false;
420 }
421 }
422 return true;
423 }
424 else
425 {
426 // User must have at least one of the specified roles
427 foreach ( $roles as $role )
428 {
429 // Check if it's a role ID (integer) or role name (string)
430 if ( is_int( $role ) || ( is_string( $role ) && ctype_digit( $role ) ) )
431 {
432 // Match by role ID
433 if ( in_array( (int)$role, $userRoleIds ) )
434 {
435 return true;
436 }
437 }
438 else
439 {
440 // Match by role name
441 if ( in_array( $role, $userRoleNames ) )
442 {
443 return true;
444 }
445 }
446 }
447 return false;
448 }
449 }
450
459 static function memberOfRoleByUser( $user, $roleID )
460 {
461 $result = false;
462
463 // Fetch user ID
464 $userID = $user->attribute('contentobject_id');
465
466 // Perform comparison
467 $result = self::memberOfRoleByUserId( $userID, $roleID );
468
469 return $result;
470 }
471
480 static function memberOfRoleByUserId( $userID, $roleID )
481 {
482 $result = false;
483
484 if ( $roleID <= 0 || $userID <= 0 )
485 {
486 return false;
487 }
488
489 $user = eZUser::fetch( $userID );
490 if ( !$user instanceof eZUser )
491 {
492 return false;
493 }
494
495 $roles = $user->roles();
496
497 foreach ( $roles as $role )
498 {
499 if ( $role instanceof eZRole && $role->attribute('id') == $roleID )
500 {
501 $result = true;
502 break;
503 }
504 }
505
506 return $result;
507 }
508
517 public function userHasPolicy( $userId, $module, $function )
518 {
519 if ( empty( $module ) || empty( $function ) || $userId <= 0 )
520 {
521 return false;
522 }
523
524 $user = eZUser::fetch( $userId );
525 if ( !$user instanceof eZUser )
526 {
527 return false;
528 }
529
530 $roles = $user->roles();
531
532 foreach ( $roles as $role )
533 {
534 if ( !$role instanceof eZRole )
535 {
536 continue;
537 }
538
539 // Check if eZRole has the hasPolicy method (from kernel override extension)
540 if ( method_exists( $role, 'hasPolicy' ) )
541 {
542 if ( $role->hasPolicy( $module, $function ) )
543 {
544 return true;
545 }
546 }
547 else
548 {
549 // Fallback: manually check policies if hasPolicy method doesn't exist
550 $policies = $role->policyList();
551
552 foreach ( $policies as $policy )
553 {
554 if ( !$policy instanceof eZPolicy )
555 {
556 continue;
557 }
558
559 $policyModule = $policy->attribute('module_name');
560 $policyFunction = $policy->attribute('function_name');
561
562 // Check for exact match or wildcard (*)
563 if ( ( $policyModule == $module || $policyModule == '*' ) &&
564 ( $policyFunction == $function || $policyFunction == '*' ) )
565 {
566 return true;
567 }
568 }
569 }
570 }
571
572 return false;
573 }
574
584 static function hasPolicyByUser( $user, $module, $function )
585 {
586 $result = false;
587
588 if ( !$user instanceof eZUser )
589 {
590 return false;
591 }
592
593 // Fetch user ID
594 $userID = $user->attribute('contentobject_id');
595
596 // Create instance to call userHasPolicy
597 $instance = new self();
598 $result = $instance->userHasPolicy( $userID, $module, $function );
599
600 return $result;
601 }
602 }
603
604 ?>
eZPersistentObject\attribute
attribute( $attr, $noFunction=false)
Definition: ezpersistentobject.php:1329
eZPolicy
Defines a policy in the permission system.
Definition: ezpolicy.php:19
eZRole
A container for policies in the permission system.
Definition: ezrole.php:35
eZTemplateRoleOperator
Definition: eztemplateroleoperator.php:24
eZTemplateRoleOperator\memberOfRoleByUserId
static memberOfRoleByUserId( $userID, $roleID)
Definition: eztemplateroleoperator.php:480
eZTemplateRoleOperator\isMemberOfRoleId
isMemberOfRoleId( $userId, $roleId)
Definition: eztemplateroleoperator.php:330
eZTemplateRoleOperator\namedParameterList
namedParameterList()
Definition: eztemplateroleoperator.php:74
eZTemplateRoleOperator\isMemberOfRole
isMemberOfRole( $userId, $roleName)
Definition: eztemplateroleoperator.php:297
eZTemplateRoleOperator\memberOfRoleByUser
static memberOfRoleByUser( $user, $roleID)
Definition: eztemplateroleoperator.php:459
eZTemplateRoleOperator\operatorList
operatorList()
Definition: eztemplateroleoperator.php:53
eZTemplateRoleOperator\__construct
__construct()
Definition: eztemplateroleoperator.php:35
eZTemplateRoleOperator\userHasPolicy
userHasPolicy( $userId, $module, $function)
Definition: eztemplateroleoperator.php:517
eZTemplateRoleOperator\$Operators
$Operators
Definition: eztemplateroleoperator.php:30
eZTemplateRoleOperator\hasPolicyByUser
static hasPolicyByUser( $user, $module, $function)
Definition: eztemplateroleoperator.php:584
eZTemplateRoleOperator\modify
modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters)
Definition: eztemplateroleoperator.php:190
eZTemplateRoleOperator\namedParameterPerOperator
namedParameterPerOperator()
Definition: eztemplateroleoperator.php:64
eZTemplateRoleOperator\isMemberOfAnyRole
isMemberOfAnyRole( $userId, $roles, $matchAll=false)
Definition: eztemplateroleoperator.php:364
eZUser
eZUser handles eZ Publish user accounts
Definition: ezuser.php:19
eZUser\fetch
static fetch( $id, $asObject=true)
Definition: ezuser.php:351
eZUser\currentUserID
static currentUserID()
Definition: ezuser.php:1731
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.