Multiple dynamic firewalls and CAS servers in Symfony2
I am developing an application in Symfony to manage multiple schools. The application has multiple databases, one for each school, and multiple CAS servers.
If I only manage a school, the configuration would be like this:
# config.yml
be_simple_sso_auth:
admin_sso:
protocol:
id: cas
version: 2
server:
id: cas
login_url: https://cas01.example.com/SCHOOLID/login
logout_url: https://cas01.example.com/SCHOOL_ID/logout
validation_url: https://cas01.example.com/SCHOOL_ID/serviceValidate
# security.yml
firewalls:
school:
pattern: ^/school/.*$
trusted_sso:
manager: admin_sso
login_action: false
logout_action: false
create_users: true
created_users_roles: [ROLE_USER, ROLE_ADMIN]
login_path: /school/login
check_path: /school/login_check
logout:
path: /school/logout
target: /school
With one school everything works fine.
Each school accesses the application through the path app.com/school/ID, for example app.com/school/29, app.com/school/54...
I wonder if there is way to have multiple dynamic firewall depending on the ID. And use this ID to redirect each CAS URL:
https://cas01.example.com/school_29/login, https://cas01.example.com/school_54/login ...
----------- UPDATED 13/12/12 -----------
I created a new file: app/config/cas.php, and I've added some CAS servers settings
# CAS 14
$container->loadFromExtension('be_simple_sso_auth', array(
'cas_14' => array(
'protocol' => array(
'id' => 'cas',
'version' => '2'
),
'server' => array(
'id' => 'cas',
'login_url' => 'https://cas01.example.com/14/login',
'logout_url' => 'https://cas01.example.com/14/logout',
'validation_url' => 'https://cas01.example.com/14/serviceValidate',
),
),
));
# CAS 15
$container->loadFromExtension('be_simple_sso_auth', array(
'cas_15' => array(
'protocol' => array(
'id' => 'cas',
'version' => '2'
),
'server' => array(
'id' => 'cas',
'login_url' => 'https://cas01.example.com/15/login',
'logout_url' => 'https://cas01.example.com/15/logout',
'validation_url' => 'https://cas01.example.com/15/serviceValidate',
),
),
));
And i import this file in config.yml
imports:
- { resource: parameters.yml }
- { resource: cas.php }
- { resource: security.yml }
And i add a new firewall for each school:
firewalls:
backend_14:
pattern: ^/backend/school/14/.*$
trusted_sso:
manager: cas_14
login_action: false #BeSimpleSsoAuthBundle:TrustedSso:login
logout_action: false #BeSimpleSsoAuthBundle:TrustedSso:logout
create_users: true
created_users_roles: [ROLE_USER, ROLE_ADMIN]
login_path: /backend/school/14/login
check_path: /backend/school/14/login_check
logout:
path: /backend/school/logout
target: /backend
backend_15:
pattern: ^/backend/school/15/.*$
trusted_sso:
manager: cas_15
login_action: false #BeSimpleSsoAuthBundle:TrustedSso:login
logout_action: false #BeSimpleSsoAuthBundle:TrustedSso:logout
create_users: true
created_users_roles: [ROLE_USER, ROLE_ADMIN]
login_path: /backend/school/15/login
check_path: /backend/school/15/login_check
logout:
path: /backend/school/logout
target: /backend
And all goes right!
Now I'm trying to generate all cas.php configuration dynamic from the Entity School. First i try creating a method in SchoolController
public function loadCasConfig()
{
$em = $this->getDoctrine()->getManager();
$schools= $em->getRepository('SchoolBundle:School')
->findBy(array(), array('name'=> 'ASC'));
foreach ($schools as $school) {
$cas_name = 'cas_'.$school->getId();
$container->loadFromExtension('be_simple_sso_auth', array(
"$cas_name" => array(
'protocol' => array(
'id' => 'cas',
'version' => '2'
),
'server' => array(
'id' => 'cas',
'login_url' => "https://cas01.example.com/$school->getId()/login",
'logout_url' => "https://cas01.example.com/$school->getId()/logout",
'validation_url' => "https://cas01.example.com/$school->getId()/serviceValidate",
),
),
));
}
}
and call it on cas.php file
<?php
use CompBackendBundleControllerSchoolController;
SchoolController::loadCasConfig();
but i have this Exception:
FileLoaderLoadException: Cannot import resource
"C:wampwwwcompapp/configcas.php" from
"C:wampwwwcompapp/configconfig.yml". (Runtime Notice: Non-static method
CompBackendBundleControllerSchoolController::loadCasConfig() should not be
called statically, assuming $this from incompatible context in C:wampwwwcompappconfigcas.php line 5)
:(. Then i try to insert the method code in the cas.php file:
use DoctrineORMEntityManager;
use CompSchoolBundleEntitySchool;
$em = $this->getDoctrine()->getManager();
$schools= $em->getRepository('SchoolBundle:School')
->findBy(array(), array('name'=> 'ASC'));
foreach ($schools as $school) {
$cas_name = 'cas_'.$school->getId();
$container->loadFromExtension('be_simple_sso_auth', array(
"$cas_name" => array(
'protocol' => array(
'id' => 'cas',
'version' => '2'
),
'server' => array(
'id' => 'cas',
'login_url' => "https://cas01.example.com/$school->getId()/login",
'logout_url' => "https://cas01.example.com/$school->getId()/logout",
'validation_url' => "https://cas01.example.com/$school->getId()/serviceValidate",
),
),
));
}
and now i have:
FatalErrorException: Error: Call to undefined method
SymfonyComponentDependencyInjectionLoaderPhpFileLoader::getDoctrine() in
C:wampwwwcompappconfigcas.php line 11
I'd like to know how I can dynamically generate the file cas.php, getting data from the database.
代码示例:
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/sites/' . $this->_activeSite . '/config.yml');
$configPath = __DIR__ . '/config/sites/' . $this->_activeSite . '/config_' . $this->getEnvironment() . '.yml';
if (file_exists($configPath)) {
$loader->load($configPath);
}
$loader->load(__DIR__.'/config/servers/' . $this->getServer() . '.yml');
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
//per-site overrides
if (file_exists(__DIR__.'/config/sites/local_' . $this->_activeSite . '.yml')) {
$loader->load(__DIR__.'/config/sites/local_' . $this->_activeSite . '.yml');
}
}
我们有类似的问题,当一个平台被多个网站使用时,所以我们有解决方法,现在每个网站都有它自己的security.yml
,它导入主security.yml
public function loadCasConfig() is not a static method. create a static..then call.
Like this:
public function loadCasConfig()
{
$em = $this->getDoctrine()->getManager();
$schools= $em->getRepository('SchoolBundle:School')
->findBy(array(), array('name'=> 'ASC'));
foreach ($schools as $school) {
$cas_name = 'cas_'.$school->getId();
$container->loadFromExtension('be_simple_sso_auth', array(
"$cas_name" => array(
'protocol' => array(
'id' => 'cas',
'version' => '2'
),
'server' => array(
'id' => 'cas',
'login_url' => "https://cas01.example.com/$school->getId()/login",
'logout_url' => "https://cas01.example.com/$school->getId()/logout",
'validation_url' => "https://cas01.example.com/$school->getId()/serviceValidate",
),
),
));
}
}
public static function loadCasConfigStatic(){
$my = new static;
return $my->loadCasConfig();
}
Then (your cas.php):
<?php
use CompBackendBundleControllerSchoolController;
SchoolController::loadCasConfigStatic();
链接地址: http://www.djcxy.com/p/76064.html