00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00045 include_once( 'lib/ezutils/classes/ezini.php' );
00046 include_once( "lib/ezdb/classes/ezdb.php" );
00047 include_once( "kernel/classes/ezpolicy.php" );
00048
eZPersistentObject
00050 {
eZRole( $row = array() )
00055 {
00056 $this->eZPersistentObject( $row );
00057 }
00058
definition()
00060 {
00061 return array( "fields" => array( "id" => "ID",
00062 "version" => "Version",
00063 "name" => "Name"
00064 ),
00065 "keys" => array( "id" ),
00066 "function_attributes" => array( "policies" => "policyList"
00067
00068 ),
00069 "increment_key" => "id",
00070 "sort" => array( "id" => "asc" ),
00071 "class_name" => "eZRole",
00072 "name" => "ezrole" );
00073 }
00074
attributes()
00076 {
00077 return eZPersistentObject::attributes();
00078 }
00079
attribute( $attr )
00081 {
00082 if ( $attr == "policies" )
00083 return $this->policyList();
00084
00085 return eZPersistentObject::attribute( $attr );
00086 }
00087
00088 function &policyList()
00089 {
00090 if ( !isset( $this->Policies ) )
00091 {
00092 $ini =& eZINI::instance();
00093 $enableCaching = $ini->variable( 'RoleSettings', 'EnableCaching' );
00094
00095 $loadFromDB = true;
00096 $roleID = $this->attribute( 'id' );
00097 if ( $enableCaching == 'true' && $this->CachePolicies )
00098 {
00099 $http =& eZHTTPTool::instance();
00100
00101 $hasPoliciesInCache = $http->hasSessionVariable( 'UserPolicies' );
00102 if ( $hasPoliciesInCache )
00103 {
00104 $policiesForAllUserRoles =& $http->sessionVariable( 'UserPolicies' );
00105 $policiesForCurrentRole =& $policiesForAllUserRoles["$roleID"];
00106 if ( count( $policiesForCurrentRole ) > 0 )
00107 {
00108 $policies = array();
00109 foreach ( array_keys( $policiesForCurrentRole ) as $key )
00110 {
00111 $policyRow = $policiesForCurrentRole[$key];
00112 $policies[] = new eZPolicy( $policyRow );
00113 }
00114 $this->Policies =& $policies;
00115 $loadFromDB = false;
00116 }
00117 }
00118 }
00119
00120 if ( $loadFromDB )
00121 {
00122 $policies =& eZPolicy::definition(),
00123 null, array( 'role_id' => $this->attribute( 'id') ), null, null,
00124 true );
00125 if ( $enableCaching )
00126 {
00127 $policiesForCurrentRole = array();
00128 foreach ( array_keys( $policies ) as $key )
00129 {
00130 $policy =& $policies[$key];
00131 $policyAttributes = array();
00132 $policyAttributes['id'] = $policy->attribute( 'id' );
00133 $policyAttributes['role_id'] = $policy->attribute( 'role_id' );
00134 $policyAttributes['module_name'] = $policy->attribute( 'module_name' );
00135 $policyAttributes['function_name'] = $policy->attribute( 'function_name' );
00136 $policyAttributes['limitation'] = $policy->attribute( 'limitation' );
00137 $policiesForCurrentRole[] = $policyAttributes;
00138 }
00139 $http =& eZHTTPTool::instance();
00140 if ( !$http->hasSessionVariable( 'UserPolicies' ) )
00141 {
00142 $policyArray =& $http->sessionVariable( 'UserPolicies' );
00143 }
00144 else
00145 {
00146 $policyArray = array();
00147 }
00148 $policyArray["$roleID"] = $policiesForCurrentRole;
00149 }
00150 $this->Policies =& $policies;
00151 }
00152 }
00153 return $this->Policies;
00154 }
00155
00156 function createNew()
00157 {
00158 $role = new eZRole( array() );
00159 $role->setAttribute( 'name', 'New Role' );
00160 $role->store();
00161 return $role;
00162 }
00163
00164 function createTmporaryVersion()
00165 {
00166 $newRole =& eZRole::createNew();
00167 $this->copyPolicies( $newRole->attribute( 'id' ) );
00168 $newRole->setAttribute( 'name', $this->attribute( 'name' ) );
00169 $newRole->setAttribute( 'version', $this->attribute( 'id' ) );
00170 $newRole->store();
00171 return $newRole;
00172 }
00173
00174 function copyPolicies( $roleID )
00175 {
00176 foreach ( $this->attribute( 'policies' ) as $policy )
00177 {
00178 $policy->copy( $roleID );
00179 }
00180 }
00181
00182 function revertFromTemporaryVersion()
00183 {
00184 $temporaryVersion =& eZRole::fetch( 0, $this->attribute( 'id' ) );
00185 if ( is_null( $temporaryVersion ) )
00186 return 0;
00187 $this->removePolicies();
00188 $this->setAttribute( 'name', $temporaryVersion->attribute( 'name') );
00189 $this->store();
00190
00191 $db =& eZDB::instance();
00192 $query = "UPDATE ezpolicy
00193 SET role_id = " . $this->attribute( 'id' ) . "
00194 WHERE role_id = " . $temporaryVersion->attribute( 'id' );
00195 $db->query( $query );
00196 $temporaryVersion->removePolicies( false );
00197 $temporaryVersion->remove();
00198
00199
00200 include_once( 'lib/ezutils/classes/ezexpiryhandler.php' );
00201 $handler =& eZExpiryHandler::instance();
00202 $handler->setTimestamp( 'user-role-cache', mktime() );
00203 $handler->setTimestamp( 'user-class-cache', mktime() );
00204 $handler->store();
00205 }
00206
00207 function remove( $roleID = false )
00208 {
00209 if ( $roleID )
00210 {
00211 $role =& eZRole::fetch( $roleID );
00212 }
00213 else
00214 {
00215 $role = $this;
00216 $roleID = $this->attribute('id');
00217 }
00218 if ( !isset( $role->ID ) )
00219 {
00220 return 0;
00221 }
00222 foreach ( $role->attribute( 'policies' ) as $policy )
00223 {
00224 $policy->remove();
00225 }
00226 $db =& eZDB::instance();
00227 $db->query( "DELETE FROM ezrole WHERE id='$roleID'" );
00228 $db->query( "DELETE FROM ezuser_role WHERE role_id = '$roleID'" );
00229 }
00230
00231 function removePolicies( $fromDB = true )
00232 {
00233 if ( $fromDB )
00234 {
00235 foreach ( $this->attribute( 'policies' ) as $policy )
00236 {
00237 $policy->remove();
00238 }
00239 }
00240 unset( $this->Policies );
00241 }
00242
fetchByUser( $idArray )
00247 {
00248 $ini =& eZINI::instance();
00249 $enableCaching = $ini->variable( 'RoleSettings', 'EnableCaching' );
00250
00251 $roleArray = false;
00252 if ( $enableCaching == 'true' )
00253 {
00254 $roleArray =& eZRole::cachedRoles();
00255 }
00256
00257 if ( $roleArray == false )
00258 {
00259 $db =& eZDB::instance();
00260 $groupString = implode( ',', $idArray );
00261 $query = "SELECT DISTINCT ezrole.id,
00262 ezrole.name
00263 FROM ezrole,
00264 ezuser_role
00265 WHERE ezuser_role.contentobject_id IN ( $groupString ) AND
00266 ezuser_role.role_id = ezrole.id";
00267
00268 $roleArray =& $db->arrayQuery( $query );
00269 if ( $enableCaching == 'true' )
00270 {
00271 $user =& eZUser::currentUser();
00272 $http =& eZHTTPTool::instance();
00273 $userID = $user->id();
00274
00275 $http->setSessionVariable( 'UserRoles', $roleArray );
00276 $http->setSessionVariable( 'PermissionCachedForUserID', $userID );
00277 $http->setSessionVariable( 'PermissionCachedForUserIDTimestamp', mktime() );
00278
00279 $http->removeSessionVariable( 'UserPolicies' );
00280 $http->removeSessionVariable( 'UserLimitations' );
00281 $http->removeSessionVariable( 'UserLimitationValues' );
00282
00283
00284 include_once( 'lib/ezutils/classes/ezexpiryhandler.php' );
00285 $handler =& eZExpiryHandler::instance();
00286 $handler->setTimestamp( 'user-role-cache', mktime() );
00287 $handler->store();
00288 }
00289 }
00290 else
00291 {
00292 }
00293 $roles = array();
00294 foreach ( $roleArray as $roleRow )
00295 {
00296 $roles[] = new eZRole( $roleRow );
00297 }
00298 return $roles;
00299 }
00300
fetchIDListByUser( $idArray )
00305 {
00306 $ini =& eZINI::instance();
00307 $db =& eZDB::instance();
00308
00309 $enableCaching = $ini->variable( 'RoleSettings', 'EnableCaching' );
00310
00311 $roleArray = false;
00312 if ( $enableCaching == 'true' )
00313 {
00314 $roleArray =& eZRole::cachedRoles();
00315 }
00316
00317 if ( $roleArray == false )
00318 {
00319 $groupString = implode( ',', $idArray );
00320 $query = "SELECT DISTINCT ezrole.id
00321 FROM ezrole,
00322 ezuser_role
00323 WHERE ezuser_role.contentobject_id IN ( $groupString ) AND
00324 ezuser_role.role_id = ezrole.id";
00325
00326 $roleArray =& $db->arrayQuery( $query );
00327 $roles = array();
00328 }
00329
00330 $keys = array_keys( $roleArray );
00331 foreach ( $keys as $key )
00332 {
00333 $roles[] = $roleArray[$key]['id'];
00334 }
00335 return $roles;
00336 }
00337
cachedRoles()
00342 {
00343 include_once( 'lib/ezutils/classes/ezexpiryhandler.php' );
00344 $handler =& eZExpiryHandler::instance();
00345 $expiredTimeStamp = 0;
00346 if ( $handler->hasTimestamp( 'user-role-cache' ) )
00347 $expiredTimeStamp = $handler->timestamp( 'user-role-cache' );
00348
00349 $returnRoles = false;
00350 $http =& eZHTTPTool::instance();
00351
00352 $permissionCachedForUser = $http->sessionVariable( 'PermissionCachedForUserID' );
00353 $permissionCachedForUserTimestamp = $http->sessionVariable( 'PermissionCachedForUserIDTimestamp' );
00354
00355 if ( $permissionCachedForUserTimestamp >= $expiredTimeStamp )
00356 {
00357 $user =& eZUser::currentUser();
00358 $userID = $user->id();
00359
00360 if ( $permissionCachedForUser == $userID )
00361 {
00362 $roleArray =& $http->sessionVariable( 'UserRoles' );
00363 if ( count( $roleArray ) > 0 )
00364 {
00365 $returnRoles =& $roleArray;
00366 }
00367 }
00368 }
00369 return $returnRoles;
00370 }
00371
assignToUser( $userID )
00376 {
00377 $db =& eZDB::instance();
00378
00379 $query = "SELECT * FROM ezuser_role WHERE role_id='$this->ID' AND contentobject_id='$userID'";
00380
00381 $rows = $db->arrayQuery( $query );
00382 if ( count( $rows ) > 0 )
00383 return false;
00384
00385 $query = "INSERT INTO ezuser_role ( role_id, contentobject_id ) VALUES ( '$this->ID', '$userID' )";
00386
00387 $db->query( $query );
00388
00389 include_once( 'lib/ezutils/classes/ezexpiryhandler.php' );
00390 $handler =& eZExpiryHandler::instance();
00391 $handler->setTimestamp( 'user-role-cache', mktime() );
00392 $handler->setTimestamp( 'user-class-cache', mktime() );
00393 $handler->store();
00394 return true;
00395 }
00396
fetchUserID()
00401 {
00402 $db =& eZDB::instance();
00403
00404 $query = "SELECT contentobject_id FROM ezuser_role WHERE role_id='$this->ID'";
00405
00406 $results = $db->arrayQuery( $query );
00407 $idArray = array();
00408 foreach ( $results as $result )
00409 {
00410 $idArray[] = $result['contentobject_id'];
00411 }
00412 return $idArray;
00413 }
00414
removeUserAssignment( $userID )
00419 {
00420 $db =& eZDB::instance();
00421
00422 $query = "DELETE FROM ezuser_role WHERE role_id='$this->ID' AND contentobject_id='$userID'";
00423
00424 $db->query( $query );
00425 }
00426
fetchUserByRole( )
00431 {
00432 $db =& eZDB::instance();
00433
00434 $query = "SELECT
00435 ezuser_role.contentobject_id as id
00436 FROM
00437 ezuser_role
00438 WHERE
00439 ezuser_role.role_id = '$this->ID'";
00440
00441 $userIDArray = $db->arrayQuery( $query );
00442 $users = array();
00443 foreach ( $userIDArray as $user )
00444 {
00445 $users[] = eZContentObject::fetch( $user['id'] );
00446 }
00447 return $users;
00448 }
00449
00450
00451 function fetch( $roleID, $version = 0 )
00452 {
00453 if ( $version != 0 )
00454 {
00455 return eZRole::definition(),
00456 null, array('version' => $version ), true);
00457 }
00458 return eZRole::definition(),
00459 null, array('id' => $roleID ), true);
00460 }
00461
00462 function fetchList( $tempVersions = false )
00463 {
00464 if ( ! $tempVersions )
00465 {
00466 return eZRole::definition(),
00467 null, array( 'version' => '0'), null,null,
00468 true );
00469 }
00470 else
00471 {
00472 return eZRole::definition(),
00473 null, array( 'version' => array( '>', '0') ), null,null,
00474 true);
00475 }
00476 }
00477
00478 function checkItem( $accessItem = array() )
00479 {
00480
00481 }
00482
00483 function getSql()
00484 {
00485
00486 }
00487
00488 function turnOffCaching()
00489 {
00490 $this->CachePolicies = false;
00491 }
00492
00493 function turnOnCaching()
00494 {
00495 $this->CachePolicies = true;
00496 }
00497
00498
00499 var $ID;
00500 var $Name;
00501 var $Modules;
00502 var $Functions;
00503 var $Sets;
00504 var $CachePolicies = true;
00505 }
00506
00507 ?>