2

我有一个模型的 RESTful 控制器UserResource。我添加了一个名为的自定义操作remote_update,并且我只想在用户的 id 匹配时限制该操作:

if user.has_role? :admin
  can :manage, :all

elsif user.has_role? :regular
  can [:remote_update], UserResource, :user_id => user.id

end

load_and_authorize_resource在控制器中使用。

问题是即使用户 ID 不匹配,用户仍然可以使用该操作。(为了测试,我正在使用 Firebug 并更改 id 的隐藏值)。

我的路线如下:

resources :user_resources do

collection do
  post 'remote_update'
end

结尾

根据https://github.com/ryanb/cancan/wiki/Authorizing-controller-actions,当我们有自定义操作时,Cancan 会尝试使用 id 从链接加载资源:

def discontinue
 # Automatically does the following:
 # @product = Product.find(params[:id])
 # authorize! :discontinue, @product
end

我没有定义 id,因为它是 POST,而不是 GET 或 PUT。思考如何构建能力?谢谢你。

4

1 回答 1

1

看起来您正在尝试使用 POST 进行更新('remote_update')。应该创建一个 POST,因此通常不应具有填充的 id。因此,我不希望 CanCan 为您进行查找。

我建议您:

手动查找产品并在您的停止方法中对其进行授权,

或者

使用 PUT

顺便说一句,这个能力对我来说是正确的。

于 2012-10-17T19:27:39.987 回答