UserModel.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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('SELECT user_id, user_email, user_password, company_id FROM "user" WHERE user_email = :email AND user_flag = \'a\'');
  16. $stmt->execute(['email' => $email]);
  17. $user = $stmt->fetch(\PDO::FETCH_ASSOC);
  18. if ($user && password_verify($password, $user['user_password'])) {
  19. unset($user['user_password']);
  20. return $user;
  21. }
  22. return null;
  23. }
  24. public function createUser(array $data, string $flag = 'a')
  25. {
  26. // Verifica se email já existe
  27. $stmt = $this->pdo->prepare('SELECT user_id FROM "user" WHERE user_email = :email');
  28. $stmt->execute(['email' => $data['email']]);
  29. if ($stmt->fetch()) {
  30. return false;
  31. }
  32. $hash = password_hash($data['password'], PASSWORD_DEFAULT);
  33. $stmt = $this->pdo->prepare(
  34. 'INSERT INTO "user" (
  35. user_name, user_email, user_password, user_phone, user_address, user_city, user_state, user_zip, user_country,
  36. user_kyc, user_birthdate, user_cpf, company_id, role_id, user_flag
  37. ) VALUES (
  38. :user_name, :user_email, :hash, :user_phone, :user_address, :user_city, :user_state, :user_zip, :user_country,
  39. :user_kyc, :user_birthdate, :user_cpf, :company_id, :role_id, :flag
  40. ) RETURNING user_id'
  41. );
  42. $ok = $stmt->execute([
  43. 'user_name' => $data['username'],
  44. 'user_email' => $data['email'],
  45. 'hash' => $hash,
  46. 'user_phone' => $data['phone'],
  47. 'user_address' => $data['address'],
  48. 'user_city' => $data['city'],
  49. 'user_state' => $data['state'],
  50. 'user_zip' => $data['zip'],
  51. 'user_country' => $data['country'],
  52. 'user_kyc' => (int)$data['kyc'],
  53. 'user_birthdate' => (int)$data['birthdate'],
  54. 'user_cpf' => $data['cpf'],
  55. 'company_id' => (int)$data['company_id'],
  56. 'role_id' => (int)$data['role_id'],
  57. 'flag' => $flag
  58. ]);
  59. if (!$ok) {
  60. return false;
  61. }
  62. $userId = $stmt->fetchColumn();
  63. return [
  64. 'user_id' => (int)$userId,
  65. 'user_name' => $data['username'],
  66. 'user_email' => $data['email'],
  67. 'company_id' => (int)$data['company_id'],
  68. 'role_id' => (int)$data['role_id']
  69. ];
  70. }
  71. public function getUsersByCompany(int $companyId): array
  72. {
  73. $stmt = $this->pdo->prepare("SELECT user_id, user_name, user_email, role_id FROM \"user\" WHERE company_id = :company_id AND user_flag = 'a'");
  74. $stmt->execute(['company_id' => $companyId]);
  75. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  76. }
  77. public function deleteUserById(int $userId, int $companyId): bool
  78. {
  79. $stmt = $this->pdo->prepare("DELETE FROM \"user\" WHERE user_id = :user_id AND company_id = :company_id");
  80. return $stmt->execute(['user_id' => $userId, 'company_id' => $companyId]);
  81. }
  82. public function updateEmail(int $userId, string $newEmail): bool
  83. {
  84. // check duplicate
  85. $chk = $this->pdo->prepare('SELECT 1 FROM "user" WHERE user_email = :email AND user_id <> :uid');
  86. $chk->execute(['email' => $newEmail, 'uid' => $userId]);
  87. if ($chk->fetchColumn()) {
  88. return false;
  89. }
  90. $stmt = $this->pdo->prepare('UPDATE "user" SET user_email = :email WHERE user_id = :uid AND user_flag = \'a\'');
  91. return $stmt->execute(['email' => $newEmail, 'uid' => $userId]);
  92. }
  93. public function changePassword(int $userId, string $currentPassword, string $newPassword): bool
  94. {
  95. $stmt = $this->pdo->prepare('SELECT user_password FROM "user" WHERE user_id = :uid AND user_flag = \'a\'');
  96. $stmt->execute(['uid' => $userId]);
  97. $hash = $stmt->fetchColumn();
  98. if (!$hash || !password_verify($currentPassword, $hash)) {
  99. return false;
  100. }
  101. $newHash = password_hash($newPassword, PASSWORD_DEFAULT);
  102. $up = $this->pdo->prepare('UPDATE "user" SET user_password = :hash WHERE user_id = :uid');
  103. return $up->execute(['hash' => $newHash, 'uid' => $userId]);
  104. }
  105. }