Class: UserPattern::ViolationRecorder

Inherits:
Object
  • Object
show all
Defined in:
lib/userpattern/violation_recorder.rb

Overview

Persists threshold violations with an anonymized user identifier. The raw user ID is NEVER stored — only a one-way HMAC hash.

Class Method Summary collapse

Class Method Details

.anonymize_user_id(user_id, model_type) ⇒ Object



23
24
25
26
# File 'lib/userpattern/violation_recorder.rb', line 23

def self.anonymize_user_id(user_id, model_type)
  salt = UserPattern.configuration.anonymous_salt
  OpenSSL::HMAC.hexdigest('SHA256', salt, "#{model_type}:#{user_id}")[0, 16]
end

.record!(violation) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/userpattern/violation_recorder.rb', line 9

def self.record!(violation)
  Violation.create!(
    model_type: violation.model_type,
    endpoint: violation.endpoint,
    period: violation.period,
    count: violation.count,
    limit: violation.limit,
    user_identifier: anonymize_user_id(violation.user_id, violation.model_type),
    occurred_at: Time.current
  )
rescue StandardError => e
  Rails.logger.error("[UserPattern] Violation record error: #{e.message}")
end