Rails-如何根据需要进行更改,要求获得不同管理员的批准 [英] Rails - How to handle requiring different administrator approval depending on the change being made

查看:97
本文介绍了Rails-如何根据需要进行更改,要求获得不同管理员的批准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,标题很抱歉,但我无法简洁地表达我要做什么。

Apologies for the terrible title, but I'm having trouble expressing succinctly what I'm trying to do.

我有一个帐户,这些帐户可以完成操作例如创建项目或积分或交易。对于这些更改,我需要获得管理员的批准。渔获物是,所有这些模型都被标记为属于特定州(例如在美国州)。我需要拥有属于每个州的管理员帐户,并且需要授权对其状态中发生的任何事情进行任何更改。因此,例如,如果您创建一个在弗吉尼亚州的项目,则我需要具有允许弗吉尼亚州管理员进入并批准该项目或批准在弗吉尼亚州进行的任何交易的功能。

I have Accounts, and these accounts can do things like create Projects or Credits or Trades. I need to require administrator approval for any changes to these things. The catch is, all of these models are tagged as belonging to a particular State (as in a US state). I need to have administrator Accounts that belong to each state and are required to authorize any changes to anything that happens in their state. So for example, if you create a project that's in Virginia, I need to have functionality that allows a Virginia administrator to come in and approve the project, or approve any trades happening in Virginia.

我能想到的最好的比喻是,在网络论坛中,您将如何拥有作为特定论坛但不是整个网站主持人的用户。

The best analogy I can think of is how in a web forum you would have users that are moderators over specific forums but not the whole site.

我是否可以通过某种方式标记属于 this 模型的任何内容,需要管理员批准?

Is there some way that I can "tag" anything that belongs to this model needs to require that admin's approval?

推荐答案

值得一看像 cancan

您可以在此处定义能力,我相信这些能力可以使您对所描述的内容进行建模。

You can define abilities there which would, I believe, allow you to model what you're describing.

因此,例如,您可以定义您的能力

class Ability
  include CanCan::Ability

  def initialize(user)
    can :manage, Project, :state_id => user.state_id
  end
end

然后您可以检查功能,以了解用户是否可以在控制器中的项目上执行操作,例如:

And you can then check abilities to if your user can perform actions on a project in your controller, say:

if can?(:update, @project)
  ...
else
  flash[:notice] = 'You can only manage projects in your state.'
end

Cancan can除了这个简单的示例以外,还可以做更多的事情,但这使您对可能的情况有所了解。

Cancan can do a lot more than just this simple example but that gives you some idea of what's possible.

这篇关于Rails-如何根据需要进行更改,要求获得不同管理员的批准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆