Openstack Horizon(kilo)二次開發之匿名訪問View

須要注意的是,這種方式不適用於class view.ui

  1. 修改horizon.base.require_auth:this

    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:code

    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=Trueorm

    setattr(view_func,'public',True)get

相關文章
相關標籤/搜索