python - 从 flask_ldap3_login 的 current_user.get_id() 中提取 uid 的正确方法?

标签 python flask flask-login

我终于让 flask-login + flask-ldap3-login 一起工作了,当我调用 current_user.get_id() 时,我得到一个像 "uid=myuid,ou=people,ou=group,ou=internal,o =组织”

我知道我可以拆分这个字符串并分割出我想要的部分,但由于这些是属性,所以感觉我应该能够获得 current_user.get_id().uid 或类似的东西,以获得唯一和准确的我想要的作品。

除了切掉 'uid=' 和 ',' 之间的部分之外,是否有任何类似的东西,或者任何更安全的方法来分割它?

最佳答案

ldap_app.User.get_id returns a unicode value :

# Declare an Object Model for the user, and make it comply with the
# flask-login UserMixin mixin.
class User(UserMixin):
    def __init__(self, dn, username, data):
        self.dn = dn
        self.username = username
        self.data = data

    def __repr__(self):
        return self.dn

    def get_id(self):
        return self.dn

基于commentsLDAP3LoginManager.save_user 中,User.data 应该获取用户数据的字典:

def save_user(self, callback):
        '''
        This sets the callback for saving a user that has been looked up from
        from ldap.
        The function you set should take a user dn (unicode), username
        (unicode) and userdata (dict), and memberships (list).
        ::
            @ldap3_manager.save_user
            def save_user(dn, username, userdata, memberships):
                return User(username=username, data=userdata)
        Your callback function MUST return the user object in your ORM
        (or similar). as this is used within the LoginForm and placed
        at ``form.user``
        Args:
            callback (function): The function to be used as the save user
                                 callback.
        '''

        self._save_user = callback
        return callback

所以current_user.data,而不是current_user.get_id(),应该有一个信息字典。

如果该字典没有您需要的内容,您可能会卡在 pulling apart the dn string 上。 :

useful_data = dict(x.split('=') for x in current_user.get_id().split(','))

关于python - 从 flask_ldap3_login 的 current_user.get_id() 中提取 uid 的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57174083/

相关文章:

python - Flask 登录 - @login_manager.token_loader 未被调用

python - 删除 Flask 和 Flask-Login 中的 session cookie

python - PuLP如何根据之前的结果设置下一个变量?

python - 使用命名管道根据输出将输入发送到程序

python - 如何在Sublime 2的搜索过程中(通过ctrl + shift + f)排除某些目录?

python - Flask-Session 无法从 itsdangerous 导入 Want_bytes

python - Pandas 按行值拆分/分组数据框

python - 我在 Heroku 上收到 404 错误,但它在本地有效

python - 类型错误:convert() 缺少 1 个必需的位置参数: 'filename'

python - flask 登录 : can't understand how it works from documentation