Skip to content

Commit 2565b4d

Browse files
authored
PYTHON-1312 Remove Database.add_user and Database.remove_user (#561)
1 parent 1cf8eb3 commit 2565b4d

File tree

5 files changed

+40
-331
lines changed

5 files changed

+40
-331
lines changed

doc/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Breaking Changes in 4.0
2727
:meth:`pymongo.database.Database.last_status`,
2828
:meth:`pymongo.database.Database.previous_error`,
2929
:meth:`pymongo.database.Database.reset_error_history`.
30+
- Removed :meth:`pymongo.database.Database.add_user` and
31+
:meth:`pymongo.database.Database.remove_user`.
3032
- Removed :meth:`pymongo.collection.Collection.parallel_scan`.
3133
- Removed :meth:`pymongo.collection.Collection.ensure_index`.
3234
- Removed :meth:`pymongo.collection.Collection.reindex`.

doc/migrate-to-pymongo4.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,39 @@ can be changed to this::
136136
names = client.list_collection_names()
137137
non_system_names = client.list_collection_names(filter={"name": {"$regex": r"^(?!system\\.)"}})
138138

139+
Database.add_user is removed
140+
............................
141+
142+
Removed :meth:`pymongo.database.Database.add_user` which was deprecated in
143+
PyMongo 3.6. Use the `createUser command`_ or `updateUser command`_ instead.
144+
To create a user::
145+
146+
db.command("createUser", "admin", pwd="password", roles=["dbAdmin"])
147+
148+
To create a read-only user::
149+
150+
db.command("createUser", "user", pwd="password", roles=["read"])
151+
152+
To change a password::
153+
154+
db.command("updateUser", "user", pwd="newpassword")
155+
156+
Or change roles::
157+
158+
db.command("updateUser", "user", roles=["readWrite"])
159+
160+
.. _createUser command: https://docs.mongodb.com/manual/reference/command/createUser/
161+
.. _updateUser command: https://docs.mongodb.com/manual/reference/command/updateUser/
162+
163+
Database.remove_user is removed
164+
...............................
165+
166+
Removed :meth:`pymongo.database.Database.remove_user` which was deprecated in
167+
PyMongo 3.6. Use the `dropUser command`_ instead::
168+
169+
db.command("dropUser", "user")
170+
171+
.. _dropUser command: https://docs.mongodb.com/manual/reference/command/createUser/
139172

140173
Collection
141174
----------

pymongo/database.py

Lines changed: 1 addition & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
from pymongo.collection import Collection
2626
from pymongo.command_cursor import CommandCursor
2727
from pymongo.errors import (CollectionInvalid,
28-
ConfigurationError,
29-
InvalidName,
30-
OperationFailure)
28+
InvalidName)
3129
from pymongo.message import _first_batch
3230
from pymongo.read_preferences import ReadPreference
3331

@@ -982,175 +980,6 @@ def __next__(self):
982980

983981
next = __next__
984982

985-
def _default_role(self, read_only):
986-
"""Return the default user role for this database."""
987-
if self.name == "admin":
988-
if read_only:
989-
return "readAnyDatabase"
990-
else:
991-
return "root"
992-
else:
993-
if read_only:
994-
return "read"
995-
else:
996-
return "dbOwner"
997-
998-
def _create_or_update_user(
999-
self, create, name, password, read_only, session=None, **kwargs):
1000-
"""Use a command to create (if create=True) or modify a user.
1001-
"""
1002-
opts = {}
1003-
if read_only or (create and "roles" not in kwargs):
1004-
warnings.warn("Creating a user with the read_only option "
1005-
"or without roles is deprecated in MongoDB "
1006-
">= 2.6", DeprecationWarning)
1007-
1008-
opts["roles"] = [self._default_role(read_only)]
1009-
1010-
if read_only:
1011-
warnings.warn("The read_only option is deprecated in MongoDB "
1012-
">= 2.6, use 'roles' instead", DeprecationWarning)
1013-
1014-
if password is not None:
1015-
if "digestPassword" in kwargs:
1016-
raise ConfigurationError("The digestPassword option is not "
1017-
"supported via add_user. Please use "
1018-
"db.command('createUser', ...) "
1019-
"instead for this option.")
1020-
opts["pwd"] = password
1021-
1022-
# Don't send {} as writeConcern.
1023-
if self.write_concern.acknowledged and self.write_concern.document:
1024-
opts["writeConcern"] = self.write_concern.document
1025-
opts.update(kwargs)
1026-
1027-
if create:
1028-
command_name = "createUser"
1029-
else:
1030-
command_name = "updateUser"
1031-
1032-
self.command(command_name, name, session=session, **opts)
1033-
1034-
def add_user(self, name, password=None, read_only=None, session=None,
1035-
**kwargs):
1036-
"""**DEPRECATED**: Create user `name` with password `password`.
1037-
1038-
Add a new user with permissions for this :class:`Database`.
1039-
1040-
.. note:: Will change the password if user `name` already exists.
1041-
1042-
.. note:: add_user is deprecated and will be removed in PyMongo
1043-
4.0. Starting with MongoDB 2.6 user management is handled with four
1044-
database commands, createUser_, usersInfo_, updateUser_, and
1045-
dropUser_.
1046-
1047-
To create a user::
1048-
1049-
db.command("createUser", "admin", pwd="password", roles=["root"])
1050-
1051-
To create a read-only user::
1052-
1053-
db.command("createUser", "user", pwd="password", roles=["read"])
1054-
1055-
To change a password::
1056-
1057-
db.command("updateUser", "user", pwd="newpassword")
1058-
1059-
Or change roles::
1060-
1061-
db.command("updateUser", "user", roles=["readWrite"])
1062-
1063-
.. _createUser: https://docs.mongodb.com/manual/reference/command/createUser/
1064-
.. _usersInfo: https://docs.mongodb.com/manual/reference/command/usersInfo/
1065-
.. _updateUser: https://docs.mongodb.com/manual/reference/command/updateUser/
1066-
.. _dropUser: https://docs.mongodb.com/manual/reference/command/createUser/
1067-
1068-
.. warning:: Never create or modify users over an insecure network without
1069-
the use of TLS. See :doc:`/examples/tls` for more information.
1070-
1071-
:Parameters:
1072-
- `name`: the name of the user to create
1073-
- `password` (optional): the password of the user to create. Can not
1074-
be used with the ``userSource`` argument.
1075-
- `read_only` (optional): if ``True`` the user will be read only
1076-
- `**kwargs` (optional): optional fields for the user document
1077-
(e.g. ``userSource``, ``otherDBRoles``, or ``roles``). See
1078-
`<http://docs.mongodb.org/manual/reference/privilege-documents>`_
1079-
for more information.
1080-
- `session` (optional): a
1081-
:class:`~pymongo.client_session.ClientSession`.
1082-
1083-
.. versionchanged:: 3.7
1084-
Added support for SCRAM-SHA-256 users with MongoDB 4.0 and later.
1085-
1086-
.. versionchanged:: 3.6
1087-
Added ``session`` parameter. Deprecated add_user.
1088-
1089-
.. versionchanged:: 2.5
1090-
Added kwargs support for optional fields introduced in MongoDB 2.4
1091-
1092-
.. versionchanged:: 2.2
1093-
Added support for read only users
1094-
"""
1095-
warnings.warn("add_user is deprecated and will be removed in PyMongo "
1096-
"4.0. Use db.command with createUser or updateUser "
1097-
"instead", DeprecationWarning, stacklevel=2)
1098-
if not isinstance(name, str):
1099-
raise TypeError("name must be an instance of str")
1100-
if password is not None:
1101-
if not isinstance(password, str):
1102-
raise TypeError("password must be an instance of str")
1103-
if len(password) == 0:
1104-
raise ValueError("password can't be empty")
1105-
if read_only is not None:
1106-
read_only = common.validate_boolean('read_only', read_only)
1107-
if 'roles' in kwargs:
1108-
raise ConfigurationError("Can not use "
1109-
"read_only and roles together")
1110-
1111-
try:
1112-
uinfo = self.command("usersInfo", name, session=session)
1113-
# Create the user if not found in uinfo, otherwise update one.
1114-
self._create_or_update_user(
1115-
(not uinfo["users"]), name, password, read_only,
1116-
session=session, **kwargs)
1117-
except OperationFailure as exc:
1118-
# Unauthorized. Attempt to create the user in case of
1119-
# localhost exception.
1120-
if exc.code == 13:
1121-
self._create_or_update_user(
1122-
True, name, password, read_only, session=session, **kwargs)
1123-
else:
1124-
raise
1125-
1126-
def remove_user(self, name, session=None):
1127-
"""**DEPRECATED**: Remove user `name` from this :class:`Database`.
1128-
1129-
User `name` will no longer have permissions to access this
1130-
:class:`Database`.
1131-
1132-
.. note:: remove_user is deprecated and will be removed in PyMongo
1133-
4.0. Use the dropUser command instead::
1134-
1135-
db.command("dropUser", "user")
1136-
1137-
:Parameters:
1138-
- `name`: the name of the user to remove
1139-
- `session` (optional): a
1140-
:class:`~pymongo.client_session.ClientSession`.
1141-
1142-
.. versionchanged:: 3.6
1143-
Added ``session`` parameter. Deprecated remove_user.
1144-
"""
1145-
warnings.warn("remove_user is deprecated and will be removed in "
1146-
"PyMongo 4.0. Use db.command with dropUser "
1147-
"instead", DeprecationWarning, stacklevel=2)
1148-
cmd = SON([("dropUser", name)])
1149-
# Don't send {} as writeConcern.
1150-
if self.write_concern.acknowledged and self.write_concern.document:
1151-
cmd["writeConcern"] = self.write_concern.document
1152-
self.command(cmd, session=session)
1153-
1154983
def authenticate(self, name=None, password=None,
1155984
source=None, mechanism='DEFAULT', **kwargs):
1156985
"""**DEPRECATED**: Authenticate to use this database.

0 commit comments

Comments
 (0)