git.gnu.io has moved to IP address 209.51.188.249 -- please double check where you are logging in.

Consumer.php 2.77 KB
Newer Older
1 2 3 4
<?php
/**
 * Table Definition for consumer
 */
5
require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6

7
class Consumer extends Managed_DataObject
8 9 10 11 12
{
    ###START_AUTOCODE
    /* the code below is auto generated do not remove the above tag */

    public $__table = 'consumer';                        // table name
13 14
    public $consumer_key;                    // varchar(191)  primary_key not_null   not 255 because utf8mb4 takes more space
    public $consumer_secret;                 // varchar(191)   not_null   not 255 because utf8mb4 takes more space
15
    public $seed;                            // char(32)   not_null
16 17
    public $created;                         // datetime   not_null
    public $modified;                        // timestamp   not_null default_CURRENT_TIMESTAMP
18 19 20

    /* the code above is auto generated do not remove the tag below */
    ###END_AUTOCODE
21

22 23 24 25 26
    public static function schemaDef()
    {
        return array(
            'description' => 'OAuth consumer record',
            'fields' => array(
27 28
                'consumer_key' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'unique identifier, root URL'),
                'consumer_secret' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'secret value'),
29 30 31 32 33 34 35 36 37
                'seed' => array('type' => 'char', 'length' => 32, 'not null' => true, 'description' => 'seed for new tokens by this consumer'),

                'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
                'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
            ),
            'primary key' => array('consumer_key'),
        );
    }

38 39 40
    static function generateNew()
    {
        $cons = new Consumer();
41
        $rand = common_random_hexstr(16);
42 43 44 45 46 47 48 49 50

        $cons->seed            = $rand;
        $cons->consumer_key    = md5(time() + $rand);
        $cons->consumer_secret = md5(md5(time() + time() + $rand));
        $cons->created         = common_sql_now();

        return $cons;
    }

51 52 53 54 55 56
    /**
     * Delete a Consumer and related tokens and nonces
     *
     * XXX: Should this happen in an OAuthDataStore instead?
     *
     */
57
    function delete($useWhere=false)
58 59 60 61 62 63
    {
        // XXX: Is there any reason NOT to do this kind of cleanup?

        $this->_deleteTokens();
        $this->_deleteNonces();

64
        return parent::delete($useWhere);
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    }

    function _deleteTokens()
    {
        $token = new Token();
        $token->consumer_key = $this->consumer_key;
        $token->delete();
    }

    function _deleteNonces()
    {
        $nonce = new Nonce();
        $nonce->consumer_key = $this->consumer_key;
        $nonce->delete();
    }
80
}