<?php

require_once("userauth.interface.php");
require_once("ldap.class.php");

/**
 * user authentication :: LDAP
 * implements userauth interface
 * 
 * @author hahn
 * 
 * Axel <axel.hahn@unibe.ch>
 * 2024-08-29  Axel php8 only; added variable types
 */
class userauthLdap implements iUserAuth
{

    /**
     * object for ldap actions
     * @var object
     */
    private object $_oLdap;

    // ----------------------------------------------------------------------
    // 
    // ----------------------------------------------------------------------

    /**
     * Constructor
     */
    public function __construct()
    {
        global $aConfig;
        $this->_oLdap = new imlldap($aConfig['auth']['ldap']);

        // first test of ldap connection
        // $this->_oLdap->debugOn();
        $this->_oLdap->connect();
    }

    /**
     * Destructor
     * Close ldap connection
     */
    public function __destruct()
    {
        $this->_oLdap->close();
    }

    // ----------------------------------------------------------------------
    // implementation
    // ----------------------------------------------------------------------

    /**
     * Verify if a given user and password combination is correct
     * 
     * @param string  $sUser      username
     * @param string  $sPassword  password
     * @return boolean
     */
    public function authenticate(string $sUser, string $sPassword): bool
    {
        return $this->_oLdap->verifyPassword($sUser, $sPassword);
    }

}