Commit e8bb9906 authored by Jérome Perrin's avatar Jérome Perrin

Make preferences independant of scripts with proxy roles or unrestricted

methods.
Prior to that change, catalog search was returning too many preferences and a
wrong result was sometimes cached.
Also mark test_system_preference_value_prefererred_clear_cache_disabled
@expectedFailure


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@41415 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 6df3d01d
......@@ -225,36 +225,45 @@ class PreferenceTool(BaseTool):
sorted so that the first in the list should be applied first
"""
tv = getTransactionalVariable()
user = getToolByName(self, 'portal_membership').getAuthenticatedMember()
tv_key = 'PreferenceTool._getSortedPreferenceList/%s/%s' % (user,
sql_catalog_id)
if tv.get(tv_key, None) is None:
prefs = []
# XXX will also cause problems with Manager (too long)
# XXX For manager, create a manager specific preference
# or better solution
user_is_manager = 'Manager' in user.getRolesInContext(self)
for pref in self.searchFolder(portal_type='Preference', sql_catalog_id=sql_catalog_id):
pref = pref.getObject()
if pref is not None and pref.getProperty('preference_state',
'broken') in ('enabled', 'global'):
# XXX quick workaround so that manager only see user preference
# they actually own.
if user_is_manager and pref.getPriority() == Priority.USER :
if pref.getOwnerTuple()[1] == user.getId():
security_manager = getSecurityManager()
user = security_manager.getUser()
acl_users = self.getPortalObject().acl_users
try:
# reset a security manager without any proxy role or unrestricted method,
# wich affects the catalog search that we do to find applicable
# preferences.
actual_user = acl_users.getUser(str(user))
if actual_user is not None:
newSecurityManager(self.REQUEST, actual_user.__of__(acl_users))
tv_key = 'PreferenceTool._getSortedPreferenceList/%s/%s' % (user,
sql_catalog_id)
if tv.get(tv_key, None) is None:
prefs = []
# XXX will also cause problems with Manager (too long)
# XXX For manager, create a manager specific preference
# or better solution
user_is_manager = 'Manager' in user.getRolesInContext(self)
for pref in self.searchFolder(portal_type='Preference', sql_catalog_id=sql_catalog_id):
pref = pref.getObject()
if pref is not None and pref.getProperty('preference_state',
'broken') in ('enabled', 'global'):
# XXX quick workaround so that manager only see user preference
# they actually own.
if user_is_manager and pref.getPriority() == Priority.USER :
if pref.getOwnerTuple()[1] == user.getId():
prefs.append(pref)
else :
prefs.append(pref)
else :
prefs.append(pref)
prefs.sort(key=lambda x: x.getPriority(), reverse=True)
# add system preferences before user preferences
sys_prefs = [x.getObject() for x in self.searchFolder(portal_type='System Preference', sql_catalog_id=sql_catalog_id) \
if x.getObject().getProperty('preference_state', 'broken') in ('enabled', 'global')]
sys_prefs.sort(key=lambda x: x.getPriority(), reverse=True)
preference_list = sys_prefs + prefs
tv[tv_key] = preference_list
else:
preference_list = tv[tv_key]
return preference_list
prefs.sort(key=lambda x: x.getPriority(), reverse=True)
# add system preferences before user preferences
sys_prefs = [x.getObject() for x in self.searchFolder(portal_type='System Preference', sql_catalog_id=sql_catalog_id) \
if x.getObject().getProperty('preference_state', 'broken') in ('enabled', 'global')]
sys_prefs.sort(key=lambda x: x.getPriority(), reverse=True)
preference_list = sys_prefs + prefs
tv[tv_key] = preference_list
return tv[tv_key]
finally:
setSecurityManager(security_manager)
def _getActivePreferenceByPortalType(self, portal_type):
enabled_prefs = self._getSortedPreferenceList()
......
......@@ -37,7 +37,9 @@ from zExceptions import Unauthorized
from AccessControl.ZopeGuards import guarded_hasattr
from DateTime import DateTime
from Products.ERP5Type.tests.backportUnittest import expectedFailure
from Products.ERP5Type.tests.testERP5Type import PropertySheetTestCase
from Products.ERP5Type.tests.utils import createZODBPythonScript
from Products.ERP5Form.PreferenceTool import Priority
from Products.ERP5.PropertySheet.HtmlStylePreference import HtmlStylePreference
......@@ -358,6 +360,50 @@ class TestPreferences(PropertySheetTestCase):
self.assertEquals(None,
portal_preferences.getPreferredAccountingTransactionAtDate())
def test_proxy_roles(self):
# make sure we can get preferences in a script with proxy roles
portal_workflow = self.getWorkflowTool()
portal_preferences = self.getPreferenceTool()
# create 2 users: user_a and user_b
uf = self.portal.acl_users
uf._doAddUser('user_a', '', ['Member', ], [])
uf._doAddUser('user_b', '', ['Member', ], [])
self.login('user_a')
user_a = portal_preferences.newContent(
id='user_a', portal_type='Preference',
# this preference have group priority, so preference for user_b would get
# picked first
priority=Priority.GROUP,
preferred_accounting_transaction_simulation_state_list=['user_a'])
transaction.commit(); self.tic()
# enable a pref
portal_workflow.doActionFor(
user_a, 'enable_action', wf_id='preference_workflow')
self.login('user_b')
# create a pref for user_b
user_b = portal_preferences.newContent(
id='user_b', portal_type='Preference',
preferred_accounting_transaction_simulation_state_list=['user_b'])
transaction.commit(); self.tic()
# enable this preference
portal_workflow.doActionFor(
user_b, 'enable_action', wf_id='preference_workflow')
self.login('ERP5TypeTestCase')
script = createZODBPythonScript(
self.portal.portal_skins.custom,
'PreferenceTool_testPreferencesProxyRole', '',
'return context.getPreferredAccountingTransactionSimulationStateList()')
script.manage_proxy(['Manager'])
self.login('user_a')
self.assertEquals(['user_a'],
portal_preferences.PreferenceTool_testPreferencesProxyRole())
def test_GlobalPreference(self):
# globally enabled preference are preference for anonymous users.
......@@ -617,6 +663,7 @@ class TestPreferences(PropertySheetTestCase):
self.assertEqual(system_preference_string,
portal_preferences.getDummystring())
@expectedFailure
def test_system_preference_value_prefererred_clear_cache_disabled(self):
default_preference_string = 'Default Name'
normal_preference_string = 'Normal Preference'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment