UserModel.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 isEmailActive(string $email): bool
  45. {
  46. $stmt = $this->pdo->prepare('SELECT 1 FROM "user" WHERE user_email = :email AND user_flag = \'a\'');
  47. $stmt->execute(['email' => $email]);
  48. return (bool)$stmt->fetchColumn();
  49. }
  50. public function createUser(array $data, string $flag = 'a')
  51. {
  52. // Verifica se email já existe
  53. $stmt = $this->pdo->prepare('SELECT 1 FROM "user" WHERE user_email = :email AND user_flag = \'a\'');
  54. $stmt->execute(['email' => $data['email']]);
  55. if ($stmt->fetchColumn()) {
  56. return false;
  57. }
  58. $hash = password_hash($data['password'], PASSWORD_DEFAULT);
  59. $stmt = $this->pdo->prepare(
  60. 'INSERT INTO "user" (
  61. user_name,
  62. user_email,
  63. user_password,
  64. user_phone,
  65. user_address,
  66. user_city,
  67. user_state,
  68. user_zip,
  69. user_country,
  70. user_kyc,
  71. kyc_external_id,
  72. user_birthdate,
  73. user_cpf,
  74. company_id,
  75. role_id,
  76. user_flag
  77. ) VALUES (
  78. :user_name,
  79. :user_email,
  80. :hash,
  81. :user_phone,
  82. :user_address,
  83. :user_city,
  84. :user_state,
  85. :user_zip,
  86. :user_country,
  87. :user_kyc,
  88. :kyc_external_id,
  89. :user_birthdate,
  90. :user_cpf,
  91. :company_id,
  92. :role_id,
  93. :flag
  94. ) RETURNING user_id'
  95. );
  96. $ok = $stmt->execute([
  97. 'user_name' => $data['username'],
  98. 'user_email' => $data['email'],
  99. 'hash' => $hash,
  100. 'user_phone' => $data['phone'],
  101. 'user_address' => $data['address'],
  102. 'user_city' => $data['city'],
  103. 'user_state' => $data['state'],
  104. 'user_zip' => $data['zip'],
  105. 'user_country' => $data['country'],
  106. 'user_kyc' => (int)$data['kyc'],
  107. 'kyc_external_id' => $data['kyc_external_id'] ?? '',
  108. 'user_birthdate' => (int)$data['birthdate'],
  109. 'user_cpf' => $data['cpf'],
  110. 'company_id' => (int)$data['company_id'],
  111. 'role_id' => (int)$data['role_id'],
  112. 'flag' => $flag
  113. ]);
  114. if (!$ok) {
  115. return false;
  116. }
  117. $userId = $stmt->fetchColumn();
  118. return [
  119. 'user_id' => (int)$userId,
  120. 'user_name' => $data['username'],
  121. 'user_email' => $data['email'],
  122. 'company_id' => (int)$data['company_id'],
  123. 'role_id' => (int)$data['role_id']
  124. ];
  125. }
  126. public function getUsersByCompany(int $companyId): array
  127. {
  128. $stmt = $this->pdo->prepare("SELECT user_id, user_name, user_email, role_id FROM \"user\" WHERE company_id = :company_id AND user_flag = 'a'");
  129. $stmt->execute(['company_id' => $companyId]);
  130. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  131. }
  132. public function getUserInfoById(int $userId, int $companyId): ?array
  133. {
  134. $stmt = $this->pdo->prepare(
  135. 'SELECT
  136. user_id,
  137. user_name,
  138. user_email,
  139. user_phone,
  140. user_cpf,
  141. user_birthdate,
  142. user_kyc,
  143. role_id,
  144. user_flag,
  145. user_address,
  146. user_city,
  147. user_state,
  148. user_zip,
  149. user_country
  150. FROM "user"
  151. WHERE user_id = :user_id AND company_id = :company_id'
  152. );
  153. $stmt->execute([
  154. 'user_id' => $userId,
  155. 'company_id' => $companyId,
  156. ]);
  157. $row = $stmt->fetch(\PDO::FETCH_ASSOC);
  158. return $row ?: null;
  159. }
  160. public function deleteUserById(int $userId, int $companyId): bool
  161. {
  162. $stmt = $this->pdo->prepare("UPDATE \"user\" SET user_flag = 'd' WHERE user_id = :user_id AND company_id = :company_id AND user_flag = 'a'");
  163. $stmt->execute(['user_id' => $userId, 'company_id' => $companyId]);
  164. return $stmt->rowCount() > 0;
  165. }
  166. public function updateEmail(int $userId, string $newEmail): bool
  167. {
  168. // check duplicate
  169. $chk = $this->pdo->prepare('SELECT 1 FROM "user" WHERE user_email = :email AND user_id <> :uid AND user_flag = \'a\'');
  170. $chk->execute(['email' => $newEmail, 'uid' => $userId]);
  171. if ($chk->fetchColumn()) {
  172. return false;
  173. }
  174. $stmt = $this->pdo->prepare('UPDATE "user" SET user_email = :email WHERE user_id = :uid AND user_flag = \'a\'');
  175. return $stmt->execute(['email' => $newEmail, 'uid' => $userId]);
  176. }
  177. public function changePassword(int $userId, string $currentPassword, string $newPassword): bool
  178. {
  179. $stmt = $this->pdo->prepare('SELECT user_password FROM "user" WHERE user_id = :uid AND user_flag = \'a\'');
  180. $stmt->execute(['uid' => $userId]);
  181. $hash = $stmt->fetchColumn();
  182. if (!$hash || !password_verify($currentPassword, $hash)) {
  183. return false;
  184. }
  185. $newHash = password_hash($newPassword, PASSWORD_DEFAULT);
  186. $up = $this->pdo->prepare('UPDATE "user" SET user_password = :hash WHERE user_id = :uid');
  187. return $up->execute(['hash' => $newHash, 'uid' => $userId]);
  188. }
  189. public function updateKycExternalId(int $userId, string $externalId): bool
  190. {
  191. $stmt = $this->pdo->prepare('UPDATE "user" SET kyc_external_id = :external_id WHERE user_id = :uid');
  192. return $stmt->execute([
  193. 'external_id' => $externalId,
  194. 'uid' => $userId,
  195. ]);
  196. }
  197. public function findByExternalId(string $externalId): ?int
  198. {
  199. $stmt = $this->pdo->prepare(
  200. 'SELECT user_kyc
  201. FROM "user"
  202. WHERE kyc_external_id = :external_id'
  203. );
  204. $stmt->execute(['external_id' => $externalId]);
  205. $user = $stmt->fetchColumn();
  206. return $user;
  207. }
  208. }