Skip to content

Instantly share code, notes, and snippets.

@digitalresistor
Created October 29, 2013 04:02
Show Gist options
  • Save digitalresistor/7209017 to your computer and use it in GitHub Desktop.
Save digitalresistor/7209017 to your computer and use it in GitHub Desktop.
Mah auth policy for Pyramid =)
# Let it be known that this code was written by the very awesome and talented @mmerickel
#
# Thanks! Much appreciated.
class BertsAuthPolicyMakerFactoryConfig(object):
def __init__(self, policy_factory, attr='auth_policy'):
self.policy_factory = policy_factory
self.attr = attr
def _get_policy(self, request):
policy = getattr(request, self.attr, None)
if policy is None:
policy = self.policy_factory(request)
setattr(request, self.attr, policy)
return policy
def unauthenticated_userid(self, request):
return self._get_policy(request).unauthenticated_userid
def authenticated_userid(self, request):
return self._get_policy(request).authenticated_userid
def effective_principals(self, request):
return self._get_policy(request).effective_principals
def BertsSuperCoolAuthPolicyFactory(object):
def __init__(self, request):
self.request = request
@property
def unauthenticated_userid(self):
return 42
@property
def authenticated_userid(self):
return self.unauthenticated_userid
@property
def effective_principals(self):
return [Everyone]
auth_policy = BertsAuthPolicyMakerFactoryConfig(BertsSuperCoolAuthPolicyFactory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment