2011-08-03 73 views
1

我是symfony和php5的新手(我是Java,Spring,Grails开发人员)。如何为symfony2创建自定义用户提供程序?

我正在开发一个项目,它有一个java-middleware和一个symfony 2的php前端。 java中间件存储用户和我的应用程序需要的所有东西。

我不希望symfony2拥有自己的数据库。每个symfony2需要的信息都来自java-middleware通过WSDL和我的php-soap-api,我可以将其包含到我的symfony2项目中。

用户需要在前端登录。所以我必须编写登录和注销功能。 java-middleware提供了一个登录方法,我可以通过php-soap-api在php中调用。

我应该如何在symfony2中实现登录/注销功能?我是否应该实现一个自定义的用户提供商这个php-soap-api?如果是的话,我该怎么做? (http://symfony.com/doc/2.0/cookbook/security/custom_provider.html)不可用。

感谢支持! whitenexx

+0

好奇你为什么选择一个为期6天的,稀疏记录的重大项目框架? –

回答

1

看一看在User Provider Interface文档......我想一个办法就是建立自己的实现接口,这将作为WSDL调用的封装,然后正确设置你的安全上下文(安全。 yml)来使用它。

我有类似的问题,我也想建立自己的用户提供者。

+0

在这里你可以找到一个简单的[代码模板](http://tracehello.wordpress.com/2011/05/08/symfony2-custom-userprovider/) – csparpa

1

是的,您需要创建自己的用户提供程序并将其作为服务添加到Symfony应用程序中。创建必须实现UserProviderInterface

为了让您的类开始这里的用户提供程序类是位于命名空间/包/安全/ Provider.php我的自定义类的一个实例:

namespace CB\WebsiteBundle\Security; 

use Symfony\Component\Security\Core\User\UserInterface; 
use Symfony\Component\Security\Core\User\UserProviderInterface; 
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; 

use CB\WebsiteBundle\Entity\User; 

class Provider implements UserProviderInterface { 

    protected $user; 
    public function __contsruct (UserInterface $user) { 
     $this->user = $user; 
    } 

    /** 
    * Loads the user for the given username. 
    * 
    * This method must throw UsernameNotFoundException if the user is not 
    * found. 
    * 
    * @throws UsernameNotFoundException if the user is not found 
    * @param string $username The username 
    * 
    * @return UserInterface 
    */ 
    function loadUserByUsername($username) { 
     $user = User::find(array('username'=>$username)); 
     if(empty($user)){ 
      throw new UsernameNotFoundException('Could not find user. Sorry!'); 
     } 
     $this->user = $user; 
     return $user; 
    } 

    /** 
    * Refreshes the user for the account interface. 
    * 
    * It is up to the implementation if it decides to reload the user data 
    * from the database, or if it simply merges the passed User into the 
    * identity map of an entity manager. 
    * 
    * @throws UnsupportedUserException if the account is not supported 
    * @param UserInterface $user 
    * 
    * @return UserInterface 
    */ 
    function refreshUser(UserInterface $user) { 
     return $user; 
    } 

    /** 
    * Whether this provider supports the given user class 
    * 
    * @param string $class 
    * 
    * @return Boolean 
    */ 
    function supportsClass($class) { 
     return $class === 'MC\WebsiteBundle\Entity\User'; 
    } 
} 

的一些关键这个类的注意事项是它使用了一个自定义的用户实体,你将自己定义这个实体,这将有助于连接到你的Java中间件。在我的情况下,我连接到REST api来获取我的用户信息,我的用户类使用User :: find($ criteria)静态函数通过用户名查找用户。

一旦你有你自己的用户类,它与你的中间件交互,你有你的新的供应商,你需要添加提供程序作为服务组合中的配置:YourBundle /资源/ config.xml中:

<parameters> 
    <parameter key="cb_security_user.class">CB\WebsiteBundle\Entity\User</parameter> 
    <parameter key="cb_security_provider.class">CB\WebsiteBundle\Security\Provider</parameter> 
</parameters> 

<services> 
    <service id="cb_security_user" class="%cb_security_user.class%" /> 
    <service id="cb_security_provider" class="%cb_security_provider.class%"> 
     <argument type="service" id="cb_security_user" /> 
    </service> 
</services> 

你可以找到我的博客张贴在这里更多的细节:Custom User Providers in Symfony2

希望这有助于!

+0

我跑过这个答案,因为它被标记为可能不合适。克林特通常不认为适合链接到内容(尤其是您自己的内容),而无需进一步阐明信息。你应该删除这个答案(和确切的副本发布[这里](http://stackoverflow.com/questions/8577529/symfony-2-custom-user-provider/8593824#8593824))或在这里总结你的解决方案。 –

+0

谢谢你通知我。我想我以前是这么做的。我会通过我的答案,并确保删除仅链接的内容。 – Clint

+0

@michael - 我编辑了我的答案,这有帮助吗? – Clint

相关问题