public static function getContentType(Route $route) { if ($route->hasContentType()) { return $route->getContentType(); } switch ($route->getType()) { case Route::TYPE_JSON: return self::JSON; case Route::TYPE_XML: return self::XML; case Route::TYPE_PAGE: case Route::TYPE_HTML: return self::HTML; default: return self::DEF; } }
/** * @param string $routesFile * @param boolean $public Default public setting * @param boolean $sessions Default sessions setting * @throws PlinthException */ public function loadRoutes($routesFile, $public, $sessions) { if (!file_exists($routesFile)) { throw new PlinthException('Add routing.json to your application config'); } $routes = json_decode(file_get_contents($routesFile), true); if (!is_array($routes)) { throw new PlinthException('Cannot parse routing.json config'); } foreach ($routes as $routeName => $routeInfo) { $newroute = new Route(array('name' => $routeName) + $routeInfo, $this->Main(), $public, $sessions); $this->_routes[$routeName] = $newroute; if ($newroute->isDefault()) { if ($this->_defaultRoute !== false) { throw new PlinthException('You can only define 1 default route'); } $this->_defaultRoute = $newroute; } } }
/** * @param Route $route * @throws PlinthException */ public function isRouteAuthorized(Route $route) { $loginpage = $this->main->getSetting('loginpage'); if (!$route->isPublic()) { if (!$this->main->getUserService()->isSessionValid()) { if ($route->getName() === $loginpage) { throw new PlinthException('Please set your login page to public'); } $this->disableAction(); $this->main->getRouter()->redirect($loginpage); $this->main->handleRequest(true); } else { if ($route->hasRoles()) { $roles = $this->main->getUserService()->getUser()->getRouteRoles(); if (!is_array($roles)) { throw new PlinthException('The route roles for a user needs to return a array of scalar values.'); } if (!$this->main->getRouter()->isUserRoleAllowed($roles)) { $this->main->getResponse()->hardExit(Response::CODE_403); } } } } }