2013-04-18 72 views

回答

3

检查用户模型思维的东西。附近有确切你想要的:

# Return true if the user is allowed to do the specified action on a specific context 
    # Action can be: 
    # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') 
    # * a permission Symbol (eg. :edit_project) 
    # Context can be: 
    # * a project : returns true if user is allowed to do the specified action on this project 
    # * an array of projects : returns true if user is allowed on every project 
    # * nil with options[:global] set : check if user has at least one role allowed for this action, 
    # or falls back to Non Member/Anonymous permissions depending if the user is logged 
    def allowed_to?(action, context, options={}, &block) 

您可以通过插件扩展现有的模型和方法添加你喜欢组合现有:

//init.rb

ActionDispatch::Callbacks.to_prepare do 
    unless User.included_modules.include?(MyPlugin::UserPatch) 
    User.send(:include, MyPlugin::UserPatch) 
    end 
end 

// user_patch。 rb类似这样的:

def self.included(base) 
    base.class_eval do 
    unloadable 

    # need to pass context to be able trigger allowed_to method in old way. 
    has_right_view_project(project,context) 
    self.allowed_to?({:controller => context[:controller], :action => context[:action]}, project, :global => true) 
    end 
end 

其实很容易使用现有的方法。

1
if User.current.allowed_to?(:view_private_notes, @project) 
    puts "i know what you did!" 
end 
相关问题