Skip to content

Commit

Permalink
Merge pull request #53 from plone/backport-510-511
Browse files Browse the repository at this point in the history
Back-port fix for issue #27
  • Loading branch information
mauritsvanrees authored Nov 27, 2019
2 parents fe64a84 + 1aca99d commit 74bdbe2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
5 changes: 5 additions & 0 deletions news/27.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- Notify PropertiesUpdated event when member properties are changed
[ezvirtual]

- Add simple test for PropertiesUpdated event
[ezvirtual]
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

version = '6.0.4.dev0'


longdescription = open("README.rst").read()
longdescription += '\n'
longdescription += open("CHANGES.rst").read()
Expand Down
54 changes: 54 additions & 0 deletions src/Products/PlonePAS/tests/test_memberdatatool.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
from plone.app.testing import TEST_USER_ID as default_user
from Products.PlonePAS.tests import dummy
from Products.PlonePAS.testing import PRODUCTS_PLONEPAS_INTEGRATION_TESTING
from zope.component import getGlobalSiteManager
from zope.component import getMultiAdapter
from Products.CMFCore.interfaces import IMember
from Products.CMFCore.interfaces import IMemberData
from Products.PluggableAuthService.interfaces.events import \
IPropertiesUpdatedEvent

import unittest



class TestMemberDataTool(unittest.TestCase):

layer = PRODUCTS_PLONEPAS_INTEGRATION_TESTING
Expand Down Expand Up @@ -88,3 +93,52 @@ def testMemberDataAdapter(self):

wrapped_user = self.memberdata.wrapUser(member)
self.assertEqual(wrapped_user.__class__, MemberData)

def testPropertiesUpdatedEvent(self):

def event_handler(context, event):
self._properties_updated_handler_called = True

gsm = getGlobalSiteManager()
gsm.registerHandler(event_handler,
(IMemberData, IPropertiesUpdatedEvent))

self._properties_updated_handler_called = False

username = 'ez'
roles = ['Member']
fullname = 'Ez Zy'
email = 'ez@ezmail.net'

self.membership.addMember(username, 'secret', roles, [])
member = self.membership.getMemberById(username)

self.assertFalse(self._properties_updated_handler_called)

member.setMemberProperties({
'fullname': fullname,
'email': email})

self.assertTrue(self._properties_updated_handler_called)

# Test that notify(PropertiesUpdated) isn't called on user login.
self._properties_updated_handler_called = False

# Imitate a login as the plone.app.testing login method doesn't seem to
# set these member properties.
member.setMemberProperties({
'login_time': DateTime('2018-02-15'),
'last_login_time': DateTime('2018-02-15')})

self.assertFalse(self._properties_updated_handler_called)

# Test notify(PropertiesUpdated) isn't called when login_time is
# present as we're assuming this should only be changed on login.
self._properties_updated_handler_called = False
member.setMemberProperties({
'login_time': DateTime('2018-02-15'),
'fullname': 'Bed Rock'})

self.assertFalse(self._properties_updated_handler_called)
gsm.unregisterHandler(event_handler,
(IMemberData, IPropertiesUpdatedEvent))
7 changes: 7 additions & 0 deletions src/Products/PlonePAS/tools/memberdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
from Products.PlonePAS.interfaces.memberdata import IMemberDataTool
from Products.PlonePAS.interfaces.plugins import IUserManagement
from Products.PlonePAS.interfaces.propertysheets import IMutablePropertySheet
from Products.PluggableAuthService.events import PropertiesUpdated
from Products.PluggableAuthService.interfaces.authservice import \
IPluggableAuthService
from Products.PluggableAuthService.interfaces.plugins import IPropertiesPlugin
from Products.PluggableAuthService.interfaces.plugins import \
IRoleAssignerPlugin
from zope.component import adapter
from zope.event import notify
from zope.interface import implementer

import six
Expand Down Expand Up @@ -275,6 +277,11 @@ def setMemberProperties(self, mapping, force_local=0, force_empty=False):
if modified:
self.notifyModified()

# Trigger PropertiesUpdated event when member properties are updated,
# excluding user login events
if not set(mapping.keys()) & set(('login_time', 'last_login_time')):
notify(PropertiesUpdated(self, mapping))

def getProperty(self, id, default=_marker):
"""PAS-specific method to fetch a user's properties. Looks
through the ordered property sheets.
Expand Down

0 comments on commit 74bdbe2

Please sign in to comment.