Oauth_application_user.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. );
  50. }
  51. public static function getByUserAndToken($user, $token)
  52. {
  53. if (empty($user) || empty($token)) {
  54. return null;
  55. }
  56. $oau = new Oauth_application_user();
  57. $oau->profile_id = $user->id;
  58. $oau->token = $token;
  59. $oau->limit(1);
  60. $result = $oau->find(true);
  61. return empty($result) ? null : $oau;
  62. }
  63. public function updateKeys(&$orig)
  64. {
  65. $this->_connect();
  66. $parts = array();
  67. foreach (array('profile_id', 'application_id', 'token', 'access_type') as $k) {
  68. if (strcmp($this->$k, $orig->$k) != 0) {
  69. $parts[] = $k . ' = ' . $this->_quote($this->$k);
  70. }
  71. }
  72. if (count($parts) == 0) {
  73. // No changes
  74. return true;
  75. }
  76. $toupdate = implode(', ', $parts);
  77. $table = $this->tableName();
  78. $tableName = $this->escapedTableName();
  79. $qry = 'UPDATE ' . $tableName . ' SET ' . $toupdate .
  80. ' WHERE profile_id = ' . $orig->profile_id .
  81. ' AND application_id = ' . $orig->application_id .
  82. " AND token = '" . $orig->token . "'";
  83. $orig->decache();
  84. $result = $this->query($qry);
  85. if ($result) {
  86. $this->encache();
  87. }
  88. return $result;
  89. }
  90. }