| [ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of Ldapusermanage class 4 // 5 // Created on: <28-Jul-2003 15:12:08 wy> 6 // 7 // SOFTWARE NAME: eZ publish 8 // SOFTWARE RELEASE: 3.9.0 9 // BUILD VERSION: 17785 10 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 11 // SOFTWARE LICENSE: GNU General Public License v2.0 12 // NOTICE: > 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of version 2.0 of the GNU General 15 // Public License as published by the Free Software Foundation. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of version 2.0 of the GNU General 23 // Public License along with this program; if not, write to the Free 24 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 25 // MA 02110-1301, USA. 26 // 27 // 28 29 /*! \file ldapusermanage.php 30 */ 31 32 include_once ( "lib/ezutils/classes/ezmodule.php" ); 33 include_once ( "lib/ezdb/classes/ezdb.php" ); 34 include_once ( 'lib/ezutils/classes/ezini.php' ); 35 include_once ( 'kernel/classes/datatypes/ezuser/ezuser.php' ); 36 include_once ( 'kernel/classes/datatypes/ezuser/ezusersetting.php' ); 37 include_once ( 'kernel/classes/ezcontentobject.php' ); 38 39 eZModule::setGlobalPathList( array( "kernel" ) ); 40 if ( !$isQuiet ) 41 $cli->output( "Checking LDAP users ..." ); 42 43 // fetching ldap users already stored in the database 44 $db =& eZDB::instance(); 45 $query = "SELECT contentobject_id, login 46 FROM ezcontentobject, ezuser 47 WHERE remote_id like 'LDAP%' 48 AND ezcontentobject.id=contentobject_id"; 49 $LDAPUsers = $db->arrayQuery( $query ); 50 51 // get LDAP ini settings 52 $ini =& eZINI::instance(); 53 $LDAPIni =& eZINI::instance( 'ldap.ini' ); 54 55 $LDAPVersion = $LDAPIni->variable( 'LDAPSettings', 'LDAPVersion' ); 56 $LDAPServer = $LDAPIni->variable( 'LDAPSettings', 'LDAPServer' ); 57 $LDAPHost = $LDAPServer; 58 59 $LDAPPort = $LDAPIni->variable( 'LDAPSettings', 'LDAPPort' ); 60 $LDAPBaseDN = $LDAPIni->variable( 'LDAPSettings', 'LDAPBaseDn' ); 61 $LDAPBindUser = $LDAPIni->variable( 'LDAPSettings', 'LDAPBindUser' ); 62 $LDAPBindPassword = $LDAPIni->variable( 'LDAPSettings', 'LDAPBindPassword' ); 63 64 $LDAPSearchScope = $LDAPIni->variable( 'LDAPSettings', 'LDAPSearchScope' ); 65 $LDAPLoginAttribute = $LDAPIni->variable( 'LDAPSettings', 'LDAPLoginAttribute' ); 66 $LDAPLogin = $LDAPLoginAttribute; 67 $LDAPFirstNameAttribute = $LDAPIni->variable( 'LDAPSettings', 'LDAPFirstNameAttribute' ); 68 $LDAPLastNameAttribute = $LDAPIni->variable( 'LDAPSettings', 'LDAPLastNameAttribute' ); 69 $LDAPEmailAttribute = $LDAPIni->variable( 'LDAPSettings', 'LDAPEmailAttribute' ); 70 71 $defaultUserPlacement = $ini->variable( "UserSettings", "DefaultUserPlacement" ); 72 73 $LDAPUserGroupAttributeType = $LDAPIni->variable( 'LDAPSettings', 'LDAPUserGroupAttributeType' ); 74 $LDAPUserGroupAttribute = $LDAPIni->variable( 'LDAPSettings', 'LDAPUserGroupAttribute' ); 75 76 if ( $LDAPIni->hasVariable( 'LDAPSettings', 'Utf8Encoding' ) ) 77 { 78 $Utf8Encoding = $LDAPIni->variable( 'LDAPSettings', 'Utf8Encoding' ); 79 if ( $Utf8Encoding == "true" ) 80 $isUtf8Encoding = true; 81 else 82 $isUtf8Encoding = false; 83 } 84 else 85 { 86 $isUtf8Encoding = false; 87 } 88 89 if ( $LDAPIni->hasVariable( 'LDAPSettings', 'LDAPSearchFilters' ) ) 90 { 91 $LDAPFilters = $LDAPIni->variable( 'LDAPSettings', 'LDAPSearchFilters' ); 92 } 93 if ( $LDAPIni->hasVariable( 'LDAPSettings', 'LDAPUserGroupType' ) and $LDAPIni->hasVariable( 'LDAPSettings', 'LDAPUserGroup' ) ) 94 { 95 $LDAPUserGroupType = $LDAPIni->variable( 'LDAPSettings', 'LDAPUserGroupType' ); 96 $LDAPUserGroup = $LDAPIni->variable( 'LDAPSettings', 'LDAPUserGroup' ); 97 } 98 99 $LDAPEqualSign = trim($LDAPIni->variable( 'LDAPSettings', "LDAPEqualSign" ) ); 100 $LDAPBaseDN = str_replace( $LDAPEqualSign, "=", $LDAPBaseDN ); 101 102 $retrieveAttributes = array( $LDAPLoginAttribute, 103 $LDAPFirstNameAttribute, 104 $LDAPLastNameAttribute, 105 $LDAPEmailAttribute ); 106 if ( $LDAPUserGroupAttributeType ) 107 $retrieveAttributes[] = $LDAPUserGroupAttribute; 108 109 110 $extraNodeAssignments = array(); 111 if ( $LDAPUserGroupType != null ) 112 { 113 if ( $LDAPUserGroupType == "name" ) 114 { 115 if ( is_array( $LDAPUserGroup ) ) 116 { 117 foreach ( array_keys( $LDAPUserGroup ) as $key ) 118 { 119 $groupName = $LDAPUserGroup[$key]; 120 $db->escapeString( $groupName ); 121 $groupQuery = "SELECT ezcontentobject_tree.node_id 122 FROM ezcontentobject, ezcontentobject_tree 123 WHERE ezcontentobject.name like '$groupName' 124 AND ezcontentobject.id=ezcontentobject_tree.contentobject_id 125 AND ezcontentobject.contentclass_id=3"; 126 $groupObject = $db->arrayQuery( $groupQuery ); 127 if ( count( $groupObject ) > 0 and $key == 0 ) 128 { 129 $defaultUserPlacement = $groupObject[0]['node_id']; 130 } 131 else if ( count( $groupObject ) > 0 ) 132 { 133 $extraNodeAssignments[] = $groupObject[0]['node_id']; 134 } 135 } 136 } 137 else 138 { 139 $groupName = $LDAPUserGroup; 140 $db->escapeString( $groupName ); 141 $groupQuery = "SELECT ezcontentobject_tree.node_id 142 FROM ezcontentobject, ezcontentobject_tree 143 WHERE ezcontentobject.name like '$groupName' 144 AND ezcontentobject.id=ezcontentobject_tree.contentobject_id 145 AND ezcontentobject.contentclass_id=3"; 146 $groupObject = $db->arrayQuery( $groupQuery ); 147 148 if ( count( $groupObject ) > 0 ) 149 { 150 $defaultUserPlacement = $groupObject[0]['node_id']; 151 } 152 } 153 } 154 else if ( $LDAPUserGroupType == "id" ) 155 { 156 if ( is_array( $LDAPUserGroup ) ) 157 { 158 foreach ( array_keys( $LDAPUserGroup ) as $key ) 159 { 160 $groupID =(int) $LDAPUserGroup[$key]; 161 $groupQuery = "SELECT ezcontentobject_tree.node_id 162 FROM ezcontentobject, ezcontentobject_tree 163 WHERE ezcontentobject.id='$groupID' 164 AND ezcontentobject.id=ezcontentobject_tree.contentobject_id 165 AND ezcontentobject.contentclass_id=3"; 166 $groupObject = $db->arrayQuery( $groupQuery ); 167 if ( count( $groupObject ) > 0 and $key == 0 ) 168 { 169 $defaultUserPlacement = $groupObject[0]['node_id']; 170 } 171 else if ( count( $groupObject ) > 0 ) 172 { 173 $extraNodeAssignments[] = $groupObject[0]['node_id']; 174 } 175 } 176 } 177 else 178 { 179 $groupID =(int) $LDAPUserGroup; 180 $groupQuery = "SELECT ezcontentobject_tree.node_id 181 FROM ezcontentobject, ezcontentobject_tree 182 WHERE ezcontentobject.id='$groupID' 183 AND ezcontentobject.id=ezcontentobject_tree.contentobject_id 184 AND ezcontentobject.contentclass_id=3"; 185 $groupObject = $db->arrayQuery( $groupQuery ); 186 187 if ( count( $groupObject ) > 0 ) 188 { 189 $defaultUserPlacement = $groupObject[0]['node_id']; 190 } 191 } 192 } 193 } 194 195 //connect to LDAP server 196 $ds = ldap_connect( $LDAPHost, $LDAPPort ); 197 if ( $ds ) 198 { 199 ldap_set_option( $ds, LDAP_OPT_PROTOCOL_VERSION, $LDAPVersion ); 200 if ( $LDAPBindUser == '' ) 201 { 202 $r = ldap_bind( $ds ); 203 } 204 else 205 { 206 $r = ldap_bind( $ds, $LDAPBindUser, $LDAPBindPassword ); 207 } 208 if ( !$r ) 209 { 210 eZDebug::writeError( 'Cannot bind in to LDAP server', 'ldapusermanage.php' ); 211 return false; 212 } 213 ldap_set_option( $ds, LDAP_OPT_SIZELIMIT, 0 ); 214 ldap_set_option( $ds, LDAP_OPT_TIMELIMIT, 0 ); 215 } 216 else 217 { 218 eZDebug::writeError( 'Cannot initialize connection for LDAP server', 'ldapusermanage.php' ); 219 return false; 220 } 221 222 $db->begin(); 223 foreach ( array_keys ( $LDAPUsers ) as $key ) 224 { 225 $LDAPUser =& $LDAPUsers[$key]; 226 $login = $LDAPUser['login']; 227 $userID = $LDAPUser['contentobject_id']; 228 229 $LDAPFilter = "( &"; 230 if ( count( $LDAPFilters ) > 0 ) 231 { 232 foreach ( array_keys( $LDAPFilters ) as $key ) 233 { 234 $LDAPFilter .= "(" . $LDAPFilters[$key] . ")"; 235 } 236 } 237 $LDAPFilter .= "($LDAPLogin=$login)"; 238 $LDAPFilter .= ")"; 239 $LDAPFilter = str_replace( $LDAPEqualSign, "=", $LDAPFilter ); 240 241 if ( $LDAPSearchScope == "one" ) 242 $sr = ldap_list( $ds, $LDAPBaseDN, $LDAPFilter, $retrieveAttributes ); 243 else if ( $LDAPSearchScope == "base" ) 244 $sr = ldap_read( $ds, $LDAPBaseDN, $LDAPFilter, $retrieveAttributes ); 245 else 246 $sr = ldap_search( $ds, $LDAPBaseDN, $LDAPFilter, $retrieveAttributes ); 247 248 $info = ldap_get_entries( $ds, $sr ); 249 if ( $info["count"] != 1 ) 250 { 251 $cli->output( "Disable user " . $cli->stylize( 'emphasize', $login ) ); 252 // Disable the user 253 $userSetting = eZUserSetting::fetch( $userID ); 254 $userSetting->setAttribute( "is_enabled", false ); 255 $userSetting->store(); 256 } 257 else 258 { 259 // Update user information 260 $contentObject =& eZContentObject::fetch( $userID ); 261 262 $parentNodeID = $contentObject->attribute( 'main_parent_node_id' ); 263 $currentVersion = $contentObject->attribute( 'current_version' ); 264 265 $version =& $contentObject->attribute( 'current' ); 266 $contentObjectAttributes =& $version->contentObjectAttributes(); 267 268 if ( $isUtf8Encoding ) 269 { 270 $firstName = utf8_decode( $info[0][$LDAPFirstNameAttribute][0] ); 271 $lastName = utf8_decode( $info[0][$LDAPLastNameAttribute][0] ); 272 $ldapEMail = utf8_decode( $info[0][$LDAPEmailAttribute][0] ); 273 } 274 else 275 { 276 $firstName = $info[0][$LDAPFirstNameAttribute][0]; 277 $lastName = $info[0][$LDAPLastNameAttribute][0]; 278 $ldapEMail = $info[0][$LDAPEmailAttribute][0]; 279 } 280 281 $contentObjectAttributes[0]->setAttribute( 'data_text', $firstName ); 282 $contentObjectAttributes[0]->store(); 283 284 $contentObjectAttributes[1]->setAttribute( 'data_text', $lastName ); 285 $contentObjectAttributes[1]->store(); 286 287 $contentClass =& $contentObject->attribute( 'content_class' ); 288 $name = $contentClass->contentObjectName( $contentObject ); 289 $contentObject->setName( $name ); 290 291 $existUser = eZUser::fetch( $userID ); 292 $existUser->setAttribute('email', $ldapEMail ); 293 $existUser->setAttribute('password_hash', "" ); 294 $existUser->setAttribute('password_hash_type', 0 ); 295 $existUser->store(); 296 297 // If user has changed to another group, update it. 298 if ( $LDAPUserGroupAttributeType != null ) 299 { 300 $republishRequired = false; 301 $IsLDAPMain = true; 302 $hasOtherNodeType = false; 303 $hasLDAPNodeType = false; 304 $otherNodeArray = array(); 305 $LDAPNodeArray = array(); 306 $newLDAPNodeArray = array(); 307 $parentNodes =& $contentObject->parentNodes( $currentVersion );; 308 foreach( array_keys( $parentNodes ) as $key ) 309 { 310 $parentNode =& $parentNodes[$key]; 311 $parentNodeID = $parentNode->attribute( 'node_id' ); 312 $parentNodeName = $parentNode->attribute( 'name' ); 313 $nodeAssignment = eZNodeAssignment::fetch( $contentObject->attribute( 'id' ), $currentVersion, $parentNodeID ); 314 $isMain = $nodeAssignment->attribute( 'is_main' ); 315 $remoteID = $nodeAssignment->attribute( 'parent_remote_id' ); 316 if ( preg_match( "/LDAP/i", $remoteID ) ) 317 { 318 $LDAPNodeArray[] = array( 'parent_node_name' => $parentNodeName, 'parent_node_id' => $parentNodeID, 'is_main' => $isMain ); 319 } 320 else 321 { 322 $otherNodeArray[] = array( 'parent_node_name' => $parentNodeName, 'parent_node_id' => $parentNodeID, 'is_main' => $isMain ); 323 $hasOtherNodeType = true; 324 if ( $isMain ) 325 { 326 $IsLDAPMain = false; 327 } 328 } 329 } 330 $LDAPUserGroupCount = count( $LDAPNodeArray ); 331 $groupAttributeCount = $info[0][$LDAPUserGroupAttribute]['count']; 332 333 if ( $LDAPUserGroupAttributeType == "name" ) 334 { 335 for ( $i = 0; $i < $groupAttributeCount; $i++ ) 336 { 337 if ( $isUtf8Encoding ) 338 { 339 $groupName = utf8_decode( $info[0][$LDAPUserGroupAttribute][$i] ); 340 } 341 else 342 { 343 $groupName = $info[0][$LDAPUserGroupAttribute][$i]; 344 } 345 $exist = false; 346 foreach( $LDAPNodeArray as $LDAPNode ) 347 { 348 $existGroupName = $LDAPNode['parent_node_name']; 349 $existGroupID = $LDAPNode['parent_node_id']; 350 if ( strcasecmp( $existGroupName, $groupName ) == 0 ) 351 { 352 $exist = true; 353 $hasLDAPNodeType = true; 354 if ( $IsLDAPMain and count( $newLDAPNodeArray ) == 0 ) 355 { 356 $newLDAPNodeArray[] = array( 'parent_node_name' => $existGroupName, 'parent_node_id' => $existGroupID, 'is_main' => 1 ); 357 } 358 else 359 { 360 $newLDAPNodeArray[] = array( 'parent_node_name' => $existGroupName, 'parent_node_id' => $existGroupID, 'is_main' => 0 ); 361 } 362 $LDAPUserGroupCount--; 363 } 364 } 365 366 if ( $exist == false ) 367 { 368 $groupName = $db->escapeString( $groupName ); 369 $groupQuery = "SELECT ezcontentobject_tree.node_id 370 FROM ezcontentobject, ezcontentobject_tree 371 WHERE ezcontentobject.name like '$groupName' 372 AND ezcontentobject.id=ezcontentobject_tree.contentobject_id 373 AND ezcontentobject.contentclass_id=3"; 374 $groupObject = $db->arrayQuery( $groupQuery ); 375 376 if ( count( $groupObject ) > 0 ) 377 { 378 $hasLDAPNodeType = true; 379 if ( $IsLDAPMain and count( $newLDAPNodeArray ) == 0 ) 380 { 381 $newLDAPNodeArray[] = array( 'parent_node_name' => $groupName, 'parent_node_id' => $groupObject[0]['node_id'], 'is_main' => 1 ); 382 } 383 else 384 { 385 $newLDAPNodeArray[] = array( 'parent_node_name' => $groupName, 'parent_node_id' => $groupObject[0]['node_id'], 'is_main' => 0 ); 386 } 387 $republishRequired = true; 388 } 389 } 390 } 391 392 if ( $LDAPUserGroupCount != 0 ) 393 { 394 $republishRequired = true; 395 } 396 } 397 else if ( $LDAPUserGroupAttributeType == "id" ) 398 { 399 for ( $i = 0; $i < $groupAttributeCount; $i++ ) 400 { 401 if ( $isUtf8Encoding ) 402 { 403 $groupID = utf8_decode( $info[0][$LDAPUserGroupAttribute][$i] ); 404 } 405 else 406 { 407 $groupID = $info[0][$LDAPUserGroupAttribute][$i]; 408 } 409 410 $groupName = "LDAP " . $groupID; 411 412 $exist = false; 413 foreach( $LDAPNodeArray as $LDAPNode ) 414 { 415 $existGroupName = $LDAPNode['parent_node_name']; 416 $existGroupID = $LDAPNode['parent_node_id']; 417 if ( strcasecmp( $existGroupName, $groupName ) == 0 ) 418 { 419 $exist = true; 420 $hasLDAPNodeType = true; 421 if ( $IsLDAPMain and count( $newLDAPNodeArray ) == 0 ) 422 { 423 $newLDAPNodeArray[] = array( 'parent_node_name' => $existGroupName, 'parent_node_id' => $existGroupID, 'is_main' => 1 ); 424 } 425 else 426 { 427 $newLDAPNodeArray[] = array( 'parent_node_name' => $existGroupName, 'parent_node_id' => $existGroupID, 'is_main' => 0 ); 428 } 429 $LDAPUserGroupCount--; 430 } 431 } 432 433 if ( $exist == false ) 434 { 435 $groupName = $db->escapeString( $groupName ); 436 $groupQuery = "SELECT ezcontentobject_tree.node_id 437 FROM ezcontentobject, ezcontentobject_tree 438 WHERE ezcontentobject.name like '$groupName' 439 AND ezcontentobject.id=ezcontentobject_tree.contentobject_id 440 AND ezcontentobject.contentclass_id=3"; 441 $groupObject = $db->arrayQuery( $groupQuery ); 442 443 if ( count( $groupObject ) > 0 ) 444 { 445 $hasLDAPNodeType = true; 446 if ( $IsLDAPMain and count( $newLDAPNodeArray ) == 0 ) 447 { 448 $newLDAPNodeArray[] = array( 'parent_node_name' => $groupName, 'parent_node_id' => $groupObject[0]['node_id'], 'is_main' => 1 ); 449 } 450 else 451 { 452 $newLDAPNodeArray[] = array( 'parent_node_name' => $groupName, 'parent_node_id' => $groupObject[0]['node_id'], 'is_main' => 0 ); 453 } 454 $republishRequired = true; 455 } 456 } 457 } 458 459 if ( $LDAPUserGroupCount != 0 ) 460 { 461 $republishRequired = true; 462 } 463 } 464 if ( $republishRequired ) 465 { 466 $noRemoveAssignmentList = array(); 467 if ( $hasOtherNodeType ) 468 { 469 foreach ( $otherNodeArray as $otherNode ) 470 { 471 $noRemoveAssignmentList[$otherNode['parent_node_id']] = $otherNode['is_main']; 472 } 473 } 474 475 if ( $hasLDAPNodeType ) 476 { 477 foreach ( $newLDAPNodeArray as $newLDAPNode ) 478 { 479 $noRemoveAssignmentList[$newLDAPNode['parent_node_id']] = $newLDAPNode['is_main']; 480 } 481 } 482 483 if ( !$hasOtherNodeType and !$hasLDAPNodeType ) 484 { 485 $noRemoveAssignmentList[$defaultUserPlacement] = 1; 486 } 487 488 $newVersion = $contentObject->createNewVersion(); 489 $newVersionNr = $newVersion->attribute( 'version' ); 490 $nodeAssignmentList =& $newVersion->attribute( 'node_assignments' ); 491 $noAddAssignmentList = array(); 492 foreach ( array_keys( $nodeAssignmentList ) as $key ) 493 { 494 $nodeAssignment =& $nodeAssignmentList[$key]; 495 $parentNodeID = $nodeAssignment->attribute( 'parent_node' ); 496 if ( array_key_exists( $parentNodeID, $noRemoveAssignmentList ) ) 497 { 498 $noAddAssignmentList[] = $parentNodeID; 499 $nodeAssignment ->setAttribute( 'parent_remote_id', 'LDAP_' . $parentNodeID ); 500 $nodeAssignment ->store(); 501 } 502 else 503 { 504 eZNodeAssignment::removeByID( $nodeAssignment->attribute( 'id' ) ); 505 } 506 } 507 508 if ( $hasOtherNodeType ) 509 { 510 foreach ( $otherNodeArray as $otherNode ) 511 { 512 if ( !in_array( $otherNode['parent_node_id'], $noAddAssignmentList ) ) 513 { 514 $newVersion->assignToNode( $otherNode['parent_node_id'], $otherNode['is_main'] ); 515 } 516 } 517 } 518 519 if ( $hasLDAPNodeType ) 520 { 521 foreach ( $newLDAPNodeArray as $newLDAPNode ) 522 { 523 if ( !in_array( $newLDAPNode['parent_node_id'], $noAddAssignmentList ) ) 524 { 525 $newVersion->assignToNode( $newLDAPNode['parent_node_id'], $newLDAPNode['is_main'] ); 526 } 527 $assignment = eZNodeAssignment::fetch( $contentObject->attribute( 'id' ), $newVersionNr, $newLDAPNode['parent_node_id'] ); 528 $assignment->setAttribute( 'parent_remote_id', "LDAP_" . $newLDAPNode['parent_node_id'] ); 529 $assignment->store(); 530 } 531 } 532 533 if ( !$hasOtherNodeType and !$hasLDAPNodeType ) 534 { 535 if ( !in_array( $defaultUserPlacement, $noAddAssignmentList ) ) 536 { 537 $newVersion->assignToNode( $defaultUserPlacement, 1 ); 538 } 539 } 540 include_once ( 'lib/ezutils/classes/ezoperationhandler.php' ); 541 $adminUser = eZUser::fetchByName( 'admin' ); 542 $adminUserContentObjectID = $adminUser->attribute( 'contentobject_id' ); 543 eZUser::setCurrentlyLoggedInUser( $adminUser, $adminUserContentObjectID ); 544 $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $userID, 545 'version' => $newVersionNr ) ); 546 $cli->output( $cli->stylize( 'emphasize', $existUser->attribute('login') ) . " has changed group, updated." ); 547 } 548 } 549 } 550 } 551 $db->commit(); 552 553 if ( !$isQuiet ) 554 $cli->output( "All LDAP users have been updated!" ); 555 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |