Skip to content

Instantly share code, notes, and snippets.

@develop7
Created November 6, 2010 23:08
Show Gist options
  • Save develop7/665787 to your computer and use it in GitHub Desktop.
Save develop7/665787 to your computer and use it in GitHub Desktop.
<?php
/**
* dsDynamicCredentialsFilter
*
* This filter is created to add some credentials to user dynamically.
*
* It does following things:
*
* 1. retrieves current route,
* 2. checks whether it is sfObjectRoute
* 3. retrieves an object inside of that sfObjectRoute
* 4. calls $object->getAddedCredentialsNames($user) where $user is instance of myUser
* 5. adds credentials returned by #4 to $user
*
* @author Andrei Dziahel <[email protected]>
*/
class dsDynamicCredentialsFilter extends sfFilter
{
public function execute($filterChain)
{
if ($this->isFirstCall())
{
$context = $this->getContext();
/* @var $user sfGuardSecurityUser */
$user = $context->getUser(); //TODO: fix hardcoded user instance
$routename = $context->getRouting()->getCurrentRouteName();
$routes = $context->getRouting()->getRoutes();
/* @var $route sfObjectRoute */
$route = $routes[$routename];
if ($route instanceof sfObjectRoute && $route->isBound())
{
$object = false;
try
{
$object = $route->getObject();
}
catch (LogicException $e)
{
$object = false;
}
if (false !== $object && method_exists($object, 'getAddedCredentialsNames'))
{
$names = call_user_func(array($object, 'getAddedCredentialsNames'), $user); //TODO: fix hardcoded method name
$user->addCredentials($names);
}
else
{
if (sfConfig::get('sf_logging_enabled'))
{
$context->getEventDispatcher()->notify(new sfEvent($this,
'application.log',
array('Can\'t retrieve object for route "'.$routename.'", skipping')));
}
}
}
}
$filterChain->execute();
}
}
<?php
class Post extends BasePost
{
public function getAddedCredentialsNames(sfGuardSecurityUser $user)
{
if ($this->get('owner_id') == $user->getProfile()->getId())
{
return 'post_edit';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment