以下是我针对Symfony 3.0提出的一个解决方案。
由于我要实现一个已有的PostgreSQL数据库,并且想要重用已有的PHP代码,ORM在我的情况下不适用(反向工程需要太多时间)。因此,我一直在积极寻找如何在没有ORM的情况下使用Symfony。(我已经实现了DBAL,因为我编写的PDO可以与之一起使用……到目前为止……只需要进行一些小修补)。
让我们开始:
我需要理解的重要概念来自于这个页面:http://symfony.com/doc/current/cookbook/security/custom_provider.html以及关于security.yml文件的所有内容。
关于security文件,我花了一段时间才明白“提供程序”节点只有两个本地选项:“entity”或“memory”。而且,使用“entity”时,人们必须使用ORM。因此,我想出了需要开发自己的提供程序,以便使用常规PDO。
这里是我的一部分代码,以便让你了解我最终是如何使其工作的。下面是以下文件:
encoders:
CustomBundle\Entity\CustomUsers:
algorithm: [enter an encoding algorithm, eg: MD5, sha512, etc.]
role_hierarchy:
ROLE_NAME2: ROLE_NAME1
ROLE_NAME3: ROLE_NAME2
providers:
custom_users_provider:
id: custom_users_provider
firewalls:
main_login:
pattern: ^/login$
anonymous: ~
main:
pattern: ^/
anonymous: true
provider: custom_users_provider
form_login:
check_path: /login_check
login_path: /login
provider: custom_users_provider
username_parameter: email
password_parameter: password
logout: true
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/secured_for_role_name, role: ROLE_NAME }
<?php
namespace CustomBundle\Security;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use CustomBundle\DependencyInjection\PdoCustom;
use CustomBundle\Entity\CustomUsers;
class CustomUsersProvider implements UserProviderInterface
{
private $dbalConnection;
private $logger;
public function __construct($dbalConnection){
$this->dbalConnection = $dbalConnection;
////////////////EASTER EGG//////////////////////////
$this->logger = $GLOBALS['kernel']->getContainer()->get('logger');
$logger->info('EASTER EGG: YOU CAN ADD LOG THAT CAN BE FOUND UNDER [projectname]\var\logs\dev.log WHEN LAUNCHING THRU app_dev.php, WICH COULD BE USEFUL TOO');
////////////////////////////////////////////////////
}
public function loadUserByUsername($username)
{
$PdoCustom = new PdoCustom($this->dbalConnection);
$userData = $PdoCustom->getUser($username);
if ($userData) {
$password = $userData->password;
$salt = null;
$role = $userData->role;
$resToReturn = new CustomUsers();
$resToReturn->setCharact($username, $password, $role);
return $resToReturn;
}
throw new UsernameNotFoundException(
sprintf('Username "%s" does not exist.', $username)
);
}
public function refreshUser(UserInterface $user)
{
if (!$user instanceof CustomUsers) {
throw new UnsupportedUserException(
sprintf('Instances of "%s" are not supported.', get_class($user))
);
}
return $this->loadUserByUsername($user->getUsername());
}
public function supportsClass($class)
{
return $class === 'CustomBundle\Entity\CustomUsers';
}
}
?>
custom_bundle.custom_users_provider.class : CustomBundle\Security\CustomUsersProvider
custom_users_provider:
class: %custom_bundle.custom_users_provider.class%
arguments: ["@doctrine.dbal.default_connection"]
<?php
namespace CustomBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\EquatableInterface;
class CustomUsers implements UserInterface, EquatableInterface {
public $email;
public $password;
public $role;
public function __construct(){
}
public function setCharact($email,$password,$role){
$this->email = $email;
$this->password = $password;
$this->status = $status;
}
public function getRoles(){
return $this->role;
}
public function getPassword(){
return $this->password;
}
public function getSalt(){
return null;
}
public function getUsername(){
return $this->email;
}
public function eraseCredentials(){
}
public function isEqualTo(UserInterface $user)
{
if (!$user instanceof CustomUsers) {
return false;
}
if ($this->user_password !== $user->getPassword()) {
return false;
}
//if ($this->salt !== $user->getSalt()) {
// return false;
//}
if ($this->email !== $user->getUsername()) {
return false;
}
return true;
}
}
?>
就是这样,你仍然需要自己做一些工作来弄清楚所有的内容,但我希望这会给你指引。我之前在寻找如何在没有ORM的情况下使用Symfony时非常困难。
http://symfony.com/doc/current/cookbook/security/entity_provider.html
如果您不想使用Doctrine 2对象关系管理器(ORM),可以考虑使用Doctrine 2数据库访问层(DBAL)。这是在PDO之上构建的一个轻量级SQL层。它具有一些用于构建SQL的辅助程序。
http://symfony.com/doc/current/cookbook/doctrine/dbal.html
当然,您可以直接使用PDO:http://symfony.com/doc/current/cookbook/configuration/pdo_session_storage.html