Skip to content

Commit ef67b40

Browse files
rozzabehackett
authored andcommitted
Added read_only support to add_user
PYTHON-312
1 parent b7aa005 commit ef67b40

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

pymongo/database.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ def __iter__(self):
537537
def next(self):
538538
raise TypeError("'Database' object is not iterable")
539539

540-
def add_user(self, name, password):
540+
def add_user(self, name, password, read_only=False):
541541
"""Create user `name` with password `password`.
542542
543543
Add a new user with permissions for this :class:`Database`.
@@ -547,13 +547,15 @@ def add_user(self, name, password):
547547
:Parameters:
548548
- `name`: the name of the user to create
549549
- `password`: the password of the user to create
550+
- `read_only` (optional): if ``True`` it will make user read only
550551
551552
.. versionadded:: 1.4
552553
"""
553554
pwd = helpers._password_digest(name, password)
554555
self.system.users.update({"user": name},
555556
{"user": name,
556-
"pwd": pwd},
557+
"pwd": pwd,
558+
"readOnly": read_only},
557559
upsert=True, safe=True)
558560

559561
def remove_user(self, name):

test/test_database.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ def test_authenticate_add_remove_user(self):
292292
self.assertFalse(db.authenticate("Gustave", u"Dor\xe9"))
293293
self.assert_(db.authenticate("Gustave", u"password"))
294294

295+
db.add_user("Ross", "password", read_only=True)
296+
self.assert_(db.authenticate("Ross", u"password"))
297+
self.assert_(db.system.users.find({"readOnly": True}).count())
298+
295299
# just make sure there are no exceptions here
296300
db.logout()
297301
db.logout()

0 commit comments

Comments
 (0)