Permalink
Browse files

First import

  • Loading branch information...
0 parents commit 583e3d10b6e4c090c598912257a60c6acaf8a4a2 @amol- amol- committed Sep 14, 2011
Showing with 168 additions and 0 deletions.
  1. +3 −0 .hgignore
  2. +3 −0 setup.cfg
  3. +26 −0 setup.py
  4. +2 −0 tgming/__init__.py
  5. +81 −0 tgming/auth.py
  6. +53 −0 tgming/properties.py
@@ -0,0 +1,3 @@
+.pyc
+.swp
+.egg-info
@@ -0,0 +1,3 @@
+[egg_info]
+tag_build = dev
+tag_svn_revision = true
@@ -0,0 +1,26 @@
+from setuptools import setup, find_packages
+import sys, os
+
+version = '0.0'
+
+setup(name='tgming',
+ version=version,
+ description="",
+ long_description="""\
+""",
+ classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
+ keywords='',
+ author='Alessandro Molina',
+ author_email='alessandro.molina@axant.it',
+ url='',
+ license='MIT',
+ packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
+ include_package_data=True,
+ zip_safe=False,
+ install_requires=[
+ # -*- Extra requirements: -*-
+ ],
+ entry_points="""
+ # -*- Entry points: -*-
+ """,
+ )
@@ -0,0 +1,2 @@
+from auth import setup_ming_auth
+from properties import SynonymProperty, ProgrammaticRelationProperty
@@ -0,0 +1,81 @@
+from zope.interface import implements
+from repoze.who.interfaces import IAuthenticator, IMetadataProvider
+from repoze.who.plugins.friendlyform import FriendlyFormPlugin
+from repoze.who.plugins.auth_tkt import AuthTktCookiePlugin
+from repoze.who.middleware import PluggableAuthenticationMiddleware
+
+class MingAuthenticatorPlugin(object):
+ implements(IAuthenticator)
+
+ def __init__(self, user_class):
+ self.user_class = user_class
+
+ # IAuthenticator
+ def authenticate(self, environ, identity):
+ if not ('login' in identity and 'password' in identity):
+ return None
+
+ user = self.user_class.query.get(user_name=identity.get('login', None))
+ if user:
+ if user.validate_password(identity.get('password', None)):
+ return identity['login']
+
+class MingUserMDPlugin(object):
+ implements(IMetadataProvider)
+
+ def __init__(self, user_class):
+ self.user_class = user_class
+
+ def add_metadata(self, environ, identity):
+ identity['user'] = self.user_class.query.get(user_name=identity['repoze.who.userid'])
+
+ if identity['user']:
+ identity['groups'] = [g.group_name for g in identity['user'].groups]
+ identity['permissions'] = [p.permission_name for p in identity['user'].permissions]
+ else:
+ identity['groups'] = []
+ identity['permissions'] = []
+
+ if 'repoze.what.credentials' not in environ:
+ environ['repoze.what.credentials'] = {}
+
+ environ['repoze.what.credentials']['groups'] = identity['groups']
+ environ['repoze.what.credentials']['permissions'] = identity['permissions']
+ environ['repoze.what.credentials']['repoze.what.userid'] = identity['repoze.who.userid']
+
+def setup_ming_auth(app, skip_authentication, **auth_args):
+ cookie_secret = auth_args.get('cookie_secret', 'secret')
+ cookie_name = auth_args.get('cookie_name', 'authtkt')
+
+ form_plugin = FriendlyFormPlugin(auth_args.get('login_url', '/login'),
+ auth_args.get('login_handler', '/login_handler'),
+ auth_args['post_login_url'],
+ auth_args.get('logout_handler', '/logout_handler'),
+ auth_args['post_logout_url'],
+ login_counter_name=auth_args.get('login_counter_name'),
+ rememberer_name='cookie')
+
+ challengers = [('form', form_plugin)]
+
+ auth = MingAuthenticatorPlugin(user_class=auth_args['user_class'])
+ authenticators = [('mingauth', auth)]
+
+ cookie = AuthTktCookiePlugin(cookie_secret, cookie_name)
+ identifiers = [('cookie', cookie), ('form', form_plugin)]
+
+ provider = MingUserMDPlugin(user_class=auth_args['user_class'])
+ mdproviders = [('ming_user_md', provider)]
+
+ from repoze.who.classifiers import default_request_classifier
+ from repoze.who.classifiers import default_challenge_decider
+
+ app = PluggableAuthenticationMiddleware(
+ app,
+ identifiers=identifiers,
+ authenticators=authenticators,
+ challengers=challengers,
+ mdproviders=mdproviders,
+ classifier=default_request_classifier,
+ challenge_decider=default_challenge_decider)
+
+ return app
@@ -0,0 +1,53 @@
+from ming import schema as s
+from ming.orm import FieldProperty, RelationProperty
+from ming.orm.property import ORMProperty, LazyProperty, OneToManyJoin
+from pymongo.objectid import ObjectId
+
+class SynonymProperty(ORMProperty):
+ include_in_repr = True
+
+ def __init__(self, getter, setter=None):
+ super(ORMProperty, self).__init__()
+ self.getter = getter
+ self.setter = setter
+
+ def __get__(self, instance, cls=None):
+ if not instance:
+ return self
+ return self.getter(instance)
+
+ def __set__(self, instance, value):
+ if not self.setter:
+ raise TypeError, 'read-only property'
+ else:
+ self.setter(instance, value)
+
+ def repr(self, doc):
+ return repr(self)
+
+ def __repr__(self):
+ return '<%s>' % (self.__class__.__name__,)
+
+class ProgrammaticRelationProperty(RelationProperty):
+ include_in_repr = False
+
+ def __init__(self, related, getter, setter=None):
+ super(ProgrammaticRelationProperty, self).__init__(related)
+ self.getter = getter
+ self.setter = setter
+
+ def __get__(self, instance, cls=None):
+ if not instance:
+ return self
+ return self.getter(instance)
+
+ def __set__(self, instance, value):
+ if not self.setter:
+ raise TypeError, 'read-only property'
+ else:
+ self.setter(instance, value)
+
+ @LazyProperty
+ def join(self):
+ return OneToManyJoin(self.cls, self.related, None)
+

0 comments on commit 583e3d1

Please sign in to comment.