jablonka.czprosek.czf

freenet-router

Subversion Repositories:
[/] [trunk/] [freenet-router/] [var/] [www/] [freenet-router/] [Framework/] [Environment/] [Dispatcher.php] - Blame information for rev 2

 

Line No. Rev Author Line
12simandl<?php
2 
3namespace Phem\Environment;
4 
5use Phem\Core\Object;
6use Phem\Environment\EnvironmentManager;
7use Phem\Libraries\Security\Authorization\IAuthorizationProvider;
8use Phem\Libraries\Security\Model\Common\Controller;
9use Phem\Libraries\Security\Model\Common\Task;
10use Phem\Libraries\Security\Model\Common\User;
11 
12class Dispatcher extends Object
13{
14 
15 private function runTask($controllerClass,$taskName)
16 {
17 $controller = new $controllerClass();
18 /* Perform the Request task */
19 $controller->execute($taskName);
20 
21 /* Redirect if set by the controller */
22 $controller->redirect();
23 }
24 
25 private function findControllerClass($controllerName)
26 {
27 $controllerInApp = file_exists(APP_DIR . DS . CONTROLLER_DIR
28 . DS . $controllerName . CONTROLLER_SUFFIX . '.php');
29 $controllerInFramework = file_exists(FRAMEWORK_DIR . DS . CONTROLLER_DIR
30 . DS . $controllerName . CONTROLLER_SUFFIX . '.php');
31 
32 /* If required controller does not exist, revert to default */
33 if ($controllerInApp && $controllerInFramework)
34 {
35 throw new ApplicationException($controllerName
36 . CONTROLLER_SUFFIX
37 . " is controller name reserverd by framework");
38 }
39 else if ($controllerInApp)
40 {
41 $ns = APP_NAMESPACE;
42 }
43 else if ($controllerInFramework)
44 {
45 $ns = ROOT_NAMESPACE;
46 }
47 else if (!$controllerInFramework && !$controllerInApp)
48 {
49 $controllerName = DEFAULT_CONTROLLER;
50 $ns = APP_NAMESPACE;
51 }
52 
53 /* Create the (default) controller */
54 return $ns . '\\' . CONTROLLER_DIR . '\\' . $controllerName . CONTROLLER_SUFFIX;
55 }
56 
57 public function dispatchRequest()
58 {
59 
60 $session = EnvironmentManager::getSession();
61 
62 /* Require specific controller if requested */
63 $controllerName = EnvironmentManager::getRequestVar(
64 CONTROLLER_ROUTE_ATTR, DEFAULT_CONTROLLER, 'word'
65 );
66 
67 $controllerClass = $this->findControllerClass($controllerName);
68 
69 $task = new Task();
70 $task->setName(EnvironmentManager
71 ::getRequestVar(TASK_ROUTE_ATTR, DEFAULT_TASK, 'word'));
72 $controllerObj = new Controller();
73 $controllerObj->setName($controllerName);
74 $task->setController($controllerObj);
75 
76 $loggedUser = $session->getVar(ROOT_NAMESPACE . ".loggedUser");
77 if ($loggedUser === null)
78 {
79 $loggedUser = "guest";
80 $session->setVar(ROOT_NAMESPACE . ".loggedUser","guest");
81 }
82 
83 if (!LOGIN_REQUIRED || $controllerObj->getName() == DISPATCHER_CONTROLLER_NAME)
84 {
85 $this->runTask($controllerClass, $task->getName());
86 return;
87 }
88 
89 $dispatcherControllerClass = ROOT_NAMESPACE . '\\' . CONTROLLER_DIR . '\\'
90 . DISPATCHER_CONTROLLER_NAME . CONTROLLER_SUFFIX;
91 
92 $loggedUserObj = EnvironmentManager::getUserManager()
93 ->getUser($loggedUser);
94 
95 if ($loggedUserObj === null)
96 {
97 $loggedUserObj = new User();
98 $loggedUserObj->setUsername($loggedUser);
99 }
100 
101 $canTask = EnvironmentManager::getAuthorizationProvider()
102 ->canTask($loggedUserObj, $task);
103 
104 if ($canTask == IAuthorizationProvider::PERMISSION_ALLOWED)
105 {
106 $this->runTask($controllerClass, $task->getName());
107 }
108 else if ($canTask == IAuthorizationProvider::PERMISSION_DENIED)
109 {
110 $this->runTask($dispatcherControllerClass,
111 DISPATCHER_DENIED_ACCESS_TASK_NAME);
112 }
113 else if ($canTask == IAuthorizationProvider::PERMISSION_UNKNOWN)
114 {
115 switch (LOGIN_POLICY)
116 {
117 case "ALLOW":
118 $this->runTask($controllerClass, $task->getName());
119 break;
120 case "LOGIN":
121 $this->runTask($dispatcherControllerClass,
122 DISPATCHER_LOGIN_TASK_NAME);
123 break;
124 case "DENY":
125 default:
126 $this->runTask($dispatcherControllerClass,
127 DISPATCHER_DENIED_ACCESS_TASK_NAME);
128 break;
129 }
130 
131 }
132 }
133 
134}

Powered by WebSVN 2.2.1