2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2011-10-18 14:47:17 +01:00
|
|
|
class PrivateUser < ActiveRecord::Base
|
|
|
|
require 'digest/sha2'
|
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
|
|
|
|
|
2011-10-31 00:11:49 +00:00
|
|
|
def event_log_message
|
2011-11-28 15:41:42 +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)
|
|
|
|
!PrivateUser.exists?(:user_id => user_id, :platform_id => platform_id)
|
2011-10-27 14:04:03 +01:00
|
|
|
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
|
2011-11-28 15:41:42 +00:00
|
|
|
end
|