29 lines
703 B
Ruby
29 lines
703 B
Ruby
class PrivateUser < ActiveRecord::Base
|
|
require 'digest/sha2'
|
|
require 'active_support/secure_random'
|
|
|
|
belongs_to :platform
|
|
belongs_to :user
|
|
|
|
validate :login, :uniqueness => true
|
|
|
|
class << self
|
|
def can_generate_more?(user_id)
|
|
!PrivateUser.exists?(:user_id => user_id)
|
|
end
|
|
|
|
def generate_pair(platform_id, user_id)
|
|
login = "login_#{ActiveSupport::SecureRandom.hex(16)}"
|
|
pass = "pass_#{ActiveSupport::SecureRandom.hex(16)}"
|
|
|
|
PrivateUser.create(
|
|
:login => login,
|
|
:password => Digest::SHA2.new.hexdigest(pass),
|
|
:platform_id => platform_id,
|
|
:user_id => user_id
|
|
)
|
|
|
|
{:login => login, :pass => pass}
|
|
end
|
|
end
|
|
end |