def send_reset_password_mail(user_email, request): user = User().load({'email': user_email}) if user.id: msg = Message() msg.add_recipient(user_email) minutes = 30 token = User.encode_auth_token(user.id, days=0, seconds=0, minutes=minutes).decode() link = f'http://{request.host}/reset/{token}' msg.html = get_reset_password_html( username=f"{user.first_name} {user.last_name}", link=link, minutes=minutes, token=token) msg.subject = 'Reset Password Request' msg.sender = '*****@*****.**' mail.send(msg) response_object = { 'status': 'success', 'message': 'An email with a reset was sent link.', } return response_object, 200 else: response_object = { 'status': 'fail', 'message': 'User provided email does not exist.', } return response_object, 409
def delete_super_domain(data): super_dom = SuperDomain(**data).load() if super_dom.id: dms = Domain.get_all(query={'super_domain_id': super_dom.id}) dm: Domain for dm in dms: TargetField.drop(domain_id=dm.id) dm.delete() User.remove_domain_for_users(super_dom.id) super_dom.delete() return super_dom
def update_password(data): token = data['token'] resp = User.decode_auth_token(token) if not isinstance(resp, str): user = User().load({'_id': resp['token']}) user.password = data['password'] user.modified_on = datetime.datetime.utcnow() save_changes(user) save_token(token) return { 'status': 'success', 'message': 'Password Changed.', }, 200 else: response_object = { 'status': 'fail', 'message': 'No user with provided id found.', } return response_object, 409
def save_new_user(data): user = User().load({'email': data['email']}) if not user.id: new_user = User(**dict(id=str(uuid.uuid4()), email=data['email'], last_name=data['last_name'], first_name=data['first_name'], password=data['password'], admin=data.get('admin', False), roles=data.get('roles', []), created_on=datetime.datetime.utcnow(), modified_on=datetime.datetime.utcnow())) save_changes(new_user) return generate_token(new_user) else: response_object = { 'status': 'fail', 'message': 'User already exists. Please Log in.', } return response_object, 409
def generate_token(user): try: # generate the auth token auth_token = User.encode_auth_token(user.id) response_object = { 'status': 'success', 'message': 'Successfully registered.', 'Authorization': auth_token.decode() } return response_object, 201 except Exception as e: response_object = { 'status': 'fail', 'message': 'Some error occurred. Please try again.' } return response_object, 401
def get_a_user(public_id): user = User().load({'_id': public_id}) return user if user.id else None
def get_all_users(): return User.get_all()