1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
3 # |
3 # |
4 # This file is part of CubicWeb. |
4 # This file is part of CubicWeb. |
5 # |
5 # |
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
353 if req is None: |
353 if req is None: |
354 req = self._orig_cnx[0].request() |
354 req = self._orig_cnx[0].request() |
355 user = req.create_entity('CWUser', login=unicode(login), |
355 user = req.create_entity('CWUser', login=unicode(login), |
356 upassword=password, **kwargs) |
356 upassword=password, **kwargs) |
357 req.execute('SET X in_group G WHERE X eid %%(x)s, G name IN(%s)' |
357 req.execute('SET X in_group G WHERE X eid %%(x)s, G name IN(%s)' |
358 % ','.join(repr(g) for g in groups), |
358 % ','.join(repr(str(g)) for g in groups), |
359 {'x': user.eid}) |
359 {'x': user.eid}) |
360 user.cw_clear_relation_cache('in_group', 'subject') |
360 user.cw_clear_relation_cache('in_group', 'subject') |
361 if commit: |
361 if commit: |
362 req.cnx.commit() |
362 req.cnx.commit() |
363 return user |
363 return user |
432 self.session.set_pool() |
432 self.session.set_pool() |
433 return self.session.execute(rql, args) |
433 return self.session.execute(rql, args) |
434 |
434 |
435 # other utilities ######################################################### |
435 # other utilities ######################################################### |
436 |
436 |
|
437 def grant_permission(self, entity, group, pname, plabel=None): |
|
438 """insert a permission on an entity. Will have to commit the main |
|
439 connection to be considered |
|
440 """ |
|
441 pname = unicode(pname) |
|
442 plabel = plabel and unicode(plabel) or unicode(group) |
|
443 e = entity.eid |
|
444 with security_enabled(self.session, False, False): |
|
445 peid = self.execute( |
|
446 'INSERT CWPermission X: X name %(pname)s, X label %(plabel)s,' |
|
447 'X require_group G, E require_permission X ' |
|
448 'WHERE G name %(group)s, E eid %(e)s', |
|
449 locals())[0][0] |
|
450 return peid |
|
451 |
437 @contextmanager |
452 @contextmanager |
438 def temporary_appobjects(self, *appobjects): |
453 def temporary_appobjects(self, *appobjects): |
439 self.vreg._loadedmods.setdefault(self.__module__, {}) |
454 self.vreg._loadedmods.setdefault(self.__module__, {}) |
440 for obj in appobjects: |
455 for obj in appobjects: |
441 self.vreg.register(obj) |
456 self.vreg.register(obj) |
443 yield |
458 yield |
444 finally: |
459 finally: |
445 for obj in appobjects: |
460 for obj in appobjects: |
446 self.vreg.unregister(obj) |
461 self.vreg.unregister(obj) |
447 |
462 |
448 # vregistry inspection utilities ########################################### |
463 def assertModificationDateGreater(self, entity, olddate): |
|
464 entity.cw_attr_cache.pop('modification_date', None) |
|
465 self.failUnless(entity.modification_date > olddate) |
|
466 |
|
467 |
|
468 # workflow utilities ####################################################### |
|
469 |
|
470 def assertPossibleTransitions(self, entity, expected): |
|
471 transitions = entity.cw_adapt_to('IWorkflowable').possible_transitions() |
|
472 self.assertListEqual(sorted(tr.name for tr in transitions), |
|
473 sorted(expected)) |
|
474 |
|
475 |
|
476 # views and actions registries inspection ################################## |
449 |
477 |
450 def pviews(self, req, rset): |
478 def pviews(self, req, rset): |
451 return sorted((a.__regid__, a.__class__) |
479 return sorted((a.__regid__, a.__class__) |
452 for a in self.vreg['views'].possible_views(req, rset=rset)) |
480 for a in self.vreg['views'].possible_views(req, rset=rset)) |
453 |
481 |