Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

ezobjectpermission.php
Go to the documentation of this file.
1 <?php
3 //
4 // $Id: ezobjectpermission.php 9458 2002-04-23 15:33:45Z bf $
5 //
6 // Definition of eZObjectPermission class
7 //
8 // Created on: <27-Feb-2001 08:05:56 fh>
9 //
10 // This source file is part of Exponential Basic, publishing software.
11 //
12 // Copyright (C) 1999-2001 eZ Systems. All rights reserved.
13 //
14 // This program is free software; you can redistribute it and/or
15 // modify it under the terms of the GNU General Public License
16 // as published by the Free Software Foundation; either version 2
17 // of the License, or (at your option) any later version.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, US
27 //
28
31
49 // include_once( "ezuser/classes/ezmodule.php" );
50 // include_once( "ezuser/classes/ezuser.php" );
51 // include_once( "ezuser/classes/ezusergroup.php" );
52 // include_once( "classes/ezdb.php" );
53
54 class eZObjectPermission
55 {
62 function __construct( )
63 {
64 }
65
66 /*
67 \static
68 Returns true if the user has the desired permission to the desired object.
69 $objectID is the ID of the object you are interested in. This could be a bug, an article etc..
70 $moduleTable is the nickname of the table where the permission is found. The nicknames can be found in site.ini
71 $permission either 'r' for readpermission, 'w' for writepermission or 'u' for upload permission.
72 $user (of type eZUser )is the user you want to check permissions for. Default is currentUser.
73
74 NOTE: If you object has an owner, and this user allways should have rights, you must check this yourself.
75 */
76 static public function hasPermission( $objectID, $moduleTable, $permission, $user = false )
77 {
78 if ( !is_a( $user, "eZUser" ) )
79 $user =& eZUser::currentUser();
80
81 if ( is_object( $user ) && $user->hasRootAccess() )
82 return true;
83
84 if ( $permission != 'u' && $permission != 'w' && $permission != 'r' )
85 return false;
86
87 $SQLGroups = "GroupID = '-1'";
88 if ( is_a( $user, "eZUser" ) )
89 {
90 $groups =& $user->groups( false );
91 $first = true;
92 if ( count( $groups ) > 0 )
93 {
94 foreach ( $groups as $groupItem )
95 {
96 if ( $first == true )
97 {
98 $SQLGroups = "GroupID='$groupItem' ";
99 }
100 else
101 {
102 $SQLGroups .= "OR GroupID='$groupItem' ";
103 }
104 $first = false;
105 }
106 $SQLGroups .= "OR GroupID = '-1' ";
107 }
108 }
109
110 $tableName = getTableName( $moduleTable );
111 if ( $tableName == "" )
112 {
113 return false;
114 }
115
116 $SQLPermission= "";
117 if ( $permission == 'r' )
118 {
119 $SQLPermission = "AND ReadPermission='1'";
120 }
121 else if ( $permission == 'w' )
122 {
123 $SQLPermission = "AND WritePermission='1'";
124 }
125 else if ( $permission == 'u' )
126 {
127 $SQLPermission = "AND UploadPermission='1'";
128 }
129
130 $query = "SELECT count( ID ) as ID FROM $tableName WHERE ObjectID='$objectID' AND ( $SQLGroups ) $SQLPermission";
131 $database =& eZDB::globalDatabase();
132
133 $database->query_single( $res, $query );
134 if ( $res[$database->fieldName( "ID" )] != 0 )
135 return true;
136
137 return false;
138 }
139
140
141 static public function hasPermissionWithDefinition( $objectID, $moduleTable, $permission, $user=false, $categoryID=false )
142 {
143 if ( !is_a( $user, "eZUser" ) )
144 {
145 $user =& eZUser::currentUser();
146 }
147
148 if ( is_object( $user ) && $user->hasRootAccess() )
149 return true;
150
151 if ( !$categoryID )
152 return false;
153
154 if ( $permission != 'u' && $permission != 'w' && $permission != 'r' )
155 return false;
156
157 $SQLGroups = "Object.GroupID = '-1' AND Category.GroupID = '-1' ";
158 if ( is_a( $user, "eZUser" ) )
159 {
160 $groups =& $user->groups( false );
161 $first = true;
162 if ( count( $groups ) > 0 )
163 {
164 foreach ( $groups as $groupItem )
165 {
166 if ( $first == true )
167 {
168 $SQLGroups = "( Object.GroupID='$groupItem' ";
169 }
170 else
171 {
172 $SQLGroups .= "OR Object.GroupID='$groupItem' ";
173 }
174 $first = false;
175 }
176
177 $first = true;
178 $SQLGroups .= ") AND ( Category.GroupID='-1' ";
179 foreach ( $groups as $groupItem )
180 {
181 $SQLGroups .= " OR Category.GroupID='$groupItem' ";
182
183 }
184
185 $SQLGroups .= " ) ";
186 foreach ( $groups as $groupItem )
187 {
188 $SQLGroups .= "OR ( ( Object.GroupID = '-1' AND Category.GroupID = '-1' )
189 OR ( Object.GroupID = '$groupItem' AND Category.GroupID = '-1' )
190 OR ( Object.GroupID = '-1' AND Category.GroupID = '$groupItem' ) )";
191 }
192
193 }
194 }
195
196 $tableName = getTableName( $moduleTable, true );
197
198 if ( $tableName == "" )
199 {
200 return false;
201 }
202
203 $SQLRead = "";
204 $SQLWrite = "";
205 if ( $permission == 'r' )
206 {
207 $SQLRead = "AND Object.ReadPermission='1' AND Category.ReadPermission='1'";
208 }
209 else if ( $permission == 'w' )
210 {
211 $SQLWrite = "AND Object.WritePermission='1' AND Category.WritePermission='1'";
212 }
213
214 $query = "SELECT count( Object.ID ) as ID FROM $tableName WHERE Object.ObjectID='$objectID' AND ( $SQLGroups ) $SQLRead $SQLWrite AND Object.ObjectID=Definition.ArticleID AND Category.ObjectID=Definition.CategoryID AND Category.ObjectID='$categoryID' GROUP BY Object.ObjectID";
215
216 $database =& eZDB::globalDatabase();
217
218 $database->query_single( $res, $query );
219
220 if ( $res[$database->fieldName( "ID" )] != 0 )
221 {
222 return true;
223 }
224
225 return false;
226 }
227
236 static public function setPermission( $group, $objectID, $moduleTable, $permission )
237 {
238 if ( is_a( $group, "eZUserGroup" ) )
239 {
240 $groupID = $group->id();
241 }
242 else if ( $group == -1 )
243 {
244 $groupID = -1;
245 }
246 else
247 {
248 $groupID = $group;
249 }
250
251 $SQLPermission = "";
252 if ( $permission == 'r' )
253 {
254 $SQLPermission = "ReadPermission";
255 }
256 else if ( $permission == 'w' )
257 {
258 $SQLPermission = "WritePermission";
259 }
260 else if ( $permission == 'u' )
261 {
262 $SQLPermission = "UploadPermission";
263 }
264 else // bogus $permission input.
265 {
266 return false;
267 }
268
269 $tableName = getTableName( $moduleTable );
270 if ( $tableName == "" )
271 {
272 return false;
273 }
274
275 $db =& eZDB::globalDatabase();
276 $dbError = false;
277 $db->begin( );
278
279 $queryexists = "SELECT count( ID ) AS ID FROM $tableName WHERE ObjectID='$objectID' AND GroupID='$groupID'";
280 $db->query_single( $res, $queryexists );
281
282 if ( $res[$db->fieldName("ID")] == 0 )
283 {
284 $db->lock( $tableName );
285
286 $nextID = $db->nextID( $tableName, "ID" );
287
288 $query = "INSERT INTO $tableName ( ID, $SQLPermission, ObjectID, GroupID )
289 VALUES
290 ( '$nextID', '1', '$objectID', '$groupID' )";
291
292 $res = $db->query( $query );
293 }
294 else if ( $res[$db->fieldName("ID")] == 1 )
295 {
296 $query = "UPDATE $tableName SET $SQLPermission='1' WHERE ObjectID='$objectID' AND GroupID='$groupID'";
297 $res = $db->query( $query );
298 }
299 else
300 {
301 print( "Duplicate objects in database. Please contact your administrator" );
302 exit();
303 }
304
305 $db->unlock();
306
307 if ( $res == false )
308 $dbError = true;
309
310 if ( $dbError == true )
311 $db->rollback( );
312 else
313 $db->commit();
314 }
315
320 static public function removePermissions( $objectID, $moduleTable, $permission )
321 {
322 $tableName = getTableName( $moduleTable );
323 if ( $tableName == "" )
324 {
325 return false;
326 }
327
328 $i = 0;
329 $SQLPermission = "SET ";
330 if ( strstr( $permission, 'r' ) )
331 {
332 $SQLPermission = "ReadPermission='0'";
333 $i++;
334 }
335 if ( strstr( $permission, 'w' ) )
336 {
337 if ( $i > 0 )
338 $SQLPermission .= ", ";
339 $SQLPermission = "WritePermission='0'";
340 $i++;
341 }
342 if ( strstr( $permission, 'u' ) )
343 {
344 if ( $i > 0 )
345 $SQLPermission .= ", ";
346 $SQLPermission = "UploadPermission='0'";
347 $i++;
348 }
349 if ( $i == 0 )
350 return false;
351
352 $query = "UPDATE $tableName SET $SQLPermission WHERE ObjectID='$objectID'";
353
354 $database =& eZDB::globalDatabase();
355 $database->query( $query );
356 }
357
365 static public function getGroups( $objectID, $moduleTable, $permission, $GroupReturn=true )
366 {
367 $ret = array();
368 $tableName = getTableName( $moduleTable );
369 if ( $tableName == "" )
370 {
371 return $ret;
372 }
373
374 $SQLPermission = "";
375 if ( $permission == 'r' )
376 {
377 $SQLPermission = "ReadPermission='1'";
378 }
379 else if ( $permission == 'w' )
380 {
381 $SQLPermission = "WritePermission='1'";
382 }
383 else if ( $permission == 'u' )
384 {
385 $SQLPermission = "UploadPermission='1'";
386 }
387 else // bogus $permission input.
388 {
389 return $ret;
390 }
391
392 $query = "SELECT GroupID FROM $tableName WHERE ObjectID='$objectID' AND $SQLPermission";
393
394 $db =& eZDB::globalDatabase();
395 $db->array_query( $res, $query );
396
397 if ( count( $res ) > 0 )
398 {
399 $i = 0;
400 foreach ( $res as $groupID )
401 {
402 $id = $groupID[$db->fieldName("GroupID")];
403 if ( $id == -1 )
404 {
405 $res = array();
406 $res[0] = -1;
407 return $res;
408 }
409 $ret[$i] = $GroupReturn ? new eZUserGroup( $id ) : $id;
410 $i++;
411 }
412 }
413 return $ret;
414 }
415
425 static public function getObjects( $moduleTable, $permission, $count = false , $user=false )
426 {
427 $ret = array();
428
429 if ( $user == false )
430 {
431 $user =& eZUser::currentUser();
432 }
433
434
435 $SQLReturn = $count == true ? "count( ObjectID ) AS ObjectID" : "ObjectID";
436
437 $SQLGroups = "GroupID = '-1'";
438 if ( is_a( $user, "eZUser" ) )
439 {
440 $groups =& $user->groups( false );
441 $first = true;
442 if ( count( $groups ) > 0 )
443 {
444 foreach ( $groups as $groupItem )
445 {
446 if ( $first == true )
447 {
448 $SQLGroups = "GroupID='$groupItem' ";
449 }
450 else
451 {
452 $SQLGroups .= "OR GroupID='$groupItem' ";
453 }
454 $first = false;
455 }
456 $SQLGroups .= "OR GroupID = '-1' ";
457 }
458 }
459
460 $tableName = getTableName( $moduleTable );
461 if ( $tableName == "" )
462 {
463 return $ret;
464 }
465
466 $SQLPermission = "";
467 if ( $permission == 'r' )
468 {
469 $SQLPermission = "ReadPermission='1'";
470 }
471 else if ( $permission == 'w' )
472 {
473 $SQLPermission = "WritePermission='1'";
474 }
475 else if ( $permission == 'u' )
476 {
477 $SQLPermission = "UploadPermission='1'";
478 }
479 else // bogus $permission input.
480 {
481 return $ret;
482 }
483
484 $db =& eZDB::globalDatabase();
485 if ( is_a( $user, "eZUser" ) and $user->hasRootAccess() )
486 $query = "SELECT $SQLReturn FROM $tableName";
487 else
488 $query = "SELECT $SQLReturn FROM $tableName WHERE ( $SQLGroups ) AND $SQLPermission";
489
490
491 $db->array_query( $res, $query );
492 if ( $count == true )
493 {
494 return $res[0][$db->fieldName("ObjectID")];
495 }
496 else
497 {
498 if ( count( $res ) > 0 )
499 {
500 $i = 0;
501 foreach ( $res as $groupID )
502 {
503 $ret[$i] = $groupID[$db->fieldName("ObjectID")];
504 $i++;
505 }
506 }
507
508 }
509 return $ret;
510 }
511 }
512
513
517 function getTableName( $name, $withDefinition=false )
518 {
519 $ret = "";
520 switch ( $name )
521 {
522 case "article_article" :
523 if ( $withDefinition )
524 $ret = "eZArticle_ArticlePermission as Object, eZArticle_CategoryPermission as Category, eZArticle_ArticleCategoryDefinition as Definition";
525 else
526 $ret = "eZArticle_ArticlePermission";
527 break;
528
529 case "article_category" :
530 $ret = "eZArticle_CategoryPermission";
531 break;
532
533 case "trade_product" :
534 if ( $withDefinition )
535 $ret = "eZTrade_ProductPermission as Object, eZTrade_CategoryPermission as Category, eZTrade_ProductCategoryDefinition as Definition";
536 else
537 $ret = "eZTrade_ProductPermission";
538 break;
539
540 case "trade_category" :
541 $ret = "eZTrade_CategoryPermission";
542 break;
543
544 case "imagecatalogue_image" :
545 $ret = "eZImageCatalogue_ImagePermission";
546 break;
547
548 case "imagecatalogue_category" :
549 $ret = "eZImageCatalogue_CategoryPermission";
550 break;
551
552 case "filemanager_folder" :
553 $ret = "eZFileManager_FolderPermission";
554 break;
555
556 case "filemanager_file" :
557 $ret = "eZFileManager_FilePermission";
558 break;
559
560 case "mediacatalogue_category" :
561 $ret = "eZMediaCatalogue_CategoryPermission";
562 break;
563
564 case "mediacatalogue_media" :
565 $ret = "eZMediaCatalogue_MediaPermission";
566 break;
567
568 case "bug_module" :
569 $ret = "eZBug_ModulePermission";
570 break;
571
572 default :
573 $ret = "";
574 break;
575 }
576 return $ret;
577 }
578
579 ?>
$i
$i
Definition: admin/adlist.php:91
$group
$group
Definition: admin/articleedit.php:799
eZDB\globalDatabase
static & globalDatabase()
Definition: ezdb.php:134
eZObjectPermission
Definition: ezobjectpermission.php:55
eZObjectPermission\hasPermission
static hasPermission( $objectID, $moduleTable, $permission, $user=false)
Definition: ezobjectpermission.php:76
eZObjectPermission\getGroups
static getGroups( $objectID, $moduleTable, $permission, $GroupReturn=true)
Definition: ezobjectpermission.php:365
eZObjectPermission\setPermission
static setPermission( $group, $objectID, $moduleTable, $permission)
Definition: ezobjectpermission.php:236
eZObjectPermission\removePermissions
static removePermissions( $objectID, $moduleTable, $permission)
Definition: ezobjectpermission.php:320
eZObjectPermission\__construct
__construct()
Definition: ezobjectpermission.php:62
eZObjectPermission\hasPermissionWithDefinition
static hasPermissionWithDefinition( $objectID, $moduleTable, $permission, $user=false, $categoryID=false)
Definition: ezobjectpermission.php:141
eZObjectPermission\getObjects
static getObjects( $moduleTable, $permission, $count=false, $user=false)
Definition: ezobjectpermission.php:425
eZUserGroup
Definition: ezusergroup.php:66
eZUser\currentUser
static & currentUser()
Definition: ezuser.php:896
$name
$name
Definition: companytypelist.php:194
$id
$id
Definition: companytypelist.php:193
$query
if(isset( $Year) &&!is_numeric( $Year)||isset( $Month) &&!is_numeric( $Month)) $query
Definition: entryexitpages.php:65
$permission
$permission
Definition: eventedit.php:315
$db
$db
Definition: ezarticle/user/authorlist.php:53
$groups
if( $CategoryID !=0) $groups
Definition: ezbulkmail/admin/categoryedit.php:108
$groupID
$groupID
Definition: ezgroupeventcalendar/user/dayview.php:90
getTableName
getTableName( $name, $withDefinition=false)
Definition: ezobjectpermission.php:517
$ret
$ret
Definition: ezsitemanager/admin/filelist.php:88
$res
$res
Definition: ezstats/admin/cron.php:45
exit
exit
Definition: folder.php:81
$count
$count
Definition: onlinetypelist.php:108
$categoryID
else $categoryID
Definition: productview.php:66
$user
$user
Definition: user/adlist.php:39
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.