Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[rb] Support authenticating directly against a db.
  • Loading branch information
bobthecow committed Nov 24, 2012
1 parent 1758705 commit e5e36c6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Expand Up @@ -7,6 +7,7 @@
* Handle crazy characters in collection names (PHP backend) – See #56
* Better heuristic for guessing document creation date from ObjectId — See #55
* Catch more connection auth errors (Ruby backend).
* Support authenticating directly against a DB for non-admin users (Ruby backend) — See #16


## v2.1.4
Expand Down
21 changes: 20 additions & 1 deletion genghis.rb
Expand Up @@ -408,10 +408,20 @@ def initialize(dsn)

# name this server something useful
name = uri.host

if user = uri.auths.map{|a| a['username']}.first
name = "#{user}@#{name}"
end

name = "#{name}:#{uri.port}" unless uri.port == 27017

if db = uri.auths.map{|a| a['db_name']}.first
unless db == 'admin'
name = "#{name}/#{db}"
@db = db
end
end

@name = name
rescue Mongo::MongoArgumentError => e
@error = "Malformed server DSN: #{e.message}"
Expand Down Expand Up @@ -510,7 +520,16 @@ def connection
end

def info
@info ||= connection['admin'].command({:listDatabases => true})
@info ||= begin
if @db.nil?
connection['admin'].command({:listDatabases => true})
else
{
'databases' => [{'name' => @db}],
'totalSize' => connection[@db].stats['fileSize']
}
end
end
end
end
end
Expand Down
21 changes: 20 additions & 1 deletion src/rb/genghis/models/server.rb
Expand Up @@ -16,10 +16,20 @@ def initialize(dsn)

# name this server something useful
name = uri.host

if user = uri.auths.map{|a| a['username']}.first
name = "#{user}@#{name}"
end

name = "#{name}:#{uri.port}" unless uri.port == 27017

if db = uri.auths.map{|a| a['db_name']}.first
unless db == 'admin'
name = "#{name}/#{db}"
@db = db
end
end

@name = name
rescue Mongo::MongoArgumentError => e
@error = "Malformed server DSN: #{e.message}"
Expand Down Expand Up @@ -118,7 +128,16 @@ def connection
end

def info
@info ||= connection['admin'].command({:listDatabases => true})
@info ||= begin
if @db.nil?
connection['admin'].command({:listDatabases => true})
else
{
'databases' => [{'name' => @db}],
'totalSize' => connection[@db].stats['fileSize']
}
end
end
end
end
end
Expand Down

0 comments on commit e5e36c6

Please sign in to comment.