Module: Octo::Helpers::ClientHelper

Defined in:
lib/octocore-cassandra/helpers/client_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_consumer(username, email, password) ⇒ String

Create a new Client

Parameters:

  • username (String)

    The name of the client

  • email (String)

    The email of the client

  • password (String)

    The password of the client

Returns:

  • (String)

    The status of request



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/octocore-cassandra/helpers/client_helper.rb', line 17

def add_consumer(username, email, password)
  unless enterprise_name_exists?(username)

    # create enterprise
    e = Octo::Enterprise.new
    e.name = username
    e.save!

    enterprise_id = e.id.to_s

    # create its Authentication stuff
    auth = Octo::Authorization.new
    auth.enterprise_id = enterprise_id
    auth.username = e.name
    auth.email = email
    custom_id = enterprise_id
    auth.password = password
    auth.save!
    'success'
  else
    'Not creating client as client name exists'
  end
end

#check_admin(username) ⇒ Boolean

check user is admin or client

Parameters:

  • username (String)

    The username of the client

Returns:

  • (Boolean)

    Is Admin or not



97
98
99
100
# File 'lib/octocore-cassandra/helpers/client_helper.rb', line 97

def check_admin(username)
  consumer = fetch_consumer(username)
  consumer.admin
end

#destroy_session(username) ⇒ Object

Destroy client session

Parameters:

  • username (String)

    The name of the client



88
89
90
91
92
# File 'lib/octocore-cassandra/helpers/client_helper.rb', line 88

def destroy_session(username)
  consumer = fetch_consumer(username)
  consumer.session_token = nil
  consumer.save!
end

#enterprise_name_exists?(enterprise_name) ⇒ Boolean

check enterprise exist

Parameters:

  • enterprise_name (String)

    The name of the enterprise

Returns:

  • (Boolean)

    Exist or not



54
55
56
# File 'lib/octocore-cassandra/helpers/client_helper.rb', line 54

def enterprise_name_exists?(enterprise_name)
  Octo::Enterprise.all.select { |x| x.name == enterprise_name}.length > 0
end

#fetch_consumer(username) ⇒ Hash

fetch client data

Parameters:

  • username (String)

    The name of the client

Returns:

  • (Hash)

    Client Data



61
62
63
# File 'lib/octocore-cassandra/helpers/client_helper.rb', line 61

def fetch_consumer(username)
  Octo::Authorization.where(username: username).first
end

#save_session(username) ⇒ String

Create new client session

Parameters:

  • username (String)

    The name of the client

Returns:



79
80
81
82
83
84
# File 'lib/octocore-cassandra/helpers/client_helper.rb', line 79

def save_session(username)
  consumer = fetch_consumer(username)
  consumer.session_token = SecureRandom.hex
  consumer.save!
  consumer.session_token.to_s
end

#validate_password(username, password) ⇒ Boolean

Validate Client authentication

Parameters:

  • username (String)

    The name of the client

  • password (String)

    The password of the client

Returns:

  • (Boolean)

    Authenticated or not



45
46
47
48
49
# File 'lib/octocore-cassandra/helpers/client_helper.rb', line 45

def validate_password( username, password)
  consumer = fetch_consumer(username)
  hash_password = Digest::SHA1.hexdigest(password + consumer.enterprise_id)
  hash_password == consumer.password
end

#validate_token(username, token) ⇒ Boolean

Validate Client session

Parameters:

  • username (String)

    The name of the client

  • token (String)

    The session token of the client

Returns:

  • (Boolean)

    Authenticated or not



69
70
71
72
73
74
# File 'lib/octocore-cassandra/helpers/client_helper.rb', line 69

def validate_token(username, token)
  Octo::Authorization.all.select do |x| 
    x.username == username &&
    x.session_token == token
  end.length > 0
end