代码之家  ›  专栏  ›  技术社区  ›  Sniggerfardimungus

if/then/else/return/end的声明性ruby编程替换?

  •  4
  • Sniggerfardimungus  · 技术社区  · 16 年前

    我的控制器里到处都是这个:

    if not session[:admin]
      flash[:notice] = "You don't have the rights to do #{:action}."
      redirect_to :action=>:index
      return
    end
    

    以及它的兄弟姐妹:

    if not session[:user] and not session[:admin]
      flash[:notice] = "You don't have the rights to do #{:action}."
      redirect_to :action=>:index
      return
    end
    

    当我想在方法中使用它时,我想将其全部缩减为一个声明行:

    def action_which_requires_rights
        require_rights :admin
        #or:
        #:require_rights :user_or_admin
    end
    

    显然,如果require_rights失败,我不希望执行方法的其余部分。我发誓有办法这样做,但我找不到我读到它的地方。我在想象吗?

    5 回复  |  直到 16 年前
        1
  •  8
  •   MatthewFord    16 年前

    首先你可以做到: unless session[:admin] 而不是 if not ...

    然后你可以有一个调用你的方法的前过滤器,这个方法会把你的重定向到“URL”并返回。

    我有一个问题,我希望你不仅仅是把管理员的id存储在会话中,作为你唯一的身份验证方法,在你的用户模型上有一个属性,查询可能是一个更安全的选择。

        2
  •  6
  •   Chuck    16 年前

    正如其他人所说,前置过滤器似乎是正确的工具。但我会解决你问的实际模式。

    不幸的是,一个方法不能导致它的调用方法返回。与您所寻找的模式最接近的两个匹配项:

    一个街区:

    def require_rights(rights)
      if session[rights]
        yield
      else
        flash[:notice] = "You don't have the rights to do #{:action}."
        redirect_to :action=>:index
      end
    end
    

    这样你就可以:

    def action_which_requires_rights
      require_rights :admin do
        #do whatever here
      end
    end
    

    或返回值:

    def require_rights(rights)
      return true if session[rights]
      flash[:notice] = "You don't have the rights to do #{:action}."
      redirect_to :action=>:index
      false
    end
    

    这样你就可以:

    def action_which_requires_rights
      require_rights :admin or return
      #do whatever here
    end
    

    我更喜欢这个块,因为它适合类似的方法,并使调用者这样做 or return 我觉得有点不自然。

        3
  •  3
  •   Sophie Alpert    16 年前

    在筛选之前查看。它们可以停止执行,并且可以局限于某些动作。

        4
  •  1
  •   aivarsak    16 年前

    如果不允许用户执行此操作,则我不会向用户显示操作(我将使用助手来完成此操作)

    在控制器中,正如其他答案中提到的,imho最好的方法是使用before过滤器来控制访问权限。

    我还建议使用restful身份验证插件来管理用户角色。

        5
  •  0
  •   Vincent Robert    16 年前

    你可以尝试一些抛出异常的方法。

    def action_for_admins
      require_rights :admin
    end
    
    begin 
      action_for_admins
    rescue
      <%= You don't have the rights to do that %>
    end
    

    那么要求权利应该是这样的

    def require_rights(*rights)
      rights.each do |right|
        raise "Missing right #{right.to_s}" if not user.has_right?(right)
      end
    end
    

    注意,我是Ruby或Rails的初学者,所以可能不是 .

    推荐文章