Changes

m
/
Line 70: Line 70:  
     session_name("DOLSESSID_".$dolibarr_main_db_name);
 
     session_name("DOLSESSID_".$dolibarr_main_db_name);
 
     session_start();
 
     session_start();
    // On est pas déjà authentifié, on demande le login/mot de passe
  −
    // A l'issu de cette demande, le login et un jeton doivent avoir été placé
  −
    // en session dans dol_user et dol_token et la page rappelée.
  −
    // MODE AUTO
  −
    if (in_array('auto',$authmode) && ! $login)
  −
    {
  −
        $login=$dolibarr_auto_user;
  −
        dolibarr_syslog ("Authentification ok (en mode force)");
  −
    }
  −
    // MODE HTTP (Basic)
  −
    if (in_array('http',$authmode) && ! $login)
  −
    {
  −
        $login=$_SERVER["REMOTE_USER"];
  −
    }
  −
    // MODE DOLIBARR
  −
    if (in_array('dolibarr',$authmode) && ! $login)
  −
    {
  −
        require_once(DOL_DOCUMENT_ROOT."/includes/pear/Auth/Auth.php");
  −
        $pear = $dolibarr_main_db_type.'://'.$dolibarr_main_db_user.':'.$dolibarr_main_db_pass.'@'.$dolibarr_main_db_host.'/'.$dolibarr_main_db_name;
  −
        if ($conf->global->DATABASE_PWD_ENCRYPTED)
  −
        {
  −
            $cryptType = "md5";
  −
        }
  −
        else
  −
        {
  −
            $cryptType = "none";
  −
        }
  −
        $params = array(
  −
            "dsn" => $pear,
  −
            "table" => MAIN_DB_PREFIX."user",
  −
            "usernamecol" => "login",
  −
            "passwordcol" => "pass",
  −
            "cryptType" => $cryptType,
  −
        );
  −
        $aDol = new DOLIAuth("DB", $params, "dol_loginfunction");
  −
        $aDol->setSessionName("DOLSESSID_".$dolibarr_main_db_name);
  −
        $aDol->start();        $result = $aDol->getAuth(); // Si deja logue avec succes, renvoie vrai, sinon effectue un redirect sur page loginfunction et renvoie false
  −
        if ($result)
  −
        {
  −
            // Authentification Auth OK, on va chercher le login
  −
            $login=$aDol->getUsername();
  −
            dolibarr_syslog ("Authentification ok (en mode Pear Base Dolibarr)");
  −
        }
  −
        else
  −
        {
  −
            if (isset($_POST["loginfunction"]))
  −
            {
  −
                // Echec authentification
  −
                dolibarr_syslog("Authentification ko (en mode Pear Base Dolibarr) pour '".$_POST["username"]."'");
  −
            }
  −
            else
  −
            {
  −
                // Non authentifie
  −
                //dolibarr_syslog("Authentification non realise");
  −
            }
  −
            exit;
  −
        }
  −
    }
  −
    // MODE LDAP
  −
    if ($conf->ldap->enabled && in_array('ldap',$authmode) && ! $login)
  −
    {
  −
        // Authentification Apache KO ou non active, pas de mode force on demande le login
  −
        require_once(DOL_DOCUMENT_ROOT."/includes/pear/Auth/Auth.php");
  −
        $params = array(
  −
            'dsn' => $ldap,
  −
            'host' => $conf->global->LDAP_SERVER_HOST,
  −
            'port' => $conf->global->LDAP_SERVER_PORT,            'version' => $conf->global->LDAP_SERVER_PORT,
  −
            'basedn' => $conf->global->LDAP_SERVER_DN,
  −
            'binddn' => $conf->global->LDAP_ADMIN_DN,
  −
            'bindpw' => $conf->global->LDAP_ADMIN_PASS,
  −
            //'userattr' => $conf->global->LDAP_FIELD_LOGIN_SAMBA,
  −
            'userattr' => 'samAccountName',            'userfilter' => '(objectClass=user)',
  −
        );
  −
        $aDol = new DOLIAuth("LDAP", $params, "dol_loginfunction");
  −
        $aDol->start();
  −
        $result = $aDol->getAuth(); // Si deja logue avec succes, renvoie vrai, sinon effectue un redirect sur page loginfunction et renvoie false
  −
        if ($result)
  −
        {
  −
            // Authentification Auth OK, on va chercher le login
  −
            $login=$aDol->getUsername();
  −
            dolibarr_syslog ("Authentification ok (en mode Pear Base LDAP)");
  −
        }
  −
        else
  −
        {
  −
            if (isset($_POST["loginfunction"]))
  −
            {
  −
                // Echec authentification
  −
                dolibarr_syslog("Authentification ko (en mode Pear Base LDAP) pour '".$_POST["username"]."'");
  −
            }
  −
            else
  −
            {
  −
                // Non authentifie
  −
                //dolibarr_syslog("Authentification non realise");
  −
            }
  −
            exit;
  −
        }
  −
    }
     −
Pour cette analyse, nous ignorerons le mode '''http''' et le mode '''LDAP''' pour nous concentrer sur le mode '''dolibarr'''.
+
    // Si on rentre ici suite a soumission d'un couple user/password alors
Il y a deux choses qui nous intéressent en particulier ici.
     −
# l'instanciation de l'objet DOLIAuth (avec en paramètres les informations nécessaires à la connexion à la DB, et une string 'dol_loginfunction' que l'on devine être le nom de la fonction de login)
+
        // Selon la valeur dolibarr_main_authentication, on appelle la fonction
# l'appel à la méthode '''start()''' sur cet objet DOLIAuth
+
        // dans le bon fichier qui verifie si un couple user/mot de passe est correcte
   −
=== Objet DOLIAuth ===
+
    // Sinon, on affiche la page de login
 
  −
L'instanciation est simple à analyser. En allant faire un tour dans htdocs/includes/pear/Auth/Auth.php, on a vite fait de comprendre qu'on assigne ''dol_loginfunction'' à un attribut de l'objet DOLIAuth. Sans plus.
  −
 
  −
Pour la méthode ''start()'', par contre, nous irons un petit peu plus loin dans l'analyse. Toujours dans htdocs/includes/pear/Auth/Auth.php, on retrouve la définition de la méthode en question:
  −
 
  −
    /**
  −
    * Start new auth session
  −
    *
  −
    * @access public
  −
    * @return void
  −
    */
  −
    function start()
  −
    {
  −
        $this->assignData();
  −
        session_start();
  −
        if (!$this->checkAuth()) {
  −
            $this->login();
  −
        }
  −
    }
  −
 
  −
Et cela nous mène donc vers la méthode ''assignData()'' (on notera au passage que l'on appelle aussi session_start() et $this->login() un peu plus tard).
  −
 
  −
    /**
  −
    * Assign data from login form to internal values
  −
    *
  −
    * This function takes the values for username and password
  −
    * from $HTTP_POST_VARS and assigns them to internal variables.
  −
    * If you wish to use another source apart from $HTTP_POST_VARS,
  −
    * you have to derive this function.
  −
    *
  −
    * @access private
  −
    * @global $HTTP_POST_VARS
  −
    * @see    Auth
  −
    * @return void
  −
    */
  −
    function assignData()
  −
    {
  −
        $post = &$this->_importGlobalVariable("post");
  −
        if (isset($post['username']) && $post['username'] != "") {
  −
            $this->username = (get_magic_quotes_gpc() == 1 ? stripslashes($post['username']) : $post['username']);
  −
        }
  −
        if (isset($post['password']) && $post['password'] != "") {
  −
            $this->password = (get_magic_quotes_gpc() == 1 ? stripslashes($post['password']) : $post['password'] );
  −
        }
  −
    }
  −
 
  −
Fonction toute simple donc, mais qui, par l'intermédiaire de ''_importGlobalVariable("post")'', va récupérer username et password comme ils ont été entrés dans le formulaire d'authentification.
  −
 
  −
Toutefois, toujours pas de vérification de ces données jusqu'ici. C'est le bon moment pour analyser la méthode '''login()'''.
  −
 
  −
    /**
  −
    * Login function
  −
    *
  −
    * @access private
  −
    * @return void
  −
    */
  −
    function login()
  −
    {
  −
        $login_ok = false;
  −
        /**
  −
        * When the user has already entered a username,
  −
        * we have to validate it.
  −
        */
  −
        if (!empty($this->username)) {
  −
            if (true === $this->storage->fetchData($this->username, $this->password)) {
  −
                $login_ok = true;
  −
            }
  −
        }
  −
        if (!empty($this->username) && $login_ok) {
  −
            $this->setAuth($this->username);
  −
            if (!empty($this->loginCallback)) {
  −
                call_user_func($this->loginCallback,$this->username);
  −
            }
  −
        }
  −
        /**
  −
        * If the login failed or the user entered no username,
  −
        * output the login screen again.
  −
        */
  −
        if (!empty($this->username) && !$login_ok) {
  −
            $this->status = AUTH_WRONG_LOGIN;
  −
        }
  −
        if ((empty($this->username) || !$login_ok) && $this->showLogin) {
  −
            $this->drawLogin($this->storage->activeUser);
  −
            return;
  −
        }
  −
    }
  −
 
  −
Voilà. Je n'irai pas jusqu'à expliquer le fonctionnement de cette dernière méthode (qui appelle drawLogin() en cas d'échec, laquelle affiche le formulaire de login), mais cette explication s'arrête ici. Si vous avez des problèmes d'identification sur votre plateforme, cette procédure détaillée devrait vous faire gagner pas mal de temps.