Oauth_application_user.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Table Definition for oauth_application_user
  4. */
  5. require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
  6. class Oauth_application_user extends Managed_DataObject
  7. {
  8. ###START_AUTOCODE
  9. /* the code below is auto generated do not remove the above tag */
  10. public $__table = 'oauth_application_user'; // table name
  11. public $profile_id; // int(4) primary_key not_null
  12. public $application_id; // int(4) primary_key not_null
  13. public $access_type; // tinyint(1)
  14. public $token; // varchar(191) not 255 because utf8mb4 takes more space
  15. public $created; // datetime not_null
  16. public $modified; // timestamp not_null default_CURRENT_TIMESTAMP
  17. /* the code above is auto generated do not remove the tag below */
  18. ###END_AUTOCODE
  19. public static function schemaDef()
  20. {
  21. return array(
  22. 'fields' => array(
  23. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'user of the application'),
  24. 'application_id' => array('type' => 'int', 'not null' => true, 'description' => 'id of the application'),
  25. 'access_type' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'access type, bit 1 = read, bit 2 = write'),
  26. 'token' => array('type' => 'varchar', 'length' => 191, 'description' => 'request or access token'),
  27. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  28. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  29. ),
  30. 'primary key' => array('profile_id', 'application_id'),
  31. 'foreign keys' => array(
  32. 'oauth_application_user_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  33. 'oauth_application_user_application_id_fkey' => array('oauth_application', array('application_id' => 'id')),
  34. ),
  35. );
  36. }
  37. static function getByUserAndToken($user, $token)
  38. {
  39. if (empty($user) || empty($token)) {
  40. return null;
  41. }
  42. $oau = new Oauth_application_user();
  43. $oau->profile_id = $user->id;
  44. $oau->token = $token;
  45. $oau->limit(1);
  46. $result = $oau->find(true);
  47. return empty($result) ? null : $oau;
  48. }
  49. function updateKeys(&$orig)
  50. {
  51. $this->_connect();
  52. $parts = array();
  53. foreach (array('profile_id', 'application_id', 'token', 'access_type') as $k) {
  54. if (strcmp($this->$k, $orig->$k) != 0) {
  55. $parts[] = $k . ' = ' . $this->_quote($this->$k);
  56. }
  57. }
  58. if (count($parts) == 0) {
  59. // No changes
  60. return true;
  61. }
  62. $toupdate = implode(', ', $parts);
  63. $table = $this->tableName();
  64. if(common_config('db','quote_identifiers')) {
  65. $table = '"' . $table . '"';
  66. }
  67. $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
  68. ' WHERE profile_id = ' . $orig->profile_id
  69. . ' AND application_id = ' . $orig->application_id
  70. . " AND token = '$orig->token'";
  71. $orig->decache();
  72. $result = $this->query($qry);
  73. if ($result) {
  74. $this->encache();
  75. }
  76. return $result;
  77. }
  78. }