2011-10-18 14:47:17 +01:00
|
|
|
class PrivateUser < ActiveRecord::Base
|
|
|
|
require 'digest/sha2'
|
|
|
|
require 'active_support/secure_random'
|
2011-10-27 14:04:03 +01:00
|
|
|
|
|
|
|
belongs_to :platform
|
|
|
|
belongs_to :user
|
2011-10-18 14:47:17 +01:00
|
|
|
|
|
|
|
validate :login, :uniqueness => true
|
|
|
|
|
|
|
|
class << self
|
2011-10-27 14:04:03 +01:00
|
|
|
def can_generate_more?(user_id)
|
|
|
|
!PrivateUser.exists?(:user_id => user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
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(
|
|
|
|
:login => login,
|
|
|
|
:password => Digest::SHA2.new.hexdigest(pass),
|
2011-10-27 14:04:03 +01:00
|
|
|
:platform_id => platform_id,
|
|
|
|
:user_id => user_id
|
2011-10-18 14:47:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
{:login => login, :pass => pass}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|