如果用户未登录,则显示基本身份验证弹出窗口

时间:2018-02-23 16:01:08

标签: python flask flask-login

我有几个文件可以在我的网站上下载。 HTML内容引用此方法:

from flask_httpauth import HTTPBasicAuth
auth = HTTPBasicAuth()
...
@app.route('/customer_section/download/<product_id>')
@login_required
def customer_download_file(product_id):
    # get the company the currently logged in user belongs to
    user_company = current_user.company

    # get the product entity
    product = Product.query.filter_by(id=product_id).first_or_404()

    # now get the company, the product belongs to
    product_company = product.project.owner

    if current_user.is_admin or (not current_user.is_admin and product_company.id == user_company.id):

        print("Splitting Path %s to folder and filename..." % product.path)

        # Split the path to folder and filename
        folder, filename = os.path.split(product.path)

        return send_from_directory(folder, filename, mimetype="application/octet-stream", as_attachment=True)
    else:
        # Unauthorized
        abort(401)

当用户在登录视图中通过flask_login登录时,这非常有用。 因此,用户通过我的登录视图登录,然后以某种方式浏览我的网站,点击下载链接下载文件。

但是如果用户试图直接下载文件会怎么样? 然后,我想向用户显示一个弹出窗口,以便用户可以输入他的凭据并可以下载该文件。

这可能吗?

抱歉,我想我的描述不够清楚:当用户调用我的下载方法时,我不会尝试显示我的自定义弹出视图。如果未登录的用户尝试调用该特定URL,我希望显示默认(特定于浏览器)的“基本身份验证”弹出窗口。好像我有一个限制性的.htaccess文件,确保只有授权用户才能继续。如果用户通过我的网页到达相同的URL(登录后,此方法只返回文件流)。

0 个答案:

没有答案
相关问题