Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Group model"""
- from sqlalchemy import Table, ForeignKey, Column
- from sqlalchemy.orm import relation
- from sqlalchemy.types import Unicode, Integer
- from pyvent.model import Base
- from pyvent.model.user import User
- from pyvent.model.permission import Permission
- from pyvent.model.meta import metadata
- group_permission_table = Table('group_permission', metadata,
- Column('group_id', Integer, ForeignKey('group.id')),
- Column('permission_id', Integer, ForeignKey('permission.id')),
- )
- group_user_table = Table('group_user', metadata,
- Column('group_id', Integer, ForeignKey('group.id')),
- Column('permission_id', Integer, ForeignKey('permission.id'))
- )
- class Group(Base):
- __tablename__ = "group"
- id = Column(Integer, primary_key=True)
- name = Column(Unicode(255), unique=True, nullable=False)
- permissions = relation(Permission, secondary=group_permission_table, backref='groups')
- users = relation(User, secondary=group_user_table, backref='groups')
- ------------------
- permission file
- """Permission model"""
- from sqlalchemy import Column
- from sqlalchemy.types import Unicode, Integer
- from sqlalchemy.orm import relation
- from pyvent.model import Base
- class Permission(Base):
- __tablename__ = "permission"
- id = Column(Integer, primary_key=True)
- name = Column(Unicode(255), unique=True, nullable=False)
- ------------------
- User file
- """User model"""
- import os
- from hashlib import sha1
- from sqlalchemy import Column
- from sqlalchemy.types import Unicode, Integer
- from sqlalchemy.orm import relation
- from pyvent.model import Base
- class User(Base):
- __tablename__ = "user"
- id = Column(Integer, primary_key=True)
- username = Column(Unicode(255), unique=True, nullable=False)
- email = Column(Unicode(255), unique=True, nullable=False)
- password = Column(Unicode(80), nullable=False)
- fullname = Column(Unicode(255), nullable=False)
- def _set_password(self, password):
- """Hash password on the ply."""
- hashed_password = password
- if isinstance(password, unicode):
- password_8bit = password.encode('UTF-8')
- else:
- password_8bit = password
- salt = sha1()
- salt.update(os.urandom(60))
- hash = sha1()
- hash.update(password_8bit + salt.hexdigest())
- hashed_password = salt.hexdigest() + hash.hexdigest()
- # Make sure the hashed password is an UTF-8 object at the end of the
- # process because SQLAlchemy _wants_ a unicode object for Unicode fields
- if not isinstance(hashed_password, unicode):
- hashed_password = hashed_password.decode('UTF-8')
- self.password = hashed_password
- def _get_passord(self):
- """Return the password hashed"""
- return self.password
- def validate_password(self, password):
- """ check the password against existing credentials.
- :param password: the password that was provided by the user to try
- and authenticate. This is the clear text version that we will
- need to match against the hashed one in the database.
- :type password: unicode object
- :return: Whether the password is valid.
- :rtype: bool
- """
- hashed_pass = sha1()
- hashed_pass.update(password + self.password[:40])
- return self.password[40:] == hashed_pass.hexdigest()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement