“is_authenticated”是什么意思? Flask-Login中使用的方法? [英] What's the point of the "is_authenticated" method used in Flask-Login?

查看:951
本文介绍了“is_authenticated”是什么意思? Flask-Login中使用的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  

我正在通过Flask Mega-Tutorial进行工作, class User(db.Model):
id = db.Column(db.Integer,primary_key = True)
nickname = db.Column(db.String(64),unique = True)
email = db.Column(db.String(120),unique = True)
role = db.Column(db.SmallInteger,default = ROLE_USER)
posts = db.relationship('Post', (self):
返回True

def is_active(self):


def is_authenticated(self)返回True
$ b $ def is_anonymous(self):
return False
$ b def get_id(self):
return unicode(self.id)

def __repr __(self):
return'< User%r>'%(self.nickname)

is_authenticated,is_active和is_anonymous对我来说似乎很奇怪 - 他们什么时候会返回除了它们的pr之外的任何东西edefined的值?

有人可以向我解释为什么Flask-Login让我使用这些看似无用的方法吗? 首先, is_anonymous() is_authenticated()是彼此相反的。你可以定义一个作为另一个的否定,如果你想。

你可以使用这两个方法来确定用户是否登录。



当没有人登录时,Flask-Login的 current_user 被设置为 AnonymousUser 目的。此对象响应 is_authenticated() is_active() False is_anonymous() True 。



is_active()方法有另一个重要用途。不像我在本教程中建议的那样总是返回 True ,您可以使禁用或停用的用户返回 False 这些用户将不被允许登录。


I'm working through the Flask Mega-Tutorial right now and I've come across this bit of code:

class User(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    nickname = db.Column(db.String(64), unique = True)
    email = db.Column(db.String(120), unique = True)
    role = db.Column(db.SmallInteger, default = ROLE_USER)
    posts = db.relationship('Post', backref = 'author', lazy = 'dynamic')

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return unicode(self.id)

    def __repr__(self):
        return '<User %r>' % (self.nickname)

is_authenticated, is_active, and is_anonymous seem quite strange to me - when would they ever return anything other than their predefined value?

Could somebody explain to me why Flask-Login makes me use these seemingly useless methods?

解决方案

First of all, is_anonymous() and is_authenticated() are each other's inverse. You could define one as the negation of the other, if you want.

You can use these two methods to determine if a user is logged in.

When nobody is logged in Flask-Login's current_user is set to an AnonymousUser object. This object responds to is_authenticated() and is_active() with False and to is_anonymous() with True.

The is_active() method has another important use. Instead of always returning True like I proposed in the tutorial, you can make it return False for banned or deactivated users and those users will not be allowed to login.

这篇关于“is_authenticated”是什么意思? Flask-Login中使用的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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