Module: Octo::Helpers::ClientHelper
- Defined in:
- lib/octocore-cassandra/helpers/client_helper.rb
Instance Method Summary collapse
-
#add_consumer(username, email, password) ⇒ String
Create a new Client.
-
#check_admin(username) ⇒ Boolean
check user is admin or client.
-
#destroy_session(username) ⇒ Object
Destroy client session.
-
#enterprise_name_exists?(enterprise_name) ⇒ Boolean
check enterprise exist.
-
#fetch_consumer(username) ⇒ Hash
fetch client data.
-
#save_session(username) ⇒ String
Create new client session.
-
#validate_password(username, password) ⇒ Boolean
Validate Client authentication.
-
#validate_token(username, token) ⇒ Boolean
Validate Client session.
Instance Method Details
#add_consumer(username, email, password) ⇒ String
Create a new Client
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
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
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
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
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
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
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
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 |