如何获取当前已登录Flask-Login的用户列表? [英] How to get the list of users who are currently logged in Flask-Login?

查看:890
本文介绍了如何获取当前已登录Flask-Login的用户列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Flask-SQLAlchemy Web应用程序中,我实现了 Flask-Login 用于用户身份验证和授权.现在,我正在构建一个管理员"部分,管理员用户可以在其中添加,编辑或删除任何用户并查看用户列表.因此,在删除任何用户之前,我想知道该用户当前是从某个位置登录到该应用程序,还是只是与该Web应用程序进行了任何活动会话.我该如何帮忙呢?还是在应用程序级别甚至有可能?

In my Flask-SQLAlchemy web application I have implemented Flask-Login for user authentication and authorization. Now I am building an Admin section where the admin user can add, edit or delete any user and view user list. So before deleting any user I want to know whether that user is currently logged in to the application from somewhere or simply having any active session with the web application. How can I accomplice this? Or Is this even possible in the application level?

[注意:如您所见,这与无关 current_user.is_authenticated]

[Note: As you can see, this is NOT about current_user.is_authenticated]

推荐答案

您将必须自行实现此功能,因为无法使用flask-login本地实现此功能.

You will have to implement this feature yourself as there is not a way to do it natively with flask-login.

我可以想到各种各样的方法,但是最快的方法可能只是跟踪用户上次访问您的网站的时间.然后,您可以设置一个合理的timeout,如果用户在过去30分钟内(或您感到满意的长时间)未做任何事情,则假定该用户未登录.

I can think of myriad ways to do this, but the quickest is probably just keeping track of the last time a user accessed your site. Then you can set a reasonable timeout where you assume the user is not logged in if they haven't done anything for the last 30 minutes (or however long you feel comfortable with)

例如:

@app.before_request
def update_last_active():
    current_user.last_active = datetime.utcnow()
    db.session.commit()

每当用户向您的服务器发送请求时,这就会更新User.last_active.

This would update User.last_active every time the user sends a request to your server.

这篇关于如何获取当前已登录Flask-Login的用户列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆