Exemplo n.º 1
0
 static function publishUpdateUser($parentNodeIDs, $defaultUserPlacement, $userAttributes, $isUtf8Encoding = false)
 {
     if (!is_array($userAttributes) or !isset($userAttributes['login']) or empty($userAttributes['login'])) {
         eZDebug::writeWarning('Empty user login passed.', __METHOD__);
         return false;
     }
     if ((!is_array($parentNodeIDs) or count($parentNodeIDs) < 1) and !is_numeric($defaultUserPlacement)) {
         eZDebug::writeWarning('No one parent node IDs was passed for publishing new user (login = "******")', __METHOD__);
         return false;
     }
     $parentNodeIDs[] = $defaultUserPlacement;
     $parentNodeIDs = array_unique($parentNodeIDs);
     $login = $userAttributes['login'];
     $first_name = $userAttributes['first_name'];
     $last_name = $userAttributes['last_name'];
     $email = $userAttributes['email'];
     if ($isUtf8Encoding) {
         $first_name = utf8_decode($first_name);
         $last_name = utf8_decode($last_name);
     }
     $user = eZUser::fetchByName($login);
     $createNewUser = is_object($user) ? false : true;
     if ($createNewUser) {
         if (!isset($first_name) or empty($first_name) or !isset($last_name) or empty($last_name) or !isset($email) or empty($email)) {
             eZDebug::writeWarning('Cannot create user with empty first name (last name or email).', __METHOD__);
             return false;
         }
         $ini = eZINI::instance();
         $userClassID = $ini->variable("UserSettings", "UserClassID");
         $userCreatorID = $ini->variable("UserSettings", "UserCreatorID");
         $defaultSectionID = $ini->variable("UserSettings", "DefaultSectionID");
         $class = eZContentClass::fetch($userClassID);
         $contentObject = $class->instantiate($userCreatorID, $defaultSectionID);
         $contentObject->store();
         $userID = $contentObjectID = $contentObject->attribute('id');
         $version = $contentObject->version(1);
         $version->setAttribute('modified', time());
         $version->setAttribute('status', eZContentObjectVersion::STATUS_DRAFT);
         $version->store();
         $user = eZLDAPUser::create($userID);
         $user->setAttribute('login', $login);
     } else {
         $userID = $contentObjectID = $user->attribute('contentobject_id');
         $contentObject = eZContentObject::fetch($userID);
         $version = $contentObject->attribute('current');
     }
     //================= common part 1: start ========================
     $contentObjectAttributes = $version->contentObjectAttributes();
     // find and set 'name' and 'description' attributes (as standard user group class)
     $firstNameIdentifier = 'first_name';
     $lastNameIdentifier = 'last_name';
     $firstNameAttribute = null;
     $lastNameAttribute = null;
     foreach ($contentObjectAttributes as $attribute) {
         if ($attribute->attribute('contentclass_attribute_identifier') == $firstNameIdentifier) {
             $firstNameAttribute = $attribute;
         } else {
             if ($attribute->attribute('contentclass_attribute_identifier') == $lastNameIdentifier) {
                 $lastNameAttribute = $attribute;
             }
         }
     }
     //================= common part 1: end ==========================
     // If we are updating an existing user, we must find out if some data should be changed.
     // In that case, we must create a new version and publish it.
     if (!$createNewUser) {
         $userDataChanged = false;
         $firstNameChanged = false;
         $lastNameChanged = false;
         $emailChanged = false;
         if ($firstNameAttribute and $firstNameAttribute->attribute('data_text') != $first_name) {
             $firstNameChanged = true;
         }
         $firstNameAttribute = false;
         // We will load this again from the new version we will create, if it has changed
         if ($lastNameAttribute and $lastNameAttribute->attribute('data_text') != $last_name) {
             $lastNameChanged = true;
         }
         $lastNameAttribute = false;
         // We will load this again from the new version we will create, if it has changed
         if ($user->attribute('email') != $email) {
             $emailChanged = true;
         }
         if ($firstNameChanged or $lastNameChanged or $emailChanged) {
             $userDataChanged = true;
             // Create new version
             $version = $contentObject->createNewVersion();
             $contentObjectAttributes = $version->contentObjectAttributes();
             foreach ($contentObjectAttributes as $attribute) {
                 if ($attribute->attribute('contentclass_attribute_identifier') == $firstNameIdentifier) {
                     $firstNameAttribute = $attribute;
                 } else {
                     if ($attribute->attribute('contentclass_attribute_identifier') == $lastNameIdentifier) {
                         $lastNameAttribute = $attribute;
                     }
                 }
             }
         }
     }
     //================= common part 2: start ========================
     if ($firstNameAttribute) {
         $firstNameAttribute->setAttribute('data_text', $first_name);
         $firstNameAttribute->store();
     }
     if ($lastNameAttribute) {
         $lastNameAttribute->setAttribute('data_text', $last_name);
         $lastNameAttribute->store();
     }
     if (!isset($userDataChanged) or $userDataChanged === true) {
         $contentClass = $contentObject->attribute('content_class');
         $name = $contentClass->contentObjectName($contentObject);
         $contentObject->setName($name);
     }
     if (!isset($emailChanged) or $emailChanged === true) {
         $user->setAttribute('email', $email);
     }
     $user->setAttribute('password_hash', "");
     $user->setAttribute('password_hash_type', 0);
     $user->store();
     $debugArray = array('Updating user data', 'createNewUser' => $createNewUser, 'userDataChanged' => isset($userDataChanged) ? $userDataChanged : null, 'login' => $login, 'first_name' => $first_name, 'last_name' => $last_name, 'email' => $email, 'firstNameAttribute is_object' => is_object($firstNameAttribute), 'lastNameAttribute is_object' => is_object($lastNameAttribute), 'content object id' => $contentObjectID, 'version id' => $version->attribute('version'));
     eZDebug::writeNotice(var_export($debugArray, true), __METHOD__);
     //================= common part 2: end ==========================
     if ($createNewUser) {
         reset($parentNodeIDs);
         // prepare node assignments for publishing new user
         foreach ($parentNodeIDs as $parentNodeID) {
             $newNodeAssignment = eZNodeAssignment::create(array('contentobject_id' => $contentObjectID, 'contentobject_version' => 1, 'parent_node' => $parentNodeID, 'parent_remote_id' => uniqid('LDAP_'), 'is_main' => $defaultUserPlacement == $parentNodeID ? 1 : 0));
             $newNodeAssignment->store();
         }
         $operationResult = eZOperationHandler::execute('content', 'publish', array('object_id' => $contentObjectID, 'version' => 1));
     } else {
         if ($userDataChanged) {
             // Publish object
             $operationResult = eZOperationHandler::execute('content', 'publish', array('object_id' => $contentObjectID, 'version' => $version->attribute('version')));
             // Refetch object
             $contentObject = eZContentObject::fetch($contentObjectID);
             $version = $contentObject->attribute('current');
         }
         $LDAPIni = eZINI::instance('ldap.ini');
         $keepGroupAssignment = $LDAPIni->hasVariable('LDAPSettings', 'KeepGroupAssignment') ? $LDAPIni->variable('LDAPSettings', 'KeepGroupAssignment') == "enabled" : false;
         if ($keepGroupAssignment == false) {
             $objectIsChanged = false;
             $db = eZDB::instance();
             $db->begin();
             // First check existing assignments, remove any that should not exist
             $assignedNodesList = $contentObject->assignedNodes();
             $existingParentNodeIDs = array();
             foreach ($assignedNodesList as $node) {
                 $parentNodeID = $node->attribute('parent_node_id');
                 if (!in_array($parentNodeID, $parentNodeIDs)) {
                     $node->removeThis();
                     $objectIsChanged = true;
                 } else {
                     $existingParentNodeIDs[] = $parentNodeID;
                 }
             }
             // Then check assignments that should exist, add them if they are missing
             foreach ($parentNodeIDs as $parentNodeID) {
                 if (!in_array($parentNodeID, $existingParentNodeIDs)) {
                     $newNode = $contentObject->addLocation($parentNodeID, true);
                     $newNode->updateSubTreePath();
                     $newNode->setAttribute('contentobject_is_published', 1);
                     $newNode->sync();
                     $existingParentNodeIDs[] = $parentNodeID;
                     $objectIsChanged = true;
                 }
             }
             // Then ensure that the main node is correct
             $currentMainParentNodeID = $contentObject->attribute('main_parent_node_id');
             if ($currentMainParentNodeID != $defaultUserPlacement) {
                 $existingNode = eZContentObjectTreeNode::fetchNode($contentObjectID, $defaultUserPlacement);
                 if (!is_object($existingNode)) {
                     eZDebug::writeError("Cannot find assigned node as {$defaultUserPlacement}'s child.", __METHOD__);
                 } else {
                     $existingNodeID = $existingNode->attribute('node_id');
                     $versionNum = $version->attribute('version');
                     eZContentObjectTreeNode::updateMainNodeID($existingNodeID, $contentObjectID, $versionNum, $defaultUserPlacement);
                     $objectIsChanged = true;
                 }
             }
             $db->commit();
             // Finally, clear object view cache if something was changed
             if ($objectIsChanged) {
                 eZContentCacheManager::clearObjectViewCache($contentObjectID, true);
             }
         }
     }
     eZUser::updateLastVisit($userID);
     //eZUser::setCurrentlyLoggedInUser( $user, $userID );
     // Reset number of failed login attempts
     eZUser::setFailedLoginAttempts($userID, 0);
     return $user;
 }