module Platformx::AuthHelpers

Public Instance Methods

x_admin_authorize!() click to toggle source

Authorizes the current user as an admin or redirects the user to the admin login page.

# File lib/platformx/auth.rb, line 14
def x_admin_authorize!
  redirect '/admin/login' unless x_admin_authorized? || request.url.include?("/admin/login")
end
x_admin_authorized?() click to toggle source

Checks if the current user authorizes as an admin

@return [Boolean] if current user authorizes as an admin

# File lib/platformx/auth.rb, line 9
def x_admin_authorized?
  session[:admin_authorized]
end
x_admin_logout!() click to toggle source

Logs out the amdin

# File lib/platformx/auth.rb, line 19
def x_admin_logout!
  session[:admin_authorized] = false
  session.clear
end
x_authorize!() click to toggle source

Authorizes the user and redirects the user to the registration page.

# File lib/platformx/auth.rb, line 34
def x_authorize!
  redirect '/login' unless x_authorized? || request.url.include?("/login") || request.url.include?("/auth")
end
x_authorized?() click to toggle source

Checks if the user is logged in

@return [Boolean] if uesr logged in

# File lib/platformx/auth.rb, line 29
def x_authorized?
  session[:authorized]
end
x_decrypt(str = "") click to toggle source

Decodes an encoded string

@param str [String] the encoded string

@return [String] decoded string

# File lib/platformx/auth.rb, line 64
def x_decrypt(str = "")
  str = Base64.urlsafe_decode64(str)
  #decrypted_value = Encryptor.decrypt(:value => str, :key => '=PeuMX7B4LQ#@jG*s;tYGdF')
  return str.to_i
end
x_encrypt(int = "") click to toggle source

Encodes a value to a base 64 string.

@note The provided object should respond to `#to_s` to obtain a string representation of the object.

@param int [Object] the value required to be encrypted

@return [String] encoded string

# File lib/platformx/auth.rb, line 53
def x_encrypt(int = "")
  str = int.to_s
  #encrypted_value = Encryptor.encrypt(:value => str, :key => "=PeuMX7B4LQ#@jG*s;tYGdF")
  return Base64.urlsafe_encode64(str)
end
x_generate_password(len = "10") click to toggle source

Generate a random password

@param len [Integer] the length of the password

@return [String] random generated password

# File lib/platformx/auth.rb, line 75
def x_generate_password(len = "10")
  random_password = Array.new(len).map { (65 + rand(58)).chr }.join
  return random_password
end
x_logout!() click to toggle source

Logs out the currenly logged in user

# File lib/platformx/auth.rb, line 39
def x_logout!
   session[:authorized] = false
   session.clear
end