Openstack Horizon(kilo)二次开发之匿名访问View

本秂侑毒 提交于 2020-01-07 15:42:53

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

需要注意的是,这种方式不适用于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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!