Symfony2中有多个动态防火墙和CAS服务器

我正在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网址:

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 CompBackendBundleControllerSchoolController;

SchoolController::loadCasConfig();

但我有这个例外:

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)

:(。然后我尝试在cas.php文件中插入方法代码:

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",
            ),
        ),

    ));

}

现在我有:

FatalErrorException: Error: Call to undefined method 
SymfonyComponentDependencyInjectionLoaderPhpFileLoader::getDoctrine() in 
C:wampwwwcompappconfigcas.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


公共函数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 CompBackendBundleControllerSchoolController;

SchoolController::loadCasConfigStatic();
链接地址: http://www.djcxy.com/p/76063.html

上一篇: Multiple dynamic firewalls and CAS servers in Symfony2

下一篇: Get number of times keyword is present