Example #1
0
    $ld = CLdapServer::GetByID($ID);
    ClearVars("str_");
    if (!($arFields = $ld->ExtractFields("str_"))) {
        $ID = 0;
    } else {
        if ($MOD_RIGHT < "W") {
            $str_ADMIN_PASSWORD = "";
        }
        $ldp = CLDAP::Connect(array("SERVER" => $arFields['SERVER'], "PORT" => $arFields['PORT'], "ADMIN_LOGIN" => $arFields['ADMIN_LOGIN'], "ADMIN_PASSWORD" => $arFields['ADMIN_PASSWORD'], "BASE_DN" => $arFields['BASE_DN'], "GROUP_FILTER" => $arFields['GROUP_FILTER'], "GROUP_ID_ATTR" => $arFields['GROUP_ID_ATTR'], "GROUP_NAME_ATTR" => $arFields['GROUP_NAME_ATTR'], "GROUP_MEMBERS_ATTR" => $arFields['GROUP_MEMBERS_ATTR'], "CONVERT_UTF8" => $arFields['CONVERT_UTF8'], "USER_FILTER" => $arFields['USER_FILTER'], "USER_GROUP_ATTR" => $arFields['USER_GROUP_ATTR'], "USER_GROUP_ACCESSORY" => $arFields['USER_GROUP_ACCESSORY'], "USER_DEPARTMENT_ATTR" => $arFields['USER_DEPARTMENT_ATTR'], "USER_MANAGER_ATTR" => $arFields['USER_MANAGER_ATTR'], "MAX_PAGE_SIZE" => $arFields['MAX_PAGE_SIZE']));
        $db_groups = CLdapServer::GetGroupMap($ID);
        while ($arGroup = $db_groups->Fetch()) {
            $arGroups[$arGroup['GROUP_ID'] . ' ' . md5($arGroup['LDAP_GROUP_ID'])] = $arGroup;
        }
        if (!isset($noimportGroups)) {
            $noimportGroups = array();
            $db_groups = CLdapServer::GetGroupBan($ID);
            while ($arGroup = $db_groups->Fetch()) {
                $noimportGroups[md5($arGroup['LDAP_GROUP_ID'])] = $arGroup['LDAP_GROUP_ID'];
            }
        }
        //$ADMIN_PASSWORD = $arFields['ADMIN_PASSWORD'];
        if (!$bPostback) {
            $arUserFieldMap = $arFields["FIELD_MAP"];
        }
    }
}
//if(strlen($Add)<=0)
$DB->InitTableVarsForEdit("b_ldap_server", "", "str_");
if (is_array($_REQUEST['LDAP_GROUP'])) {
    foreach ($_REQUEST['LDAP_GROUP'] as $t_id => $arGroup) {
        if (strlen($arGroup['LDAP_GROUP_ID']) > 0 || $arGroup['GROUP_ID'] > 0) {
Example #2
0
 public static function Sync($ldap_server_id)
 {
     global $DB, $USER, $APPLICATION;
     if (!is_object($USER)) {
         $USER = new CUser();
         $bUSERGen = true;
     }
     $dbLdapServers = CLdapServer::GetById($ldap_server_id);
     if (!($oLdapServer = $dbLdapServers->GetNextServer())) {
         return false;
     }
     if (!$oLdapServer->Connect()) {
         return false;
     }
     if (!$oLdapServer->BindAdmin()) {
         $oLdapServer->Disconnect();
         return false;
     }
     $APPLICATION->ResetException();
     $db_events = GetModuleEvents("ldap", "OnLdapBeforeSync");
     while ($arEvent = $db_events->Fetch()) {
         $arParams['oLdapServer'] = $oLdapServer;
         if (ExecuteModuleEventEx($arEvent, array(&$arParams)) === false) {
             if (!($err = $APPLICATION->GetException())) {
                 $APPLICATION->ThrowException("Unknown error");
             }
             return false;
         }
     }
     // select all users from LDAP
     $arLdapUsers = array();
     $ldapLoginAttr = strtolower($oLdapServer->arFields["~USER_ID_ATTR"]);
     $APPLICATION->ResetException();
     $dbLdapUsers = $oLdapServer->GetUserList();
     $ldpEx = $APPLICATION->GetException();
     while ($arLdapUser = $dbLdapUsers->Fetch()) {
         $arLdapUsers[strtolower($arLdapUser[$ldapLoginAttr])] = $arLdapUser;
     }
     unset($dbLdapUsers);
     // select all Bitrix CMS users for this LDAP
     $arUsers = array();
     CTimeZone::Disable();
     $dbUsers = CUser::GetList($o, $b, array("EXTERNAL_AUTH_ID" => "LDAP#" . $ldap_server_id));
     CTimeZone::Enable();
     while ($arUser = $dbUsers->Fetch()) {
         $arUsers[strtolower($arUser["LOGIN"])] = $arUser;
     }
     unset($dbUsers);
     if (!$ldpEx || $ldpEx->msg != 'LDAP_SEARCH_ERROR') {
         $arDelLdapUsers = array_diff(array_keys($arUsers), array_keys($arLdapUsers));
     }
     if (strlen($oLdapServer->arFields["SYNC_LAST"]) > 0) {
         $syncTime = MakeTimeStamp($oLdapServer->arFields["SYNC_LAST"]);
     } else {
         $syncTime = 0;
     }
     $arCache = array();
     // selecting a list of groups, from which users will not be imported
     $noImportGroups = array();
     $dbGroups = CLdapServer::GetGroupBan($ldap_server_id);
     while ($arGroup = $dbGroups->Fetch()) {
         $noImportGroups[md5($arGroup['LDAP_GROUP_ID'])] = $arGroup['LDAP_GROUP_ID'];
     }
     $cnt = 0;
     // have to update $oLdapServer->arFields["FIELD_MAP"] for user fields
     // for each one of them looking for similar in user list
     foreach ($arLdapUsers as $userLogin => $arLdapUserFields) {
         if (!is_array($arUsers[$userLogin])) {
             if ($oLdapServer->arFields["SYNC_USER_ADD"] != "Y") {
                 continue;
             }
             // if user is not found among already existing ones, then import him
             // в $arLdapUserFields - user fields from ldap
             $userActive = $oLdapServer->getLdapValueByBitrixFieldName("ACTIVE", $arLdapUserFields);
             if ($userActive != "Y") {
                 continue;
             }
             $arUserFields = $oLdapServer->GetUserFields($arLdapUserFields, $departmentCache);
             // $arUserFields here contains LDAP user fields for a LDAP user
             // make a check, whether this user belongs to those groups only, from which import will not be made...
             $allUserGroups = $arUserFields['LDAP_GROUPS'];
             $userImportIsBanned = true;
             foreach ($allUserGroups as $groupId) {
                 $groupId = trim($groupId);
                 if (!empty($groupId) && !array_key_exists(md5($groupId), $noImportGroups)) {
                     $userImportIsBanned = false;
                     break;
                 }
             }
             // ...if he does not, then import him
             if (!$userImportIsBanned || empty($allUserGroups)) {
                 $oLdapServer->SetUser($arUserFields);
             }
         } else {
             // if date of update is set, then compare it
             $ldapTime = time();
             if ($syncTime > 0 && strlen($oLdapServer->arFields["SYNC_ATTR"]) > 0 && preg_match("'([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})\\.0Z'", $arLdapUserFields[strtolower($oLdapServer->arFields["SYNC_ATTR"])], $arTimeMatch)) {
                 $ldapTime = gmmktime($arTimeMatch[4], $arTimeMatch[5], $arTimeMatch[6], $arTimeMatch[2], $arTimeMatch[3], $arTimeMatch[1]);
                 $userTime = MakeTimeStamp($arUsers[$userLogin]["TIMESTAMP_X"]);
             }
             if ($syncTime < $ldapTime || $syncTime < $userTime) {
                 // make an update
                 $arUserFields = $oLdapServer->GetUserFields($arLdapUserFields, $arCache);
                 $arUserFields["ID"] = $arUsers[$userLogin]["ID"];
                 //echo $arUserFields["LOGIN"]." - updated<br>";
                 $oLdapServer->SetUser($arUserFields);
                 $cnt++;
             }
         }
     }
     foreach ($arDelLdapUsers as $userLogin) {
         $USER = new CUser();
         if (isset($arUsers[$userLogin]) && $arUsers[$userLogin]['ACTIVE'] == 'Y') {
             $ID = intval($arUsers[$userLogin]["ID"]);
             $USER->Update($ID, array('ACTIVE' => 'N'));
         }
     }
     $oLdapServer->Disconnect();
     CLdapServer::Update($ldap_server_id, array("~SYNC_LAST" => $DB->CurrentTimeFunction()));
     if ($bUSERGen) {
         unset($USER);
     }
     return $cnt;
 }
Example #3
0
			unset($dbUsers);

			if(!$ldpEx || $ldpEx->msg != 'LDAP_SEARCH_ERROR')
				$arDelLdapUsers = array_diff(array_keys($arUsers), array_keys($arLdapUsers));

			if(strlen($ldp->arFields["SYNC_LAST"])>0)
				$syncTime = MakeTimeStamp($ldp->arFields["SYNC_LAST"]);
			else
				$syncTime = 0;

			if(is_array($_REQUEST['LDAPMAP']))
				$ldp->arFields["FIELD_MAP"] = array_merge($ldp->arFields["FIELD_MAP"], $_REQUEST['LDAPMAP']);

			// selecting a list of groups, from which users will not be imported
			$noImportGroups = array();
			$dbGroups = CLdapServer::GetGroupBan($ldapServer);
			while($arGroup = $dbGroups->Fetch())
				$noImportGroups[md5($arGroup['LDAP_GROUP_ID'])] = $arGroup['LDAP_GROUP_ID'];

			// department ids are cached here, thus each user never queried more than once
			// if no intranet installed it is simply not used
			$departmentCache = array();
			$strUserImportError = '';

			foreach($arLdapUsers as $userLogin=>$arLdapUserFields)
			{
				if(!is_array($arUsers[$userLogin]))
				{
					// if user is not found among already existing ones, then import him

					// в $arLdapUserFields - поля текущего user'а, взятые из ldap