我有一个问题,我需要能够让匿名用户通过 portal_catalog 搜索和检索自定义内容,但他们无法查看。
我使用了自定义内容类型和自定义工作流程,我得到的很可能是权限问题。我通过 ZMI -> portal_workflow 定义了一个自定义工作流,然后将其作为 XML 定义导出到源代码中。我将匿名用户的权限设置为“访问内容信息”而不是“查看”。请注意,代码片段中的“活动”是启用了该权限的工作流状态——sales_workflow
大脑查找适用于“经理”角色,但当角色切换为“匿名”时,目录返回一个空列表。
import unittest2 as unittest
from . import INTEGRATION_TESTING
from AccessControl import getSecurityManager
from plone.app.testing import setRoles, logout
from plone.app.testing import TEST_USER_ID
from Products.CMFCore.utils import getToolByName
def drop_to_anonymous(self):
"""
Drop site roles to anonymous user only.
Note this is a class method and not a function
assign this method as a class member and then call it
"""
logout()
setRoles(self.portal, TEST_USER_ID, ['Anonymous'])
user = getSecurityManager().getUser()
roles = user.getRolesInContext(self.portal)
self.assertListEqual(['Anonymous'], roles)
class TestSalesRepWorkflow(unittest.TestCase):
layer = INTEGRATION_TESTING
drop_to_anonymous = drop_to_anonymous
def setUp(self):
self.portal = self.layer['portal']
self.wftool = getToolByName(self.portal, 'portal_workflow')
self.catalog = getToolByName(self.portal, 'portal_catalog')
def test_workflow_lookup_anon(self):
setRoles(self.portal, TEST_USER_ID, ['Manager'])
self.portal.invokeFactory(
'CustomProduct',
'prod1',
title="Product 1"
)
prod1 = self.portal['prod1']
self.wftool.doActionFor(prod1, action='activate')
review_state = self.wftool.getInfoFor(prod1, 'review_state')
prod1.reindexObject()
self.assertEqual('active', review_state)
lookup = self.catalog(portal_type='CustomProduct', Title='Product 1',
review_state='active')
#This test passes with managerial permissions
self.assertEqual(len(lookup), 1)
#Repeat the same test in 'Anonymous' role
self.drop_to_anonymous()
lookup1 = self.catalog(portal_type='CustomProduct', Title='Product 1',
review_state='active')
#When dropped to anonymous role, the test fails,
#lookup returns an empty list
self.assertEqual(len(lookup1), 1)
有没有办法在不彻底修改权限的情况下解决这个问题?
使用 unrestrictedSearchResults 似乎可以修复搜索,但是每当我尝试在大脑上运行“getObject”时,都会引发以下错误:
Unauthorized: You are not allowed to access 'XXX' in this context