2013-07-18 26 views
1

是否有可能讓重氮元素主題的元素依賴於Plone中的用戶角色?例如:我想爲某些特定角色的主題提供不同的頁眉圖像,並在用戶登錄後立即在網站上進行這些更改。基於用戶角色改變重氮元素主題元素

This question可能有關係,但我更願意管理它只能通過角色分配。

回答

5

這可以通過指定theme parameter來實現。未經檢驗的,但你可以定義一個參數是這樣的:

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

或類似的東西:

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

然後使用該參數在rules.xml文件。這將爲管理者切換主題:

<notheme if="$is_manager" /> 

這對於標題沒有任何作用,但你應該能夠從中推斷出來。

+0

哇!我很驚訝這種工作方式是多麼容易和強大。你也可以使用其他的例子,但直接通過重氮主題設置條件更快,更容易。對於那些嘗試它的人:確保在更改rules.xml文件後重新啓動plone。重新加載CSS不足以傳播更改。 – FvD

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 
+0

非常感謝您建議的方法!在我將答案標記爲正確之前,請給我幾天的時間來嘗試並測試代碼。 – FvD