This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
class Analytics < ApplicationRecord
|
||||
scope :hits_by_ip, ->(ip, col = "*") { select("#{col}").where(ip_address: ip).order("id DESC") }
|
||||
|
||||
def self.count_by_col(col)
|
||||
calculate(:count, col)
|
||||
end
|
||||
|
||||
def self.parse_field(field)
|
||||
valid_fields = ["ip_address", "referrer", "user_agent"]
|
||||
|
||||
if valid_fields.include?(field)
|
||||
field
|
||||
else
|
||||
"1"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,4 @@
|
||||
# frozen_string_literal: true
|
||||
class ApplicationRecord < ActiveRecord::Base
|
||||
self.abstract_class = true
|
||||
end
|
||||
@@ -0,0 +1,31 @@
|
||||
# frozen_string_literal: true
|
||||
class Benefits < ApplicationRecord
|
||||
|
||||
def self.save(file, backup = false)
|
||||
data_path = Rails.root.join("public", "data")
|
||||
full_file_name = "#{data_path}/#{file.original_filename}"
|
||||
f = File.open(full_file_name, "wb+")
|
||||
f.write file.read
|
||||
f.close
|
||||
make_backup(file, data_path, full_file_name) if backup == "true"
|
||||
end
|
||||
|
||||
def self.make_backup(file, data_path, full_file_name)
|
||||
if File.exist?(full_file_name)
|
||||
silence_streams(STDERR) { system("cp #{full_file_name} #{data_path}/bak#{Time.zone.now.to_i}_#{file.original_filename}") }
|
||||
end
|
||||
end
|
||||
|
||||
def self.silence_streams(*streams)
|
||||
on_hold = streams.collect { |stream| stream.dup }
|
||||
streams.each do |stream|
|
||||
stream.reopen(RUBY_PLATFORM =~ /mswin/ ? "NUL:" : "/dev/null")
|
||||
stream.sync = true
|
||||
end
|
||||
yield
|
||||
ensure
|
||||
streams.each_with_index do |stream, i|
|
||||
stream.reopen(on_hold[i])
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
class KeyManagement < ApplicationRecord
|
||||
belongs_to :work_info
|
||||
belongs_to :user
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
class Message < ApplicationRecord
|
||||
belongs_to :user
|
||||
validates_presence_of :creator_id, :receiver_id, :message
|
||||
|
||||
def creator_name
|
||||
if creator = User.where(id: self.creator_id).first
|
||||
creator.full_name
|
||||
else
|
||||
"Name unavailable"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
# frozen_string_literal: true
|
||||
class PaidTimeOff < ApplicationRecord
|
||||
belongs_to :user
|
||||
has_many :schedule, foreign_key: :user_id, primary_key: :user_id, dependent: :destroy
|
||||
|
||||
def sick_days_remaining
|
||||
self.sick_days_earned - self.sick_days_taken
|
||||
end
|
||||
|
||||
def pto_days_remaining
|
||||
self.pto_earned - self.pto_taken
|
||||
end
|
||||
|
||||
def sick_days_taken_percentage
|
||||
result = self.sick_days_taken.to_f / self.sick_days_earned.to_f * 100.0
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
class Pay < ApplicationRecord
|
||||
# Associations
|
||||
belongs_to :user
|
||||
|
||||
# Validations
|
||||
validates :bank_account_num, presence: true
|
||||
validates :bank_routing_num, presence: true
|
||||
validates :percent_of_deposit, presence: true
|
||||
|
||||
# callbacks
|
||||
before_save :encrypt_bank_account_num
|
||||
|
||||
def as_json
|
||||
super(only: [:bank_account_num, :bank_routing_num, :percent_of_deposit, :id])
|
||||
end
|
||||
|
||||
def encrypt_bank_account_num
|
||||
self.bank_account_num = Encryption.encrypt_sensitive_value(self.bank_account_num)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
class Performance < ApplicationRecord
|
||||
belongs_to :user
|
||||
|
||||
def reviewer_name
|
||||
u = User.find_by_id(self.reviewer)
|
||||
u.full_name if u.respond_to?("fullname")
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,4 @@
|
||||
# frozen_string_literal: true
|
||||
class Retirement < ApplicationRecord
|
||||
belongs_to :user
|
||||
end
|
||||
@@ -0,0 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
class Schedule < ApplicationRecord
|
||||
belongs_to :paid_time_off
|
||||
|
||||
validates_presence_of :date_begin, :date_end, :event_desc, :event_name, :event_type
|
||||
end
|
||||
Executable
+67
@@ -0,0 +1,67 @@
|
||||
# frozen_string_literal: true
|
||||
require "encryption"
|
||||
|
||||
class User < ApplicationRecord
|
||||
validates :password, presence: true,
|
||||
confirmation: true,
|
||||
length: {within: 6..40},
|
||||
on: :create,
|
||||
if: :password
|
||||
|
||||
validates_presence_of :email
|
||||
validates_uniqueness_of :email
|
||||
validates_format_of :email, with: /.+@.+\..+/i
|
||||
|
||||
has_one :retirement, dependent: :destroy
|
||||
has_one :paid_time_off, dependent: :destroy
|
||||
has_one :work_info, dependent: :destroy
|
||||
has_many :performance, dependent: :destroy
|
||||
has_many :pay, dependent: :destroy
|
||||
has_many :messages, foreign_key: :receiver_id, dependent: :destroy
|
||||
|
||||
before_save :hash_password
|
||||
after_create { generate_token(:auth_token) }
|
||||
before_create :build_benefits_data
|
||||
|
||||
def build_benefits_data
|
||||
build_retirement(POPULATE_RETIREMENTS.sample)
|
||||
build_paid_time_off(POPULATE_PAID_TIME_OFF.sample).schedule.build(POPULATE_SCHEDULE.sample)
|
||||
build_work_info(POPULATE_WORK_INFO.sample)
|
||||
# Uncomment below line to use encrypted SSN(s)
|
||||
#work_info.build_key_management(:iv => SecureRandom.hex(32))
|
||||
performance.build(POPULATE_PERFORMANCE.sample)
|
||||
end
|
||||
|
||||
def full_name
|
||||
"#{self.first_name} #{self.last_name}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.authenticate(email, password)
|
||||
auth = nil
|
||||
user = find_by_email(email)
|
||||
raise "#{email} doesn't exist!" if !(user)
|
||||
if user.password == Digest::MD5.hexdigest(password)
|
||||
auth = user
|
||||
else
|
||||
raise "Incorrect Password!"
|
||||
end
|
||||
return auth
|
||||
end
|
||||
|
||||
def hash_password
|
||||
if will_save_change_to_password?
|
||||
self.password = Digest::MD5.hexdigest(self.password)
|
||||
end
|
||||
end
|
||||
|
||||
def generate_token(column)
|
||||
loop do
|
||||
self[column] = Encryption.encrypt_sensitive_value(self.id)
|
||||
break unless User.exists?(column => self[column])
|
||||
end
|
||||
|
||||
self.save!
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,42 @@
|
||||
# frozen_string_literal: true
|
||||
class WorkInfo < ApplicationRecord
|
||||
belongs_to :user
|
||||
has_one :key_management, foreign_key: :user_id, primary_key: :user_id, dependent: :destroy
|
||||
#before_save :encrypt_ssn
|
||||
|
||||
# We should probably use this
|
||||
def last_four
|
||||
"***-**-" << self.decrypt_ssn[-4, 4]
|
||||
end
|
||||
|
||||
def encrypt_ssn
|
||||
aes = OpenSSL::Cipher.new(cipher_type)
|
||||
aes.encrypt
|
||||
aes.key = key[0..31]
|
||||
aes.iv = iv if iv != nil
|
||||
self.encrypted_ssn = aes.update(self.SSN) + aes.final
|
||||
self.SSN = nil
|
||||
end
|
||||
|
||||
def decrypt_ssn
|
||||
aes = OpenSSL::Cipher.new(cipher_type)
|
||||
aes.decrypt
|
||||
aes.key = key[0..31]
|
||||
aes.iv = iv if iv != nil
|
||||
aes.update(self.encrypted_ssn) + aes.final
|
||||
end
|
||||
|
||||
def key
|
||||
raise "Key Missing" unless KEY.present?
|
||||
KEY
|
||||
end
|
||||
|
||||
def iv
|
||||
raise "No IV for this User" unless self.key_management.try(:iv).present?
|
||||
self.key_management.iv
|
||||
end
|
||||
|
||||
def cipher_type
|
||||
"aes-256-cbc"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user