<?php
/**
 * This file handles the connection and actions My Library Authenticate
 *
 * @author Eduardo Bonfandini [eduardo@solis.coop.br]
 *
 * @version $Id$
 *
 * \b Maintainers \n
 * Eduardo Bonfandini [eduardo@solis.coop.br]
 * Jamiel Spezia [jamiel@solis.coop.br]
 * Luiz Gregory Filho [luiz@solis.coop.br]
 * Moises Heberle [moises@solis.coop.br]
 *
 * @since
 * Class created on 01/08/2008
 *
 * \b Organization: \n
 * SOLIS - Cooperativa de Solucoes Livres \n
 * The Gnuteca3 Development Team
 *
 * \b CopyLeft: \n
 * CopyLeft (L) 2008 SOLIS - Cooperativa de Solucoes Livres \n
 *
 * \b License: \n
 * Licensed under GPL (for further details read the COPYING file or http://www.gnu.org/copyleft/gpl.html )
 *
 * \b History: \n
 * See history in SVN repository: http://gnuteca.solis.coop.br
 */
class BusinessGnuteca3BusAuthenticate extends GnutecaBusiness
{
    public  $session;
    private $busPerson;
    private $busBond;


    /**
     * Class constructor
     **/
    function __construct()
    {
        parent::__construct();
        $this->MIOLO        = MIOLO::getInstance();
        $this->module       = MIOLO::getCurrentModule();

        $this->busPerson    = $this->MIOLO->getBusiness($this->module, 'BusPerson');
        $this->busBond      = $this->MIOLO->getBusiness($this->module, 'BusBond');

        $this->session      = new MSession();
    }


    /**
     * Try to authenticate (or login)
     *
     * @return TRUE if succed, otherwise FALSE
     **/
    public function authenticate($user, $password)
    {
        if ($user && $password)
        {
        	if ( !is_numeric( $user ) )
        	{
        		return false;
		    }
		    if ( MUtil::getBooleanValue( MY_LIBRARY_AUTHENTICATE_LDAP ) )
		    {
	                $name = $this->authenticateLdap($user, $password);
		    }
		    else
		    {
	                $name = $this->busPerson->authenticate($user, $password);
	                $name = $name[0][0];
		    }

            if ($name)
	        {
    		    // save data on session
                $this->createSession($user, $name);
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    /**
     * Make user login (authentication) using LDAP
     *
     * @param int $user the user id (personId)
     * @param string $password the password of user
     * @return $name ou false if no successfull
     */
    public function authenticateLdap($user,$pass)
    {
        $MIOLO  = MIOLO::getInstance();
        $module = MIOLO::getCurrentModule();
        $MIOLO->Uses('security/mauthldap.class.php');
        $authLdap = new MAuthLdap();
        $authLdap->connect();
        // verifica usuário e senha, sem gravar na sessao (3o parametro)
        if ( $r = $authLdap->authenticate($user, $pass, false) )
        {
            $data = $this->busPerson->getBasicPersonInformations($user);
            return $data->name;
        }
        else
        {
            return false;
        }
    }

    /**
     * Save login in SESSION
     **/
    public function createSession($user, $name)
    {
        $this->session->setValue('MyLibraryLogged',true);
        $this->session->setValue('MyLibraryUserCode',$user);
        $this->session->setValue('MyLibraryUserName',$name);
    }

    /**
     * Change the password of an user
     *
     * @param integer $user code
     * @param string $password the password
     * @param string $retype of password
     * @return boolean
     */
    public function changePassword($user, $password, $retype)
    {
		$ok = $this->busPerson->changePassword($user, $password, $retype);
		return $ok;
    }


    /**
    * Make logoff of current logged user
    *
    */
    public function logoff()
    {
        $this->session->setValue('MyLibraryLogged',null);
        $this->session->setValue('MyLibraryUserCode',null);
        $this->session->setValue('MyLibraryUserName',null);
    }


    /**
    * Verify if some user is logged
    */
    public function checkAcces()
    {
        return $this->session->getValue('MyLibraryLogged');
    }


    /**
    * Get the code of logged user
    */
    public function getUserCode()
    {
    	$MIOLO = MIOLO::getInstance();
        $session = $MIOLO->getSession();
        return $session->getValue('MyLibraryUserCode');
    }


    /**
    * Get the name of logged user
    */
    public function getUserName()
    {
        return $this->session->getValue('MyLibraryUserName');
    }



    /**
     * retorna o link da pessoa
     *
     * @return int
     */
    public function getPersonLink()
    {
        if(!$this->getUserCode())
        {
            return false;
        }

        if($bond = $this->busBond->getPersonLink($this->getUserCode()))
        {
            return $bond->linkId;
        }

        return false;
    }

}
?>
