def extractCredentials(self, request): # Avoid creating anon user if this is a regular user # We actually have to poke request ourselves to avoid users from # root becoming anonymous... engine = createZopeEngine() if getattr(request, '_auth', None): return {} if not self._compiled: return {} portal = aq_inner(aq_parent(self._getPAS())) context = engine.getContext(request=request, portal=portal) for header_name, regexp, roles, condition in self._compiled: try: condition = engine.compile(condition) header = request.get(header_name) if header: check_header = re.compile(regexp) if check_header.match(header) and condition(context): return dict(AutoRole=True) except Exception as e: logger.exception("Couldn't evaluate AutoRole rule") return {}
def getRolesForPrincipal(self, principal, request=None): """ Assign roles based on 'request'. """ # we need this for uncontexted calls if request is None: return [] engine = createZopeEngine() if (self.anon_only and principal is not None and principal.getUserName() != 'Anonymous User'): return [] if not self._compiled: return [] result = set() portal = aq_inner(aq_parent(self._getPAS())) context = engine.getContext(request=request, portal=portal) for header_name, regexp, roles, condition in self._compiled: try: condition = engine.compile(condition) header = request.get(header_name) if header: check_header = re.compile(regexp) if check_header.match(header) and condition(context): result.update(roles) except Exception as e: logger.exception("Couldn't evaluate AutoRole rule") return list(result)
def _makeEngine(self): from Products.PageTemplates.Expressions import createZopeEngine return createZopeEngine()
def createOOoZopeEngine(): e = createZopeEngine() e._create_context = OOoContext return e