Module: Platformx::AuthHelpers

Defined in:
lib/platformx/auth.rb

Instance Method Summary collapse

Instance Method Details

#x_admin_authorize!Object

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



14
15
16
# 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?Boolean

Checks if the current user authorizes as an admin

Returns:

  • (Boolean)

    if current user authorizes as an admin



9
10
11
# File 'lib/platformx/auth.rb', line 9

def x_admin_authorized?
  session[:admin_authorized]
end

#x_admin_logout!Object

Logs out the amdin



19
20
21
22
# File 'lib/platformx/auth.rb', line 19

def x_admin_logout!
  session[:admin_authorized] = false
  session.clear
end

#x_authorize!Object

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



34
35
36
# 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?Boolean

Checks if the user is logged in

Returns:

  • (Boolean)

    if uesr logged in



29
30
31
# File 'lib/platformx/auth.rb', line 29

def x_authorized?
  session[:authorized]
end

#x_decrypt(str = "") ⇒ String

Decodes an encoded string

Parameters:

  • str (String) (defaults to: "")

    the encoded string

Returns:

  • (String)

    decoded string



64
65
66
67
68
# 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 = "") ⇒ String

Note:

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

Encodes a value to a base 64 string.

Parameters:

  • int (Object) (defaults to: "")

    the value required to be encrypted

Returns:

  • (String)

    encoded string



53
54
55
56
57
# 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") ⇒ String

Generate a random password

Parameters:

  • len (Integer) (defaults to: "10")

    the length of the password

Returns:

  • (String)

    random generated password



75
76
77
78
# 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!Object

Logs out the currenly logged in user



39
40
41
42
# File 'lib/platformx/auth.rb', line 39

def x_logout!
   session[:authorized] = false
   session.clear
end