Oauth_application_user.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. defined('GNUSOCIAL') || die();
  17. require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
  18. /**
  19. * Table Definition for oauth_application_user
  20. */
  21. class Oauth_application_user extends Managed_DataObject
  22. {
  23. ###START_AUTOCODE
  24. /* the code below is auto generated do not remove the above tag */
  25. public $__table = 'oauth_application_user'; // table name
  26. public $profile_id; // int(4) primary_key not_null
  27. public $application_id; // int(4) primary_key not_null
  28. public $access_type; // tinyint(1)
  29. public $token; // varchar(191) not 255 because utf8mb4 takes more space
  30. public $created; // datetime() not_null default_0000-00-00%2000%3A00%3A00
  31. public $modified; // datetime() not_null default_CURRENT_TIMESTAMP
  32. /* the code above is auto generated do not remove the tag below */
  33. ###END_AUTOCODE
  34. public static function schemaDef()
  35. {
  36. return array(
  37. 'fields' => array(
  38. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'user of the application'),
  39. 'application_id' => array('type' => 'int', 'not null' => true, 'description' => 'id of the application'),
  40. 'access_type' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'access type, bit 1 = read, bit 2 = write'),
  41. 'token' => array('type' => 'varchar', 'length' => 191, 'description' => 'request or access token'),
  42. 'created' => array('type' => 'datetime', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'),
  43. 'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'),
  44. ),
  45. 'primary key' => array('profile_id', 'application_id'),
  46. 'foreign keys' => array(
  47. 'oauth_application_user_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  48. 'oauth_application_user_application_id_fkey' => array('oauth_application', array('application_id' => 'id')),
  49. ),
  50. );
  51. }
  52. public static function getByUserAndToken($user, $token)
  53. {
  54. if (empty($user) || empty($token)) {
  55. return null;
  56. }
  57. $oau = new Oauth_application_user();
  58. $oau->profile_id = $user->id;
  59. $oau->token = $token;
  60. $oau->limit(1);
  61. $result = $oau->find(true);
  62. return empty($result) ? null : $oau;
  63. }
  64. public function updateKeys(&$orig)
  65. {
  66. $this->_connect();
  67. $parts = array();
  68. foreach (array('profile_id', 'application_id', 'token', 'access_type') as $k) {
  69. if (strcmp($this->$k, $orig->$k) != 0) {
  70. $parts[] = $k . ' = ' . $this->_quote($this->$k);
  71. }
  72. }
  73. if (count($parts) == 0) {
  74. // No changes
  75. return true;
  76. }
  77. $toupdate = implode(', ', $parts);
  78. $table = $this->tableName();
  79. $tableName = $this->escapedTableName();
  80. $qry = 'UPDATE ' . $tableName . ' SET ' . $toupdate .
  81. ' WHERE profile_id = ' . $orig->profile_id .
  82. ' AND application_id = ' . $orig->application_id .
  83. " AND token = '" . $orig->token . "'";
  84. $orig->decache();
  85. $result = $this->query($qry);
  86. if ($result) {
  87. $this->encache();
  88. }
  89. return $result;
  90. }
  91. }