Skip to content

Commit 567199f

Browse files
committed
RUBY-577: add_user support for roles and userSource
1 parent b75530f commit 567199f

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

lib/mongo/db.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,19 +193,30 @@ def remove_stored_function(function_name)
193193
end
194194

195195
# Adds a user to this database for use with authentication. If the user already
196-
# exists in the system, the password will be updated.
196+
# exists in the system, the password and any additional fields provided in opts
197+
# will be updated.
197198
#
198199
# @param [String] username
199200
# @param [String] password
200201
# @param [Boolean] read_only
201202
# Create a read-only user.
202203
#
204+
# @param [Hash] opts
205+
# Optional fields for the user document (e.g. +userSource+, or +roles+)
206+
#
207+
# See {http://docs.mongodb.org/manual/reference/privilege-documents}
208+
# for more information.
209+
#
210+
# @note The use of the opts argument to provide or update additional fields
211+
# on the user document requires MongoDB >= 2.4.0
212+
#
203213
# @return [Hash] an object representing the user.
204-
def add_user(username, password, read_only = false)
214+
def add_user(username, password=nil, read_only=false, opts={})
205215
users = self[SYSTEM_USER_COLLECTION]
206216
user = users.find_one({:user => username}) || {:user => username}
207-
user['pwd'] = Mongo::Support.hash_password(username, password)
208-
user['readOnly'] = true if read_only;
217+
user['pwd'] = Mongo::Support.hash_password(username, password) if password
218+
user['readOnly'] = true if read_only
219+
user.merge!(opts)
209220
begin
210221
users.save(user)
211222
rescue OperationFailure => ex

0 commit comments

Comments
 (0)