<?php

class kataoLoginHandler extends catalyzViewGroupLoginHandler {
    public function Login($login, $password, $remember) {
        try {
            if (null == $katao_user = KataoUserPeer::getUserByLoginAndPassword($login, $password)) {
                throw new Exception('Invalid credentials.');
            }
            $credentials = $katao_user->getCredentials();
        }
        catch (Exception $e) {
            sfContext::getInstance()->getUser()->setAttribute('catalyz_login_error', 'Login et/ou mot de passe incorrect.');
            return false;
        }

        $user = sfContext::getInstance()->getUser();
        $user->setAuthenticated(true);
        $user->addCredentials($credentials);
        $user->setInstance($katao_user);
        $user->setActivePeriod(KataoPeriodPeer::getActivePeriod());
        $user->setActiveNode($katao_user->getKataoMemberId()?$katao_user->getKataoMember()->getKataoNode():null);

        if ($remember) {
            $this->sendCreditentialCookie($login, $password);
        }

    	wpConfig::getInstance(true);

        return true;
    }

    protected function getUserCredentials($login, $password) {
        if (null == $katao_user = KataoUserPeer::getUserByLoginAndPassword($login, $password)) {
            throw new Exception('Invalid credentials.');
        }
        return $katao_user->getCredentials();
    }
}