Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1. """Group model"""
  2. from sqlalchemy import Table, ForeignKey, Column
  3. from sqlalchemy.orm import relation
  4. from sqlalchemy.types import Unicode, Integer
  5. from pyvent.model import Base
  6. from pyvent.model.user import User
  7. from pyvent.model.permission import Permission
  8. from pyvent.model.meta import metadata
  9.  
  10. group_permission_table = Table('group_permission', metadata,
  11.     Column('group_id', Integer, ForeignKey('group.id')),
  12.     Column('permission_id', Integer, ForeignKey('permission.id')),
  13. )
  14.  
  15. group_user_table = Table('group_user', metadata,
  16.     Column('group_id', Integer, ForeignKey('group.id')),
  17.     Column('permission_id', Integer, ForeignKey('permission.id'))
  18. )
  19.  
  20. class Group(Base):
  21.     __tablename__ = "group"
  22.  
  23.     id = Column(Integer, primary_key=True)
  24.     name = Column(Unicode(255), unique=True, nullable=False)
  25.     permissions = relation(Permission, secondary=group_permission_table, backref='groups')
  26.     users = relation(User, secondary=group_user_table, backref='groups')
  27.  
  28. ------------------
  29. permission file
  30. """Permission model"""
  31. from sqlalchemy import Column
  32. from sqlalchemy.types import Unicode, Integer
  33. from sqlalchemy.orm import relation
  34.  
  35. from pyvent.model import Base
  36.  
  37. class Permission(Base):
  38.     __tablename__ = "permission"
  39.  
  40.     id = Column(Integer, primary_key=True)
  41.     name = Column(Unicode(255), unique=True, nullable=False)
  42. ------------------
  43.  
  44. User file
  45. """User model"""
  46. import os
  47. from hashlib import sha1
  48. from sqlalchemy import Column
  49. from sqlalchemy.types import Unicode, Integer
  50. from sqlalchemy.orm import relation
  51. from pyvent.model import Base
  52.  
  53. class User(Base):
  54.     __tablename__ = "user"
  55.  
  56.     id = Column(Integer, primary_key=True)
  57.     username = Column(Unicode(255), unique=True, nullable=False)
  58.     email = Column(Unicode(255), unique=True, nullable=False)
  59.     password = Column(Unicode(80), nullable=False)
  60.     fullname = Column(Unicode(255), nullable=False)
  61.  
  62.     def _set_password(self, password):
  63.         """Hash password on the ply."""
  64.         hashed_password = password
  65.  
  66.         if isinstance(password, unicode):
  67.             password_8bit = password.encode('UTF-8')
  68.         else:
  69.             password_8bit = password
  70.  
  71.         salt = sha1()
  72.         salt.update(os.urandom(60))
  73.         hash = sha1()
  74.         hash.update(password_8bit + salt.hexdigest())
  75.         hashed_password = salt.hexdigest() + hash.hexdigest()
  76.  
  77.         # Make sure the hashed password is an UTF-8 object at the end of the
  78.         # process because SQLAlchemy  _wants_ a unicode object for Unicode fields
  79.         if not isinstance(hashed_password, unicode):
  80.             hashed_password = hashed_password.decode('UTF-8')
  81.         self.password = hashed_password
  82.  
  83.     def _get_passord(self):
  84.         """Return the password hashed"""
  85.         return self.password
  86.  
  87.     def validate_password(self, password):
  88.         """ check the password against existing credentials.
  89.  
  90.        :param password: the password that was provided by the user to try
  91.            and authenticate. This is the clear text version that we will
  92.            need to match against the hashed one in the database.
  93.        :type password: unicode object
  94.        :return: Whether the password is valid.
  95.        :rtype: bool
  96.        """
  97.         hashed_pass = sha1()
  98.         hashed_pass.update(password + self.password[:40])
  99.         return self.password[40:] == hashed_pass.hexdigest()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement