def update(self, user): if 'email' in request.form: email = request.form.get('email', '').strip() if not email: abort(HTTP.BAD_REQUEST) user.email = email if 'display_name' in request.form: user.display_name = request.form.get('display_name') if 'password' in request.form: user.password = request.form.get('password') if user.password == '': user.is_active = False else: if 'is_active' in request.form: user.is_active = parse_bool(request.form.get('is_active')) if 'is_admin' in request.form: is_admin = parse_bool(request.form.get('is_admin')) user.role = Roles.get('ADMIN' if is_admin else 'USER') return user
def mk_user(email, password, is_public, is_active, is_admin): role = Roles.get('ADMIN' if is_admin else 'USER') user = User(email=email, password=password, is_public=is_public, is_active=is_active, role=role) db.session.add(user) db.session.commit() return user
def create(self, display_name, email, password, is_active, is_admin): role = Roles.get('ADMIN' if is_admin else 'USER') user = User(display_name=display_name, email=email, password=password, is_active=is_active, role=role) db.session.add(user) db.session.commit() return user
def mk_user(self, is_public=True, is_active=True, is_admin=False): email = str(random.random()) password = str(random.random()) role = Roles.get('ADMIN' if is_admin else 'USER') user = User(email=email, password=password, is_public=is_public, is_active=is_active, role=role) self._db.session.add(user) self._db.session.commit() return user
class User(db.Model): __tablename__ = 'users' pk = db.Column(dbtypes.GUID, default=uuid.uuid4, primary_key=True) display_name = db.Column(db.String(256)) email = db.Column(db.String(256), unique=True, nullable=False) password = db.Column(db.String(256), nullable=True) salt = db.Column(db.String(256), nullable=True) # Metadata # Should these attributes be in their own table? is_public = db.Column(db.Boolean, nullable=False, default=False) is_active = db.Column(db.Boolean, nullable=False, default=False) role = db.Column(db.Enum(*Roles.as_tuple(), name="role"), nullable=False, default=Roles.USER) creation_date = db.Column(db.DateTime, nullable=False) def __init__(self, *args, **kwargs): kwargs['creation_date'] = datetime.now() super(User, self).__init__(*args, **kwargs) def __setattr__(self, *args, **kwargs): if args[0] == 'password': password = args[1] salt = None if password not in (None, ''): password, salt = self.hash_password(password) self.salt = salt args = ('password', password) super(User, self).__setattr__(*args, **kwargs) def hash_password(self, password, salt=None): salt = salt or self.salt or bcrypt.gensalt() _pass = bcrypt.hashpw(password.encode('utf-8'), salt.encode('utf-8')) return (_pass, salt) def verify_password(self, password): _password, salt = self.hash_password(password) return _password == self.password def generate_auth_token(self, expiration=None): if not expiration: expiration = app.config.get('AUTH_EXPIRATION_TIME', 3600) if not isinstance(expiration, int): raise ValueError s = Serializer(app.config['SECRET_KEY'], expires_in=expiration) return s.dumps({'pk': str(self.pk)}) @staticmethod def verify_auth_token(token): s = Serializer(app.config['SECRET_KEY']) try: data = s.loads(token) except (SignatureExpired, BadSignature): return None user = User.query.get(data['pk']) return user def __repr__(self): return "<User ('%s')>" % self.email