1

I am trying to get some auth mechanism working for my webapp (written in Sinatra). Currently I am playing around with sinatra-can which looks great. The issue I now face is that I need access to the specific model from the can method. Lets say I have a route that looks like that:

class ProjMgmt < Sinatra::Base
  get '/edit/:project' do 
    project = Project.where(name: param[:project]).first
    authorize! :edit, project
    project.to_html
  end
end

There are two models defined, Project and Manager. They are stored in a MongoDB (via mongoid, NO datamapper, ActiveRecord or so) and have a has_and_belongs_to_many relation, eg. the relations can be accessed via project.managers or manager.projects.

Now, only managers that have a relation to the particular project should be able to edit the project. What I want to have is something like that on authorize!:

class Ability
  include CanCan::Ability
  def initialize(user)
    can :edit, project if project.managers.include? user
  end
end

Obviously, that does not work since Ability does not know about any project.

Is there any nice approach to this? Must not necessarily be CanCan...

4

1 回答 1

1

像这样试试

def initialize(user)
  can :edit, Project do |project|
    project.managers.include? user
  end
end
于 2013-10-26T12:56:26.797 回答