Skip to content

Commit a1f7a54

Browse files
author
Luke Lovett
committed
PYTHON-714 Work around localhost exception issues in add_user when connected to MongoDB >= 2.7.1.
1 parent 952953d commit a1f7a54

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

pymongo/database.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -790,17 +790,20 @@ def add_user(self, name, password=None, read_only=None, **kwargs):
790790
try:
791791
uinfo = self.command("usersInfo", name,
792792
read_preference=ReadPreference.PRIMARY)
793+
self._create_or_update_user(
794+
(not uinfo["users"]), name, password, read_only, **kwargs)
793795
except OperationFailure, exc:
794796
# MongoDB >= 2.5.3 requires the use of commands to manage
795797
# users.
796798
if exc.code in common.COMMAND_NOT_FOUND_CODES:
797799
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
804807

805808
def remove_user(self, name):
806809
"""Remove user `name` from this :class:`Database`.

0 commit comments

Comments
 (0)