Zend FR

Consultez la FAQ sur le ZF avant de poster une question

Vous n'êtes pas identifié.

#1 23-08-2012 15:36:42

beweed
Nouveau membre
Date d'inscription: 23-08-2012
Messages: 2

Zend\Auth : controller + adapter

Bonjour tout le monde,

Je suis à la recherche d'un tutoriel/exemple pour la construction d'un module d'authentification simple.
En particulier le controller (loginAction) et l'utilisation de la librairie zend Auth.

Mon environnement : zend 2 + doctrine 2

Le formulaire et la récup des données (login+password) c'est ok ; mais l'utilisation de Zend Auth me fait galérer..



J'ai déjà regardé du côté des modules ACL existants, mais ils sont difficiles à assimiler et à ré-adapter à sa sauce.
Quand à la documentation zf2.readthedocs, c'est trop vague ...

Bref j'espère que vous pourrez m'éclairer.


merci de votre aide!



EDIT:
Dans ma recherche j'ai trouvé une piste de réflexion :
"Zend\Authentication: The ZF1 style database adapter could be re-created with findOneBy(), using the Entity name instead of directly specifying the database table/column."

Dernière modification par beweed (23-08-2012 16:45:17)

Hors ligne

 

#2 25-08-2012 14:51:17

Orkin
Administrateur
Lieu: Paris
Date d'inscription: 09-12-2011
Messages: 1261

Re: Zend\Auth : controller + adapter

Salut, tu peux d'inspirer ou utiliser le module ZfcUser : https://github.com/ZF-Commons/ZfcUser

Hors ligne

 

#3 27-08-2012 12:55:30

beweed
Nouveau membre
Date d'inscription: 23-08-2012
Messages: 2

Re: Zend\Auth : controller + adapter

@Orkin : Merci de ta réponse. Donc j'avais déjà regardé un peu.
Mais c'est le bazar avec Doctrine il utilise le xml. Ici je cherchais vraiment la solution la plus "simple" de se connecter.

J'ai résolu mon problème. Voici comment :



1 - J'ai un module nommé "User"

2 - Une entity "User" définie ainsi :

Code:

namespace User\Entity;

use Doctrine\ORM\Mapping as ORM;
    

/**
 * @ORM\Entity
 * @ORM\Table(name="membre")
 * @property string $username
 * @property string $password
 * @property int $id
 */
class User
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer");
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $username;

    /**
     * @ORM\Column(type="string")
     */
    protected $password;

    /**
     * Magic getter to expose protected properties.
     *
     * @param string $property
     * @return mixed
     */
    public function __get($property) 
    {
        return $this->$property;
    }

    /**
     * Magic setter to save protected properties.
     *
     * @param string $property
     * @param mixed $value
     */
    public function __set($property, $value) 
    {
        $this->$property = $value;
    }  
    
}

3 - Un controller nommé "UserController", défini ainsi :

Code:

namespace User\Controller;

use Zend\Mvc\Controller\AbstractActionController,
    Zend\View\Model\ViewModel,   
    Zend\Authentication\AuthenticationService,
    Zend\Authentication\Adapter\AdapterInterface,
        
    Doctrine\ORM\EntityManager,
    DoctrineModule\Authentication\Adapter\DoctrineObjectRepository as DoctrineAdapter,
        
    User\Entity\User,  
    User\Form\UserForm;

class UserController extends AbstractActionController 
{
    
    /**
     * @var Doctrine\ORM\EntityManager
     */
    protected $em;
    
    public function setEntityManager(EntityManager $em)
    {
        $this->em = $em;
    }
 
    public function getEntityManager()
    {
        if (null === $this->em)
            $this->em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        return $this->em;        
    } 
    
    public function getRepository()
    {
        if (null === $this->em) 
            $this->em = $this->getEntityManager()->getRepository('User\Entity\User');
        return $this->em;
    }
    

    public function indexAction()
    {    
        
        $form = new UserForm();
        return array('form' => $form);
    }
    

    public function loginAction()
    {
        
        $form = new UserForm();
        $user = new User();
        $form->get('submit')->setAttribute('label', 'Add');
        $request = $this->getRequest();
        if ($request->isPost()) {

            $username=$_POST['username'];
            $password=md5($_POST['password']);                

            $adapter = new DoctrineAdapter();
            
            $adapter->setIdentityValue($username);
            $adapter->setCredentialValue($password);
            
            $adapter->setOptions(array(
                'objectManager'=>$this->getEntityManager(),
                'identityClass'=>'User\Entity\User',
                'identityProperty'=>'username',
                'credentialProperty'=>'password'
            ));
            
            $auth = new AuthenticationService();
            
            $result=$auth->authenticate($adapter);
            return array('result'=>$result);
        }
    }
}

4 - Mon fichier module.config.php

Code:

namespace User;

return array(
    'controllers' => array(
        'invokables' => array(
            'User\Controller\User' => 'User\Controller\UserController',
        ),
    ), 
    
    'router' => array(
        'routes' => array(
            'user' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/user[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'User\Controller\User',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),
    
    'view_manager' => array(
        'template_path_stack' => array(
            'user' => __DIR__ . '/../view',
        ),
    ),
    
    'doctrine' => array(
        
        'driver' => array(
            __NAMESPACE__ . '_driver' => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
            ),
            'orm_default' => array(
                'drivers' => array(
                    __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
                )
            )          
        ),

Ici nous avons donc une authentification simple avec Doctrine 2 ORM
J'espère que ça pourra aider quelqu'un d'autre que moi big_smile

A+

Hors ligne

 

Pied de page des forums

Propulsé par PunBB
© Copyright 2002–2005 Rickard Andersson
Traduction par punbb.fr

Graphisme réalisé par l'agence Rodolphe Eveilleau
Développement par Kitpages