server/repository.py
changeset 6390 3766853656d7
parent 6368 f907cc7f2875
child 6426 541659c39f6a
equal deleted inserted replaced
6389:72ba82a26e05 6390:3766853656d7
    32 __docformat__ = "restructuredtext en"
    32 __docformat__ = "restructuredtext en"
    33 
    33 
    34 import sys
    34 import sys
    35 import threading
    35 import threading
    36 import Queue
    36 import Queue
       
    37 from itertools import chain
    37 from os.path import join
    38 from os.path import join
    38 from datetime import datetime
    39 from datetime import datetime
    39 from time import time, localtime, strftime
    40 from time import time, localtime, strftime
    40 
    41 
    41 from logilab.common.decorators import cached
    42 from logilab.common.decorators import cached
    43 from logilab.common import flatten
    44 from logilab.common import flatten
    44 
    45 
    45 from yams import BadSchemaDefinition
    46 from yams import BadSchemaDefinition
    46 from yams.schema import role_name
    47 from yams.schema import role_name
    47 from rql import RQLSyntaxError
    48 from rql import RQLSyntaxError
       
    49 from rql.utils import rqlvar_maker
    48 
    50 
    49 from cubicweb import (CW_SOFTWARE_ROOT, CW_MIGRATION_MAP, QueryError,
    51 from cubicweb import (CW_SOFTWARE_ROOT, CW_MIGRATION_MAP, QueryError,
    50                       UnknownEid, AuthenticationError, ExecutionError,
    52                       UnknownEid, AuthenticationError, ExecutionError,
    51                       ETypeNotSupportedBySources, MultiSourcesError,
    53                       ETypeNotSupportedBySources, MultiSourcesError,
    52                       BadConnectionId, Unauthorized, ValidationError,
    54                       BadConnectionId, Unauthorized, ValidationError,
   586             session.commit()
   588             session.commit()
   587         finally:
   589         finally:
   588             session.close()
   590             session.close()
   589         return True
   591         return True
   590 
   592 
       
   593     def find_users(self, fetch_attrs, **query_attrs):
       
   594         """yield user attributes for cwusers matching the given query_attrs
       
   595         (the result set cannot survive this method call)
       
   596 
       
   597         This can be used by low-privileges account (anonymous comes to
       
   598         mind).
       
   599 
       
   600         `fetch_attrs`: tuple of attributes to be fetched
       
   601         `query_attrs`: dict of attr/values to restrict the query
       
   602         """
       
   603         assert query_attrs
       
   604         if not hasattr(self, '_cwuser_attrs'):
       
   605             cwuser = self.schema['CWUser']
       
   606             self._cwuser_attrs = set(str(rschema)
       
   607                                      for rschema, _eschema in cwuser.attribute_definitions()
       
   608                                      if not rschema.meta)
       
   609         cwuserattrs = self._cwuser_attrs
       
   610         for k in chain(fetch_attrs, query_attrs.iterkeys()):
       
   611             if k not in cwuserattrs:
       
   612                 raise Exception('bad input for find_user')
       
   613         session = self.internal_session()
       
   614         try:
       
   615             varmaker = rqlvar_maker()
       
   616             vars = [(attr, varmaker.next()) for attr in fetch_attrs]
       
   617             rql = 'Any %s WHERE X is CWUser, ' % ','.join(var[1] for var in vars)
       
   618             rql += ','.join('X %s %s' % (var[0], var[1]) for var in vars) + ','
       
   619             rset = session.execute(rql + ','.join('X %s %%(%s)s' % (attr, attr)
       
   620                                                   for attr in query_attrs.iterkeys()),
       
   621                                    query_attrs)
       
   622             return rset.rows
       
   623         finally:
       
   624             session.close()
       
   625 
   591     def connect(self, login, **kwargs):
   626     def connect(self, login, **kwargs):
   592         """open a connection for a given user
   627         """open a connection for a given user
   593 
   628 
   594         base_url may be needed to send mails
   629         base_url may be needed to send mails
   595         cnxtype indicate if this is a pyro connection or a in-memory connection
   630         cnxtype indicate if this is a pyro connection or a in-memory connection