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

preference: more scalable way of disabling other preferences

This relies on selecting preferences using owner= catalog key.
This way also supports the edge case where the user has a "Owner" role defined in acl_users.
parent 4acd4a3a
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_body</string> </key>
<value> <string># When validating a user preference, we invalidate other user preferences.\n
\n
from Products.ERP5Type.Message import translateString\n
\n
portal = context.getPortalObject()\n
\n
\n
if context.getPriority() != 3: # XXX 3 is Priority.USER\n
return\n
\n
for preference in portal.portal_preferences.searchFolder(\n
owner=str(portal.portal_membership.getAuthenticatedMember()),\n
portal_type=context.getPortalType()):\n
preference = preference.getObject()\n
assert portal.portal_membership.getAuthenticatedMember().allowed(preference, [\'Owner\', ]), preference\n
\n
if preference != context and \\\n
preference.getPreferenceState() == \'enabled\' and \\\n
preference.getPriority() == context.getPriority():\n
preference.disable(\n
comment=translateString(\n
\'Automatically disabled when enabling ${preference_title}.\',\n
mapping={\'preference_title\': context.getTitle()}))\n
</string> </value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Preference_disableOtherPreferences</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -50,29 +50,7 @@
</item>
<item>
<key> <string>_body</string> </key>
<value> <string>from Products.ERP5Type.Message import translateString\n
\n
pref = sci[\'object\']\n
portal = sci.getPortal()\n
LOG = lambda msg: pref.log("PreferenceWorkflow.disableOtherPreferences on %s"%pref, msg)\n
\n
# deactivate all other preferences of the same level that the user can view\n
for p in portal.portal_preferences.searchFolder():\n
p = p.getObject()\n
try :\n
if portal.portal_membership.getAuthenticatedMember().allowed(p, [\'Owner\', ]) :\n
if p != pref and \\\n
p.getPreferenceState() == \'enabled\' and \\\n
p.getPriority() == pref.getPriority() and \\\n
p.getPortalType() == pref.getPortalType() :\n
p.portal_workflow.doActionFor(p,\n
\'disable_action\',\n
wf_id=\'preference_workflow\', \n
comment=translateString(\n
\'Automatically disabled when enabling ${preference_title}.\',\n
mapping={\'preference_title\': pref.getTitle()}))\n
except \'Unauthorized\', a :\n
LOG("not authorized to deactivate %s"%p)\n
<value> <string>sci[\'object\'].activate().Preference_disableOtherPreferences()\n
</string> </value>
</item>
<item>
......
......@@ -86,10 +86,10 @@ class Preference( Folder ):
self._clearCache()
Folder._edit(self, **kw)
def enable(self):
def enable(self, **kw):
"""Workflow method"""
self._clearCache()
def disable(self):
def disable(self, **kw):
"""Workflow method"""
self._clearCache()
......@@ -153,6 +153,7 @@ class TestPreferences(PropertySheetTestCase):
self.assertEqual(None,
self.getPreferenceTool().getActiveSystemPreference())
self.assertEqual(person2.getPreferenceState(), 'enabled')
self.tic()
# enabling a preference disable all other of the same level
self.assertEqual(person1.getPreferenceState(), 'disabled')
self.assertEqual(group.getPreferenceState(), 'enabled')
......
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