Пример #1
0
    def login(self, email_address):
        if email_address in self.open_connections:
            return

        password_var_name = "{name}_EMAIL_PASSWORD".format(
            name=email_address.split("@")[0].upper())

        password = CONFIG.get(password_var_name)
        if password is None:
            logger.error(
                f"Trying to connect to an unknown email: {email_address}")
            raise ValueError(f"{email_address} is unknown")

        client = self.get_email_client()
        client.login(email_address, password)
        logger.info(f"Connected to {email_address}")

        self.open_connections[email_address] = client
Пример #2
0
def create_user(
        user_data: UserModel,
        session: Session = Depends(get_session),
        language: str = Cookie(None),
):
    # validate data
    if language not in ("fr", "en"):
        language = "fr"
    if not helpers.validate_email(user_data.email):
        raise HTTPException(400, "Invalid email address")
    if not helpers.validate_password(user_data.password):
        raise HTTPException(400, "Invalid password")
    if session.query(exists().where(Users.email == user_data.email)).scalar():
        raise HTTPException(400, "Email already exists")
    if session.query(exists().where(
            Credentials.username == user_data.username)).scalar():
        raise HTTPException(400, "Username already exists")

    # create all objects
    user = Users(email=user_data.email)
    credentials = Credentials(
        user=user,
        username=user_data.username,
        password=helpers.get_password_hash(user_data.password),
    )

    owner = Owners(user=user, name=user_data.username)

    try:
        session.add_all((user, credentials, owner))
        session.commit()
        logger.info(f"User created: {user.email}",
                    user_id=user.id,
                    user_email=user.email)
    except Exception as exc:
        logger.exception(f"Could not create user {user_data.email}")
        raise HTTPException(400,
                            "Database error when creating the user") from exc

    if not helpers.send_event_user_created(user=user, language=language):
        logger.error("User created but could not publish the rabbitmq message",
                     user_id=user.id)

    return user
Пример #3
0
def reset_user_password(properties, payload):
    logger.info(
        "Received message to reset a user password !",
        properties=properties,
        payload=payload,
    )

    try:
        payload = json.loads(payload)
    except:
        logger.exception("Message is not in JSON format")
        return False

    required_keys = ("user", "language", "reset_link")

    if any(key not in payload for key in required_keys):
        logger.error("Incorrect message received - some keys are missing",
                     payload=payload)
        return False

    send_email(payload, RESET_PASSWORD_EMAIL, reset_link=payload["reset_link"])
    return True
Пример #4
0
def welcome_new_user(properties, payload):
    logger.info(
        "Received message to welcome a new user !",
        properties=properties,
        payload=payload,
    )

    try:
        payload = json.loads(payload)
    except:
        logger.exception("Message is not in JSON format")
        return False

    required_keys = ("user", "language", "activation_link")

    if any(key not in payload for key in required_keys):
        logger.error("Incorrect message received - some keys are missing",
                     payload=payload)
        return False

    send_email(payload,
               WELCOME_EMAIL,
               activation_link=payload["activation_link"])
    return True
Пример #5
0
def initialize_user(properties, body):
    body = json.loads(body)
    user_id = body.get("user", {}).get("id")
    language = body.get("language")

    if user_id is None:
        logger.error(
            "Incorrect message received: missing 'user.id' key",
            body=body,
            properties=properties,
        )
        return False

    if language is None:
        logger.error(
            "Incorrect message received: missing 'language' key",
            body=body,
            properties=properties,
        )
        return False

    if language not in LANGUAGES:
        logger.error(f"Incorrect language: {body['languages']}")
        return False

    objects = [
        HiveConditions(name=item[language], user_id=user_id)
        for item in DEFAULT_HIVE_CONDITIONS
    ]
    objects.extend(
        HoneyTypes(name=item[language], user_id=user_id)
        for item in DEFAULT_HONEY_TYPES)
    objects.extend(
        SwarmHealthStatuses(name=item[language], user_id=user_id)
        for item in DEFAULT_SWARM_HEALTH_STATUSES)
    objects.extend(
        EventTypes(name=item[language], user_id=user_id)
        for item in DEFAULT_EVENT_TYPES)
    objects.extend(
        EventStatuses(name=item[language], user_id=user_id)
        for item in DEFAULT_EVENT_STATUSES)

    db_client = db()
    with db_client as session:
        session.bulk_save_objects(objects)
        session.commit()

    logger.info("Default data created for user", user_id=user_id)
    return True