Game server and admin dashboard for TerranQuest.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

User.lib.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /*
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. */
  7. class User {
  8. private $uid = null;
  9. private $username;
  10. private $email;
  11. private $realname;
  12. private $has2fa = false;
  13. private $exists = false;
  14. public function __construct(int $uid, string $username = "") {
  15. // Check if user exists
  16. $resp = AccountHubApi::get("userexists", ["uid" => $uid]);
  17. if ($resp['status'] == "OK" && $resp['exists'] === true) {
  18. $this->exists = true;
  19. } else {
  20. $this->uid = $uid;
  21. $this->username = $username;
  22. $this->exists = false;
  23. }
  24. if ($this->exists) {
  25. // Get user info
  26. $resp = AccountHubApi::get("userinfo", ["uid" => $uid]);
  27. if ($resp['status'] == "OK") {
  28. $this->uid = $resp['data']['uid'] * 1;
  29. $this->username = $resp['data']['username'];
  30. $this->email = $resp['data']['email'];
  31. $this->realname = $resp['data']['name'];
  32. } else {
  33. sendError("Login server error: " . $resp['msg']);
  34. }
  35. }
  36. }
  37. public static function byUsername(string $username): User {
  38. $resp = AccountHubApi::get("userinfo", ["username" => $username]);
  39. if (!isset($resp['status'])) {
  40. sendError("Login server error: " . $resp);
  41. }
  42. if ($resp['status'] == "OK") {
  43. return new self($resp['data']['uid'] * 1);
  44. } else {
  45. return new self(-1, $username);
  46. }
  47. }
  48. public function exists(): bool {
  49. return $this->exists;
  50. }
  51. public function has2fa(): bool {
  52. if (!$this->exists) {
  53. return false;
  54. }
  55. $resp = AccountHubApi::get("hastotp", ['username' => $this->username]);
  56. if ($resp['status'] == "OK") {
  57. return $resp['otp'] == true;
  58. } else {
  59. return false;
  60. }
  61. }
  62. function getUsername() {
  63. return $this->username;
  64. }
  65. function getUID() {
  66. return $this->uid;
  67. }
  68. function getEmail() {
  69. return $this->email;
  70. }
  71. function getName() {
  72. return $this->realname;
  73. }
  74. /**
  75. * Check the given plaintext password against the stored hash.
  76. * @param string $password
  77. * @return bool
  78. */
  79. function checkPassword(string $password): bool {
  80. $resp = AccountHubApi::get("auth", ['username' => $this->username, 'password' => $password]);
  81. if ($resp['status'] == "OK") {
  82. return true;
  83. } else {
  84. return false;
  85. }
  86. }
  87. function check2fa(string $code): bool {
  88. if (!$this->has2fa) {
  89. return true;
  90. }
  91. $resp = AccountHubApi::get("verifytotp", ['username' => $this->username, 'code' => $code]);
  92. if ($resp['status'] == "OK") {
  93. return $resp['valid'];
  94. } else {
  95. return false;
  96. }
  97. }
  98. /**
  99. * Check if the given username has the given permission (or admin access)
  100. * @global $database $database
  101. * @param string $code
  102. * @return boolean TRUE if the user has the permission (or admin access), else FALSE
  103. */
  104. function hasPermission(string $code): bool {
  105. $resp = AccountHubApi::get("permission", ['username' => $this->username, 'code' => $code]);
  106. if ($resp['status'] == "OK") {
  107. return $resp['has_permission'];
  108. } else {
  109. return false;
  110. }
  111. }
  112. /**
  113. * Get the account status.
  114. * @return \AccountStatus
  115. */
  116. function getStatus(): AccountStatus {
  117. $resp = AccountHubApi::get("acctstatus", ['username' => $this->username]);
  118. if ($resp['status'] == "OK") {
  119. return AccountStatus::fromString($resp['account']);
  120. } else {
  121. return null;
  122. }
  123. }
  124. function sendAlertEmail(string $appname = SITE_TITLE) {
  125. $resp = AccountHubApi::get("alertemail", ['username' => $this->username, 'appname' => SITE_TITLE]);
  126. if ($resp['status'] == "OK") {
  127. return true;
  128. } else {
  129. return $resp['msg'];
  130. }
  131. }
  132. }
  133. class AccountStatus {
  134. const NORMAL = 1;
  135. const LOCKED_OR_DISABLED = 2;
  136. const CHANGE_PASSWORD = 3;
  137. const TERMINATED = 4;
  138. const ALERT_ON_ACCESS = 5;
  139. private $status;
  140. public function __construct(int $status) {
  141. $this->status = $status;
  142. }
  143. public static function fromString(string $status): AccountStatus {
  144. switch ($status) {
  145. case "NORMAL":
  146. return new self(self::NORMAL);
  147. case "LOCKED_OR_DISABLED":
  148. return new self(self::LOCKED_OR_DISABLED);
  149. case "CHANGE_PASSWORD":
  150. return new self(self::CHANGE_PASSWORD);
  151. case "TERMINATED":
  152. return new self(self::TERMINATED);
  153. case "ALERT_ON_ACCESS":
  154. return new self(self::ALERT_ON_ACCESS);
  155. default:
  156. return new self(0);
  157. }
  158. }
  159. /**
  160. * Get the account status/state as an integer.
  161. * @return int
  162. */
  163. public function get(): int {
  164. return $this->status;
  165. }
  166. /**
  167. * Get the account status/state as a string representation.
  168. * @return string
  169. */
  170. public function getString(): string {
  171. switch ($this->status) {
  172. case self::NORMAL:
  173. return "NORMAL";
  174. case self::LOCKED_OR_DISABLED:
  175. return "LOCKED_OR_DISABLED";
  176. case self::CHANGE_PASSWORD:
  177. return "CHANGE_PASSWORD";
  178. case self::TERMINATED:
  179. return "TERMINATED";
  180. case self::ALERT_ON_ACCESS:
  181. return "ALERT_ON_ACCESS";
  182. default:
  183. return "OTHER_" . $this->status;
  184. }
  185. }
  186. }