UserModel.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace Models;
  3. class UserModel
  4. {
  5. private \PDO $pdo;
  6. public function __construct()
  7. {
  8. if (isset($GLOBALS['pdo']) && $GLOBALS['pdo'] instanceof \PDO) {
  9. $this->pdo = $GLOBALS['pdo'];
  10. return;
  11. }
  12. }
  13. public function validateLogin(string $email, string $password): ?array
  14. {
  15. $stmt = $this->pdo->prepare(
  16. 'SELECT
  17. user_id,
  18. user_name,
  19. user_email,
  20. user_password,
  21. user_phone,
  22. user_cpf,
  23. user_address,
  24. user_city,
  25. user_state,
  26. user_zip,
  27. user_country,
  28. user_birthdate,
  29. company_id,
  30. role_id,
  31. user_kyc,
  32. kyc_external_id
  33. FROM "user"
  34. WHERE user_email = :email AND user_flag = \'a\''
  35. );
  36. $stmt->execute(['email' => $email]);
  37. $user = $stmt->fetch(\PDO::FETCH_ASSOC);
  38. if ($user && password_verify($password, $user['user_password'])) {
  39. unset($user['user_password']);
  40. return $user;
  41. }
  42. return null;
  43. }
  44. public function createUser(array $data, string $flag = 'a')
  45. {
  46. // Verifica se email já existe
  47. $stmt = $this->pdo->prepare('SELECT user_id FROM "user" WHERE user_email = :email');
  48. $stmt->execute(['email' => $data['email']]);
  49. if ($stmt->fetch()) {
  50. return false;
  51. }
  52. $hash = password_hash($data['password'], PASSWORD_DEFAULT);
  53. $stmt = $this->pdo->prepare(
  54. 'INSERT INTO "user" (
  55. user_name,
  56. user_email,
  57. user_password,
  58. user_phone,
  59. user_address,
  60. user_city,
  61. user_state,
  62. user_zip,
  63. user_country,
  64. user_kyc,
  65. kyc_external_id,
  66. user_birthdate,
  67. user_cpf,
  68. company_id,
  69. role_id,
  70. user_flag
  71. ) VALUES (
  72. :user_name,
  73. :user_email,
  74. :hash,
  75. :user_phone,
  76. :user_address,
  77. :user_city,
  78. :user_state,
  79. :user_zip,
  80. :user_country,
  81. :user_kyc,
  82. :kyc_external_id,
  83. :user_birthdate,
  84. :user_cpf,
  85. :company_id,
  86. :role_id,
  87. :flag
  88. ) RETURNING user_id'
  89. );
  90. $ok = $stmt->execute([
  91. 'user_name' => $data['username'],
  92. 'user_email' => $data['email'],
  93. 'hash' => $hash,
  94. 'user_phone' => $data['phone'],
  95. 'user_address' => $data['address'],
  96. 'user_city' => $data['city'],
  97. 'user_state' => $data['state'],
  98. 'user_zip' => $data['zip'],
  99. 'user_country' => $data['country'],
  100. 'user_kyc' => (int)$data['kyc'],
  101. 'kyc_external_id' => $data['kyc_external_id'] ?? '',
  102. 'user_birthdate' => (int)$data['birthdate'],
  103. 'user_cpf' => $data['cpf'],
  104. 'company_id' => (int)$data['company_id'],
  105. 'role_id' => (int)$data['role_id'],
  106. 'flag' => $flag
  107. ]);
  108. if (!$ok) {
  109. return false;
  110. }
  111. $userId = $stmt->fetchColumn();
  112. return [
  113. 'user_id' => (int)$userId,
  114. 'user_name' => $data['username'],
  115. 'user_email' => $data['email'],
  116. 'company_id' => (int)$data['company_id'],
  117. 'role_id' => (int)$data['role_id']
  118. ];
  119. }
  120. public function getUsersByCompany(int $companyId): array
  121. {
  122. $stmt = $this->pdo->prepare("SELECT user_id, user_name, user_email, role_id FROM \"user\" WHERE company_id = :company_id AND user_flag = 'a'");
  123. $stmt->execute(['company_id' => $companyId]);
  124. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  125. }
  126. public function deleteUserById(int $userId, int $companyId): bool
  127. {
  128. $stmt = $this->pdo->prepare("DELETE FROM \"user\" WHERE user_id = :user_id AND company_id = :company_id");
  129. return $stmt->execute(['user_id' => $userId, 'company_id' => $companyId]);
  130. }
  131. public function updateEmail(int $userId, string $newEmail): bool
  132. {
  133. // check duplicate
  134. $chk = $this->pdo->prepare('SELECT 1 FROM "user" WHERE user_email = :email AND user_id <> :uid');
  135. $chk->execute(['email' => $newEmail, 'uid' => $userId]);
  136. if ($chk->fetchColumn()) {
  137. return false;
  138. }
  139. $stmt = $this->pdo->prepare('UPDATE "user" SET user_email = :email WHERE user_id = :uid AND user_flag = \'a\'');
  140. return $stmt->execute(['email' => $newEmail, 'uid' => $userId]);
  141. }
  142. public function changePassword(int $userId, string $currentPassword, string $newPassword): bool
  143. {
  144. $stmt = $this->pdo->prepare('SELECT user_password FROM "user" WHERE user_id = :uid AND user_flag = \'a\'');
  145. $stmt->execute(['uid' => $userId]);
  146. $hash = $stmt->fetchColumn();
  147. if (!$hash || !password_verify($currentPassword, $hash)) {
  148. return false;
  149. }
  150. $newHash = password_hash($newPassword, PASSWORD_DEFAULT);
  151. $up = $this->pdo->prepare('UPDATE "user" SET user_password = :hash WHERE user_id = :uid');
  152. return $up->execute(['hash' => $newHash, 'uid' => $userId]);
  153. }
  154. public function updateKycExternalId(int $userId, string $externalId): bool
  155. {
  156. $stmt = $this->pdo->prepare('UPDATE "user" SET kyc_external_id = :external_id WHERE user_id = :uid');
  157. return $stmt->execute([
  158. 'external_id' => $externalId,
  159. 'uid' => $userId,
  160. ]);
  161. }
  162. public function findByExternalId(string $externalId): ?array
  163. {
  164. $stmt = $this->pdo->prepare(
  165. 'SELECT user_id, user_kyc, kyc_external_id
  166. FROM "user"
  167. WHERE kyc_external_id = :external_id
  168. LIMIT 1'
  169. );
  170. $stmt->execute(['external_id' => $externalId]);
  171. $user = $stmt->fetch(\PDO::FETCH_ASSOC);
  172. return $user ?: null;
  173. }
  174. }