推荐学习书目
› 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
auuuny4
V2EX  ›  Python

Flask 验证多用户问题

  •  
  •   auuuny4 ·
    auuunya · Jul 14, 2019 · 3157 views
    This topic created in 2636 days ago, the information mentioned may be changed or developed.

    如题:现有一个 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 replies  •  2019-07-14 22:36:46 +08:00
    qile1
        1
    qile1  
       Jul 14, 2019 via Android
    这个用 else 不行?或者 if a=b and c=d:
    这样不行?
    auuuny4
        2
    auuuny4  
    OP
       Jul 14, 2019
    @qile1 不行,if 进去判断 false 直接 return 了,不 return 的话,又越权了
    whusnoopy
        3
    whusnoopy  
       Jul 14, 2019
    为啥这个不是白名单机制,现在这个更像是黑名单,但是又不太懂你的设置规则

    要更清晰应该是这样吧

    ```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={})
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2429 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 07:22 · PVG 15:22 · LAX 00:22 · JFK 03:22
    ♥ Do have faith in what you're doing.