2013-10-22 15:53:08 -04:00
|
|
|
class ApiKey < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :created_by, class_name: User
|
|
|
|
|
|
|
|
validates_presence_of :key
|
|
|
|
validates_uniqueness_of :user_id
|
|
|
|
|
|
|
|
def regenerate!(updated_by)
|
|
|
|
self.key = SecureRandom.hex(32)
|
|
|
|
self.created_by = updated_by
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_master_key
|
2014-05-06 09:41:59 -04:00
|
|
|
api_key = ApiKey.find_by(user_id: nil)
|
2013-10-22 15:53:08 -04: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 01:40:35 -05: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-05-27 21:49:50 -04:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2013-12-05 01:40:35 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_api_keys_on_key (key)
|
|
|
|
# index_api_keys_on_user_id (user_id) UNIQUE
|
|
|
|
#
|