server/schemaserial.py
changeset 3890 d7a270f50f54
parent 3737 e8c669e33689
parent 3877 7ca53fc72a0a
child 3998 94cc7cad3d2d
equal deleted inserted replaced
3810:5b75fd66c80e 3890:d7a270f50f54
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     7 """
     7 """
     8 __docformat__ = "restructuredtext en"
     8 __docformat__ = "restructuredtext en"
     9 
     9 
       
    10 import os
    10 import sys
    11 import sys
    11 import os
    12 import os
    12 from itertools import chain
    13 from itertools import chain
    13 
    14 
    14 from logilab.common.shellutils import ProgressBar
    15 from logilab.common.shellutils import ProgressBar
   254         for something in permsdict.get(action, ()):
   255         for something in permsdict.get(action, ()):
   255             if isinstance(something, tuple):
   256             if isinstance(something, tuple):
   256                 actperms.append(erschema.rql_expression(*something))
   257                 actperms.append(erschema.rql_expression(*something))
   257             else: # group name
   258             else: # group name
   258                 actperms.append(something)
   259                 actperms.append(something)
   259         erschema.set_permissions(action, actperms)
   260         erschema.set_action_permissions(action, actperms)
   260 
   261 
   261 
   262 
   262 def deserialize_rdef_constraints(session):
   263 def deserialize_rdef_constraints(session):
   263     """return the list of relation definition's constraints as instances"""
   264     """return the list of relation definition's constraints as instances"""
   264     res = {}
   265     res = {}
   514         yield erperms2rql(schema[etype], groupmapping)
   515         yield erperms2rql(schema[etype], groupmapping)
   515     for rtype in sorted(schema.relations()):
   516     for rtype in sorted(schema.relations()):
   516         yield erperms2rql(schema[rtype], groupmapping)
   517         yield erperms2rql(schema[rtype], groupmapping)
   517 
   518 
   518 def erperms2rql(erschema, groupmapping):
   519 def erperms2rql(erschema, groupmapping):
       
   520     if hasattr(erschema, 'iter_rdefs'):
       
   521         # relation schema
       
   522         if erschema.final:
       
   523             etype = 'CWAttribute'
       
   524         else:
       
   525             etype = 'CWRelation'
       
   526         for subject, object in erschema.iter_rdefs():
       
   527             permissions = erschema.rproperty(subject, object, 'permissions')
       
   528             for rql, args in _erperms2rql(erschema.rproperties(subject, object),
       
   529                                           groupmapping):
       
   530                 args['st'] = str(subject)
       
   531                 args['rt'] = str(erschema)
       
   532                 args['ot'] = str(object)
       
   533                 yield rql + 'X is %s, X from_entity ST, X to_entity OT, '\
       
   534                       'X relation_type RT, RT name %%(rt)s, ST name %%(st)s, '\
       
   535                       'OT name %%(ot)s' % etype, args
       
   536     else:
       
   537         # entity schema
       
   538         for rql, args in _erperms2rql(erschema, groupmapping):
       
   539             args['name'] = str(eschema)
       
   540             yield rql + 'X is CWEType, X name %(name)s', args
       
   541 
       
   542 def _erperms2rql(erschema, groupmapping):
   519     """return rql insert statements to enter the entity or relation
   543     """return rql insert statements to enter the entity or relation
   520     schema's permissions in the database as
   544     schema's permissions in the database as
   521     [read|add|delete|update]_permission relations between CWEType/CWRType
   545     [read|add|delete|update]_permission relations between CWEType/CWRType
   522     and CWGroup entities
   546     and CWGroup entities
   523     """
   547     """
   524     etype = isinstance(erschema, schemamod.EntitySchema) and 'CWEType' or 'CWRType'
       
   525     for action in erschema.ACTIONS:
   548     for action in erschema.ACTIONS:
   526         for group in sorted(erschema.get_groups(action)):
   549         for group_or_rqlexpr in erschema.action_permissions(action):
   527             try:
   550             if isinstance(group_or_rqlexpr, basestring):
   528                 yield ('SET X %s_permission Y WHERE X is %s, X name "%s", Y eid %s'
   551                 # group
   529                        % (action, etype, erschema, groupmapping[group]), None)
   552                 try:
   530             except KeyError:
   553                     yield ('SET X %s_permission Y WHERE Y eid %%(g)s' % action,
   531                 continue
   554                            {'g': groupmapping[group_or_rqlexpr]})
   532         for rqlexpr in sorted(erschema.get_rqlexprs(action)):
   555                 except KeyError:
   533             yield ('INSERT RQLExpression E: E expression %%(e)s, E exprtype %%(t)s, '
   556                     continue
   534                    'E mainvars %%(v)s, X %s_permission E '
   557             else:
   535                    'WHERE X is %s, X name "%s"' % (action, etype, erschema),
   558                 # rqlexpr
   536                    {'e': unicode(rqlexpr.expression), 'v': unicode(rqlexpr.mainvars),
   559                 rqlexpr = group_or_rqlexpr
   537                     't': unicode(rqlexpr.__class__.__name__)})
   560                 yield ('INSERT RQLExpression E: E expression %%(e)s, E exprtype %%(t)s, '
       
   561                        'E mainvars %%(v)s, X %s_permission E WHERE ' % action,
       
   562                        {'e': unicode(rqlexpr.expression),
       
   563                         'v': unicode(rqlexpr.mainvars),
       
   564                         't': unicode(rqlexpr.__class__.__name__)})
   538 
   565 
   539 
   566 
   540 def updateeschema2rql(eschema):
   567 def updateeschema2rql(eschema):
   541     relations, values = eschema_relations_values(eschema)
   568     relations, values = eschema_relations_values(eschema)
   542     values['et'] = eschema.type
   569     values['et'] = eschema.type