V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
mu1er
V2EX  ›  Python

Flask 验证多用户问题

  •  
  •   mu1er ·
    auuunya · 2019-07-14 13:03:24 +08:00 · 1963 次点击
    这是一个创建于 1741 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题:现有一个 Company 用户与一个 User 用户表, 如果登录 User 的话,能访问到 Company 的某些页面, 现在想加个 permission_require 裝式器,传两个参数,c and u 代码如下:

    def permissions_required(user,company):
        def decorator(f):
            @wraps(f)
            def decorated_function(*args,**kwargs):
                if current_user.role.name == 'User':
                    if not current_user.can(user):
                        return jsonify(code=-10, msg='没有权限', data={})
                elif current_user.role.name == 'Company':
                    if not current_user.can(company):
                        return jsonify(code=-10, msg='没有权限', data={})
                return f(*args,**kwargs)
            return decorated_function
        return decorator
    

    现存问题: 判断第一个为 Flase 后直接 return 了 不 Return 的话,又越权了。 有没有人解决过相关的问题?

    3 条回复    2019-07-14 22:36:46 +08:00
    qile1
        1
    qile1  
       2019-07-14 13:13:42 +08:00 via Android
    这个用 else 不行?或者 if a=b and c=d:
    这样不行?
    mu1er
        2
    mu1er  
    OP
       2019-07-14 13:26:49 +08:00
    @qile1 不行,if 进去判断 false 直接 return 了,不 return 的话,又越权了
    whusnoopy
        3
    whusnoopy  
       2019-07-14 22:36:46 +08:00
    为啥这个不是白名单机制,现在这个更像是黑名单,但是又不太懂你的设置规则

    要更清晰应该是这样吧

    ```python
    if current_user.role.name == 'User' and current_user.can(user):
    return f(*args, **kwargs)
    if current_user.role.name == 'Company' and current_user.can(company):
    return f**args, **kwargs)
    return jsonify(code=-10, msg='没有权限', data={})
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3352 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 10:43 · PVG 18:43 · LAX 03:43 · JFK 06:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.