<?php

abstract class catalyz_LoginHandler {
    abstract public function Login($login, $password, $remember);
    abstract public function hasAccessToToolbar();
    abstract public function canViewContent(ContentTreeNode $content_tree_node);

    function Logout() {
        $sf_user = sfContext::getInstance()->getUser();
        $sf_user->setAuthenticated(false);
        $sf_user->clearCredentials();
        $duration = time() - 3600;
        setcookie('Login', '', $duration, '/');
        setcookie('Password', '', $duration, '/');
    }

    protected function getRememberKey() {
        return sfConfig::get('app_permissions_remember_key');
    }
    protected function sendCreditentialCookie($login, $password) {
        $duration = time() + 30 * 24 * 3600;

        $crypt = new Encryption();
        $cryptedPassword = $crypt->encrypt($this->getRememberKey(), $password, strlen($password));
        setcookie('Login', $login, $duration, '/');
        setcookie('Password', $cryptedPassword, $duration, '/');
    }

    public function ForgotPassword($username) {
        // todo
    }
    public function ForgotLogin($name) {
        // todo
    }

    public function AutoLogin() {
        if (!empty($_COOKIE['Login']) && !empty($_COOKIE['Password'])) {
            $crypt = new Encryption();
            $clearPassword = $crypt->decrypt($this->getRememberKey(), $_COOKIE['Password']);

            $this->Login($_COOKIE['Login'], $clearPassword, true);
        }
    }

    public function hasAccessToPermsTab() {
        return false;
    }
}

interface ILoginHandlerPermissionAdminProvider {
    public function getEditPermissionTemplate();

    /**
     * ILoginHandlerPermissionAdminProvider::updateNodePermisions()
     *
     * @param Node $node
     * @param sfWebRequest $request
     * @return
     */
    public function updateNodePermisions($node, $request);
}

?>