2011-10-18 14:47:17 +01:00
|
|
|
class PrivateUser < ActiveRecord::Base
|
|
|
|
require 'digest/sha2'
|
2014-01-21 04:51:49 +00:00
|
|
|
|
2011-10-27 14:04:03 +01:00
|
|
|
belongs_to :platform
|
|
|
|
belongs_to :user
|
2011-10-18 14:47:17 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
validate :login, uniqueness: true
|
2011-10-18 14:47:17 +01:00
|
|
|
|
2011-10-31 00:11:49 +00:00
|
|
|
def event_log_message
|
2014-01-21 04:51:49 +00:00
|
|
|
{platform: platform.name, user: user.uname}.inspect
|
2011-10-31 00:11:49 +00:00
|
|
|
end
|
|
|
|
|
2011-10-18 14:47:17 +01:00
|
|
|
class << self
|
2011-11-02 00:45:19 +00:00
|
|
|
def can_generate_more?(user_id, platform_id)
|
2014-01-21 04:51:49 +00:00
|
|
|
!PrivateUser.exists?(user_id: user_id, platform_id: platform_id)
|
2011-10-27 14:04:03 +01:00
|
|
|
end
|
2014-01-21 04:51:49 +00:00
|
|
|
|
2011-10-27 14:04:03 +01:00
|
|
|
def generate_pair(platform_id, user_id)
|
2011-10-18 14:47:17 +01:00
|
|
|
login = "login_#{ActiveSupport::SecureRandom.hex(16)}"
|
|
|
|
pass = "pass_#{ActiveSupport::SecureRandom.hex(16)}"
|
|
|
|
|
|
|
|
PrivateUser.create(
|
2014-01-21 04:51:49 +00:00
|
|
|
login: login,
|
|
|
|
password: Digest::SHA2.new.hexdigest(pass),
|
|
|
|
platform_id: platform_id,
|
|
|
|
user_id: user_id
|
2011-10-18 14:47:17 +01:00
|
|
|
)
|
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
{login: login, pass: pass}
|
2011-10-18 14:47:17 +01:00
|
|
|
end
|
|
|
|
end
|
2011-11-28 15:41:42 +00:00
|
|
|
end
|