Overview

Namespaces

  • Mapbender
    • Component
      • HTTP
    • CoreBundle
      • Command
      • Component
        • Exception
      • Controller
      • DataFixtures
        • ORM
      • DependencyInjection
      • Element
        • Type
      • Entity
      • EventListener
      • Extension
      • Form
        • DataTransformer
        • EventListener
        • Type
      • Security
      • Template
    • DrupalIntegrationBundle
      • DependencyInjection
      • Security
        • Authentication
          • Provider
          • Token
        • Authorization
          • Voter
        • Factory
        • Firewall
        • User
      • Session
    • KmlBundle
      • Element
    • ManagerBundle
      • Controller
      • Form
        • DataTransformer
        • Type
    • MonitoringBundle
      • Command
      • Component
      • Controller
      • DependencyInjection
      • Entity
      • EventListener
      • Form
    • PrintBundle
      • Component
      • Controller
    • WmcBundle
      • Component
        • Exception
      • Element
        • Type
      • Entity
      • Form
        • EventListener
        • Type
    • WmsBundle
      • Component
        • Exception
      • Controller
      • DependencyInjection
      • Element
        • Type
      • Entity
      • Event
      • Form
        • EventListener
        • Type
    • WmtsBundle
      • Component
        • Exception
      • Controller
      • Entity
      • Form
        • Type
  • None
  • PHP

Classes

  • ApplicationController
  • GroupController
  • ProxyController
  • TranslationController
  • WelcomeController
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download
  1: <?php
  2: 
  3: /**
  4:  * TODO: License
  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:  * Application controller.
 20:  *
 21:  * @author Christian Wygoda
 22:  */
 23: class ApplicationController extends Controller {
 24:     /**
 25:      * Get runtime URLs
 26:      *
 27:      * @param string $slug
 28:      * @return array
 29:      */
 30:     private function getUrls($slug) {
 31:         $base_url = $this->get('request')->getBaseUrl();
 32:         $element_url = $this->get('router')
 33:             ->generate('mapbender_core_application_element',
 34:                        array('slug' => $slug));
 35:         $translation_url = $this->get('router')
 36:             ->generate('mapbender_core_translation_trans');
 37:         $proxy_url = $this->get('router')
 38:             ->generate('owsproxy3_core_owsproxy_entrypoint');
 39: 
 40:         // hack to get proper urls when embedded in drupal
 41:         $drupal_mark = function_exists('mapbender_menu') ? '?q=mapbender' : 'mapbender';
 42:         $base_url = str_replace('mapbender', $drupal_mark, $base_url);
 43:         $element_url = str_replace('mapbender', $drupal_mark, $element_url);
 44:         $translation_url = str_replace('mapbender', $drupal_mark, $translation_url);
 45:         $proxy_url = str_replace('mapbender', $drupal_mark, $proxy_url);
 46: 
 47:         return array(
 48:             'base' => $base_url,
 49:             // @TODO: Can this be done less hack-ish?
 50:             'asset' => rtrim($this->get('templating.helper.assets')
 51:                 ->getUrl('.'), '.'),
 52:             'element' => $element_url,
 53:             'trans' => $translation_url,
 54:             'proxy' => $proxy_url);
 55:     }
 56: 
 57:     /**
 58:      * Asset controller.
 59:      *
 60:      * Dumps the assets for the given application and type. These are up to
 61:      * date and this controller will be used during development mode.
 62:      *
 63:      * @Route("/application/{slug}/assets/{type}")
 64:      */
 65:     public function assetsAction($slug, $type) {
 66:         $response = new Response();
 67:         $application = $this->getApplication($slug);
 68:         $assets = $application->getAssets($type);
 69:         $asset_modification_time = new \DateTime();
 70:         $asset_modification_time->setTimestamp($assets->getLastModified());
 71: 
 72:         // @TODO: Make filters part of the bundle configuration
 73:         // @TODO: I'd like to have source maps support in here for easier
 74:         //      debugging of minified code, see
 75:         //      http://www.thecssninja.com/javascript/source-mapping
 76:         $filters = array(
 77:             'js' => array(),
 78:             'css' => array($this->container->get('assetic.filter.cssrewrite')));
 79: 
 80:         // Set target path for CSS rewrite to work
 81:         // Replace backward slashes (Windows paths) with forward slashes...
 82:         $path = $this->get('request')->server->get('PATH_INFO');
 83:         if(!$path) {
 84:             $path = $this->get('request')->server->get('REQUEST_URI');
 85:         }
 86: 
 87:         $target = str_replace('\\', '/', $this->get('request')->server->get('SCRIPT_FILENAME')
 88:             . $path);
 89: 
 90:         $mimetypes = array(
 91:             'css' => 'text/css',
 92:             'js' => 'application/javascript');
 93: 
 94:         $application_update_time = new \DateTime();
 95:         $application_entity = $this->getApplication($slug)->getEntity();
 96: 
 97:         // Determine last-modified timestamp for both DB- and YAML-based apps
 98:         if($application->getEntity()->getSource() === ApplicationEntity::SOURCE_DB) {
 99:             $updateTime = max($application->getEntity()->getUpdated(),
100:                 $asset_modification_time);
101:         } else {
102:             $cacheUpdateTime = new \DateTime($this->container->getParameter('mapbender.cache_creation'));
103:             $updateTime = max($cacheUpdateTime, $asset_modification_time);
104:         }
105: 
106:         $response->setLastModified($updateTime);
107:         if($response->isNotModified($this->get('request'))) {
108:             return $response;
109:         }
110: 
111:         // @TODO: I'd rather use $assets->dump, but that clones each asset
112:         // which assigns a new weird targetPath. Gotta check that some time.
113:         $parts = array();
114:         foreach($assets->all() as $asset) {
115:             foreach($filters[$type] as $filter) {
116:                 $asset->ensureFilter($filter);
117:             }
118:             $asset->setTargetPath($target);
119:             $parts[] = $asset->dump();
120:         }
121: 
122: 
123:         $response->headers->set('Content-Type', $mimetypes[$type]);
124:         $response->setContent(implode("\n", $parts));
125:         return $response;
126:     }
127: 
128:     /**
129:      * Element action controller.
130:      *
131:      * Passes the request to the element's httpAction.
132:      * @Route("/application/{slug}/element/{id}/{action}",
133:      *     defaults={ "id" = null, "action" = null },
134:      *     requirements={ "action" = ".+" })
135:      */
136:     public function elementAction($slug, $id, $action) {
137:         $element = $this->getApplication($slug)->getElement($id);
138: 
139:         //$this->checkAllowedRoles($element->getRoles());
140: 
141:         return $element->httpAction($action);
142:     }
143: 
144:     /**
145:      * Main application controller.
146:      *
147:      * @Route("/application/{slug}.{_format}", defaults={ "_format" = "html" })
148:      * @Template()
149:      */
150:     public function applicationAction($slug) {
151:         $application = $this->getApplication($slug);
152: 
153:         // At this point, we are allowed to acces the application. In order
154:         // to use the proxy in following request, we have to mark the session
155:         $this->get("session")->set("proxyAllowed",true);
156: 
157:         return new Response($application->render());
158:     }
159: 
160:     /**
161:      * Get the application by slug.
162:      *
163:      * Tries to get the application with the given slug and throws an 404
164:      * exception if it can not be found. This also checks access control and
165:      * therefore may throw an AuthorizationException.
166:      *
167:      * @return Mapbender\CoreBundle\Component\Application
168:      */
169:     private function getApplication($slug) {
170:         $application = $this->get('mapbender')
171:             ->getApplication($slug, $this->getUrls($slug));
172: 
173:         if($application === null) {
174:             throw new NotFoundHttpException(
175:                 'The application can not be found.');
176:         }
177: 
178:         $this->checkApplicationAccess($application);
179: 
180:         return $application;
181:     }
182: 
183:     /**
184:      * Check access permissions for given application.
185:      *
186:      * This will check if any ACE in the ACL for the given applications entity
187:      * grants the VIEW permission.
188:      *
189:      * @param Application $application
190:      */
191:     public function checkApplicationAccess(Application $application) {
192:         $securityContext = $this->get('security.context');
193: 
194:         $application_entity = $application->getEntity();
195:         if($application_entity::SOURCE_YAML === $application_entity->getSource()
196:                 && count($application_entity->yaml_roles)) {
197:             $passed = false;
198:             foreach($application_entity->yaml_roles as $role) {
199:                 if($securityContext->isGranted($role)) {
200:                     $passed = true;
201:                     break;
202:                 }
203:             }
204:             if(!$passed) {
205:                 throw new AccessDeniedException('You are not granted view permissions for this application.');
206:             }
207:         }
208: 
209:         $granted = $securityContext->isGranted('VIEW', $application_entity);
210:         if(false === $granted) {
211:             throw new AccessDeniedException('You are not granted view permissions for this application.');
212:         }
213: 
214:         if(!$application_entity->isPublished() and !$securityContext->isGranted('EDIT', $application_entity)) {
215:             throw new AccessDeniedException('This application is not published at the moment');
216:         }
217:     }
218: }
219: 
Mapbender3 API documenation API documentation generated by ApiGen 2.8.0