Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

133 lines
3.4KB

  1. <?php
  2. namespace App\Models;
  3. use CodeIgniter\Model;
  4. use Faker\Generator;
  5. use Myth\Auth\Authorization\GroupModel;
  6. use Myth\Auth\Entities\User;
  7. /**
  8. * @method User|null first()
  9. */
  10. class UserModel extends Model
  11. {
  12. protected $table = 'users';
  13. protected $primaryKey = 'id';
  14. protected $returnType = User::class;
  15. protected $useSoftDeletes = true;
  16. protected $allowedFields = [
  17. 'email', 'password_hash', 'reset_hash', 'reset_at', 'reset_expires', 'activate_hash',
  18. 'status', 'status_message', 'active', 'force_pass_reset', 'permissions', 'deleted_at',
  19. 'idprestador', 'rh_prestador_solicitud_id'
  20. ];
  21. protected $useTimestamps = true;
  22. protected $validationRules = [
  23. 'email' => 'required|valid_email|is_unique[users.email,id,{id}]',
  24. 'password_hash' => 'required',
  25. ];
  26. protected $validationMessages = [];
  27. protected $skipValidation = false;
  28. protected $afterInsert = ['addToGroup'];
  29. /**
  30. * The id of a group to assign.
  31. * Set internally by withGroup.
  32. *
  33. * @var int|null
  34. */
  35. protected $assignGroup;
  36. /**
  37. * Logs a password reset attempt for posterity sake.
  38. */
  39. public function logResetAttempt(string $email, ?string $token = null, ?string $ipAddress = null, ?string $userAgent = null)
  40. {
  41. $this->db->table('auth_reset_attempts')->insert([
  42. 'email' => $email,
  43. 'ip_address' => $ipAddress,
  44. 'user_agent' => $userAgent,
  45. 'token' => $token,
  46. 'created_at' => date('Y-m-d H:i:s'),
  47. ]);
  48. }
  49. /**
  50. * Logs an activation attempt for posterity sake.
  51. */
  52. public function logActivationAttempt(?string $token = null, ?string $ipAddress = null, ?string $userAgent = null)
  53. {
  54. $this->db->table('auth_activation_attempts')->insert([
  55. 'ip_address' => $ipAddress,
  56. 'user_agent' => $userAgent,
  57. 'token' => $token,
  58. 'created_at' => date('Y-m-d H:i:s'),
  59. ]);
  60. }
  61. /**
  62. * Sets the group to assign any users created.
  63. *
  64. * @return $this
  65. */
  66. public function withGroup(string $groupName)
  67. {
  68. $group = $this->db->table('auth_groups')->where('name', $groupName)->get()->getFirstRow();
  69. $this->assignGroup = $group->id;
  70. return $this;
  71. }
  72. /**
  73. * Clears the group to assign to newly created users.
  74. *
  75. * @return $this
  76. */
  77. public function clearGroup()
  78. {
  79. $this->assignGroup = null;
  80. return $this;
  81. }
  82. /**
  83. * If a default role is assigned in Config\Auth, will
  84. * add this user to that group. Will do nothing
  85. * if the group cannot be found.
  86. *
  87. * @param mixed $data
  88. *
  89. * @return mixed
  90. */
  91. protected function addToGroup($data)
  92. {
  93. if (is_numeric($this->assignGroup)) {
  94. $groupModel = model(GroupModel::class);
  95. $groupModel->addUserToGroup($data['id'], $this->assignGroup);
  96. }
  97. return $data;
  98. }
  99. /**
  100. * Faked data for Fabricator.
  101. */
  102. public function fake(Generator &$faker): User
  103. {
  104. return new User([
  105. 'email' => $faker->email,
  106. 'password' => bin2hex(random_bytes(16)),
  107. ]);
  108. }
  109. public function findUserWithSolicitud($solicitudId)
  110. {
  111. return $this
  112. // ->select('id')
  113. ->where('rh_prestador_solicitud_id', $solicitudId)
  114. ->first();
  115. }
  116. }