IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Zend_Acl & Zend_Auth PHP Discussion :

Authentification sur 3 champs [ZF 1.9]


Sujet :

Zend_Acl & Zend_Auth PHP

  1. #1
    Membre habitué Avatar de guiyomh
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    328
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 328
    Points : 155
    Points
    155
    Par défaut Authentification sur 3 champs
    Bonjour,

    voilà je cherche a faire une authentification sur 3 champs en base.
    • email
    • password
    • actif (bool)


    le champs actif étant à 0 (emil non-confirmé) ou 1 (email-confirmé).

    Je souhaiterais que seul ceux qui ont confirmé leur mail puissent s'identifier.

    Savez vous comment-faire ?
    ---
    "Rien n'est impossible"
    http://journaldeguillaume.blogspot.com

  2. #2
    Membre habitué Avatar de guiyomh
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    328
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 328
    Points : 155
    Points
    155
    Par défaut
    Je pense avoir trouvé un bon moyen pour le faire, j'ai fais ma propre class : My_Auth_Adapter_DbTable
    dont voici le code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    <?php
    /**
     * Zend Framework
     *
     * LICENSE
     *
     * This source file is subject to the new BSD license that is bundled
     * with this package in the file LICENSE.txt.
     * It is also available through the world-wide-web at this URL:
     * http://framework.zend.com/license/new-bsd
     * If you did not receive a copy of the license and are unable to
     * obtain it through the world-wide-web, please send an email
     * to license@zend.com so we can send you a copy immediately.
     *
     * @category   Zend
     * @package    Zend_Auth
     * @subpackage Zend_Auth_Adapter
     * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
     * @license    http://framework.zend.com/license/new-bsd     New BSD License
     * @version    $Id: DbTable.php 16200 2009-06-21 18:50:06Z thomas $
     */
     
     
    /**
     * @see Zend_Auth_Adapter_Interface
     */
    require_once 'Zend/Auth/Adapter/Interface.php';
     
    /**
     * @see Zend_Db_Adapter_Abstract
     */
    require_once 'Zend/Db/Adapter/Abstract.php';
     
    /**
     * @see Zend_Auth_Result
     */
    require_once 'Zend/Auth/Result.php';
     
     
    /**
     * @category   Zend
     * @package    Zend_Auth
     * @subpackage Zend_Auth_Adapter
     * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
     * @license    http://framework.zend.com/license/new-bsd     New BSD License
     */
    class My_Auth_Adapter_DbTable implements Zend_Auth_Adapter_Interface
    {
     
        /**
         * Database Connection
         *
         * @var Zend_Db_Adapter_Abstract
         */
        protected $_zendDb = null;
     
        /**
         * @var Zend_Db_Select
         */
        protected $_dbSelect = null;
     
        /**
         * $_tableName - the table name to check
         *
         * @var string
         */
        protected $_tableName = null;
     
        /**
         * $_identityColumn - the column to use as the identity
         *
         * @var string
         */
        protected $_identityColumn = null;
     
         /**
         * $_actifColumn - the column to use as the actif
         *
         * @var string
         */
        protected $_actifColumn = null;
     
        /**
         * $_credentialColumns - columns to be used as the credentials
         *
         * @var string
         */
        protected $_credentialColumn = null;
     
        /**
         * $_identity - Identity value
         *
         * @var string
         */
        protected $_identity = null;
     
        /**
         * $_actif - Actif value
         *
         * @var string
         */
        protected $_actif = 1;
     
        /**
         * $_credential - Credential values
         *
         * @var string
         */
        protected $_credential = null;
     
        /**
         * $_credentialTreatment - Treatment applied to the credential, such as MD5() or PASSWORD()
         *
         * @var string
         */
        protected $_credentialTreatment = null;
     
        /**
         * $_authenticateResultInfo
         *
         * @var array
         */
        protected $_authenticateResultInfo = null;
     
        /**
         * $_resultRow - Results of database authentication query
         *
         * @var array
         */
        protected $_resultRow = null;
     
        /**
         * __construct() - Sets configuration options
         *
         * @param  Zend_Db_Adapter_Abstract $zendDb
         * @param  string                   $tableName
         * @param  string                   $identityColumn
         * @param  string                   $credentialColumn
         * @param  string                   $credentialTreatment
         * @return void
         */
        public function __construct(Zend_Db_Adapter_Abstract $zendDb, $tableName = null, $identityColumn = null,
                                    $credentialColumn = null, $credentialTreatment = null, $actifColumn = null)
        {
            $this->_zendDb = $zendDb;
     
            if (null !== $tableName) {
                $this->setTableName($tableName);
            }
     
            if (null !== $identityColumn) {
                $this->setIdentityColumn($identityColumn);
            }
     
            if (null !== $credentialColumn) {
                $this->setCredentialColumn($credentialColumn);
            }
     
            if (null !== $credentialTreatment) {
                $this->setCredentialTreatment($credentialTreatment);
            }
            if (null !== $actifColumn) {
                $this->setActifColumn($actifColumn);
            }
        }
     
        /**
         * setTableName() - set the table name to be used in the select query
         *
         * @param  string $tableName
         * @return My_Auth_Adapter_DbTable Provides a fluent interface
         */
        public function setTableName($tableName)
        {
            $this->_tableName = $tableName;
            return $this;
        }
     
        /**
         * setIdentityColumn() - set the column name to be used as the identity column
         *
         * @param  string $identityColumn
         * @return My_Auth_Adapter_DbTable Provides a fluent interface
         */
        public function setIdentityColumn($identityColumn)
        {
            $this->_identityColumn = $identityColumn;
            return $this;
        }
     
        /**
         * setActifColumn() - set the column name to be used as the actif column
         *
         * @param  string $actifColumn
         * @return My_Auth_Adapter_DbTable Provides a fluent interface
         */
        public function setActifColumn($actifColumn)
        {
            $this->_actifColumn = $actifColumn;
            return $this;
        }
     
        /**
         * setCredentialColumn() - set the column name to be used as the credential column
         *
         * @param  string $credentialColumn
         * @return My_Auth_Adapter_DbTable Provides a fluent interface
         */
        public function setCredentialColumn($credentialColumn)
        {
            $this->_credentialColumn = $credentialColumn;
            return $this;
        }
     
        /**
         * setCredentialTreatment() - allows the developer to pass a parameterized string that is
         * used to transform or treat the input credential data
         *
         * In many cases, passwords and other sensitive data are encrypted, hashed, encoded,
         * obscured, or otherwise treated through some function or algorithm. By specifying a
         * parameterized treatment string with this method, a developer may apply arbitrary SQL
         * upon input credential data.
         *
         * Examples:
         *
         *  'PASSWORD(?)'
         *  'MD5(?)'
         *
         * @param  string $treatment
         * @return My_Auth_Adapter_DbTable Provides a fluent interface
         */
        public function setCredentialTreatment($treatment)
        {
            $this->_credentialTreatment = $treatment;
            return $this;
        }
     
        /**
         * setActif() - set the value to be used as the actif
         *
         * @param  string $value
         * @return My_Auth_Adapter_DbTable Provides a fluent interface
         */
        public function setActif($value)
        {
            $this->_actif = $value;
            return $this;
        }
     
        /**
         * setIdentity() - set the value to be used as the identity
         *
         * @param  string $value
         * @return My_Auth_Adapter_DbTable Provides a fluent interface
         */
        public function setIdentity($value)
        {
            $this->_identity = $value;
            return $this;
        }
     
        /**
         * setCredential() - set the credential value to be used, optionally can specify a treatment
         * to be used, should be supplied in parameterized form, such as 'MD5(?)' or 'PASSWORD(?)'
         *
         * @param  string $credential
         * @return My_Auth_Adapter_DbTable Provides a fluent interface
         */
        public function setCredential($credential)
        {
            $this->_credential = $credential;
            return $this;
        }
     
        /**
         * getDbSelect() - Return the preauthentication Db Select object for userland select query modification
         *
         * @return Zend_Db_Select
         */
        public function getDbSelect()
        {
            if ($this->_dbSelect == null) {
                $this->_dbSelect = $this->_zendDb->select();
            }
     
            return $this->_dbSelect;
        }
     
        /**
         * getResultRowObject() - Returns the result row as a stdClass object
         *
         * @param  string|array $returnColumns
         * @param  string|array $omitColumns
         * @return stdClass|boolean
         */
        public function getResultRowObject($returnColumns = null, $omitColumns = null)
        {
            if (!$this->_resultRow) {
                return false;
            }
     
            $returnObject = new stdClass();
     
            if (null !== $returnColumns) {
     
                $availableColumns = array_keys($this->_resultRow);
                foreach ( (array) $returnColumns as $returnColumn) {
                    if (in_array($returnColumn, $availableColumns)) {
                        $returnObject->{$returnColumn} = $this->_resultRow[$returnColumn];
                    }
                }
                return $returnObject;
     
            } elseif (null !== $omitColumns) {
     
                $omitColumns = (array) $omitColumns;
                foreach ($this->_resultRow as $resultColumn => $resultValue) {
                    if (!in_array($resultColumn, $omitColumns)) {
                        $returnObject->{$resultColumn} = $resultValue;
                    }
                }
                return $returnObject;
     
            } else {
     
                foreach ($this->_resultRow as $resultColumn => $resultValue) {
                    $returnObject->{$resultColumn} = $resultValue;
                }
                return $returnObject;
     
            }
        }
     
        /**
         * authenticate() - defined by Zend_Auth_Adapter_Interface.  This method is called to
         * attempt an authenication.  Previous to this call, this adapter would have already
         * been configured with all nessissary information to successfully connect to a database
         * table and attempt to find a record matching the provided identity.
         *
         * @throws Zend_Auth_Adapter_Exception if answering the authentication query is impossible
         * @return Zend_Auth_Result
         */
        public function authenticate()
        {
            $this->_authenticateSetup();
            $dbSelect = $this->_authenticateCreateSelect();
            $resultIdentities = $this->_authenticateQuerySelect($dbSelect);
     
            if ( ($authResult = $this->_authenticateValidateResultset($resultIdentities)) instanceof Zend_Auth_Result) {
                return $authResult;
            }
     
            $authResult = $this->_authenticateValidateResult(array_shift($resultIdentities));
            return $authResult;
        }
     
        /**
         * _authenticateSetup() - This method abstracts the steps involved with making sure
         * that this adapter was indeed setup properly with all required peices of information.
         *
         * @throws Zend_Auth_Adapter_Exception - in the event that setup was not done properly
         * @return true
         */
        protected function _authenticateSetup()
        {
            $exception = null;
     
            if ($this->_tableName == '') {
                $exception = 'A table must be supplied for the My_Auth_Adapter_DbTable authentication adapter.';
            } elseif ($this->_identityColumn == '') {
                $exception = 'An identity column must be supplied for the My_Auth_Adapter_DbTable authentication adapter.';
            } elseif ($this->_credentialColumn == '') {
                $exception = 'A credential column must be supplied for the My_Auth_Adapter_DbTable authentication adapter.';
            } elseif ($this->_identity == '') {
                $exception = 'A value for the identity was not provided prior to authentication with My_Auth_Adapter_DbTable.';
            } elseif ($this->_credential === null) {
                $exception = 'A credential value was not provided prior to authentication with My_Auth_Adapter_DbTable.';
            }
     
            if (null !== $exception) {
                /**
                 * @see Zend_Auth_Adapter_Exception
                 */
                require_once 'Zend/Auth/Adapter/Exception.php';
                throw new Zend_Auth_Adapter_Exception($exception);
            }
     
            $this->_authenticateResultInfo = array(
                'code'     => Zend_Auth_Result::FAILURE,
                'identity' => $this->_identity,
                'messages' => array()
                );
     
            return true;
        }
     
        /**
         * _authenticateCreateSelect() - This method creates a Zend_Db_Select object that
         * is completely configured to be queried against the database.
         *
         * @return Zend_Db_Select
         */
        protected function _authenticateCreateSelect()
        {
            // build credential expression
            if (empty($this->_credentialTreatment) || (strpos($this->_credentialTreatment, '?') === false)) {
                $this->_credentialTreatment = '?';
            }
     
            if(null == $this->_actifColumn)
            {
                $credentialExpression = new Zend_Db_Expr(
                '(CASE WHEN ' .
                $this->_zendDb->quoteInto(
                    $this->_zendDb->quoteIdentifier($this->_credentialColumn, true)
                    . ' = ' . $this->_credentialTreatment, $this->_credential
                    )
                . ' THEN 1 ELSE 0 END) AS '
                . $this->_zendDb->quoteIdentifier('zend_auth_credential_match')
                );
            }
            else{
                $credentialExpression = new Zend_Db_Expr(
                '(CASE WHEN ' .
                $this->_zendDb->quoteInto(
                    $this->_zendDb->quoteIdentifier($this->_credentialColumn, true)
                    . ' = ' . $this->_credentialTreatment, $this->_credential
                    ) .
                ' AND '.
                $this->_zendDb->quoteInto(
                    $this->_zendDb->quoteIdentifier($this->_actifColumn, true)
                    . ' = ?' ,$this->_actif
                    ) 
                . ' THEN 1 ELSE 0 END) AS '
                . $this->_zendDb->quoteIdentifier('zend_auth_credential_match')
                );
            }
     
            // get select
            $dbSelect = clone $this->getDbSelect();
            $dbSelect->from($this->_tableName, array('*', $credentialExpression))
                     ->where($this->_zendDb->quoteIdentifier($this->_identityColumn, true) . ' = ?', $this->_identity);
     
            return $dbSelect;
        }
     
        /**
         * _authenticateQuerySelect() - This method accepts a Zend_Db_Select object and
         * performs a query against the database with that object.
         *
         * @param Zend_Db_Select $dbSelect
         * @throws Zend_Auth_Adapter_Exception - when a invalid select object is encoutered
         * @return array
         */
        protected function _authenticateQuerySelect(Zend_Db_Select $dbSelect)
        {
            try {
                if ($this->_zendDb->getFetchMode() != Zend_DB::FETCH_ASSOC) {
                    $origDbFetchMode = $this->_zendDb->getFetchMode();
                    $this->_zendDb->setFetchMode(Zend_DB::FETCH_ASSOC);
                }
                $resultIdentities = $this->_zendDb->fetchAll($dbSelect->__toString());
                if (isset($origDbFetchMode)) {
                    $this->_zendDb->setFetchMode($origDbFetchMode);
                    unset($origDbFetchMode);
                }
            } catch (Exception $e) {
                /**
                 * @see Zend_Auth_Adapter_Exception
                 */
                require_once 'Zend/Auth/Adapter/Exception.php';
                throw new Zend_Auth_Adapter_Exception('The supplied parameters to My_Auth_Adapter_DbTable failed to '
                                                    . 'produce a valid sql statement, please check table and column names '
                                                    . 'for validity.');
            }
            return $resultIdentities;
        }
     
        /**
         * _authenticateValidateResultSet() - This method attempts to make certian that only one
         * record was returned in the result set
         *
         * @param array $resultIdentities
         * @return true|Zend_Auth_Result
         */
        protected function _authenticateValidateResultSet(array $resultIdentities)
        {
     
     
            if (count($resultIdentities) < 1) {
                $this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
                $this->_authenticateResultInfo['messages'][] = 'A record with the supplied identity could not be found.';
                return $this->_authenticateCreateAuthResult();
            } elseif (count($resultIdentities) > 1) {
                $this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS;
                $this->_authenticateResultInfo['messages'][] = 'More than one record matches the supplied identity.';
                return $this->_authenticateCreateAuthResult();
            }
     
            return true;
        }
     
        /**
         * _authenticateValidateResult() - This method attempts to validate that the record in the
         * result set is indeed a record that matched the identity provided to this adapter.
         *
         * @param array $resultIdentity
         * @return Zend_Auth_Result
         */
        protected function _authenticateValidateResult($resultIdentity)
        {
            if ($resultIdentity['zend_auth_credential_match'] != '1') {
                $this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
                $this->_authenticateResultInfo['messages'][] = 'Supplied credential is invalid.';
                return $this->_authenticateCreateAuthResult();
            }
     
            unset($resultIdentity['zend_auth_credential_match']);
            $this->_resultRow = $resultIdentity;
     
            $this->_authenticateResultInfo['code'] = Zend_Auth_Result::SUCCESS;
            $this->_authenticateResultInfo['messages'][] = 'Authentication successful.';
            return $this->_authenticateCreateAuthResult();
        }
     
        /**
         * _authenticateCreateAuthResult() - This method creates a Zend_Auth_Result object
         * from the information that has been collected during the authenticate() attempt.
         *
         * @return Zend_Auth_Result
         */
        protected function _authenticateCreateAuthResult()
        {
            return new Zend_Auth_Result(
                $this->_authenticateResultInfo['code'],
                $this->_authenticateResultInfo['identity'],
                $this->_authenticateResultInfo['messages']
                );
        }
     
    }
    Elle s'utilise comme celle de zend en mode standard,
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    $db = Zend_Registry::get('dbAdapter');
            $dbAdapter = new My_Auth_Adapter_DbTable($db,'budget_user','email','password','MD5(?)');
            $dbAdapter->setCredential($params['password'])->setIdentity($params['email']);
    ou alors pour avoir un 3ème champs (activation) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    $db = Zend_Registry::get('dbAdapter');
            $dbAdapter = new My_Auth_Adapter_DbTable($db,'budget_user','email','password','MD5(?)','actif')->setActif(1);
            $dbAdapter->setCredential($params['password'])->setIdentity($params['email']);
    Le contructeur prend un champs en plus (facultatif) pour spécifié le nom de votre 3ème column à vérifier.
    Et vous pouvez appeler la méthode ->setActif(1) pour précisé la valeur que dois prendre cette nouvelle colonne. (Par défault elle est définit à 1)

    Je sais pas si c'est bien mais ça marche nickel.
    ---
    "Rien n'est impossible"
    http://journaldeguillaume.blogspot.com

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Recherche d'un mot avec LIKE sur plusieurs champs
    Par reynhart dans le forum Langage SQL
    Réponses: 16
    Dernier message: 26/11/2004, 17h41
  2. [CR] Groupement dynamique sur plusieurs champs paramètrés
    Par CDRIK dans le forum SAP Crystal Reports
    Réponses: 8
    Dernier message: 07/06/2004, 17h55
  3. recuperer les id sur plusieurs champs
    Par matN59 dans le forum Langage SQL
    Réponses: 4
    Dernier message: 15/03/2004, 10h23
  4. [CR] Problème de sélection sur un champ date
    Par noluc dans le forum SAP Crystal Reports
    Réponses: 2
    Dernier message: 21/11/2003, 16h56
  5. [VB6] Zone de liste modifiable basée sur un champ
    Par pepper dans le forum VB 6 et antérieur
    Réponses: 13
    Dernier message: 27/11/2002, 13h41

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo