【问题标题】:flask - administering user rolesflask - 管理用户角色
【发布时间】:2018-03-29 00:34:17
【问题描述】:

我有一个网站,用户可以在其中拥有多个角色(通常一个用户将拥有多个角色)。我要做的只是添加和删除用户角色。

html 方面非常简单。我有一个选择 2 多选框,可以加载所有可能的用户角色。我选择或取消选择角色,然后提交。

表格只有两个变量;用户名和角色

我有一个带有 if 语句的简单 for 循环,但我似乎遇到了 2 个问题。 1 if 语句由于某种原因永远不是“真”。第二个问题是 for 循环似乎正在退出,它评估 1 个变量,而不是实际循环所有变量。

    formroleadmin = AdminUserRoles()  # if roles administered
    if formroleadmin.roles.data and formroleadmin.validate(): #if form is submitted
        # ar = all roles er = existing roles ur = updated roles
        allroles = usersRolesNames.query.all() #query all possible roles
        usr = formroleadmin.username.data #submitted user name
        ur = formroleadmin.roles.data #load the submitted roles
        er = usersRoles.query.filter_by(username_fk=usr).all() #get current user roles
        for ar in allroles: #loop through all possible roles
            if ar in ur:  # if the role is a submitted role
                if ar not in er:  # if role does not exist add it #if role is not assigned
                    db.session.add(usersRoles(username_fk=usr, role_fk=ar)) #add user
                    db.session.commit()
            elif ar not in ur:  # remove it if exists #if not a submitted role
                if ar in er: # remove if user is assigned role remove permission
                    db.session.filter_by(username_fk=usr, role_fk=ar).delete

内容示例

allroles = ['Admin', 'Completions', 'Completions RW', 'Operations'
ur = ['Completions', 'Operations']

所以我试图做的基本序列是遍历 allroles (ar) 并针对 ur 测试每个值。如果角色存在或不存在,那么它将被添加、删除或传递等。

【问题讨论】:

  • 你能澄清哪些陈述永远不会正确,并尝试打印 allroles 的内容以确保它是一个包含多个成员的迭代。
  • @LoganBertram 我添加了一些数据样本,希望能得到更多说明。例如,如果我在迭代时使用 Admin 作为 ur 中的第一个角色,则如果 ['Admin', 'Operations'] () 中的 'Admin' 是,但那将返回 false。
  • 除了抽象示例,您能否加载调试信息、日志或您尝试解决问题的方法?事实上,这看起来应该可以工作,所以显然还有其他事情发生。尝试在多个点记录变量,看看是否发生了您未预料到的事情。
  • 如果您使用内置服务器启动烧瓶应用程序,那么当抛出错误时,浏览器将提供对交互式调试控制台的访问。如果您正在使用其他方法,那么print(allroles)print(ur) 等。然后您应该会在 stdout 或 stderr 中看到输出。关于内置调试器的一点点:werkzeug.pocoo.org/docs/0.14/debug
  • @LoganBertram 好吧,您的日志记录技巧很有帮助。我从来没有真正使用过日志记录。老实说,修复似乎很奇怪。 if ar in ur: 改为 if str(ar) in ur: etc.

标签: flask user-roles flask-login


【解决方案1】:

通过在迭代的变量周围添加 str() 解决了这个问题。 @LoganBertram 给了我一些建议来追踪这个错误。更新了下面列出的代码(日志记录和故障排除在 iPython 中完成)。

日志是这样添加的

import logging
logging.basicConfig(level=logging.DEBUG)

for u in ar:
    if u in ur:
        logging.debug('found u {} ur {}'.format(u, ur))
    else:
        logging.debug('not found u {} ur {}'.format(u, ur))

DEBUG:root:not found u Admin ur ['Admin', 'Completions']
DEBUG:root:not found u Completions ur ['Admin', 'Completions']

我把代码改成了这个;

for u in ar:
    if str(u) in ur:
        logging.debug('found u {} ur {}'.format(u, ur))
    else:
        logging.debug('not found u {} ur {}'.format(u, ur))

得到了这个结果

DEBUG:root:found u Admin ur ['Admin', 'Completions']
DEBUG:root:found u Completions ur ['Admin', 'Completions']

【讨论】:

    猜你喜欢
    • 2011-03-20
    • 1970-01-01
    • 2020-05-18
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 2017-02-15
    • 1970-01-01
    相关资源
    最近更新 更多