Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Comp\BackendBundle\Controller\SchoolController;

SchoolController::loadCasConfig();

but i have this Exception:

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)

:(. Then i try to insert the method code in the cas.php file:

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

    ));

}

and now i have:

FatalErrorException: Error: Call to undefined method 
Symfony\Component\DependencyInjection\Loader\PhpFileLoader::getDoctrine() in 
C:\wamp\www\comp\app\config\cas.php line 11

I'd like to know how I can dynamically generate the file cas.php, getting data from the database.

like image 660
JGrinon Avatar asked Dec 11 '13 13:12

JGrinon


1 Answers

Code Example:

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');
    }
}
like image 95
Oleg Andreyev Avatar answered Sep 28 '22 18:09

Oleg Andreyev