Skip to content

Commit 1e802b3

Browse files
committed
PYTHON-1310, PYTHON-1311 - Deprecate add_user and remove_user
Also document what to use instead.
1 parent 1227f55 commit 1e802b3

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

doc/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ Deprecations:
3535
- The `useCursor` option for :meth:`~pymongo.collection.Collection.aggregate`
3636
is deprecated. The option was only necessary when upgrading from MongoDB
3737
2.4 to MongoDB 2.6. MongoDB 2.4 is no longer supported.
38+
- The :meth:`~pymongo.database.Database.add_user` and
39+
:meth:`~pymongo.database.Database.remove_user` methods are deprecated. See
40+
the method docstrings for alternatives.
3841

3942
Breaking changes include:
4043

pymongo/database.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -978,12 +978,38 @@ def _create_or_update_user(
978978

979979
def add_user(self, name, password=None, read_only=None, session=None,
980980
**kwargs):
981-
"""Create user `name` with password `password`.
981+
"""**DEPRECATED**: Create user `name` with password `password`.
982982
983983
Add a new user with permissions for this :class:`Database`.
984984
985985
.. note:: Will change the password if user `name` already exists.
986986
987+
.. note:: add_user is deprecated and will be removed in PyMongo
988+
4.0. Starting with MongoDB 2.6 user management is handled with four
989+
database commands, createUser_, usersInfo_, updateUser_, and
990+
dropUser_.
991+
992+
To create a user::
993+
994+
db.command("createUser", "admin", pwd="password", roles=["root"])
995+
996+
To create a read-only user::
997+
998+
db.command("createUser", "user", pwd="password", roles=["read"])
999+
1000+
To change a password::
1001+
1002+
db.command("updateUser", "user", pwd="newpassword")
1003+
1004+
Or change roles::
1005+
1006+
db.command("updateUser", "user", roles=["readWrite"])
1007+
1008+
.. _createUser: https://docs.mongodb.com/manual/reference/command/createUser/
1009+
.. _usersInfo: https://docs.mongodb.com/manual/reference/command/usersInfo/
1010+
.. _updateUser: https://docs.mongodb.com/manual/reference/command/updateUser/
1011+
.. _dropUser: https://docs.mongodb.com/manual/reference/command/createUser/
1012+
9871013
:Parameters:
9881014
- `name`: the name of the user to create
9891015
- `password` (optional): the password of the user to create. Can not
@@ -997,14 +1023,17 @@ def add_user(self, name, password=None, read_only=None, session=None,
9971023
:class:`~pymongo.client_session.ClientSession`.
9981024
9991025
.. versionchanged:: 3.6
1000-
Added ``session`` parameter.
1026+
Added ``session`` parameter. Deprecated add_user.
10011027
10021028
.. versionchanged:: 2.5
10031029
Added kwargs support for optional fields introduced in MongoDB 2.4
10041030
10051031
.. versionchanged:: 2.2
10061032
Added support for read only users
10071033
"""
1034+
warnings.warn("add_user is deprecated and will be removed in PyMongo "
1035+
"4.0. Use db.command with createUser or updateUser "
1036+
"instead", DeprecationWarning, stacklevel=2)
10081037
if not isinstance(name, string_type):
10091038
raise TypeError("name must be an "
10101039
"instance of %s" % (string_type.__name__,))
@@ -1036,19 +1065,27 @@ def add_user(self, name, password=None, read_only=None, session=None,
10361065
raise
10371066

10381067
def remove_user(self, name, session=None):
1039-
"""Remove user `name` from this :class:`Database`.
1068+
"""**DEPRECATED**: Remove user `name` from this :class:`Database`.
10401069
10411070
User `name` will no longer have permissions to access this
10421071
:class:`Database`.
10431072
1073+
.. note:: remove_user is deprecated and will be removed in PyMongo
1074+
4.0. Use the dropUser command instead::
1075+
1076+
db.command("dropUser", "user")
1077+
10441078
:Parameters:
10451079
- `name`: the name of the user to remove
10461080
- `session` (optional): a
10471081
:class:`~pymongo.client_session.ClientSession`.
10481082
10491083
.. versionchanged:: 3.6
1050-
Added ``session`` parameter.
1084+
Added ``session`` parameter. Deprecated remove_user.
10511085
"""
1086+
warnings.warn("remove_user is deprecated and will be removed in "
1087+
"PyMongo 4.0. Use db.command with dropUser "
1088+
"instead", DeprecationWarning, stacklevel=2)
10521089
cmd = SON([("dropUser", name)])
10531090
# Don't send {} as writeConcern.
10541091
if self.write_concern.acknowledged and self.write_concern.document:

0 commit comments

Comments
 (0)