'required|max_length[100]|is_unique[users.username,id,{id}]', 'email' => 'required|valid_email|max_length[255]|is_unique[users.email,id,{id}]', 'phone_number' => 'permit_empty|max_length[20]|is_unique[users.phone_number,id,{id}]', 'telegram_id' => 'permit_empty|integer|is_unique[users.telegram_id,id,{id}]', 'role_id' => 'required|integer', ]; protected $validationMessages = []; protected $skipValidation = false; protected $cleanValidationRules = true; protected $beforeInsert = ['hashPassword']; protected $beforeUpdate = ['hashPassword']; protected function hashPassword(array $data) { if (isset($data['data']['password_hash']) && !empty($data['data']['password_hash'])) { // Only hash if it's not already hashed (check if it starts with $2y$ which is bcrypt) if (!preg_match('/^\$2[ayb]\$.{56}$/', $data['data']['password_hash'])) { $data['data']['password_hash'] = password_hash($data['data']['password_hash'], PASSWORD_DEFAULT); } } return $data; } public function getUserByUsername(string $username) { return $this->where('username', $username)->first(); } public function getUserByEmail(string $email) { return $this->where('email', $email)->first(); } public function verifyPassword(string $password, string $hash): bool { return password_verify($password, $hash); } }