Oauth_application_user.php 4.3 KB

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