<?php

if (!defined('CREDENTIAL_ADMINISTRATOR')) {
    define('CREDENTIAL_ADMINISTRATOR', 'administrator');
}
if (!defined('CREDENTIAL_WEBMASTER')) {
    define('CREDENTIAL_WEBMASTER', 'webmaster');
}

class catalyzViewGroupLoginHandler extends catalyz_LoginHandler implements ILoginHandlerPermissionAdminProvider {
    public function Login($login, $password, $remember) {
        try {
            $credentials = $this->getUserCredentials($login, $password);
        }
        catch (Exception $e) {
            return false;
        }

        $user = sfContext::getInstance()->getUser();
        $user->setAuthenticated(true);
        $user->addCredentials($credentials);

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

        return true;
    }

    protected function getUserCredentials($login, $password) {
        if (!($login == sfConfig::get('app_auth_login') && $password == sfConfig::get('app_auth_password'))) {
            throw new Exception('Invalid credentials.');
        }
        return array(CREDENTIAL_ADMINISTRATOR, CREDENTIAL_WEBMASTER);
    }

    public function hasAccessToToolbar() {
        return sfContext::getInstance()->getUser()->hasCredential(CREDENTIAL_WEBMASTER);
    }

    public function hasAccessToPermsTab() {
        return sfContext::getInstance()->getUser()->hasCredential(CREDENTIAL_ADMINISTRATOR);
    }

    public function getEditPermissionTemplate() {
        return 'catalyzLoginHandlerViewGroup/edit_perms';
    }

    public function updateNodePermisions($node, $request) {
        $node->setPermsType($request->getParameter('perms_type'));
        $credentials = $request->getParameter('perms_groups');
        $node->setCredentials(is_array($credentials)?implode('|', array_keys($credentials)):'');
    }

    public function canViewContent(ContentTreeNode $content_tree_node) {
        list($perms_type, $credentials) = $content_tree_node->getPermsData();
        switch ($perms_type) {
            case Node::PERMS_ALLOWED_TO_ANYBODY:
                return true;
            case Node::PERMS_RESTRICTED:
                $credentials = explode('|', $credentials);
                $sfUser = sfContext::getInstance()->getUser();
                // var_dump($credentials);exit;
                return $sfUser->hasCredential(CREDENTIAL_WEBMASTER) || $sfUser->hasCredential($credentials, false);
            default:
                return false;
        } // switch
    }
	public function canContentBeSeenByAnonymous(ContentTreeNode $ContentTreeNode){
		list($perms_type, $credentials) = $content_tree_node->getPermsData();
		switch ($perms_type) {
			case Node::PERMS_ALLOWED_TO_ANYBODY:
				return true;
			case Node::PERMS_RESTRICTED:
				return FALSE;
			default:
				return false;
		} // switch
	}

	/**
	 * catalyzViewGroupLoginHandler::getDefaultPermissionTemplate()
	 *
	 * @return
	 */
	public function getDefaultPermissionTemplate()
	{
		return 'catalyzLoginHandlerViewGroup/edit_perms';
	}

	function hasPermission($perm, $default){
		return true;
	}
}