[英]Multiple dynamic firewalls and CAS servers in Symfony2
我正在Symfony開發一個應用程序來管理多所學校。 該應用程序有多個數據庫,每個學校一個,以及多個CAS服務器。
如果我只管理一所學校,配置將如下:
# 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
有一所學校一切正常。
每所學校都通過app.com/school/ID路徑訪問該應用程序,例如app.com/school/29,app.com/school/54 ...
我想知道是否有辦法根據ID有多個動態防火牆。 並使用此ID重定向每個CAS URL:
https://cas01.example.com/school_29/login , https://cas01.example.com/school_54/login ...
-----------更新13/12/12 -----------
我創建了一個新文件:app / config / cas.php,我添加了一些CAS服務器設置
# 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',
),
),
));
我在config.yml中導入此文件
imports:
- { resource: parameters.yml }
- { resource: cas.php }
- { resource: security.yml }
我為每所學校添加了一個新的防火牆:
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
一切順利!
現在我正在嘗試從Entity School生成所有cas.php配置動態。 首先,我嘗試在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",
),
),
));
}
}
並在cas.php文件中調用它
<?php
use Comp\BackendBundle\Controller\SchoolController;
SchoolController::loadCasConfig();
但我有這個例外:
FileLoaderLoadException: Cannot import resource
"C:\wamp\www\comp\app/config\cas.php" from
"C:\wamp\www\comp\app/config\config.yml". (Runtime Notice: Non-static method
Comp\BackendBundle\Controller\SchoolController::loadCasConfig() should not be
called statically, assuming $this from incompatible context in C:\wamp\www\comp\app\config\cas.php line 5)
:(。然后我嘗試在cas.php文件中插入方法代碼:
use Doctrine\ORM\EntityManager;
use Comp\SchoolBundle\Entity\School;
$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",
),
),
));
}
現在我有:
FatalErrorException: Error: Call to undefined method
Symfony\Component\DependencyInjection\Loader\PhpFileLoader::getDoctrine() in
C:\wamp\www\comp\app\config\cas.php line 11
我想知道如何動態生成文件cas.php,從數據庫中獲取數據。
代碼示例:
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()不是靜態方法。 創建一個靜態...然后調用。
像這樣:
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();
}
然后(你的cas.php):
<?php
use Comp\BackendBundle\Controller\SchoolController;
SchoolController::loadCasConfigStatic();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.