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
00037
00038
00039 include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
00040
00041 class eZSubtreeNotificationRule extends eZPersistentObject
00042 {
00043
00044
00045
00046 function eZSubtreeNotificationRule( $row )
00047 {
00048 $this->eZPersistentObject( $row );
00049 }
00050
00051 function definition()
00052 {
00053 return array( "fields" => array( "id" => array( 'name' => 'ID',
00054 'datatype' => 'integer',
00055 'default' => 0,
00056 'required' => true ),
00057 "user_id" => array( 'name' => "UserID",
00058 'datatype' => 'integer',
00059 'default' => '',
00060 'required' => true,
00061 'foreign_class' => 'eZUser',
00062 'foreign_attribute' => 'contentobject_id',
00063 'multiplicity' => '1..*' ),
00064 "use_digest" => array( 'name' => "UseDigest",
00065 'datatype' => 'integer',
00066 'default' => 0,
00067 'required' => true ),
00068 "node_id" => array( 'name' => "NodeID",
00069 'datatype' => 'integer',
00070 'default' => 0,
00071 'required' => true,
00072 'foreign_class' => 'eZContentObjectTreeNode',
00073 'foreign_attribute' => 'node_id',
00074 'multiplicity' => '1..*' ) ),
00075 "keys" => array( "id" ),
00076 "function_attributes" => array( 'node' => 'node' ),
00077 "increment_key" => "id",
00078 "sort" => array( "id" => "asc" ),
00079 "class_name" => "eZSubtreeNotificationRule",
00080 "name" => "ezsubtree_notification_rule" );
00081 }
00082
00083
00084 function create( $nodeID, $userID, $useDigest = 0 )
00085 {
00086 $rule = new eZSubtreeNotificationRule( array( 'user_id' => $userID,
00087 'use_digest' => $useDigest,
00088 'node_id' => $nodeID ) );
00089 return $rule;
00090 }
00091
00092 function fetchNodesForUserID( $userID, $asObject = true )
00093 {
00094 $nodeIDList = eZPersistentObject::fetchObjectList( eZSubtreeNotificationRule::definition(),
00095 array( 'node_id' ), array( 'user_id' => $userID ),
00096 null,null,false );
00097 $nodes = array();
00098 if ( $asObject )
00099 {
00100 foreach ( $nodeIDList as $nodeRow )
00101 {
00102 $nodes[] = eZContentObjectTreeNode::fetch( $nodeRow['node_id'] );
00103 }
00104 }
00105 else
00106 {
00107 foreach ( $nodeIDList as $nodeRow )
00108 {
00109 $nodes[] = $nodeRow['node_id'];
00110 }
00111 }
00112 return $nodes;
00113 }
00114
00115 function fetchList( $userID, $asObject = true, $offset = false, $limit = false )
00116 {
00117 return eZPersistentObject::fetchObjectList( eZSubtreeNotificationRule::definition(),
00118 null, array( 'user_id' => $userID ),
00119 null, array( 'offset' => $offset,
00120 'length' => $limit ), $asObject );
00121 }
00122
00123 function fetchListCount( $userID )
00124 {
00125 $countRes = eZPersistentObject::fetchObjectList( eZSubtreeNotificationRule::definition(),
00126 array(),
00127 array( 'user_id' => $userID ),
00128 false,
00129 null,
00130 false,
00131 false,
00132 array( array( 'operation' => 'count( id )',
00133 'name' => 'count' ) ) );
00134 return $countRes[0]['count'];
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 function fetchUserList( $nodeIDList, $contentObject )
00146 {
00147 if ( count( $nodeIDList ) == 0 )
00148 {
00149 $retValue = array();
00150 return $retValue;
00151 }
00152
00153 $db =& eZDB::instance();
00154 $concatString = $db->concatString( array( 'user_tree.path_string', "'%'" ) );
00155
00156 $sql = 'SELECT DISTINCT policy.id AS policy_id, subtree_rule.user_id,
00157 user_role.limit_identifier AS limitation,
00158 user_role.limit_value AS value
00159 FROM ezuser_role user_role,
00160 ezsubtree_notification_rule subtree_rule,
00161 ezcontentobject_tree user_tree,
00162 ezcontentobject_tree user_node,
00163 ezpolicy policy
00164 WHERE subtree_rule.node_id IN ( ' . $db->implodeWithTypeCast( ', ', $nodeIDList, 'int' ) . ' ) AND
00165 user_node.contentobject_id=subtree_rule.user_id AND
00166 user_node.path_string like ' . $concatString . " AND
00167 user_role.contentobject_id=user_tree.contentobject_id AND
00168 ( user_role.role_id=policy.role_id AND ( policy.module_name='*' OR ( policy.module_name='content' AND ( policy.function_name='*' OR policy.function_name='read' ) ) ) )";
00169
00170 $resultArray = $db->arrayQuery( $sql );
00171
00172 $policyIDArray = array();
00173 $limitedPolicyIDArray = array();
00174 $userIDArray = array();
00175 foreach( $resultArray as $result )
00176 {
00177 $userIDArray[(string)$result['user_id']] = (int)$result['user_id'];
00178 }
00179
00180 foreach( $resultArray as $result )
00181 {
00182 if ( $result['limitation'] == '' )
00183 {
00184 $policyIDArray[(string)$result['policy_id']][] =& $userIDArray[(string)$result['user_id']];
00185 }
00186 else
00187 {
00188 $limitedPolicyIDArray[] = array( 'user_id' => &$userIDArray[(string)$result['user_id']],
00189 'limitation' => $result['limitation'],
00190 'value' => $result['value'],
00191 'policyID' => $result['policy_id'] );
00192 }
00193 }
00194
00195 include_once( 'kernel/classes/ezpolicy.php' );
00196 $acceptedUserArray = array();
00197 foreach( array_keys( $policyIDArray ) as $policyID )
00198 {
00199 foreach( array_keys( $policyIDArray[$policyID] ) as $key )
00200 {
00201 if ( $policyIDArray[$policyID][$key] === false )
00202 {
00203 unset( $policyIDArray[$policyID][$key] );
00204 }
00205 }
00206
00207 if ( count( $policyIDArray[$policyID] ) == 0 )
00208 {
00209 continue;
00210 }
00211
00212 $userArray = eZSubtreeNotificationRule::checkObjectAccess( $contentObject, $policyID, $policyIDArray[$policyID] );
00213 $acceptedUserArray = array_unique( array_merge( $acceptedUserArray, $userArray ) );
00214
00215 foreach ( $userArray as $userID )
00216 {
00217 $userIDArray[(string)$userID] = false;
00218 }
00219 }
00220
00221 foreach( $limitedPolicyIDArray as $policyEntry )
00222 {
00223 if ( $policyEntry['user_id'] === false )
00224 {
00225 continue;
00226 }
00227
00228 $userArray = eZSubtreeNotificationRule::checkObjectAccess( $contentObject,
00229 $policyEntry['policyID'],
00230 array( $policyEntry['user_id'] ),
00231 array( $policyEntry['limitation'] => $policyEntry['value'] ) );
00232
00233 $acceptedUserArray = array_unique( array_merge( $acceptedUserArray, $userArray ) );
00234 foreach ( $userArray as $userID )
00235 {
00236 $userIDArray[(string)$userID] = false;
00237 }
00238 }
00239
00240 foreach( array_keys( $acceptedUserArray ) as $key )
00241 {
00242 if ( !is_int( $acceptedUserArray[$key] ) || $acceptedUserArray[$key] == 0 )
00243 {
00244 unset( $acceptedUserArray[$key] );
00245 }
00246 }
00247
00248 if ( count( $acceptedUserArray ) == 0 )
00249 {
00250 $retValue = array();
00251 return $retValue;
00252 }
00253
00254 $nodeIDWhereString = $db->implodeWithTypeCast( ', ', $nodeIDList, 'int' );
00255 $userIDWhereString = $db->implodeWithTypeCast( ', ', $acceptedUserArray, 'int' );
00256 $rules = $db->arrayQuery( "SELECT rule.user_id, rule.use_digest, ezuser.email as address
00257 FROM ezsubtree_notification_rule rule, ezuser
00258 WHERE rule.user_id=ezuser.contentobject_id AND
00259 rule.node_id IN ( $nodeIDWhereString ) AND
00260 rule.user_id IN ( $userIDWhereString )" );
00261 return $rules;
00262 }
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276 function checkObjectAccess( $contentObject, $policyID, $userIDArray, $userLimits = false )
00277 {
00278 $policy = eZPolicy::fetch( $policyID );
00279 if ( $userLimits )
00280 {
00281 reset( $userLimits );
00282 $policy->setAttribute( 'limit_identifier', 'User_' . key( $userLimits ) );
00283 $policy->setAttribute( 'limit_value', current( $userLimits ) );
00284 }
00285
00286 $limitationArray = $policy->accessArray();
00287 $limitationArray = current( current( $limitationArray ) );
00288 $accessUserIDArray = $userIDArray;
00289
00290 if ( isset( $limitationArray['*'] ) &&
00291 $limitationArray['*'] == '*' )
00292 {
00293 $returnArray = array();
00294 foreach ( $accessUserIDArray as $userID )
00295 {
00296 $returnArray[] = $userID;
00297 }
00298 return $returnArray;
00299 }
00300
00301 $limitationArray = current( $limitationArray );
00302
00303 $user =& eZUser::currentUser();
00304 $classID = $contentObject->attribute( 'contentclass_id' );
00305 $nodeArray = $contentObject->attribute( 'assigned_nodes' );
00306
00307 if ( isset( $limitationArray['Subtree' ] ) )
00308 {
00309 $checkedSubtree = false;
00310 }
00311 else
00312 {
00313 $checkedSubtree = true;
00314 $nodeSubtree = true;
00315 }
00316 if ( isset( $limitationArray['Node'] ) )
00317 {
00318 $checkedNode = false;
00319 }
00320 else
00321 {
00322 $checkedNode = true;
00323 $nodeLimit = true;
00324 }
00325
00326 foreach ( array_keys( $limitationArray ) as $key )
00327 {
00328 if ( count( $accessUserIDArray ) == 0 )
00329 {
00330 return array();
00331 }
00332 switch( $key )
00333 {
00334 case 'Class':
00335 {
00336 if ( !in_array( $contentObject->attribute( 'contentclass_id' ), $limitationArray[$key] ) )
00337 {
00338 return array();
00339 }
00340 } break;
00341
00342 case 'ParentClass':
00343 {
00344
00345 if ( !in_array( $contentObject->attribute( 'contentclass_id' ), $limitationArray[$key] ) )
00346 {
00347 return array();
00348 }
00349 } break;
00350
00351 case 'Section':
00352 case 'User_Section':
00353 {
00354 if ( !in_array( $contentObject->attribute( 'section_id' ), $limitationArray[$key] ) )
00355 {
00356 return array();
00357 }
00358 } break;
00359
00360 case 'Owner':
00361 {
00362 if ( in_array( $contentObject->attribute( 'owner_id' ), $userIDArray ) )
00363 {
00364 $accessUserIDArray = array_intersect( $contentObject->attribute( 'owner_id' ), $accessUserIDArray );
00365 }
00366 else if ( in_array( $contentObject->ID, $userIDArray ) )
00367 {
00368 $accessUserIDArray = array_intersect( $contentObject->ID, $accessUserIDArray );
00369 }
00370 else
00371 {
00372 return array();
00373 }
00374 } break;
00375
00376 case 'Node':
00377 {
00378 $nodeLimit = true;
00379 foreach ( $nodeArray as $node )
00380 {
00381 if( in_array( $node->attribute( 'node_id' ), $limitationArray[$key] ) )
00382 {
00383 $nodeLimit = false;
00384 break;
00385 }
00386 }
00387 if ( $nodeLimit && $checkedSubtree && $nodeSubtree )
00388 {
00389 return array();
00390 }
00391 $checkedNode = true;
00392 } break;
00393
00394 case 'Subtree':
00395 {
00396 $nodeSubtree = true;
00397 foreach ( $nodeArray as $node )
00398 {
00399 $path = $node->attribute( 'path_string' );
00400 $subtreeArray = $limitationArray[$key];
00401 $validSubstring = false;
00402 foreach ( $subtreeArray as $subtreeString )
00403 {
00404 if ( strstr( $path, $subtreeString ) )
00405 {
00406 $nodeSubtree = false;
00407 break;
00408 }
00409 }
00410 if ( !$nodeSubtree )
00411 {
00412 break;
00413 }
00414 }
00415 if ( $nodeSubtree && $checkedNode && $nodeLimit )
00416 {
00417 return array();
00418 }
00419 $checkedSubtree = true;
00420 } break;
00421
00422 case 'User_Subtree':
00423 {
00424 $userSubtreeLimit = true;
00425 foreach ( $nodeArray as $node )
00426 {
00427 $path = $node->attribute( 'path_string' );
00428 $subtreeArray = $limitationArray[$key];
00429 $validSubstring = false;
00430 foreach ( $subtreeArray as $subtreeString )
00431 {
00432 if ( strstr( $path, $subtreeString ) )
00433 {
00434 $userSubtreeLimit = false;
00435 break;
00436 }
00437 }
00438 if ( !$userSubtreeLimit )
00439 {
00440 break;
00441 }
00442 }
00443 if ( $userSubtreeLimit )
00444 {
00445 return array();
00446 }
00447 } break;
00448 }
00449 }
00450
00451 $returnArray = array();
00452 foreach ( $accessUserIDArray as $userID )
00453 {
00454 $returnArray[] = $userID;
00455 }
00456 return $returnArray;
00457 }
00458
00459 function &node()
00460 {
00461 if ( $this->Node == null )
00462 {
00463 $this->Node = eZContentObjectTreeNode::fetch( $this->attribute( 'node_id' ) );
00464 }
00465 return $this->Node;
00466 }
00467
00468 function removeByNodeAndUserID( $userID, $nodeID )
00469 {
00470 eZPersistentObject::removeObject( eZSubtreeNotificationRule::definition(), array( 'user_id' => $userID,
00471 'node_id' => $nodeID ) );
00472 }
00473
00474
00475
00476
00477
00478
00479
00480
00481 function removeByUserID( $userID )
00482 {
00483 eZPersistentObject::removeObject( eZSubtreeNotificationRule::definition(), array( 'user_id' => $userID ) );
00484 }
00485
00486
00487
00488
00489
00490 function cleanup()
00491 {
00492 $db =& eZDB::instance();
00493 $db->query( "DELETE FROM ezsubtree_notification_rule" );
00494 }
00495
00496 var $Node = null;
00497 }
00498
00499 ?>