employee login and authentication with session
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
class Booking < ApplicationRecord
|
||||
|
||||
belongs_to :dining_facility
|
||||
belongs_to :dining_facility, :optional => true
|
||||
belongs_to :sale, :optional => true
|
||||
has_many :booking_orders
|
||||
|
||||
|
||||
end
|
||||
|
||||
4
app/models/booking_order.rb
Normal file
4
app/models/booking_order.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
class BookingOrder < ApplicationRecord
|
||||
belongs_to :booking
|
||||
belongs_to :order
|
||||
end
|
||||
@@ -1,3 +1,6 @@
|
||||
class DiningFacility < ApplicationRecord
|
||||
belongs_to :zone
|
||||
|
||||
scope :active, -> {where(is_active: true)}
|
||||
|
||||
end
|
||||
|
||||
@@ -1,17 +1,55 @@
|
||||
class Employee < ApplicationRecord
|
||||
include BCrypt
|
||||
has_secure_password
|
||||
|
||||
#attr_accessor :password
|
||||
|
||||
validates_presence_of :name, :role, :password
|
||||
validates_presence_of :name, :role
|
||||
validates_presence_of :password, :on => [:create]
|
||||
validates :emp_id, uniqueness: true, numericality: true, length: {in: 1..4}, allow_blank: true
|
||||
validates :password, numericality: true, length: {in: 3..9}, allow_blank: true
|
||||
|
||||
|
||||
def password
|
||||
@password ||= Password.new(password_hash)
|
||||
def self.login(emp_id, password)
|
||||
user = Employee.find_by_emp_id(emp_id).authenticate(password)
|
||||
|
||||
Rails.logger.debug user
|
||||
|
||||
if (user)
|
||||
user.generate_token
|
||||
user.session_expiry = DateTime.now.utc + 30.minutes
|
||||
user.session_last_login = DateTime.now.utc
|
||||
user.save
|
||||
|
||||
return user
|
||||
end
|
||||
return nil
|
||||
|
||||
end
|
||||
|
||||
def password=(new_password)
|
||||
@password = Password.create(new_password)
|
||||
self.encrypted_access_code = @password
|
||||
def self.authenticate_by_token(session_token)
|
||||
user = Employee.find_by_token_session(session_token)
|
||||
if user && user.session_expiry.utc > DateTime.now.utc
|
||||
#Extend the login time each time authenticatation take place
|
||||
user.session_expiry = DateTime.now.utc + 30.minutes
|
||||
user.save
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def self.logout(session_token)
|
||||
user = Employee.find_by_token_session(session_token)
|
||||
if user
|
||||
user.token_session = nil
|
||||
user.session_expiry = nil
|
||||
user.save
|
||||
end
|
||||
end
|
||||
|
||||
def generate_token
|
||||
update_column :token_session, SecureRandom.hex(10)
|
||||
rescue ActiveRecord::RecordNotUnique
|
||||
retry
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
class RoomBookingOrder < ApplicationRecord
|
||||
belongs_to :room_booking
|
||||
belongs_to :order
|
||||
end
|
||||
@@ -1,4 +0,0 @@
|
||||
class TableBookingOrder < ApplicationRecord
|
||||
belongs_to :table_booking
|
||||
belongs_to :order
|
||||
end
|
||||
Reference in New Issue
Block a user