2016-04-25 6 views
3

Vor Symfony 2.8 SimplePreAuthenticatorInterface war im folgenden Namespace halten müssen Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface Es in 2,8 und geändert in 3,0-Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterfaceWeg für eine Authenticator-Klasse, die nach hinten compability Pause

jetzt ein Bündel Ich schreibe veraltet sind die muss entweder in symfony 2.7 und symfony 3.0 funktionieren, es ist ein api authenticator bundle für den privaten Gebrauch.

Ich möchte eine Fabrik für sie schreiben, die

Beispiel von FosUserBundle

if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) { 
    $tokenStorage = $this->get('security.token_storage'); 
} else { 
    $tokenStorage = $this->get('security.context'); 
} 

aber meine Klasse

Schnittstelle existance überprüft, die diese Schnittstelle ein Dienst in DI und symfony-Firewall diese direkt verwendet, ist implementiert.

Meine Frage ist, wie könnte ich diese Abstraktion in einer Art und Weise Best Practice und logisch machen.

AccessTokenAuthenticator Klasse:

<?php 

namespace MyCompany\SBundle\Security; 
// this usage for before symfony 2.8 
use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface; 

/** 
* Class AccessTokenAuthenticator 
*/ 
class AccessTokenAuthenticator implements SimplePreAuthenticatorInterface 
{ 

services.yml

# Authentication 
mycompany_security.security.accesstoken_authenticator: 
    class:  MyCompany\SBundle\Security\AccessTokenAuthenticator 
    arguments: ["@mycompany_security.security.accesstoken_userprovider"] 

Firewall-Konfiguration:

secure_area: 
    pattern: ^/ 
    stateless: true 
    simple_preauth: 
     authenticator: mycompany_security.security.accesstoken_authenticator 

Mein genaue Problem ist, auch wenn ich zwei Klassen definieren, die einander identisch sind, aber der implementation namespace anders, auch wie könnte ich das dem firew geben alle ? Wie kann dies von der Firewall abstrahiert werden?

Jede Hilfe wäre willkommen.

Antwort

0

OK ich die Antwort finden,

Wenn ich die Service-Container und security.yml Firewall-Konfigurationen konzentrieren. Ich vergesse nur, dass meine Firewall-Konfiguration von Symfony2 und Symfony3-Anwendung abweichen kann.

Auf diese Weise kann ich würde ein BaseAccessTokenAuthenticator und AccessTokenAuthenticator für entweder PreSymfony28 und Symfony30 definieren und dann werde ich die Logik alle in BaseAccessTokenAuthenticator setzen und AccessTokenAuthenticator wäre Mehrfach- und gäbe es zwei verschiedene Service sein. Eine für symfony2.7 Firewall Konfiguration eine für symfony3.0.

Basisklasse:

<?php 

namespace MyCompany\SBundle\Security; 

/** 
* Abstract Class BaseAccessTokenAuthenticator 
*/ 
abstract class BaseAccessTokenAuthenticator 
{ 
    public function createToken(Request $request, $providerKey) 
    { 
     // Implementation 
    } 

    public function authenticateToken(
     TokenInterface $token, 
     UserProviderInterface $userProvider, 
     $providerKey 
    ) { 
     // Implementation. 
    } 

    public function supportsToken(TokenInterface $token, $providerKey) 
    { 
     // Implementation. 
    } 
} 

Symfony 3.0-Klasse:

<?php 

namespace MyCompany\SBundle\Security; 

use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface; 

/** 
* Class AccessTokenAuthenticatorSf30 
*/ 
class AccessTokenAuthenticatorSf30 extends BaseAccessTokenAuthenticator implements SimplePreAuthenticatorInterface 
{ 
} 

Pre Symfony 2.8 Klasse:

<?php 

namespace MyCompany\SBundle\Security; 

use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface; 

/** 
* Class AccessTokenAuthenticatorPreSf28 
*/ 
class AccessTokenAuthenticatorPreSf28 extends BaseAccessTokenAuthenticator implements SimplePreAuthenticatorInterface 
{ 
} 

Dienstleistungen:

# Authentication 
mycompany.security.accesstoken_authenticator_pre_sf28: 
    class:  MyCompany\SBundle\Security\AccessTokenAuthenticatorPreSf28 
    arguments: ["@mycompany.security.accesstoken_userprovider"] 

# Authentication 
mycompany.security.accesstoken_authenticator_sf30: 
    class:  MyCompany\SBundle\Security\AccessTokenAuthenticatorSf30 
    arguments: ["@mycompany.security.accesstoken_userprovider"] 

breite Anwendungs-Firewall für symfony = < 2.8:

simple_preauth: 
     authenticator: mycompany.security.accesstoken_authenticator_sf28 

breite Anwendungs-Firewall für symfony> = 2.8:

simple_preauth: 
     authenticator: mycompany.security.accesstoken_authenticator_sf30 
0

Dies ist wahrscheinlich nicht PSR-kompatibel, funktioniert aber (wahrscheinlich).

Datei AccessTokenAuthenticator.php

if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) { 

    class AccessTokenAuthenticator implements \Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface { } 

} else { 
     class AccessTokenAuthenticator implements \Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface { } 
} 

Autoloading sollte es holen noch auf.

Verwandte Themen