博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Openstack Horizon(kilo)二次开发之匿名访问View
阅读量:6921 次
发布时间:2019-06-27

本文共 1927 字,大约阅读时间需要 6 分钟。

hot3.png

需要注意的是,这种方式不适用于class view.

  1. 修改horizon.base.require_auth:

    def require_auth(view_func):	    """Performs user authentication check.	    Similar to Django's `login_required` decorator, except that this throws	    :exc:`~horizon.exceptions.NotAuthenticated` exception if the user is not	    signed-in.	    """	    from horizon.exceptions import NotAuthenticated  # noqa	    @functools.wraps(view_func, assigned=available_attrs(view_func))	    def dec(request, *args, **kwargs):	        #此处添加一个判断,如果view_func的public属性为True则跳过认证.	        if getattr(view_func,'public',False):	            return view_func(request, *args, **kwargs)	        if request.user.is_authenticated():	            return view_func(request, *args, **kwargs)	        raise NotAuthenticated(_("Please log in to continue."))	    return dec
  2. 修改horizon.base.require_perms:

    def require_perms(view_func, required):	    from horizon.exceptions import NotAuthorized  # noqa	    # We only need to check each permission once for a view, so we'll use a set	    current_perms = getattr(view_func, '_required_perms', set([]))	    view_func._required_perms = current_perms | set(required)	    @functools.wraps(view_func, assigned=available_attrs(view_func))	    def dec(request, *args, **kwargs):	        #此处添加一个判断,如果view_func的public属性为True则跳过认证.	        if getattr(view_func,'public',False):	            return view_func(request, *args, **kwargs)	        if request.user.is_authenticated():	            if request.user.has_perms(view_func._required_perms):	                return view_func(request, *args, **kwargs)	        raise NotAuthorized(_("You are not authorized to access %s")	                            % request.path)	    # If we don't have any permissions, just return the original view.	    if required:	        return dec	    else:	        return view_func
  3. 给需要匿名访问的View设置属性public=True

    setattr(view_func,'public',True)

转载于:https://my.oschina.net/fmnisme/blog/527028

你可能感兴趣的文章
Linux用ssh登陆出现“Too many authentication failures for root”
查看>>
【.net 深呼吸】聊聊WCF服务返回XML或JSON格式数据
查看>>
python 中密码处理函数
查看>>
Android项目的目录结构
查看>>
cookie的写入与读出
查看>>
插入到Mysql数据库中的汉字乱码
查看>>
linux系统分析工具续-SystemTap和火焰图(Flame Graph)
查看>>
C# 之 static的用法详解
查看>>
[转] C# 键盘中的按键对应的KeyValue
查看>>
从零开始学 Java - Spring AOP 拦截器的基本实现
查看>>
算法题:合并两个有序的链表
查看>>
【MyBatis学习10】高级映射之多对多查询
查看>>
Codeforces Round #258 (Div. 2) C. Predict Outcome of the Game 水题
查看>>
擅长排列的小明
查看>>
JavaScript之链式结构序列化1
查看>>
虚拟机无法使用网卡桥接模式
查看>>
Spring获取ApplicationContext
查看>>
Echarts折线图点击事件
查看>>
activiti 开发环境
查看>>
机器学习,流式IoT和医疗设备互联
查看>>