Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple dynamic firewalls and CAS servers in Symfony2 #54

Open
JavierGrinon opened this issue Dec 17, 2013 · 0 comments
Open

Multiple dynamic firewalls and CAS servers in Symfony2 #54

JavierGrinon opened this issue Dec 17, 2013 · 0 comments

Comments

@JavierGrinon
Copy link

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.XXX.com/SCHOOLID/login
            logout_url: https://cas01.XXX.com/SCHOOL_ID/logout
            validation_url: https://cas01.XXX.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.XXX.com/school_29/login, https://cas01.XXX.com/school_54/login ...

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.XXX.com/14/login',
            'logout_url' => 'https://cas01.XXX.com/14/logout',
            'validation_url' => 'https://cas01.XXX.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.XXX.com/15/login',
            'logout_url' => 'https://cas01.XXX.com/15/logout',
            'validation_url' => 'https://cas01.XXX.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.XXX.com/$school->getId()/login",
                    'logout_url' => "https://cas01.XXX.com/$school->getId()/logout",
                    'validation_url' => "https://cas01.XXX.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.XXX.com/$school->getId()/login",
                'logout_url' => "https://cas01.XXX.com/$school->getId()/logout",
                'validation_url' => "https://cas01.XXX.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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant