2010-11-11 79 views
1

我想知道如何在Rails 2.3.8应用程序中实现一些权限逻辑。如何申请权限

例如,用户只能编辑用户创建的任务(又名“拥有”)。

我应该做这样的事情:

user.can_edit_task(task) 

或本:

task.can_be_edited_by?(user) 
#Method needs to be passed in a user object (from controller). But then how 
#can I access the current user in a "before_save" filter as below : 

def User 
    before_save: check_permissions! 

    def check_permissions 
    #this way? 
    raise some_exception if task.can_be_edited_by?(user) 
    #or this way? 
    raise some_exception if self.can_edit_task?(task) 
    end 
end 

有关如何解决这个问题的任何提示?

回答

2

请尽量采用一些现有的插件像cancan

它会挽救你的生命。

+0

看起来好像CanCan可以做我需要的东西..你能帮忙吗? – Zabba 2010-11-11 22:13:57

+0

最好是看这个截屏视频http://railscasts.com/episodes/192-authorization-with-cancan – mpapis 2010-11-11 22:24:55

+1

+1 CanCan,它怎么能不能做你所需要的? – Jonathan 2010-11-12 00:42:44