mirror of
https://github.com/discourse/discourse.git
synced 2025-05-31 12:27:16 +08:00
DEV: Apply syntax_tree formatting to app/*
This commit is contained in:
@ -1,19 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ApiKey < ActiveRecord::Base
|
||||
class KeyAccessError < StandardError; end
|
||||
class KeyAccessError < StandardError
|
||||
end
|
||||
|
||||
has_many :api_key_scopes
|
||||
belongs_to :user
|
||||
belongs_to :created_by, class_name: 'User'
|
||||
belongs_to :created_by, class_name: "User"
|
||||
|
||||
scope :active, -> { where("revoked_at IS NULL") }
|
||||
scope :revoked, -> { where("revoked_at IS NOT NULL") }
|
||||
|
||||
scope :with_key, ->(key) {
|
||||
hashed = self.hash_key(key)
|
||||
where(key_hash: hashed)
|
||||
}
|
||||
scope :with_key,
|
||||
->(key) {
|
||||
hashed = self.hash_key(key)
|
||||
where(key_hash: hashed)
|
||||
}
|
||||
|
||||
after_initialize :generate_key
|
||||
|
||||
@ -26,7 +28,9 @@ class ApiKey < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def key
|
||||
raise KeyAccessError.new "API key is only accessible immediately after creation" unless key_available?
|
||||
unless key_available?
|
||||
raise KeyAccessError.new "API key is only accessible immediately after creation"
|
||||
end
|
||||
@key
|
||||
end
|
||||
|
||||
@ -40,10 +44,12 @@ class ApiKey < ActiveRecord::Base
|
||||
|
||||
def self.revoke_unused_keys!
|
||||
return if SiteSetting.revoke_api_keys_days == 0 # Never expire keys
|
||||
to_revoke = active.where("GREATEST(last_used_at, created_at, updated_at, :epoch) < :threshold",
|
||||
epoch: last_used_epoch,
|
||||
threshold: SiteSetting.revoke_api_keys_days.days.ago
|
||||
)
|
||||
to_revoke =
|
||||
active.where(
|
||||
"GREATEST(last_used_at, created_at, updated_at, :epoch) < :threshold",
|
||||
epoch: last_used_epoch,
|
||||
threshold: SiteSetting.revoke_api_keys_days.days.ago,
|
||||
)
|
||||
|
||||
to_revoke.find_each do |api_key|
|
||||
ApiKey.transaction do
|
||||
@ -53,7 +59,12 @@ class ApiKey < ActiveRecord::Base
|
||||
api_key,
|
||||
UserHistory.actions[:api_key_update],
|
||||
changes: api_key.saved_changes,
|
||||
context: I18n.t("staff_action_logs.api_key.automatic_revoked", count: SiteSetting.revoke_api_keys_days))
|
||||
context:
|
||||
I18n.t(
|
||||
"staff_action_logs.api_key.automatic_revoked",
|
||||
count: SiteSetting.revoke_api_keys_days,
|
||||
),
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -63,7 +74,9 @@ class ApiKey < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def request_allowed?(env)
|
||||
return false if allowed_ips.present? && allowed_ips.none? { |ip| ip.include?(Rack::Request.new(env).ip) }
|
||||
if allowed_ips.present? && allowed_ips.none? { |ip| ip.include?(Rack::Request.new(env).ip) }
|
||||
return false
|
||||
end
|
||||
return true if RouteMatcher.new(methods: :get, actions: "session#scopes").match?(env: env)
|
||||
|
||||
api_key_scopes.blank? || api_key_scopes.any? { |s| s.permits?(env) }
|
||||
|
Reference in New Issue
Block a user