Add read_only parameter to add_user

This commit is contained in:
Ariel Salomon 2012-02-12 13:14:18 -08:00
parent 4a92a1e498
commit 9eadb3c24a
3 changed files with 17 additions and 1 deletions

View File

@ -175,10 +175,11 @@ module Mongo
# @param [String] password # @param [String] password
# #
# @return [Hash] an object representing the user. # @return [Hash] an object representing the user.
def add_user(username, password) def add_user(username, password, read_only = false)
users = self[SYSTEM_USER_COLLECTION] users = self[SYSTEM_USER_COLLECTION]
user = users.find_one({:user => username}) || {:user => username} user = users.find_one({:user => username}) || {:user => username}
user['pwd'] = Mongo::Support.hash_password(username, password) user['pwd'] = Mongo::Support.hash_password(username, password)
user['readOnly'] = true if read_only;
users.save(user) users.save(user)
return user return user
end end

View File

@ -26,6 +26,7 @@ class AuthenticationTest < Test::Unit::TestCase
@admin.authenticate('bob', 'secret') @admin.authenticate('bob', 'secret')
@db1.add_user('user1', 'secret') @db1.add_user('user1', 'secret')
@db2.add_user('user2', 'secret') @db2.add_user('user2', 'secret')
@db2.add_user('userRO', 'secret', true) # read-only
@admin.logout @admin.logout
assert_raise Mongo::OperationFailure do assert_raise Mongo::OperationFailure do
@ -53,6 +54,7 @@ class AuthenticationTest < Test::Unit::TestCase
assert @db1['stuff'].insert({:a => 2}, :safe => true) assert @db1['stuff'].insert({:a => 2}, :safe => true)
assert @db2['stuff'].insert({:a => 2}, :safe => true) assert @db2['stuff'].insert({:a => 2}, :safe => true)
assert @db2['stuff'].find(:safe => true)
@db1.logout @db1.logout
assert_raise Mongo::OperationFailure do assert_raise Mongo::OperationFailure do
@ -63,6 +65,12 @@ class AuthenticationTest < Test::Unit::TestCase
assert_raise Mongo::OperationFailure do assert_raise Mongo::OperationFailure do
assert @db2['stuff'].insert({:a => 2}, :safe => true) assert @db2['stuff'].insert({:a => 2}, :safe => true)
end end
@db2.authenticate('userRO', 'secret')
assert @db2['stuff'].find(:safe => true)
assert_raise Mongo::OperationFailure do
assert @db2['stuff'].insert({:a => 2}, :safe => true)
end
end end
end end

View File

@ -151,6 +151,13 @@ class DBTest < Test::Unit::TestCase
@@db.remove_user('foo:bar') @@db.remove_user('foo:bar')
end end
def test_authenticate_read_only
@@db.add_user('joebob', 'user', true) # read-only user
assert @@db.authenticate('joebob', 'user')
@@db.logout
@@db.remove_user('joebob')
end
def test_authenticate_with_connection_uri def test_authenticate_with_connection_uri
@@db.add_user('spongebob', 'squarepants') @@db.add_user('spongebob', 'squarepants')
assert Mongo::Connection.from_uri("mongodb://spongebob:squarepants@#{host_port}/#{@@db.name}") assert Mongo::Connection.from_uri("mongodb://spongebob:squarepants@#{host_port}/#{@@db.name}")