Files
danbooru/app/logical/current_user.rb
evazion 6ca007ee1f Fix #4670: Replace RequestStore with AS::CurrentAttributes.
This also requires replacing CurrentUser.name with CurrentUser.user.name
because the `name` method had a conflict with CurrentAttributes.
2021-01-16 12:43:20 -06:00

28 lines
657 B
Ruby

class CurrentUser < ActiveSupport::CurrentAttributes
attribute :user, :ip_addr, :country, :root_url, :safe_mode
alias_method :safe_mode?, :safe_mode
delegate :id, to: :user, allow_nil: true
delegate_missing_to :user
def self.scoped(user, ip_addr = "127.0.0.1", &block)
set(user: user, ip_addr: ip_addr) do
yield user
end
end
def self.as(user_or_id, &block)
if user_or_id.is_a?(String) || user_or_id.is_a?(Integer)
user = ::User.find(user_or_id)
else
user = user_or_id
end
scoped(user, &block)
end
def self.root_url
attributes[:root_url] || "https://#{Danbooru.config.hostname}"
end
end