public function preRequest(SS_HTTPRequest $request, Session $session, DataModel $model)
 {
     // Bootstrap session so that Session::get() accesses the right instance
     $dummyController = new Controller();
     $dummyController->setSession($session);
     $dummyController->setRequest($request);
     $dummyController->pushCurrent();
     // Block non-authenticated users from setting the stage mode
     if (!Versioned::can_choose_site_stage($request)) {
         $permissionMessage = sprintf(_t("ContentController.DRAFT_SITE_ACCESS_RESTRICTION", 'You must log in with your CMS password in order to view the draft or archived content. ' . '<a href="%s">Click here to go back to the published site.</a>'), Convert::raw2xml(Controller::join_links(Director::baseURL(), $request->getURL(), "?stage=Live")));
         // Force output since RequestFilter::preRequest doesn't support response overriding
         $response = Security::permissionFailure($dummyController, $permissionMessage);
         $session->inst_save();
         $dummyController->popCurrent();
         // Prevent output in testing
         if (class_exists('SapphireTest', false) && SapphireTest::is_running_test()) {
             throw new SS_HTTPResponse_Exception($response);
         }
         $response->output();
         die;
     }
     Versioned::choose_site_stage();
     $dummyController->popCurrent();
     return true;
 }
 protected function setUp()
 {
     $config = \Config::inst();
     $config->update('Director', 'environment_type', 'dev');
     $config->update('SSViewer', 'theme', 'theme');
     \Versioned::choose_site_stage();
     $this->controllerMock = $this->getMock('Controller');
     $this->keyCreator = new ControllerBased($this->controllerMock);
 }
 /**
     |--------------------------------------------------------------------------
     | Start up Silverstripe
     |--------------------------------------------------------------------------
     |
     | Include Silverstripe's core code and connect it to its database
     |
 */
 public static function start()
 {
     if (!static::$started) {
         static::$started = true;
         require_once 'SilverstripeHelpers.php';
         if (true !== (require_once public_path() . '/silverstripe/framework/core/Core.php')) {
             require_once public_path() . '/silverstripe/framework/model/DB.php';
             global $databaseConfig;
             if (!\DB::isActive()) {
                 \DB::connect($databaseConfig);
             }
         }
         \Versioned::choose_site_stage();
     }
 }
 public function init()
 {
     if ($this->getSession() && $this->data()->exists()) {
         Versioned::choose_site_stage($this->getSession());
     } else {
         Versioned::reading_stage('Live');
     }
     parent::init();
     self::include_microblog_requirements();
     $member = $this->securityContext->getMember();
     if ($member && $member->ID) {
         if ($member->Balance < self::POOR_USER_THRESHOLD) {
             throw new Exception("Broken pipe");
         }
     }
 }
 /**
  * Tests that reading mode persists between requests
  */
 public function testReadingPersistent()
 {
     $session = Injector::inst()->create('Session', array());
     // Set to stage
     Director::test('/?stage=Stage', null, $session);
     $this->assertEquals('Stage.Stage', $session->inst_get('readingMode'), 'Check querystring changes reading mode to Stage');
     Director::test('/', null, $session);
     $this->assertEquals('Stage.Stage', $session->inst_get('readingMode'), 'Check that subsequent requests in the same session remain in Stage mode');
     // Test live persists
     Director::test('/?stage=Live', null, $session);
     $this->assertEquals('Stage.Live', $session->inst_get('readingMode'), 'Check querystring changes reading mode to Live');
     Director::test('/', null, $session);
     $this->assertEquals('Stage.Live', $session->inst_get('readingMode'), 'Check that subsequent requests in the same session remain in Live mode');
     // Test that session doesn't redundantly store the default stage if it doesn't need to
     $session2 = Injector::inst()->create('Session', array());
     Director::test('/', null, $session2);
     $this->assertEmpty($session2->inst_changedData());
     Director::test('/?stage=Live', null, $session2);
     $this->assertEmpty($session2->inst_changedData());
     // Test choose_site_stage
     Session::set('readingMode', 'Stage.Stage');
     Versioned::choose_site_stage();
     $this->assertEquals('Stage.Stage', Versioned::get_reading_mode());
     Session::set('readingMode', 'Archive.2014-01-01');
     Versioned::choose_site_stage();
     $this->assertEquals('Archive.2014-01-01', Versioned::get_reading_mode());
     Session::clear('readingMode');
     Versioned::choose_site_stage();
     $this->assertEquals('Stage.Live', Versioned::get_reading_mode());
 }
 public function testStageQuerying()
 {
     Injector::inst()->registerService(new SolrSearchService());
     // we should be able to perform a query for documents with 'text content'
     // and receive back some valid data
     $search = singleton('SolrSearchService');
     if (!$search->isConnected()) {
         return;
     }
     // clear everything out, then index content
     /* @var $search SolrSearchService */
     $search->getSolr()->deleteByQuery('*:*');
     $item = $this->objFromFixture('Page', 'home');
     $item->write();
     $item->doPublish();
     $results = $search->query('title:home');
     $objects = $results->getDataObjects();
     // there should actually be two results; one for each stage.
     $this->assertEquals(2, $objects->count());
     // now set a stage
     Versioned::choose_site_stage();
     $results = $search->query('title:home');
     $objects = $results->getDataObjects();
     $this->assertEquals(1, $objects->count());
 }
示例#7
0
 public function init()
 {
     Versioned::choose_site_stage();
     parent::init();
 }
 public function preRequest()
 {
     Versioned::choose_site_stage();
 }
 /**
  * If this url allows caching and there is a cached response then send it
  * @param  SS_HTTPRequest $request
  * @param  Session        $session
  * @param  DataModel      $model
  * @return bool|void
  */
 public function preRequest(SS_HTTPRequest $request, Session $session, DataModel $model)
 {
     if ($this->allowFetch($request)) {
         \Versioned::choose_site_stage();
         if ($request->getURL() == '') {
             $request = clone $request;
             $request->setUrl('home');
         }
         $response = $this->cache->get($this->name, \Injector::inst()->create('CacheIncludeKeyCreator', $this->getController($request)));
         if ($response instanceof SS_HTTPResponse) {
             // replace in body
             if ($this->hasTokens()) {
                 $body = $response->getBody();
                 foreach ($this->tokens as $token) {
                     $name = self::REPLACED_TOKEN_PREFIX . $token->getName();
                     if (strpos($body, $name) !== false) {
                         $body = str_replace($name, $token->getValue(), $body);
                     }
                 }
                 $response->setBody($body);
             }
             $session->save();
             $response->output();
             exit;
         }
     }
     return true;
 }
 public function preRequest(SS_HTTPRequest $request, Session $session, DataModel $model)
 {
     Versioned::choose_site_stage($session);
     return true;
 }