康康条件 [英] Cancan condition

查看:77
本文介绍了康康条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中使用CanCan来管理每个项目在每个实体上的不同角色级别。
我正在这样做:

I'm using CanCan in a project to manage different role level on each entity for each project. I'm doing this:

# encoding: utf-8
class Ability
    include CanCan::Ability 
    def initialize(user)
        user ||= User.new            
        if user.is_admin == true
            can :manage, :all
        else 
            can :read, :all
            Project.all.each do |project|
                current_role_name = user.roles.find_by_project_id(project.id).role_name.name
                if current_role_name.eql?'Auteur senior'
                    can :manage, [Project, Introduction, Abstract, Text, Conclusion, Asset, Attachment], :project_id => project.id
                elsif current_role_name.eql?'Auteur junior'
                    can :manage, [Introduction, Abstract, Attachment], :project_id => project.id
                    can :update, Text, :project_id => project.id, :user_level => current_role_name 
                    can :manage, [Asset], :project_id => project.id, :user_level => current_role_name
                elsif current_role_name.eql?'Équipe phylogéniste'
                    can :manage, [Attachment], :project_id => project.id
                    can :manage, [Text, Asset], :project_id => project.id, :user_level => current_role_name
                end
            end    
        end
    end 
end

它在我检查用户角色名时有效,但是在我要使用以下条件之后:

It works when I check the user role_name but after when I want to use condition like this:

can :update, Text, :project_id => project.id, :user_level => current_role_name

此条件无效。我该如何工作?

The condition doesn't have any effect. How can I make it work?

推荐答案

我终于找到了解决方案:

I finally found the solution :

在有关的控制器中,我用load_and_authorize_resource替换了authorize_resource。

In concerned controller I replaced authorize_resource by load_and_authorize_resource and that's it.

这篇关于康康条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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