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

ERP5Type: deduplicate roles in pickles

  security.declareProtected(Permissions.AccessContentsInformation,
                            'debug')
  def debug(self):
    ""
    from zodbpickle.pickle_2 import Unpickler
    from ZODB.serialize import ObjectWriter
    from ZODB.ExportImport import persistent_id
    from io import BytesIO
    from StringIO import StringIO
    from zodbpickle.pickletools_2 import dis
    from Acquisition import aq_base

    ow = ObjectWriter(aq_base(self.getPortalObject()))
    ow._p.dump(aq_base(self).__getstate__())
    pkl = ow._file.getvalue()

    disf = StringIO()
    dis(pkl, disf)
    msg = "pickle size: %s\n %s" % (len(pkl), disf.getvalue())
    print(msg)
    return msg

** Before **

pickle size: 756
     0: }    EMPTY_DICT
    1: q    BINPUT     1
    3: (    MARK
    4: U        SHORT_BINSTRING 'default_reference'
   23: q        BINPUT     2
   25: U        SHORT_BINSTRING 'XXXX635'
   34: q        BINPUT     3
   36: U        SHORT_BINSTRING 'uid'
   41: q        BINPUT     4
   43: L        LONG       59161L
   51: U        SHORT_BINSTRING 'workflow_history'
   69: q        BINPUT     5
   71: (        MARK
   72: U            SHORT_BINSTRING '\x00\x00\x00\x00\x00\x07|,'
   82: q            BINPUT     6
   84: c            GLOBAL     'Persistence.mapping PersistentMapping'
  123: q            BINPUT     7
  125: t            TUPLE      (MARK at 71)
  126: Q        BINPERSID
  127: U        SHORT_BINSTRING '_Add_portal_content_Permission'
  159: q        BINPUT     8
  161: (        MARK
  162: U            SHORT_BINSTRING 'Assignee'
  172: q            BINPUT     9
  174: U            SHORT_BINSTRING 'Assignor'
  184: q            BINPUT     10
  186: U            SHORT_BINSTRING 'Manager'
  195: q            BINPUT     11
  197: U            SHORT_BINSTRING 'Owner'
  204: q            BINPUT     12
  206: t            TUPLE      (MARK at 161)
  207: q        BINPUT     13
  209: U        SHORT_BINSTRING '_owner'
  217: q        BINPUT     14
  219: (        MARK
  220: ]            EMPTY_LIST
  221: q            BINPUT     15
  223: U            SHORT_BINSTRING 'acl_users'
  234: q            BINPUT     16
  236: a            APPEND
  237: U            SHORT_BINSTRING 'ERP5TypeTestCase'
  255: q            BINPUT     17
  257: t            TUPLE      (MARK at 219)
  258: q        BINPUT     18
  260: U        SHORT_BINSTRING 'portal_type'
  273: q        BINPUT     19
  275: U        SHORT_BINSTRING 'Visa File'
  286: q        BINPUT     20
  288: U        SHORT_BINSTRING '_Modify_portal_content_Permission'
  323: q        BINPUT     21
  325: (        MARK
  326: U            SHORT_BINSTRING 'Assignee'
  336: q            BINPUT     22
  338: U            SHORT_BINSTRING 'Assignor'
  348: q            BINPUT     23
  350: U            SHORT_BINSTRING 'Manager'
  359: q            BINPUT     24
  361: U            SHORT_BINSTRING 'Owner'
  368: q            BINPUT     25
  370: t            TUPLE      (MARK at 325)
  371: q        BINPUT     26
  373: U        SHORT_BINSTRING '_Access_contents_information_Permission'
  414: q        BINPUT     27
  416: (        MARK
  417: U            SHORT_BINSTRING 'Assignee'
  427: q            BINPUT     28
  429: U            SHORT_BINSTRING 'Assignor'
  439: q            BINPUT     29
  441: U            SHORT_BINSTRING 'Auditor'
  450: q            BINPUT     30
  452: U            SHORT_BINSTRING 'Manager'
  461: q            BINPUT     31
  463: U            SHORT_BINSTRING 'Owner'
  470: q            BINPUT     32
  472: t            TUPLE      (MARK at 416)
  473: q        BINPUT     33
  475: U        SHORT_BINSTRING '__ac_local_roles__'
  495: q        BINPUT     34
  497: }        EMPTY_DICT
  498: q        BINPUT     35
  500: (        MARK
  501: h            BINGET     17
  503: ]            EMPTY_LIST
  504: q            BINPUT     36
  506: U            SHORT_BINSTRING 'Owner'
  513: q            BINPUT     37
  515: a            APPEND
  516: U            SHORT_BINSTRING 'IS-ERPAD*_XXXFR*'
  534: q            BINPUT     38
  536: ]            EMPTY_LIST
  537: q            BINPUT     39
  539: U            SHORT_BINSTRING 'Assignor'
  549: q            BINPUT     40
  551: a            APPEND
  552: U            SHORT_BINSTRING 'DSO*'
  558: q            BINPUT     41
  560: ]            EMPTY_LIST
  561: q            BINPUT     42
  563: U            SHORT_BINSTRING 'Auditor'
  572: q            BINPUT     43
  574: a            APPEND
  575: U            SHORT_BINSTRING 'XXXFR*'
  583: q            BINPUT     44
  585: ]            EMPTY_LIST
  586: q            BINPUT     45
  588: U            SHORT_BINSTRING 'Auditor'
  597: q            BINPUT     46
  599: a            APPEND
  600: U            SHORT_BINSTRING 'AF*_XXXFR*'
  612: q            BINPUT     47
  614: ]            EMPTY_LIST
  615: q            BINPUT     48
  617: U            SHORT_BINSTRING 'Auditor'
  626: q            BINPUT     49
  628: a            APPEND
  629: u            SETITEMS   (MARK at 500)
  630: U        SHORT_BINSTRING 'id'
  634: q        BINPUT     50
  636: U        SHORT_BINSTRING '20220624-57DF4'
  652: q        BINPUT     51
  654: U        SHORT_BINSTRING '_View_Permission'
  672: q        BINPUT     52
  674: (        MARK
  675: U            SHORT_BINSTRING 'Assignee'
  685: q            BINPUT     53
  687: U            SHORT_BINSTRING 'Assignor'
  697: q            BINPUT     54
  699: U            SHORT_BINSTRING 'Auditor'
  708: q            BINPUT     55
  710: U            SHORT_BINSTRING 'Manager'
  719: q            BINPUT     56
  721: U            SHORT_BINSTRING 'Owner'
  728: q            BINPUT     57
  730: t            TUPLE      (MARK at 674)
  731: q        BINPUT     58
  733: U        SHORT_BINSTRING 'quantity'
  743: q        BINPUT     59
  745: G        BINFLOAT   1.0
  754: u        SETITEMS   (MARK at 3)
  755: .    STOP

** After **

pickle size: 596
     0: }    EMPTY_DICT
    1: q    BINPUT     1
    3: (    MARK
    4: U        SHORT_BINSTRING 'default_reference'
   23: q        BINPUT     2
   25: U        SHORT_BINSTRING 'XXXX636'
   34: q        BINPUT     3
   36: U        SHORT_BINSTRING 'uid'
   41: q        BINPUT     4
   43: L        LONG       59700L
   51: U        SHORT_BINSTRING 'workflow_history'
   69: q        BINPUT     5
   71: (        MARK
   72: U            SHORT_BINSTRING '\x00\x00\x00\x00\x00\x07|.'
   82: q            BINPUT     6
   84: c            GLOBAL     'Persistence.mapping PersistentMapping'
  123: q            BINPUT     7
  125: t            TUPLE      (MARK at 71)
  126: Q        BINPERSID
  127: U        SHORT_BINSTRING '_Add_portal_content_Permission'
  159: q        BINPUT     8
  161: (        MARK
  162: U            SHORT_BINSTRING 'Assignee'
  172: q            BINPUT     9
  174: U            SHORT_BINSTRING 'Assignor'
  184: q            BINPUT     10
  186: U            SHORT_BINSTRING 'Manager'
  195: q            BINPUT     11
  197: U            SHORT_BINSTRING 'Owner'
  204: q            BINPUT     12
  206: t            TUPLE      (MARK at 161)
  207: q        BINPUT     13
  209: U        SHORT_BINSTRING '_owner'
  217: q        BINPUT     14
  219: (        MARK
  220: ]            EMPTY_LIST
  221: q            BINPUT     15
  223: U            SHORT_BINSTRING 'acl_users'
  234: q            BINPUT     16
  236: a            APPEND
  237: U            SHORT_BINSTRING 'ERP5TypeTestCase'
  255: q            BINPUT     17
  257: t            TUPLE      (MARK at 219)
  258: q        BINPUT     18
  260: U        SHORT_BINSTRING 'portal_type'
  273: q        BINPUT     19
  275: U        SHORT_BINSTRING 'Visa File'
  286: q        BINPUT     20
  288: U        SHORT_BINSTRING '_Modify_portal_content_Permission'
  323: q        BINPUT     21
  325: (        MARK
  326: h            BINGET     9
  328: h            BINGET     10
  330: h            BINGET     11
  332: h            BINGET     12
  334: t            TUPLE      (MARK at 325)
  335: q        BINPUT     22
  337: U        SHORT_BINSTRING '_Access_contents_information_Permission'
  378: q        BINPUT     23
  380: (        MARK
  381: h            BINGET     9
  383: h            BINGET     10
  385: U            SHORT_BINSTRING 'Auditor'
  394: q            BINPUT     24
  396: h            BINGET     11
  398: h            BINGET     12
  400: t            TUPLE      (MARK at 380)
  401: q        BINPUT     25
  403: U        SHORT_BINSTRING '__ac_local_roles__'
  423: q        BINPUT     26
  425: }        EMPTY_DICT
  426: q        BINPUT     27
  428: (        MARK
  429: h            BINGET     17
  431: ]            EMPTY_LIST
  432: q            BINPUT     28
  434: h            BINGET     12
  436: a            APPEND
  437: U            SHORT_BINSTRING 'IS-ERPAD*_XXXFR*'
  455: q            BINPUT     29
  457: ]            EMPTY_LIST
  458: q            BINPUT     30
  460: h            BINGET     10
  462: a            APPEND
  463: U            SHORT_BINSTRING 'DSO*'
  469: q            BINPUT     31
  471: ]            EMPTY_LIST
  472: q            BINPUT     32
  474: h            BINGET     24
  476: a            APPEND
  477: U            SHORT_BINSTRING 'XXXFR*'
  485: q            BINPUT     33
  487: ]            EMPTY_LIST
  488: q            BINPUT     34
  490: h            BINGET     24
  492: a            APPEND
  493: U            SHORT_BINSTRING 'AF*_XXXFR*'
  505: q            BINPUT     35
  507: ]            EMPTY_LIST
  508: q            BINPUT     36
  510: h            BINGET     24
  512: a            APPEND
  513: u            SETITEMS   (MARK at 428)
  514: U        SHORT_BINSTRING 'id'
  518: q        BINPUT     37
  520: U        SHORT_BINSTRING '20220624-1203CE'
  537: q        BINPUT     38
  539: U        SHORT_BINSTRING '_View_Permission'
  557: q        BINPUT     39
  559: (        MARK
  560: h            BINGET     9
  562: h            BINGET     10
  564: h            BINGET     24
  566: h            BINGET     11
  568: h            BINGET     12
  570: t            TUPLE      (MARK at 559)
  571: q        BINPUT     40
  573: U        SHORT_BINSTRING 'quantity'
  583: q        BINPUT     41
  585: G        BINFLOAT   1.0
  594: u        SETITEMS   (MARK at 3)
  595: .    STOP
highest protocol among opcodes = 1
parent 0d8242de
......@@ -92,6 +92,7 @@ from Products.CMFCore.WorkflowCore import WorkflowException, ObjectDeleted,\
from Products.DCWorkflow.Expression import StateChangeInfo
from Products.DCWorkflow.utils import Message as _
from Products.ERP5Type import Permissions
from Products.ERP5Type.ERP5Type import serializeRoles
from Products.ERP5Type.Cache import CachingMethod
from Products.ERP5Type.Globals import PersistentMapping, InitializeClass
from Products.ERP5Type.Utils import convertToMixedCase
......@@ -100,7 +101,7 @@ from Products.ERP5Type.Core.WorkflowTransition import (TRIGGER_AUTOMATIC,
TRIGGER_USER_ACTION,
TRIGGER_WORKFLOW_METHOD)
_marker = ''
_marker = '' # oh putain
from Products.CMFCore.Expression import getEngine
userGetIdOrUserNameExpression = Expression('user/getIdOrUserName')
......@@ -313,7 +314,7 @@ class Workflow(XMLObject):
p = Permission(name, old_roles, ob)
old_roles = p.getRoles()
if type(old_roles) != type(new_roles) or sorted(old_roles) != sorted(new_roles):
p.setRoles(new_roles)
p.setRoles(serializeRoles(new_roles))
modified = True
return modified
......
......@@ -52,6 +52,19 @@ from Products.ERP5Type.Accessor.Translation import TRANSLATION_DOMAIN_CONTENT_TR
from zLOG import LOG, ERROR
from Products.CMFCore.exceptions import zExceptions_Unauthorized
def serializeRole(role_name, _roles={}):
assert isinstance(role_name, string_types)
return _roles.setdefault(role_name, role_name)
def serializeRoles(roles, _roles_tuples={}, _role_lists={}):
role_type = type(roles)
role_list = sorted([serializeRole(role) for role in roles])
role_tuple = tuple(role_list)
if role_type is tuple:
return _roles_tuples.setdefault(role_tuple, role_tuple)
return _role_lists.setdefault(role_tuple, role_list)
@zope.interface.implementer(interfaces.ILocalRoleAssignor)
class LocalRoleAssignorMixIn(object):
"""Mixin class used by type informations to compute and update local roles
......@@ -78,7 +91,7 @@ class LocalRoleAssignorMixIn(object):
else:
user_name = getSecurityManager().getUser().getId()
group_id_role_dict = {user_name: {'Owner'}}
group_id_role_dict = {user_name: {serializeRole('Owner')}}
local_roles_group_id_group_id = {}
# Merge results from applicable roles
for role_generator in self.getFilteredRoleListFor(ob):
......@@ -96,11 +109,11 @@ class LocalRoleAssignorMixIn(object):
if local_roles_group_id:
for role in role_list:
# Feed local_roles_group_id_group_id with local roles assigned to a group
local_roles_group_id_group_id.setdefault(local_roles_group_id, set()).update(((group_id, role),))
local_roles_group_id_group_id.setdefault(local_roles_group_id, set()).update(((group_id, serializeRole(role)),))
## Update role assignments to groups
# Assign new roles
ac_local_roles = {group: sorted(role_list)
ac_local_roles = {group: serializeRoles(role_list)
for group, role_list in six.iteritems(group_id_role_dict)
if role_list}
......@@ -409,7 +422,7 @@ class ERP5TypeInformation(XMLObject,
user_id = user.getId()
else:
user_id = 'Anonymous Owner'
ob.manage_setLocalRoles(user_id, ['Owner'])
ob.manage_setLocalRoles(user_id, [serializeRole('Owner')])
else:
if activate_kw is not None:
ob.setDefaultActivateParameterDict(activate_kw)
......
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