Symfony2 - Pommbundle和userInterface

时间:2017-04-25 09:27:02

标签: symfony pomm

我在我的symfony项目中使用pommbundle,我想使用symfony安全用户。 pommbundle可能吗?

我创建了一个实体实现userinterface

USER

namespace AppBundle\Entity\Model\MyDb1\PublicSchema;

use PommProject\ModelManager\Model\FlexibleEntity;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * Clientweb
 *
 * Flexible entity for relation
 * public.ClientWEB
 *
 * @see FlexibleEntity
 */
class Clientweb extends FlexibleEntity implements UserInterface
{

    public function getRoles() {
        return $this->get('Roles');
    }

    public function getPassword() {
        return $this->get('Password');
    }

    public function getSalt() {
        return '';
    }

    public function getUsername() {
        return $this->get('Login');
    }


    public function eraseCredentials() {
        // Ici nous n'avons rien à effacer. 
        // Cela aurait été le cas si nous avions un mot de passe en clair.
    }
}

安全

security:
    encoders:
        Symfony\Component\Security\Core\User\User: plaintext
        AppBundle\Entity\Model\MyDb1\PublicSchema\Clientweb: sha512


    role_hierarchy:
        ROLE_USER:       ROLE_USER
        ROLE_PARTICULIER: ROLE_PARTICULIER
        ROLE_PROFESSIONNEL: ROLE_PROFESSIONNEL
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        in_memory:
            memory:
                users:
                    user:  { password: userpass, roles: [ 'ROLE_USER' ] }
                    particulier: { password: parpass, roles: ['ROLE_PARTICULIER'] }
                    professionnel: { password: propass, roles: ['ROLE_PROFESSIONNEL'] }
                    admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
        principal:
            id: security_userprovider

    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            pattern: ^/
            anonymous: true
            provider:  principal
            form_login:
                login_path: connexion
                check_path: connexion_check
                failure_path: connexion_failure
                default_target_path: compte_client
                always_use_default_target_path: true
            remember_me:
                name: 'CERECARE_SITE'
                secret:   '%secret%'
                lifetime: 604800
                path:     /
            logout:
                path:   deconnexion
                target: homepage

USER PROVIDER

namespace AppBundle\Entity\Provider;
            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 Symfony\Component\Config\Definition\Exception\Exception;

            use PommProject\Foundation\Pomm;
            use PommProject\Foundation\Where;

            use \AppBundle\Entity\Model\MyDb1\PublicSchema\ClientwebModel;

            class UserProvider implements UserProviderInterface {

                private $pomm;

                public function __construct(Pomm $pomm) {
                    $this->pomm = $pomm;
                }

                public function loadUserByUsername($username) {

                    $user = $this->pomm
                                 ->getDefaultSession()
                                 ->getModel(ClientwebModel::class)
                                 ->findbyLogin($username);
                    if($user!=null)
                        {
                            return $user;
                        }
                    else
                        {
                            throw new UsernameNotFoundException(sprintf('Login "%s" does not exist.', $username));
                        }
                }

                public function refreshUser(UserInterface $user) {
                    if (!$user instanceof User) {
                        throw new UnsupportedUserException(
                            sprintf('Instances of "%s" are not supported.', get_class($user))
                        );
                    }

                    return $this->loadUserByUsername($user->getUsername());
                }

                public function supportsClass($class) {
                    return $class === 'AppBundle\Entity\Model\MyDb1\PublicSchema\Clientweb';
                }

            }

    [2017-04-25 14:24:02] security.INFO: Authentication request failed. {"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\BadCredentialsException(code: 0): Bad credentials. at C:\\Program Files (x86)\\PostgreSQL\\EnterpriseDB-ApachePHP\\apache\\www\\SITE_CERECARE\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\Security\\Core\\Authentication\\Provider\\UserAuthenticationProvider.php:90, Symfony\\Component\\Security\\Core\\Exception\\BadCredentialsException(code: 0): The presented password is invalid. at C:\\Program Files (x86)\\PostgreSQL\\EnterpriseDB-ApachePHP\\apache\\www\\SITE_CERECARE\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\Security\\Core\\Authentication\\Provider\\DaoAuthenticationProvider.php:67)"} []

REQUEST


    public function findbyLogin($username)
    {
        // select employee_id, name, … from my_schema.employee where name ~* $1
        $sql = strtr(
            "select {projection} from {relation} where \"Login\" ~* $*",
            [
                '{projection}'  => $this->createProjection(), // expand projection
                '{relation}'    => $this->structure->getRelation(),
            ]
        );

        // ↓ return an iterator on flexible entities
        // ↓ parameters are escaped and converted.
        return $this->query($sql, [$username])->current();
    }

}

0 个答案:

没有答案