Zend Framework Source Code Snippets

AuthDbStorage

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
117
118
119
120121
122
123
124
125126
127
128
129
130131
132
133
<?php
 
class AuthDbStorage implements Zend_Auth_Storage_Interface
{
        private static $_session = 'session';        
        /**
         * Fields marked as required are the ones you must have in database table named 
         *
         * @var unknown_type         */
        private static $_requiredFieldUserId = 'uid';
        private static $_requiredFieldSessionId = 'sid';
        private static $_requiredFieldHostname = 'hostname';
        private static $_requiredFieldAccessed = 'accessed';        // Extra field to store some info (use serialize)
        private static $_requiredFieldExtra = 'extra';
        
        
        // Zend_Auth Adapter fields        private static $_authAdapterIdentityColumn;
        private static $_authAdapterTableName;
        private static $_timeToExpire = 120;
        
        private static $_cookieName;        
        private $_db;
        
        public function __construct(Zend_Db_Adapter_Abstract $adapter, $cookieName, $authAdapterTableName, $authAdapterIdentityColumn, $timeToExpire = null)
        {                $this->_db = $adapter;
                self::$_cookieName = $cookieName;
                self::$_authAdapterTableName = $authAdapterTableName;
                self::$_authAdapterIdentityColumn = $authAdapterIdentityColumn;
                self::$_timeToExpire = $timeToExpire;        }
        
    /**
     * Returns true if and only if storage is empty
     *     * @throws Zend_Auth_Storage_Exception If it is impossible to determine whether storage is empty
     * @return boolean
     */
    public function isEmpty()
    {                $result = $this->_db->fetchRow('SELECT * FROM '.self::$_session.' WHERE '.self::$_requiredFieldSessionId.' = ?',  array($_COOKIE[self::$_cookieName]));
                
                if(empty($result)) { return true; }
        return false;
    } 
    /**
     * Returns the contents of storage
     *
     * Behavior is undefined when storage is empty.     *
     * @throws Zend_Auth_Storage_Exception If reading contents from storage is impossible
     * @return mixed
     */
    public function read()    {           
        $result = $this->_db->fetchRow('SELECT * FROM '.self::$_session.' WHERE '.self::$_requiredFieldSessionId.' = ?', array($_COOKIE[self::$_cookieName]));
                
        if(time() - $result[self::$_requiredFieldAccessed] > self::$_timeToExpire)
                {                        $this->clear();
                        return null;
                }
                //if (is_null($result)) { throw new Zend_Auth_Storage_Exception(); }
                return $result;             }
 
    /**
     * Writes $contents to storage
     *     * @param  mixed $contents
     * @throws Zend_Auth_Storage_Exception If writing $contents to storage is impossible
     * @return void
     */
    public function write($contents, $extraContents = array())    {           
        if(is_array($contents))
        {
                $userId = $contents[self::$_requiredFieldUserId];       
        }        
        else 
        {
                $userId = $this->_db->fetchOne('SELECT * FROM '.self::$_authAdapterTableName.' WHERE '.self::$_authAdapterIdentityColumn.' = ?', array($contents));     
        }                        
                $fields = array(
                        self::$_requiredFieldUserId     => $userId,
                        self::$_requiredFieldSessionId  => $_COOKIE[self::$_cookieName],
                        self::$_requiredFieldHostname   => $_SERVER['REMOTE_ADDR'],                        self::$_requiredFieldAccessed   => time()
                );
        
                if(!empty($extraContents))
                {                        $fields[self::$_requiredFieldExtra] = serialize($extraContents); 
                }
                
                // Before write, delete all session info
                $this->clear();                
                // Check to see if session has expired
 
                // Now write new one
                $result = $this->_db->insert(self::$_session, $fields);                
        if($result == 0) { throw new Zend_Auth_Storage_Exception(); }
 
    }
     /**
     * Clears contents from storage
     *
     * @throws Zend_Auth_Storage_Exception If clearing contents from storage is impossible
     * @return void     */
    public function clear()
    {   
                try 
                {                        $this->_db->query('DELETE FROM '.self::$_session.' WHERE '.self::$_requiredFieldSessionId.' = ?', array($_COOKIE[self::$_cookieName]));
                }
                catch (Exception $ex)
                {
                        throw new Zend_Auth_Storage_Exception();                }
    }
}

Comments

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

Snippet description

Storage class for Zend_Auth implemented using Zend_Auth_Storage_Interface.

By default Zend_Auth class uses session to persist login state. This class uses Database.

Check the class definition to see what fields in database table are required.

Snippet details

Created:
branko branko
1 year ago
Edited:
branko branko
1 year ago
Revision Id:
35
Edit Message:
Initial Release
Tags:
auth storage custom
Comments:
0
Views:
112
Points:
-1 (1 votes)

History

r35

Initial Release

branko branko
1 year ago