User.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * User entity file.
  4. *
  5. * Creates the entity and makes it available.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. require_once(PATH::ENTITY . "Entity.php");
  12. require_once(PATH::ENTITY . "Player.php");
  13. /**
  14. * A User.
  15. *
  16. * Represents an application user.
  17. *
  18. * @category Entity
  19. */
  20. class User extends Entity{
  21. /**
  22. * @var int User ID.
  23. */
  24. private $id;
  25. /**
  26. * @var string User name.
  27. */
  28. private $name;
  29. /**
  30. * @var string User email.
  31. */
  32. private $mail;
  33. /**
  34. * @var string User API key.
  35. */
  36. private $api_key;
  37. /**
  38. * @var bool Indicates if the user is an administrator.
  39. */
  40. private $admin = false;
  41. /**
  42. * @var bool Internal flag to check if the user's accounts have been loaded.
  43. */
  44. private $flag_accounts = false;
  45. /**
  46. * @var Player[] All of the players accounts.
  47. */
  48. private $account = [];
  49. /**
  50. * Constructor.
  51. *
  52. * Searches the database and retrieves the information about the
  53. * user, populating it and it's items.
  54. *
  55. * @param int $id User identifier.
  56. */
  57. public function __construct($id){
  58. $statement = get_context()->get_db()->prepare("
  59. SELECT
  60. id,
  61. name,
  62. mail,
  63. api_key,
  64. admin
  65. FROM user
  66. WHERE id = :id;
  67. ");
  68. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  69. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  70. if ($r){
  71. $this->id = $r["id"];
  72. $this->name = $r["name"];
  73. $this->mail = $r["mail"];
  74. $this->api_key = $r["api_key"];
  75. $this->admin = $r["admin"];
  76. }
  77. }
  78. /**
  79. * Loads the users accounts into the entity.
  80. *
  81. * Must be called only once before trying to get the acoounts. Sets the flag
  82. * {@link User:$flag_accounts} to true.
  83. */
  84. private function load_accounts(){
  85. $statement = get_context()->get_db()->prepare("
  86. SELECT id
  87. FROM player
  88. WHERE user = :user
  89. ORDER BY last_access DESC
  90. ");
  91. $statement->bindValue(':user', $this->id, SQLITE3_TEXT);
  92. $q = $statement->execute();
  93. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  94. array_push($this->account, new Player($r["id"]));
  95. }
  96. $this->flag_accounts = true;
  97. }
  98. /**
  99. * Retrieves the user identifier.
  100. *
  101. * @return int User ID.
  102. */
  103. public function get_id(){
  104. return $this->id;
  105. }
  106. /**
  107. * Retrieves the user name.
  108. *
  109. * @return string User name.
  110. */
  111. public function get_name(){
  112. return $this->name;
  113. }
  114. /**
  115. * Retrieves the user email address.
  116. *
  117. * @return string User mail.
  118. */
  119. public function get_mail(){
  120. return $this->mail;
  121. }
  122. /**
  123. * Retrieves the user's API key.
  124. *
  125. * @return string User's API key.
  126. */
  127. public function get_api_key(){
  128. return $this->api_key;
  129. }
  130. /**
  131. * Checks if the user is a site administrator.
  132. *
  133. * @return bool True if the user is administrator, false otherwise.
  134. */
  135. public function is_admin(){
  136. return $this->admin;
  137. }
  138. /**
  139. * Retrieves the user's accounts, sorted by the last account date in descending order.
  140. *
  141. * @return Player[] The user's accounts.
  142. */
  143. public function get_accounts(){
  144. if (! $this->flag_accounts){
  145. $this->load_accounts();
  146. }
  147. return $this->account;
  148. }
  149. }