Skip to content

Commit

Permalink
Use getUserById to find the user when given a User object in adopt_user.
Browse files Browse the repository at this point in the history
  • Loading branch information
Timon Tschanz committed Jul 31, 2014
1 parent de31763 commit 62ff486
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
1.2.2dev (unreleased)
---------------------

- Use getUserById to find the user when given a User object in adopt_user.
[tschanzt]

- Made ``api.portal.get_localized_time`` also work with datetime.date
[nightmarebadger]

Expand Down
9 changes: 4 additions & 5 deletions src/plone/api/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ def adopt_user(username=None, user=None):
:type username: string
:Example: :ref:`env_adopt_user_example`
"""

if username is None:
username = user.getId()

# Grab the user object out of acl_users because this function
# accepts 'user' objects that are actually things like MemberData
# objects, which AccessControl isn't so keen on.
acl_users = portal.get().acl_users
unwrapped = acl_users.getUser(username)
if username is None:
unwrapped = acl_users.getUserById(user.getId())
else:
unwrapped = acl_users.getUser(username)
if unwrapped is None:
raise UserNotFoundError

Expand Down
7 changes: 6 additions & 1 deletion src/plone/api/tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from OFS.SimpleItem import SimpleItem
from plone import api
from plone.api.tests.base import INTEGRATION_TESTING

from plone.app.testing import TEST_USER_ID
import AccessControl
import AccessControl.SecurityManagement
import Globals
Expand Down Expand Up @@ -414,3 +414,8 @@ def test_zope_version(self):
zope_version(),
'^(\d+\.\d+|\d+\.\d+\.\d+)(a\d+|b\d+|rc\d+)?$'
)

def test_adopt_user_different_username(self):
user = api.user.get(userid=TEST_USER_ID)
with api.env.adopt_user(user=user):
self.assertEqual(api.user.get_current().getId(), TEST_USER_ID)

0 comments on commit 62ff486

Please sign in to comment.