Zend Framework Source Code Snippets

Auth Adapter Config

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
8081
82
83
84
8586
87
88
89
9091
92
93
94
9596
97
98
99
100101
102
103
104
105106
107
108
109
110111
112
113
114
115116
<?php
class Dropper_Auth_Adapter_Config implements Zend_Auth_Adapter_Interface
{
        protected $_credential;
        protected $_identity;        protected $_credentialField;
        protected $_identityField;
        
        /**
         * Global config         *
         * @var Zend_Config
         */
        protected $_config;
                public function __construct(Zend_Config $config, $credentialField, $identityField)
        {
                $this->_config = $config;
                $this->setCredentialField($credentialField)
                        ->setIdentityField($identityField);        }
        
        /**
         * Set credential field
         *         * @param string $credentialField
         * @return Dropper_Auth_Adapter_Config
         */
        public function setCredentialField($credentialField)
        {                $this->_credentialField = $credentialField;
                return $this;
        }
        
        /**         * Set identity field
         *
         * @param string $identityField
         * @return Dropper_Auth_Adapter_Config
         */        public function setIdentityField($identityField)
        {
                $this->_identityField = $identityField;
                return $this;
        }        
        /**
         * Set credential value
         *
         * @param string $credentia         * @return Dropper_Auth_Adapter_Config
         */
        public function setCredential($credential)
        {
                $this->_credential = $credential;                return $this;   
        }
        
        /**
         * Set identity value         *
         * @param string $identity
         * @return Dropper_Auth_Adapter_Config
         */
        public function setIdentity($identity)        {
                $this->_identity = $identity;
                return $this;
        }
                /**
         * Authenticate process
         *
         * @return Zend_Auth_Result
         */        public function authenticate()
        {
                $crField = explode(".", $this->_credentialField);
                $idField = explode(".", $this->_identityField);
                $tmpConfig = $this->_config;                foreach($crField as $part) {
                        $tmpConfig = $tmpConfig->get($part);
                        if (!$tmpConfig instanceof Zend_Config) {
                                $credential = $tmpConfig;
                        }                }
                
                
                $tmpConfig = $this->_config;
                foreach($idField as $part) {                        $tmpConfig = $tmpConfig->get($part);
                        if (!$tmpConfig instanceof Zend_Config) {
                                $identity = $tmpConfig;
                        }
                } 
                if (!$credential || !$identity) {
                        throw new Zend_Auth_Adapter_Exception("Credential and Identity fields shouldn't be empty");
                }
                                $result = array(
                        'code' => Zend_Auth_Result::FAILURE,
                        'messages' => array('Invalid identity or credential')
                );
                                if ($this->_identity == $identity && $this->_credential ==md5($credential)) {
                        $result = array(
                                'code' => Zend_Auth_Result::SUCCESS,
                                'messages' => array('Identity and credential are valid')
                        );                }
                
                return new Zend_Auth_Result($result['code'], $this->_identity, $result['messages']);
        }
}?>
1
2
3
4
56
7
8
9
1011
12
13
/**
 *@example
 */
<?php
    try {                                        $authAdapter = new Dropper_Auth_Adapter_Config($this->_config, "user.password", "user.login");
                                        $authAdapter->setIdentity($form->getValue('username'))
                                                                ->setCredential($form->getValue('password'));
                                        $result = Zend_Auth::getInstance()->authenticate($authAdapter);
                                } catch (Zend_Auth_Adapter_Exception $e) {                                        //exception handling
                                }
?>

Comments

rusk rusk
1 year ago

Alternative variant is not throwing an exception, just return failure when fields are empty.

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

Adapter allows you authorization via config.

Snippet details

Created:
rusk rusk
1 year ago
Edited:
rusk rusk
1 year ago
Revision Id:
95
Edit Message:
Initial Release
Tags:
auth config
Comments:
1
Views:
100
Points:
0 (0 votes)

History

r95

Initial Release

rusk rusk
1 year ago