<?php

class Pampa_Acl_Cms extends Zend_Controller_Plugin_Abstract {

  public function preDispatch(Zend_Controller_Request_Abstract $request) {
    $phpSessId = $request->getParam('PHPSESSID');
    if (!empty($phpSessId) && session_id() != $phpSessId) {
      $session_id = session_id();
      if(!empty($session_id)){
        session_destroy();
      }
      session_id($phpSessId);
      session_start();
    }

    // the auth plugin
    $auth = Pampa_Auth_Cms::getInstance();

    // the current url
    $url = $request->getControllerName() . "/" . $request->getActionName();

    // the 'auth free' urls
    $skip_urls = array("cms_login/index", "cms_login/login");

    // if the user doesnt have identity and the url is not free
    if (!$auth->hasIdentity() && !in_array($url, $skip_urls)) {
      $request->setModuleName("cms");
      $request->setControllerName("cms_login");
      $request->setActionName("index");
      $this->setRequest($request);
    }
  }

}