This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » legacy support » symfony 1.2 » sfGuard cache problem
sfGuard cache problem [message #102318] Sun, 11 July 2010 13:06 Go to next message
Schnaaf  is currently offline Schnaaf
Messages: 89
Registered: June 2006
Member
I have a problem with a cached secured module.

I have a 'sign in' link in my menu. If the user clicks on this link the login page is displayed and the credentials can be entered. When the user is logged in the menu is expanded with links to secured modules which can only be accessed by logged in users. For performance reasons these secured modules are cached. This all works fine.

The problem occures at the moment that a user is logged in and leaves the page open for a period of time. During this period of time the session and the cache both expire but the links to secured modules are still being displayed because the user has not clicked on a link. When a user clicks on a secured link in this situation the login form is displayed and at the same time a new cache file is generated containing the login form. This results in a login form being displayed again (the cached version) even if the user has logged in successful.

Is there a way I can enforce secured pages NOT being cached if the user is not autheticated?
[SOLVED] Re: sfGuard cache problem [message #102380 is a reply to message #102318 ] Mon, 12 July 2010 18:35 Go to previous message
Schnaaf  is currently offline Schnaaf
Messages: 89
Registered: June 2006
Member
To answer my own post:

apps/frontend/config/filters.yml
...
conditionalCache:
  class: conditionalCacheFilter
  param:
    pages_not_to_cache_when_not_authenticated:
      - { module: wedstrijdoverzicht, action: list }
    pages_not_to_cache_when_authenticated:
      - { module: wedstrijdformulier, action: showPermalink }
...


apps/frontend/lib/conditionalCacheFilter.class.php
<?php

class conditionalCacheFilter extends sfFilter
{
  public function execute($filterChain)
  {
  	$context = $this->getContext();
  	$cache = true;
    foreach ($this->getParameter('pages_not_to_cache_when_not_authenticated') as $page)
    {
      if($page['module'] == $this->getContext()->getRequest()->getParameter('module') && 
         $page['action'] == $this->getContext()->getRequest()->getParameter('action') &&
         !$context->getUser()->isAuthenticated()) {
      	$cache = false;
      } elseif(sfConfig::get('sf_cache')) {
      	$context->getViewCacheManager()->addCache($page['module'], $page['action'], array('lifeTime' => 86400));
      }
    }
    
    foreach ($this->getParameter('pages_not_to_cache_when_authenticated') as $page)
    {
      if($page['module'] == $this->getContext()->getRequest()->getParameter('module') && 
         $page['action'] == $this->getContext()->getRequest()->getParameter('action') &&
         $context->getUser()->isAuthenticated()) {
      	$cache = false;
      } elseif(sfConfig::get('sf_cache')) {
      	$context->getViewCacheManager()->addCache($page['module'], $page['action'], array('lifeTime' => 86400));
      }
    }
    
  	if(!$cache) {
  	  sfConfig::set('sf_cache', false);
  	}
    $filterChain->execute();
  }
} 


Previous Topic:app.yml parameters in Tasks
Next Topic:$request->getFiles($form->getName()) is empty
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software