Merge pull request #88 from Oscil8/master
Add read_only option to DB#add_user
This commit is contained in:
commit
aecad5d345
|
@ -173,12 +173,15 @@ module Mongo
|
||||||
#
|
#
|
||||||
# @param [String] username
|
# @param [String] username
|
||||||
# @param [String] password
|
# @param [String] password
|
||||||
|
# @param [Boolean] read_only
|
||||||
|
# Create a read-only user.
|
||||||
#
|
#
|
||||||
# @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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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}")
|
||||||
|
|
Loading…
Reference in New Issue