discourse/app/models/api_key.rb

23 lines
499 B
Ruby
Raw Normal View History

2013-10-23 03:53:08 +08: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
2013-10-23 05:51:04 +08:00
api_key = ApiKey.where(user_id: nil).first
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