Plone:通知用户删除其帐户

Ghi*_*taB 4 plone plone-4.x

在IPrincipalDeletedEvent上使用订户不是解决方案,因为用户已被删除,我无法获取他的电子邮件地址.

<subscriber
   for="* Products.PluggableAuthService.interfaces.events.IPrincipalDeletedEvent"
   handler="mycontent.userDeleted" />
Run Code Online (Sandbox Code Playgroud)

https://github.com/plone/Products.PlonePAS/blob/4.2/Products/PlonePAS/pas.py#L78

api.user.get(userid=user_id)当我userDeleted(user_id, event)打电话时是无.

似乎为删除的用户添加内容规则是一样的.

知道如何在他的帐户被标记为删除时获取用户的电子邮件地址吗?我只想给他发一封电子邮件:您的帐户已根据您的要求删除了.

Ghi*_*taB 7

猴子修补以在删除用户之前添加事件:

patches.zcml:

<configure xmlns="http://namespaces.zope.org/zope"
           xmlns:monkey="http://namespaces.plone.org/monkey"
           xmlns:zcml="http://namespaces.zope.org/zcml"
           i18n_domain="myapp">

    <include package="collective.monkeypatcher" />
    <include package="collective.monkeypatcher" file="meta.zcml" />


    <monkey:patch description="Add PrincipalBeforeDeleted event"
                  class="Products.PlonePAS.pas"
                  original="_doDelUser"
                  replacement="mycontent.patches._doDelUser"
                  docstringWarning="true" />
</configure>
Run Code Online (Sandbox Code Playgroud)

patches.py:

from zope.event import notify
from Products.PluggableAuthService.events import PrincipalDeleted
from Products.PlonePAS.interfaces.plugins import IUserManagement
from Products.PluggableAuthService.PluggableAuthService import \
     _SWALLOWABLE_PLUGIN_EXCEPTIONS
from Products.PluggableAuthService.PluggableAuthService import \
    PluggableAuthService

from Products.PlonePAS.pas import _doDelUser
from Products.PluggableAuthService.interfaces.events import IPASEvent

from zope.interface import implements
from Products.PluggableAuthService.events import PASEvent


class IPrincipalBeforeDeletedEvent(IPASEvent):
    """A user is marked to be removed but still into database.
    """


class PrincipalBeforeDeleted(PASEvent):
    implements(IPrincipalBeforeDeletedEvent)


def _doDelUser(self, id):
    """
    Given a user id, hand off to a deleter plugin if available.
    Fix: Add PrincipalBeforeDeleted notification
    """
    plugins = self._getOb('plugins')
    userdeleters = plugins.listPlugins(IUserManagement)

    if not userdeleters:
        raise NotImplementedError(
            "There is no plugin that can delete users.")

    for userdeleter_id, userdeleter in userdeleters:
        # vvv Custom
        notify(PrincipalBeforeDeleted(id))
        # ^^^ Custom

        try:
            userdeleter.doDeleteUser(id)
        except _SWALLOWABLE_PLUGIN_EXCEPTIONS:
            pass
        else:
            notify(PrincipalDeleted(id))


PluggableAuthService._doDelUser = _doDelUser
Run Code Online (Sandbox Code Playgroud)

然后为此活动添加了订阅者:

configure.zcml:

<subscriber
    for="* mycontent.patches.IPrincipalBeforeDeletedEvent"
    handler="mycontent.globalhandlers.userBeforeDeleted" />
Run Code Online (Sandbox Code Playgroud)

globalhandlers.py:

from Products.CMFCore.utils import getToolByName


def handleEventFail(func):
    def wrapper(*args, **kwargs):
        try:
            func(*args, **kwargs)
        except Exception:
            logger.exception('in {0}'.format(func.__name__))
    return wrapper


@handleEventFail
def userBeforeDeleted(user_id, event):
    """ Notify deleted user about this action. """
    membership_tool = getToolByName(api.portal.get(), 'portal_membership')
    user = membership_tool.getMemberById(user_id)
    email = user.getProperty('email')
    mail_text = """
Hi!
Your account ({0}) was deleted.

Best regards,
Our Best Team""".format(user_id)
    print mail_text
    print email
    # TODO send mail
Run Code Online (Sandbox Code Playgroud)