Example #1
0
    def register_user(email, password):
        """
        Will register the user with email and password (hashed with sha_512)
        :param email: user's email (can be invalid)
        :param password: sha_512 hashed password
        :return: True if the user has been registered, False if not (exceptions can be used)
        """
        user_data = Database.find_one(collection='users', query={"email": email})

        if user_data is not None:
            # If user_date is not None, the query has returned a filled object, so the user is already registered
            raise UserErrors.UserRegistered("User already registered.")
        if not Utils.validate_email(email):
            # Tell the user if the his email is valid or not
            raise UserErrors.InvalidEmail("Invalid email, try again.")

        User(email=email, password=Utils.encrypt_password(password)).save_to_mongo()
Example #2
0
    def register_user(email, password):
        """
        This method registers a user using e-mail and password.
        The password already comes hashed as sha-512.
        :param email: user's e-mail (might be invalid)
        :param password: sha512-hashed password
        :return: True if registered successfully, or False otherwise (exceptions can also be raised)
        """
        user_data = Database.find_one(UserConstants.COLLECTION,
                                      {"email": email})
        hashed_password = Utils.hash_password(password)
        if user_data is not None:
            raise UserErrors.UserAlreadyRegisteredError(
                "The e-mail you used to register already exists.")
        if not Utils.email_is_valid(email):
            raise UserErrors.InvalidEmailError(
                "The e-mail does not have the right format.")

        User(email, Utils.encrypt_password(hashed_password)).save_to_db()

        return True