def add_or_update_user(session, data): """ Add or update user details in database. :param session: current session :param data: that contains user fields details """ """ querying to add/update user to database """ if 'user_id' in data.keys( ) and data.get('user_id') != 0: ## Update existing user # Updating the user status enabled/disabled user_id = data.get('user_id') user = session.query(User).filter(User.user_id.in_([user_id])).first() if 'enabled' in data.keys(): user.enabled = data.get('enabled') if 'oldPassword' in data.keys() and 'newPassword' in data.keys(): if flask_bcrypt.check_password_hash(user.password_hash, str(data['oldPassword'])): user.password_hash = flask_bcrypt.generate_password_hash( data['newPassword']) if 'first_name' in data.keys(): user.first_name = data.get('first_name') if 'last_name' in data.keys(): user.last_name = data.get('last_name') else: ## Create a new user data['password'] = flask_bcrypt.generate_password_hash( data['password']) user = User(**data) #data.pop('password_hash', None) updated_user = session.merge(user) # print(updated_user) # if 'first_name' in data.keys(): # updated_user.first_name = data['first_name'] return updated_user
def __init__(self, odoo_contact_id, email, password, allow_show_contacts): self.alumni_uuid = str(uuid.uuid4()) self.odoo_contact_id = odoo_contact_id self.email = email self.password = flask_bcrypt.generate_password_hash(password).decode() self.user_confirmed = False self.allow_show_contacts = allow_show_contacts
def hash_password(password): """ Uses one way hashing to encrypt the password :param password: password of the user :return: hash of the password """ return flask_bcrypt.generate_password_hash(password).decode('utf-8')
def save_new_user(data): user = User.objects(email=data["email"]) if not user: hashed_password = flask_bcrypt.generate_password_hash( data['password']).decode('utf-8') new_user = User(username=data["username"], email=data["email"], first_name=data["first_name"], last_name=data["last_name"], password=hashed_password) new_user.save() logger.debug("NEW USER CREATED: ", new_user) return generate_token(new_user) else: return ResponseObject.user_reg(success=False)
def test_decode_auth_token(self): user = { "email": "*****@*****.**", "username": "******", "public_id": str(uuid.uuid4()), "password_hash": flask_bcrypt.generate_password_hash("test").decode('utf-8'), "created_date": datetime.datetime.utcnow() } User(**user).save() created_user = User.objects.get(email=user['email']) auth_token = User.encode_auth_token(str(created_user["id"])) self.assertTrue(isinstance(auth_token, bytes)) self.assertTrue(User.decode_auth_token(auth_token.decode("utf-8")))
def save_new_user(data): user = User.query.filter_by(email=data['email']).first() token = '' if not user: email = data['email'] password_hash = flask_bcrypt.generate_password_hash( data['password']).decode('utf-8') is_active = data['is_active'] is_tfa = data['is_tfa'] is_admin = data['is_admin'] user = User(email, password_hash, is_active, is_tfa, is_admin) user.save() if user.id: token = generate_confirmation_token(data['email']) body = "Welcome! Thanks for signing up. Please click this link to activate your account: " + request.base_url + "confirm/{}".format( token) subject = "Please confirm your email" email_send = 0 send_email(user.email, subject, body) response = jsonify({ 'status': 'success', 'message': 'User Successfully Registered ... ! check your email [' + email + ']', 'account_name': user.email, 'created_at': user.created_at }) response.status_code = 201 return response else: response = jsonify({ 'status': 'fail', 'message': 'User already exists. Please Log in.', }) response.status_code = 409 return response
def save_new_user(data): user = User.objects.filter(email=data['email']).first() if not user: password = flask_bcrypt.generate_password_hash( data['password']).decode('utf-8') new_user = User(public_id=str(uuid.uuid4()), email=data['email'], username=data['username'], password=password, registered_on=datetime.datetime.utcnow()) new_user.save() response_object = { 'status': 'success', 'message': 'Successfully registered.' } return response_object, 201 else: response_object = { 'status': 'fail', 'message': 'User already exists. Please Log in.', } return response_object, 409
def password(self,password): self.password_hash = flask_bcrypt.generate_password_hash(password).decode('utf-8')
def __init__(self, username, email, password, is_admin): self.username = username self.email = email self.password = flask_bcrypt.generate_password_hash(password).decode() self.is_admin = is_admin if is_admin is not None else False
def userPassword(self, userPassword): self.passwordHash = flask_bcrypt.generate_password_hash( userPassword).decode('utf-8') # hashing userPassword
def password(password): return (flask_bcrypt.generate_password_hash(password).decode("utf-8"))
def password(self, password): self.password_hash = ( flask_bcrypt.generate_password_hash(password).decode("utf-8") if password else None)
def decode_hash(value): return flask_bcrypt.generate_password_hash(value).decode('utf-8')