Created
January 5, 2016 15:30
-
-
Save srsbiz/38c8ac58c4774906e80e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AutoLoginServiceProvider implements \Silex\ServiceProviderInterface { | |
public function boot(\Silex\Application $app) { | |
if (!isset($app['security'])) { | |
throw new \LogicException('You must register the SecurityServiceProvider to use the AutoLoginServiceProvider'); | |
} | |
} | |
public function register(\Silex\Application $app) { | |
$app['security.authentication_listener.factory.auto_login'] = $app->protect(function ($name, $options) use ($app) { | |
if (!isset($app['security.authentication_listener.'.$name.'.auto_login'])) { | |
$app['security.authentication_listener.'.$name.'.auto_login'] = $app->share(function() use($app, $options, $name){ | |
return new \Jmikola\AutoLogin\Http\Firewall\AutoLoginListener( | |
$app['security'], | |
$app['security.authentication_manager'], | |
$name, | |
isset($options['request_param']) ? $options['request_param'] : '_token', | |
$app['logger'], | |
$app['dispatcher'], | |
$options | |
); | |
}); | |
} | |
if (!isset($app['security.authentication_provider.'.$name.'.auto_login'])) { | |
$app['security.authentication_provider.'.$name.'.auto_login'] = $app->share(function() use($app, $options, $name){ | |
if (isset($options['user_provider'])) { | |
$provider = $options['user_provider']; | |
} else { | |
$provider = $app['security.firewalls'][$name]['users']; | |
if ($provider instanceof \Closure) { | |
$provider = $provider($app); | |
} | |
} | |
return new \Jmikola\AutoLogin\Authentication\Provider\AutoLoginProvider( | |
$provider, | |
$app['security.user_checker'], | |
$name | |
); | |
}); | |
} | |
return array( | |
'security.authentication_provider.'.$name.'.auto_login', | |
'security.authentication_listener.'.$name.'.auto_login', | |
null, // entry point | |
'pre_auth' | |
); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment