2013-01-08 39 views
5

所以我想創建一個新的Silex應用程序並使用包含的安全捆綁包。爲了簡單起見,我打算使用基本的密碼編碼。如何在Silex中將密碼編碼器附加到用戶界面?

根據Silex文檔我創建了一個自定義用戶提供程序。但是,此用戶界面似乎並未使用默認的密碼編碼。

我能順利拿到密碼進行的

$password = $app['security.encoder.digest']->encodePassword('foo'); 

然而,當我使用的例子

// find the encoder for a UserInterface instance 
$encoder = $app['security.encoder_factory']->getEncoder($user); 

// compute the encoded password for foo 
$password = $encoder->encodePassword('foo', $user->getSalt()); 

我得到的

的RuntimeException:無編碼器已經配置了帳戶

在Symfony2中,我會使用類似如下的

encoders: 
     somename: 
      class: Acme\DemoBundle\Entity\User 
     Acme\DemoBundle\Entity\User: sha512 
     Acme\DemoBundle\Entity\User: plaintext 
     Acme\DemoBundle\Entity\User: 
      algorithm: sha512 
      encode_as_base64: true 
      iterations: 5000 
     Acme\DemoBundle\Entity\User: 
      id: my.custom.encoder.service.id 

但這似乎並不在這裏是如此。我似乎無法找到任何類型的setEncoder方法,所以我有點難住。

回答

5

您需要重建EncoderFactory添加自定義的實現:

<?php 

$app = new Silex\Application(); 
$app['myapp.encoder.base64'] = new Base64PasswordEncoder(); 
$app['security.encoder_factory'] = $app->share(function ($app) { 
    return new EncoderFactory(
     array(
      'Symfony\Component\Security\Core\User\UserInterface' => $app['security.encoder.digest'], 
      'MyApp\Model\UserInterface'       => $app['myapp.encoder.base64'], 
     ) 
    ); 
}); 

(噢,請不要使用密碼Base64Encoder();))

+0

這很有道理。我認爲這是沿着這些線路的某個地方,但是說明所有內容都使用默認設置的文檔。非常感謝你! – fafnirbcrow

1

我能使用接受的答案來解決我的問題,但我不能直接將它分配給security.encoder_factory,所以我只是分享我發現的工作。

代替:

$app['security.encoder_factory'] = $app->share(function($app) { 
    //..see above...// 
}); 

我不得不使用:

$app->register(new Silex\Provider\SecurityServiceProvider(),array(
    'security.encoder_factory' => $app->share(function($app) { 
    //... same as above ...// 
    }) 
)); 

我太新的Silex知道爲什麼這並不像上面爲我工作。我最初的猜測是版本差異(兩年前問過的問題)。我可以在調用註冊模塊之前分配給security.provider.default,但我似乎無法分配到security.encoder_factory。我似乎也必須把security.firewalls寄存器中。

相關問題