@@ -790,17 +790,20 @@ def add_user(self, name, password=None, read_only=None, **kwargs):
790
790
try :
791
791
uinfo = self .command ("usersInfo" , name ,
792
792
read_preference = ReadPreference .PRIMARY )
793
+ self ._create_or_update_user (
794
+ (not uinfo ["users" ]), name , password , read_only , ** kwargs )
793
795
except OperationFailure , exc :
794
796
# MongoDB >= 2.5.3 requires the use of commands to manage
795
797
# users.
796
798
if exc .code in common .COMMAND_NOT_FOUND_CODES :
797
799
self ._legacy_add_user (name , password , read_only , ** kwargs )
798
- return
799
- raise
800
-
801
- # Create the user if not found in uinfo, otherwise update one.
802
- self ._create_or_update_user (
803
- (not uinfo ["users" ]), name , password , read_only , ** kwargs )
800
+ # Unauthorized. MongoDB >= 2.7.1 has a narrow localhost exception,
801
+ # and we must add a user before sending commands.
802
+ elif exc .code == 13 :
803
+ self ._create_or_update_user (
804
+ True , name , password , read_only , ** kwargs )
805
+ else :
806
+ raise
804
807
805
808
def remove_user (self , name ):
806
809
"""Remove user `name` from this :class:`Database`.
0 commit comments