Plone:通知用户删除他的帐户

标签 plone plone-4.x

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

<subscriber
   for="* Products.PluggableAuthService.interfaces.events.IPrincipalDeletedEvent"
   handler="mycontent.userDeleted" />

https://github.com/plone/Products.PlonePAS/blob/4.2/Products/PlonePAS/pas.py#L78
api.user.get(userid=user_id)是 None 当我的 userDeleted(user_id, event)叫做。

似乎为删除的用户添加内容规则的工作方式相同。

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

最佳答案

猴子补丁在用户被删除之前添加一个事件:

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>

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

然后为这个事件添加了一个订阅者:

configure.zcml :
<subscriber
    for="* mycontent.patches.IPrincipalBeforeDeletedEvent"
    handler="mycontent.globalhandlers.userBeforeDeleted" />

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

关于Plone:通知用户删除他的帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48297612/

相关文章:

plone - 如何让模式小部件在 iframe 叠加层中工作

svn - Products.Poi 和 Subversion 集成

Plone:灵巧内容类型默认 View 模板

plone - 如何设置通过通用设置(结构)导入的内容的默认 View

plone - 在Plone中创建一个文件夹并设置uid

Plone更新收集过滤器

jquery - 如何调试Plone的portal_javascripts压缩导致的js错误

python - Plone - 在带有标题/作者信息的 View 中,为什么没有所有者角色的用户隐藏历史链接?

python - collective.googleanalytics 报告中的自定义维度

plone - 如何在 Plone 添加/编辑表单中隐藏字段集(选项卡)