コード例 #1
0
ファイル: ldap_mass_sync.php プロジェクト: jose-martins/glpi
/**
 * Function to import or synchronise all the users from an ldap directory
 *
 * @param $options   array
**/
function import(array $options)
{
    global $CFG_GLPI;
    $results = array(AuthLDAP::USER_IMPORTED => 0, AuthLDAP::USER_SYNCHRONIZED => 0, AuthLDAP::USER_DELETED_LDAP => 0);
    //The ldap server id is passed in the script url (parameter server_id)
    $limitexceeded = false;
    $actions_to_do = array();
    switch ($options['action']) {
        case AuthLDAP::ACTION_IMPORT:
            $actions_to_do = array(AuthLDAP::ACTION_IMPORT);
            break;
        case AuthLDAP::ACTION_SYNCHRONIZE:
            $actions_to_do = array(AuthLDAP::ACTION_SYNCHRONIZE);
            break;
        case AuthLDAP::ACTION_ALL:
            $actions_to_do = array(AuthLDAP::ACTION_IMPORT, AuthLDAP::ACTION_ALL);
            break;
    }
    foreach ($actions_to_do as $action_to_do) {
        $options['mode'] = $action_to_do;
        $options['authldaps_id'] = $options['ldapservers_id'];
        $users = AuthLdap::getAllUsers($options, $results, $limitexceeded);
        $contact_ok = true;
        if (is_array($users)) {
            foreach ($users as $user) {
                $result = AuthLdap::ldapImportUserByServerId(array('method' => AuthLDAP::IDENTIFIER_LOGIN, 'value' => $user["user"]), $action_to_do, $options['ldapservers_id']);
                if ($result) {
                    $results[$result['action']] += 1;
                }
                echo ".";
            }
        } else {
            if (!$users) {
                $contact_ok = false;
            }
        }
    }
    if ($limitexceeded) {
        echo "\nLDAP Server size limit exceeded";
        if ($CFG_GLPI['user_deleted_ldap']) {
            echo ": user deletion disabled\n";
        }
        echo "\n";
    }
    if ($contact_ok) {
        echo "\nImported: " . $results[AuthLDAP::USER_IMPORTED] . "\n";
        echo "Synchronized: " . $results[AuthLDAP::USER_SYNCHRONIZED] . "\n";
        echo "Deleted from LDAP: " . $results[AuthLDAP::USER_DELETED_LDAP] . "\n";
    } else {
        echo "Cannot contact LDAP server!\n";
    }
    echo "\n\n";
}