jablonka.czprosek.czf

freenet-router

Subversion Repositories:
[/] [trunk/] [freenet-router/] [var/] [www/] [freenet-router/] [Framework/] [Environment/] [Dispatcher.php] - Rev 2

Compare with Previous - Blame - Download


<?php

namespace Phem\Environment;

use Phem\Core\Object;
use Phem\Environment\EnvironmentManager;
use Phem\Libraries\Security\Authorization\IAuthorizationProvider;
use Phem\Libraries\Security\Model\Common\Controller;
use Phem\Libraries\Security\Model\Common\Task;
use Phem\Libraries\Security\Model\Common\User;

class Dispatcher extends Object
{

    private function runTask($controllerClass,$taskName)
    {
        $controller = new $controllerClass();
        /* Perform the Request task */
        $controller->execute($taskName);

        /* Redirect if set by the controller */
        $controller->redirect();
    }
    
    private function findControllerClass($controllerName)
    {
        $controllerInApp = file_exists(APP_DIR . DS . CONTROLLER_DIR 
                . DS . $controllerName . CONTROLLER_SUFFIX . '.php');
        $controllerInFramework = file_exists(FRAMEWORK_DIR . DS . CONTROLLER_DIR 
                . DS . $controllerName . CONTROLLER_SUFFIX . '.php');

        /* If required controller does not exist, revert to default */
        if ($controllerInApp && $controllerInFramework)
        {
            throw new ApplicationException($controllerName 
                    . CONTROLLER_SUFFIX 
                    . " is controller name reserverd by framework");
        }        
        else if ($controllerInApp)
        {
            $ns = APP_NAMESPACE;
        }
        else if ($controllerInFramework)
        {
            $ns = ROOT_NAMESPACE;
        }
        else if (!$controllerInFramework && !$controllerInApp)
        {
            $controllerName = DEFAULT_CONTROLLER;
            $ns = APP_NAMESPACE;
        }

        /* Create the (default) controller */
        return $ns . '\\' . CONTROLLER_DIR . '\\' . $controllerName . CONTROLLER_SUFFIX;
    }

    public function dispatchRequest()
    {
        
        $session = EnvironmentManager::getSession();
        
        /* Require specific controller if requested */
        $controllerName = EnvironmentManager::getRequestVar(
                        CONTROLLER_ROUTE_ATTR, DEFAULT_CONTROLLER, 'word'
        );

        $controllerClass = $this->findControllerClass($controllerName);

        $task = new Task();
        $task->setName(EnvironmentManager
                ::getRequestVar(TASK_ROUTE_ATTR, DEFAULT_TASK, 'word'));
        $controllerObj = new Controller();
        $controllerObj->setName($controllerName);
        $task->setController($controllerObj);
        
        $loggedUser = $session->getVar(ROOT_NAMESPACE . ".loggedUser");
        if ($loggedUser === null)
        {
            $loggedUser = "guest";
            $session->setVar(ROOT_NAMESPACE . ".loggedUser","guest");
        }

        if (!LOGIN_REQUIRED || $controllerObj->getName() == DISPATCHER_CONTROLLER_NAME)
        {
            $this->runTask($controllerClass, $task->getName());
            return;
        }
        
        $dispatcherControllerClass = ROOT_NAMESPACE . '\\' . CONTROLLER_DIR . '\\' 
                . DISPATCHER_CONTROLLER_NAME . CONTROLLER_SUFFIX;                  
        
        $loggedUserObj = EnvironmentManager::getUserManager()
                ->getUser($loggedUser);
        
        if ($loggedUserObj === null)
        {
            $loggedUserObj = new User();
            $loggedUserObj->setUsername($loggedUser);
        }

        $canTask = EnvironmentManager::getAuthorizationProvider()
                ->canTask($loggedUserObj, $task);

        if ($canTask == IAuthorizationProvider::PERMISSION_ALLOWED)
        {
            $this->runTask($controllerClass, $task->getName());
        }
        else if ($canTask == IAuthorizationProvider::PERMISSION_DENIED)
        {
            $this->runTask($dispatcherControllerClass, 
                    DISPATCHER_DENIED_ACCESS_TASK_NAME);
        }
        else if ($canTask == IAuthorizationProvider::PERMISSION_UNKNOWN)
        {
            switch (LOGIN_POLICY)
            {
                case "ALLOW":
                    $this->runTask($controllerClass, $task->getName());
                    break;
                case "LOGIN":
                    $this->runTask($dispatcherControllerClass, 
                            DISPATCHER_LOGIN_TASK_NAME);
                    break;
                case "DENY":
                default:
                    $this->runTask($dispatcherControllerClass, 
                            DISPATCHER_DENIED_ACCESS_TASK_NAME);
                    break;
            }

        }
    }

}

Powered by WebSVN 2.2.1