<s> import hashlib <EOL> import random <EOL> import string <EOL> import transaction <EOL> from cryptacular . bcrypt import BCRYPTPasswordManager <EOL> from pyramid . threadlocal import get_current_request <EOL> from pyramid . util import DottedNameResolver <EOL> from sqlalchemy import ( Column , <EOL> ForeignKey , <EOL> Index , <EOL> Table , <EOL> types , <EOL> Unicode ) <EOL> from sqlalchemy . exc import IntegrityError <EOL> from sqlalchemy . ext . declarative import declarative_base <EOL> from sqlalchemy . orm import ( relationship , <EOL> scoped_session , <EOL> sessionmaker , <EOL> synonym ) <EOL> from sqlalchemy . sql . expression import func <EOL> from zope . sqlalchemy import ZopeTransactionExtension <EOL> from apex . lib . db import get_or_create <EOL> DBSession = scoped_session ( sessionmaker ( extension = ZopeTransactionExtension ( ) ) ) <EOL> Base = declarative_base ( ) <EOL> auth_group_table = Table ( '<STR_LIT>' , Base . metadata , <EOL> Column ( '<STR_LIT>' , types . Integer ( ) , ForeignKey ( '<STR_LIT>' , onupdate = '<STR_LIT>' , ondelete = '<STR_LIT>' ) ) , <EOL> Column ( '<STR_LIT>' , types . Integer ( ) , ForeignKey ( '<STR_LIT>' , onupdate = '<STR_LIT>' , ondelete = '<STR_LIT>' ) ) <EOL> ) <EOL> Index ( '<STR_LIT>' , auth_group_table . c . auth_id , auth_group_table . c . group_id ) <EOL> class AuthGroup ( Base ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = '<STR_LIT>' <EOL> __table_args__ = { '<STR_LIT>' : True } <EOL> id = Column ( types . Integer ( ) , primary_key = True ) <EOL> name = Column ( Unicode ( <NUM_LIT> ) , unique = True , nullable = False ) <EOL> description = Column ( Unicode ( <NUM_LIT:255> ) , default = u'<STR_LIT>' ) <EOL> users = relationship ( '<STR_LIT>' , secondary = auth_group_table , backref = '<STR_LIT>' ) <EOL> def __repr__ ( self ) : <EOL> return u'<STR_LIT:%s>' % self . name <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> class AuthID ( Base ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = '<STR_LIT>' <EOL> __table_args__ = { '<STR_LIT>' : True } <EOL> id = Column ( types . Integer ( ) , primary_key = True ) <EOL> display_name = Column ( Unicode ( <NUM_LIT> ) , default = u'<STR_LIT>' ) <EOL> active = Column ( types . Enum ( u'<STR_LIT:Y>' , u'<STR_LIT:N>' , u'<STR_LIT:D>' , name = u'<STR_LIT>' ) , default = u'<STR_LIT:Y>' ) <EOL> created = Column ( types . DateTime ( ) , default = func . now ( ) ) <EOL> groups = relationship ( '<STR_LIT>' , secondary = auth_group_table , backref = '<STR_LIT>' ) <EOL> users = relationship ( '<STR_LIT>' ) <EOL> """<STR_LIT>""" <EOL> last_login = relationship ( '<STR_LIT>' , order_by = '<STR_LIT>' , uselist = False ) <EOL> login_log = relationship ( '<STR_LIT>' , order_by = '<STR_LIT>' ) <EOL> def in_group ( self , group ) : <EOL> """<STR_LIT>""" <EOL> return group in [ g . name for g in self . groups ] <EOL> @ classmethod <EOL> def get_by_id ( cls , id ) : <EOL> """<STR_LIT>""" <EOL> return DBSession . query ( cls ) . filter ( cls . id == id ) . first ( ) <EOL> def get_profile ( self , request = None ) : <EOL> """<STR_LIT>""" <EOL> if not request : <EOL> request = get_current_request ( ) <EOL> auth_profile = request . registry . settings . get ( '<STR_LIT>' ) <EOL> if auth_profile : <EOL> resolver = DottedNameResolver ( auth_profile . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] ) <EOL> profile_cls = resolver . resolve ( auth_profile ) <EOL> return get_or_create ( DBSession , profile_cls , auth_id = self . id ) <EOL> @ property <EOL> def group_list ( self ) : <EOL> group_list = [ ] <EOL> if self . groups : <EOL> for group in self . groups : <EOL> group_list . append ( group . name ) <EOL> return '<STR_LIT:U+002C>' . join ( map ( str , group_list ) ) <EOL> class AuthUser ( Base ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = '<STR_LIT>' <EOL> __table_args__ = { '<STR_LIT>' : True } <EOL> id = Column ( types . Integer ( ) , primary_key = True ) <EOL> auth_id = Column ( types . Integer , ForeignKey ( AuthID . id ) , index = True ) <EOL> provider = Column ( Unicode ( <NUM_LIT> ) , default = u'<STR_LIT>' , index = True ) <EOL> login = Column ( Unicode ( <NUM_LIT> ) , default = u'<STR_LIT>' , index = True ) <EOL> salt = Column ( Unicode ( <NUM_LIT> ) ) <EOL> _password = Column ( '<STR_LIT:password>' , Unicode ( <NUM_LIT> ) , default = u'<STR_LIT>' ) <EOL> email = Column ( Unicode ( <NUM_LIT> ) , default = u'<STR_LIT>' , index = True ) <EOL> created = Column ( types . DateTime ( ) , default = func . now ( ) ) <EOL> active = Column ( types . Enum ( u'<STR_LIT:Y>' , u'<STR_LIT:N>' , u'<STR_LIT:D>' , name = u'<STR_LIT>' ) , default = u'<STR_LIT:Y>' ) <EOL> def _set_password ( self , password ) : <EOL> self . salt = self . get_salt ( <NUM_LIT> ) <EOL> password = password + self . salt <EOL> self . _password = BCRYPTPasswordManager ( ) . encode ( password , rounds = <NUM_LIT:12> ) <EOL> def _get_password ( self ) : <EOL> return self . _password <EOL> password = synonym ( '<STR_LIT>' , descriptor = property ( _get_password , _set_password ) ) <EOL> def get_salt ( self , length ) : <EOL> m = hashlib . sha256 ( ) <EOL> word = '<STR_LIT>' <EOL> for i in xrange ( length ) : <EOL> word += random . choice ( string . ascii_letters ) <EOL> m . update ( word ) <EOL> return unicode ( m . hexdigest ( ) [ : length ] ) <EOL> @ classmethod <EOL> def get_by_id ( cls , id ) : <EOL> """<STR_LIT>""" <EOL> return DBSession . query ( cls ) . filter ( cls . id == id ) . first ( ) <EOL> @ classmethod <EOL> def get_by_login ( cls , login ) : <EOL> """<STR_LIT>""" <EOL> return DBSession . query ( cls ) . filter ( cls . login == login ) . first ( ) <EOL> @ classmethod <EOL> def get_by_email ( cls , email ) : <EOL> """<STR_LIT>""" <EOL> return DBSession . query ( cls ) . filter ( cls . email == email ) . first ( ) <EOL> @ classmethod <EOL> def check_password ( cls , ** kwargs ) : <EOL> if kwargs . has_key ( '<STR_LIT:id>' ) : <EOL> user = cls . get_by_id ( kwargs [ '<STR_LIT:id>' ] ) <EOL> if kwargs . has_key ( '<STR_LIT>' ) : <EOL> user = cls . get_by_login ( kwargs [ '<STR_LIT>' ] ) <EOL> if not user : <EOL> return False <EOL> try : <EOL> if BCRYPTPasswordManager ( ) . check ( user . password , <EOL> '<STR_LIT>' % ( kwargs [ '<STR_LIT:password>' ] , user . salt ) ) : <EOL> return True <EOL> except TypeError : <EOL> pass <EOL> request = get_current_request ( ) <EOL> fallback_auth = request . registry . settings . get ( '<STR_LIT>' ) <EOL> if fallback_auth : <EOL> resolver = DottedNameResolver ( fallback_auth . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] ) <EOL> fallback = resolver . resolve ( fallback_auth ) <EOL> return fallback ( ) . check ( DBSession , request , user , kwargs [ '<STR_LIT:password>' ] ) <EOL> return False <EOL> class AuthUserLog ( Base ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = '<STR_LIT>' <EOL> __table_args__ = { '<STR_LIT>' : True } <EOL> id = Column ( types . Integer , primary_key = True ) <EOL> auth_id = Column ( types . Integer , ForeignKey ( AuthID . id ) , index = True ) <EOL> user_id = Column ( types . Integer , ForeignKey ( AuthUser . id ) , index = True ) <EOL> time = Column ( types . DateTime ( ) , default = func . now ( ) ) <EOL> ip_addr = Column ( Unicode ( <NUM_LIT> ) , nullable = False ) <EOL> event = Column ( types . Enum ( u'<STR_LIT:L>' , u'<STR_LIT:R>' , u'<STR_LIT:P>' , u'<STR_LIT:F>' , name = u'<STR_LIT>' ) , default = u'<STR_LIT:L>' ) <EOL> def populate ( settings ) : <EOL> session = DBSession ( ) <EOL> default_groups = [ ] <EOL> if settings . has_key ( '<STR_LIT>' ) : <EOL> for name in settings [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) : <EOL> default_groups . append ( ( unicode ( name . strip ( ) ) , u'<STR_LIT>' ) ) <EOL> else : <EOL> default_groups = [ ( u'<STR_LIT>' , u'<STR_LIT>' ) , ( u'<STR_LIT>' , u'<STR_LIT>' ) ] <EOL> for name , description in default_groups : <EOL> group = AuthGroup ( name = name , description = description ) <EOL> session . add ( group ) <EOL> session . flush ( ) <EOL> transaction . commit ( ) <EOL> def initialize_sql ( engine , settings ) : <EOL> DBSession . configure ( bind = engine ) <EOL> Base . metadata . bind = engine <EOL> Base . metadata . create_all ( engine ) <EOL> if settings . has_key ( '<STR_LIT>' ) : <EOL> pass <EOL> try : <EOL> populate ( settings ) <EOL> except IntegrityError : <EOL> transaction . abort ( ) </s>
