我正在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,从数据库中获取数据。
答案 0 :(得分:2)
代码示例:
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');
}
}
答案 1 :(得分:1)
我们遇到过类似的问题,当多个网站使用一个平台时,我们就为它做了解决方法,现在每个网站都有自己的security.yml
导入主security.yml
答案 2 :(得分:1)
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();