1

是否可以根据 Plone 中的用户角色来制作重氮主题的元素?例如:我想在主题中为某些特定角色提供不同的标题图像,并在用户登录后立即在网站上进行这些更改。

这个问题可能是相关的,但我更愿意只通过角色分配来管理它。

4

2 回答 2

5

这可以通过指定主题参数来实现。未经测试,但您可以定义如下参数:

roles = python: portal_state.member().getRolesInContext(context)

或类似的东西:

is_manager = python: 'Manager' in portal_state.member().getRolesInContext(context)

然后在您的 rules.xml 文件中使用该参数。这将为经理关闭主题:

<notheme if="$is_manager" />

这与标题无关,但您应该能够从中推断。

于 2013-07-18T21:55:03.180 回答
1

如果您知道如何处理 Python 代码并创建浏览器视图,则可以定义一个返回一些 CSS 的浏览器视图。我在客户端项目中使用以下代码插入了一些将最近的设置header.jpg为背景的css,因此您可以在不同的部分有不同的背景。

在 configure.zcml 中:

<browser:page
    for="*"
    permission="zope.Public"
    name="header-image-css"
    class=".utils.HeaderImageCSS"
    />

在 utils.py 文件中:

HEADER_IMAGE_CSS = """
#portal-header {
    background: url("%s") no-repeat scroll right top #FFFFFF;
    position: relative;
    z-index: 2;
}

"""


class HeaderImageCSS(BrowserView):
    """CSS for header image.

    We want the nearest header.jpg in the acquisition context.  For
    caching it is best to look up the image and return its
    absolute_url, instead of simply loading header.jpg in the current
    context.  It will work, but then the same image will be loaded by
    the browser for lots of different pages.

    This is meant to be used in the rules.xml of the Diazo theme, like this:

    <after css:theme="title" css:content="style" href="@@header-image-css" />

    Because we set the Content-Type header to text/css, diazo will put
    a 'style' tag around it.  Nice.
    """

    def __call__(self):
        image = self.context.restrictedTraverse('header.jpg', None)
        if image is None:
            url = ''
        else:
            url = image.absolute_url()
        self.request.RESPONSE.setHeader('Content-Type', 'text/css')
        return HEADER_IMAGE_CSS % url

对于您的用例,您可以获得这样的角色,然后根据该信息返回不同的 css(代码未经测试):

def __call__(self):
    from zope.component import getMultiAdapter
    pps = getMultiAdapter((self.context, self.request), name='plone_portal_state')
    member = pps.member()
    roles = member.getRolesInContext(self.context)
    css = "#content {background-color: %s}"
    if 'Manager' in roles:
        color = 'red'
    elif 'Reviewer' in roles:
        color = 'blue'
    else:
        color = 'yellow'
    self.request.RESPONSE.setHeader('Content-Type', 'text/css')
    return css % color
于 2013-07-18T22:08:37.253 回答