Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions lib/moped/authenticatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ module Authenticatable
# @example Apply the authentication credentials.
# node.apply_credentials({ "moped_test" => [ "user", "pass" ]})
#
# @param [ Hash ] credentials The authentication credentials in the form:
# @param [ Hash ] logins The authentication credentials in the form:
# { database_name: [ user, password ]}
#
# @return [ Object ] The authenticated object.
#
# @since 2.0.0
def apply_credentials(logins)
unless credentials == logins
logouts = credentials.keys - logins.keys
logouts.each do |database|
logout(database)
end
logins.each do |database, (username, password)|
unless credentials[database] == [ username, password ]
login(database, username, password)
end
logouts = credentials.keys - logins.keys
logouts.each do |database|
logout(database)
end
logins.each do |database, (username, password)|
unless logged_in_databases[database] && credentials[database] == [ username, password ]
login(database, username, password)
end
end
self
Expand All @@ -44,6 +42,10 @@ def credentials
@credentials ||= {}
end

def logged_in_databases
@logged_in_databases ||= {}
end

# Login the user to the provided database with the supplied password.
#
# @example Login the user to the database.
Expand Down Expand Up @@ -73,6 +75,7 @@ def login(database, username, password)

raise Errors::AuthenticationFailure.new(authenticate, document) unless document["ok"] == 1
credentials[database] = [username, password]
logged_in_databases[database] = true
end

# Logout the user from the provided database.
Expand All @@ -93,6 +96,7 @@ def logout(database)
return
end
credentials.delete(database)
logged_in_databases.delete(database)
end
end
end
2 changes: 1 addition & 1 deletion lib/moped/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def connected?
#
# @since 1.0.0
def disconnect
credentials.clear
logged_in_databases.clear
@sock.close
rescue
ensure
Expand Down