147 lines
4.1 KiB
Ruby
147 lines
4.1 KiB
Ruby
class Activation < ActiveRecord::Base
|
|
|
|
belongs_to :order
|
|
|
|
attr_accessor :result
|
|
|
|
validate :validate_serial_key, :validate_attributes, :validate_limit
|
|
|
|
after_create :generate_final_judgement
|
|
|
|
named_scope :by_computer_id, lambda { |computer_id| { :conditions => { :computer_id => computer_id } } }
|
|
named_scope :group_computer_id_except, lambda { |computer_id| { :group => "computer_id", :conditions => ["computer_id != ?", computer_id] } }
|
|
|
|
GENERATOR_DIR = "#{Rails.root}/#{Settings.private_dir}"
|
|
GENERATOR = "BLVLicenseGenerator.exe"
|
|
|
|
RESULTS = {
|
|
"success" => {
|
|
:response => 0,
|
|
:message => :success
|
|
},
|
|
"invalid_serial_key" => {
|
|
:response => 1,
|
|
:message => :invalid_serial_key,
|
|
:message_url => :support_url
|
|
},
|
|
"limit_reached" => {
|
|
:response => 2,
|
|
:message => :limit_reached,
|
|
:message_url => :buy_url
|
|
},
|
|
"missing_attributes" => {
|
|
:response => 3,
|
|
:message => :other_error_missing_attributes,
|
|
:message_url => :support_url
|
|
},
|
|
"generator_error" => {
|
|
:response => 3,
|
|
:message => :other_error_generator_error,
|
|
:message_url => :support_url
|
|
}
|
|
}
|
|
|
|
def before_validation
|
|
self.result = "success"
|
|
self.order = Order.find_by_license_key(serial_key)
|
|
end
|
|
|
|
def validate_attributes
|
|
missing_attributes! if missing_attributes?
|
|
end
|
|
|
|
def validate_serial_key
|
|
@valid_serial_key = order.present?
|
|
invalid_serial_key! unless @valid_serial_key
|
|
end
|
|
|
|
def validate_limit
|
|
unless invalid_serial_key?
|
|
@within_limit = within_limit?
|
|
limit_reached! unless @within_limit
|
|
end
|
|
end
|
|
|
|
def computer_ids_within_limit?
|
|
computer_ids_limit = Settings.activation_limits["activations_#{BmtOrder.find(order.id).bmtID}"].to_i
|
|
order.activations.group_computer_id_except(computer_id).count("DISTINCT computer_id") < computer_ids_limit
|
|
end
|
|
|
|
def within_limit?
|
|
!new_record? || (!invalid_serial_key? && computer_ids_within_limit?)
|
|
end
|
|
|
|
def missing_attributes?
|
|
serial_key.nil? || build.blank? || product.blank? || computer_id.blank?
|
|
end
|
|
|
|
def missing_attributes!
|
|
self.result = "missing_attributes"
|
|
errors.add_to_base "Missing attributes"
|
|
end
|
|
|
|
def limit_reached!
|
|
self.result = "limit_reached"
|
|
errors.add_to_base "Limit reached."
|
|
end
|
|
|
|
def invalid_serial_key!
|
|
self.result = "invalid_serial_key"
|
|
errors.add_to_base "Invalid serial key."
|
|
end
|
|
|
|
def invalid_serial_key?
|
|
@valid_serial_key == false
|
|
end
|
|
|
|
def to_xml(options = {})
|
|
h = RESULTS[result]
|
|
h = h.merge({
|
|
:message => I18n.t("txt.activation.#{h[:message]}"),
|
|
:message_url => (options.delete(h[:message_url]) || ""),
|
|
:final_judgement => (final_judgement || "")
|
|
})
|
|
|
|
h.to_xml(:root => "activation", :dasherize => false)
|
|
end
|
|
|
|
private
|
|
|
|
def generate_final_judgement
|
|
#Format:
|
|
# ### license generator source file ###
|
|
# [1] Serial key , eg. "ABCDEF123456"
|
|
# [2] Computer ID , eg. "f54f1525a456ad867b65ee9ec8b0c83de44b3dc7"
|
|
# [3] IP Address , eg. "127.0.0.1"
|
|
# [4] Activation ID , eg. "5"
|
|
# [5] Product , eg. "BLV"
|
|
# [6] Build , eg. "1"
|
|
source_file_data = ["### license generator source file ###"]
|
|
source_file_data << serial_key
|
|
source_file_data << computer_id
|
|
source_file_data << ip
|
|
source_file_data << id
|
|
source_file_data << product
|
|
source_file_data << build
|
|
|
|
@source_file_name = "license_source_file_#{id}.txt"
|
|
@source_file_path = File.join(GENERATOR_DIR, @source_file_name)
|
|
File.open(@source_file_path, 'a') {|f| f.write(source_file_data.join("\n")) }
|
|
|
|
generator_path = File.join(GENERATOR_DIR, GENERATOR)
|
|
key = `mono #{generator_path} #{@source_file_path}`
|
|
FileUtils.rm_r(@source_file_path)
|
|
|
|
match = key.match /^(ok:\s*)(.+)/i
|
|
if match
|
|
update_attribute(:final_judgement, match[2].strip)
|
|
else
|
|
self.final_judgement = key.strip
|
|
self.result = "generator_error"
|
|
destroy
|
|
FailedActivation.create_from_activation(self)
|
|
end
|
|
end
|
|
|
|
end
|