@@ -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