rosa-build/app/models/private_user.rb

33 lines
757 B
Ruby
Raw Normal View History

class PrivateUser < ActiveRecord::Base
require 'digest/sha2'
2014-01-21 04:51:49 +00:00
belongs_to :platform
belongs_to :user
2014-01-21 04:51:49 +00:00
validate :login, uniqueness: true
def event_log_message
2014-01-21 04:51:49 +00:00
{platform: platform.name, user: user.uname}.inspect
end
class << self
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)
end
2014-01-21 04:51:49 +00:00
def generate_pair(platform_id, user_id)
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
)
2014-01-21 04:51:49 +00:00
{login: login, pass: pass}
end
end
end