1: <?php
2:
3: 4: 5:
6:
7: namespace Mapbender\CoreBundle\Controller;
8:
9: use Mapbender\CoreBundle\Component\Application;
10: use Mapbender\CoreBundle\Entity\Application as ApplicationEntity;
11: use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12: use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
13: use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
14: use Symfony\Component\Security\Core\Exception\AccessDeniedException;
15: use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16: use Symfony\Component\HttpFoundation\Response;
17:
18: 19: 20: 21: 22:
23: class ApplicationController extends Controller {
24: 25: 26: 27: 28: 29:
30: private function getUrls($slug) {
31: return array(
32: 'base' => $this->get('request')->getBaseUrl(),
33:
34: 'asset' => rtrim($this->get('templating.helper.assets')
35: ->getUrl('.'), '.'),
36: 'element' => $this->get('router')
37: ->generate('mapbender_core_application_element', array(
38: 'slug' => $slug)),
39: 'trans' => $this->get('router')
40: ->generate('mapbender_core_translation_trans'),
41: 'proxy' => $this->get('router')
42: ->generate('owsproxy3_core_owsproxy_entrypoint'));
43: }
44:
45: 46: 47: 48: 49: 50: 51: 52:
53: public function assetsAction($slug, $type) {
54: $response = new Response();
55: $application = $this->getApplication($slug);
56: $assets = $application->getAssets($type);
57: $asset_modification_time = new \DateTime();
58: $asset_modification_time->setTimestamp($assets->getLastModified());
59:
60:
61:
62:
63:
64: $filters = array(
65: 'js' => array(),
66: 'css' => array($this->container->get('assetic.filter.cssrewrite')));
67:
68:
69:
70: $target = str_replace('\\', '/', $this->get('request')->server->get('SCRIPT_FILENAME')
71: . $this->get('request')->server->get('PATH_INFO'));
72:
73: $mimetypes = array(
74: 'css' => 'text/css',
75: 'js' => 'application/javascript');
76:
77: $application_update_time = new \DateTime();
78: $application_entity = $this->getApplication($slug)->getEntity();
79:
80:
81: if($application->getEntity()->getSource() === ApplicationEntity::SOURCE_DB) {
82: $updateTime = max($application->getEntity()->getUpdated(),
83: $asset_modification_time);
84: } else {
85: $cacheUpdateTime = new \DateTime($this->container->getParameter('mapbender.cache_creation'));
86: $updateTime = max($cacheUpdateTime, $asset_modification_time);
87: }
88:
89: $response->setLastModified($updateTime);
90: if($response->isNotModified($this->get('request'))) {
91: return $response;
92: }
93:
94:
95:
96: $parts = array();
97: foreach($assets->all() as $asset) {
98: foreach($filters[$type] as $filter) {
99: $asset->ensureFilter($filter);
100: }
101: $asset->setTargetPath($target);
102: $parts[] = $asset->dump();
103: }
104:
105:
106: $response->headers->set('Content-Type', $mimetypes[$type]);
107: $response->setContent(implode("\n", $parts));
108: return $response;
109: }
110:
111: 112: 113: 114: 115: 116: 117: 118:
119: public function elementAction($slug, $id, $action) {
120: $element = $this->getApplication($slug)->getElement($id);
121:
122:
123:
124: return $element->httpAction($action);
125: }
126:
127: 128: 129: 130: 131: 132:
133: public function applicationAction($slug) {
134: $application = $this->getApplication($slug);
135:
136:
137:
138: $this->get("session")->set("proxyAllowed",true);
139:
140: return new Response($application->render());
141: }
142:
143: 144: 145: 146: 147: 148: 149: 150: 151:
152: private function getApplication($slug) {
153: $application = $this->get('mapbender')
154: ->getApplication($slug, $this->getUrls($slug));
155:
156: if($application === null) {
157: throw new NotFoundHttpException(
158: 'The application can not be found.');
159: }
160:
161: $this->checkApplicationAccess($application);
162:
163: return $application;
164: }
165:
166: 167: 168: 169: 170: 171: 172: 173:
174: public function checkApplicationAccess(Application $application) {
175: $securityContext = $this->get('security.context');
176:
177: $application_entity = $application->getEntity();
178: if($application_entity::SOURCE_YAML === $application_entity->getSource()
179: && count($application_entity->yaml_roles)) {
180: $passed = false;
181: foreach($application_entity->yaml_roles as $role) {
182: if($securityContext->isGranted($role)) {
183: $passed = true;
184: break;
185: }
186: }
187: if(!$passed) {
188: throw new AccessDeniedException('You are not granted view permissions for this application.');
189: }
190: }
191:
192: $granted = $securityContext->isGranted('VIEW', $application_entity);
193: if(false === $granted) {
194: throw new AccessDeniedException('You are not granted view permissions for this application.');
195: }
196:
197: if(!$application_entity->isPublished() and !$securityContext->isGranted('EDIT', $application_entity)) {
198: throw new AccessDeniedException('This application is not published at the moment');
199: }
200: }
201: }
202:
203: