Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User(Base):
- __tablename__ = "users"
- __table_args__ = {'mysql_engine': "InnoDB", 'mysql_charset': "utf8"}
- id = Column(Integer, primary_key=True)
- username = Column(Unicode(50), unique=True, nullable=False)
- first_name = Column(Unicode(20), default=u"")
- last_name = Column(Unicode(20), default=u"")
- _password = Column("password", Unicode(100), nullable=False)
- enabled = Column(Boolean, nullable=False, default=True)
- is_staff = Column(Boolean, nullable=False, default=False)
- role_id = Column(
- Integer,
- ForeignKey("roles.id", ondelete="CASCADE"),
- nullable=False
- )
- role = relationship(Role, backref="users")
- _role_slug = None
- def has_role(self, *args):
- return bool(self.role_slug in args)
- @property
- def name_and_role(self):
- return self.name + " - " + self.role.name
- @property
- def name(self):
- return "%s %s" % (self.first_name, self.last_name)
- @property
- def password(self):
- return self._password
- @property
- def role_slug(self):
- if not self._role_slug:
- self._role_slug = self.role.name
- return self._role_slug
- @password.setter
- def password(self, password):
- if not isinstance(password, (str, unicode, basestring)):
- raise Exception('Invalid type. Password must be a string.')
- 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()
- if not isinstance(hashed_password, unicode):
- hashed_password = hashed_password.decode('UTF-8')
- self._password = hashed_password
- def validate_password(self, password):
- 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