2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-10-23 03:53:08 +08:00
|
|
|
class ApiKey < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
2017-08-31 12:06:56 +08:00
|
|
|
belongs_to :created_by, class_name: 'User'
|
2013-10-23 03:53:08 +08:00
|
|
|
|
2017-07-24 20:45:05 +08:00
|
|
|
validates :user_id, uniqueness: true
|
2013-10-23 03:53:08 +08:00
|
|
|
validates_presence_of :key
|
|
|
|
|
|
|
|
def regenerate!(updated_by)
|
|
|
|
self.key = SecureRandom.hex(32)
|
|
|
|
self.created_by = updated_by
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_master_key
|
2014-11-20 12:38:20 +08:00
|
|
|
api_key = ApiKey.find_by(user_id: nil, hidden: false)
|
2013-10-23 03:53:08 +08:00
|
|
|
if api_key.blank?
|
|
|
|
api_key = ApiKey.create(key: SecureRandom.hex(32), created_by: Discourse.system_user)
|
|
|
|
end
|
|
|
|
api_key
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-12-05 14:40:35 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: api_keys
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# key :string(64) not null
|
|
|
|
# user_id :integer
|
|
|
|
# created_by_id :integer
|
2014-08-27 13:19:25 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2014-11-20 11:53:15 +08:00
|
|
|
# allowed_ips :inet is an Array
|
2014-12-24 17:11:41 +08:00
|
|
|
# hidden :boolean default(FALSE), not null
|
2013-12-05 14:40:35 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_api_keys_on_key (key)
|
|
|
|
# index_api_keys_on_user_id (user_id) UNIQUE
|
|
|
|
#
|