【问题标题】:Rails - CanCanCan - common abilitiesRails - CanCanCan - 常用能力
【发布时间】:2015-11-25 03:26:13
【问题描述】:

我正在使用 Rails 4、设计、角色模型和 CanCanCan。

是否可以在ability.rb 中定义多个角色共有的能力?

例如,每个登录用户都可以 CRUD 自己的个人资料页面吗?然后角色除了通用能力之外还有特定的能力?

它是如何工作的?我是否需要在角色模型中为通用能力创建一个角色,然后允许每个用户拥有多个角色,以便他们获得通用能力以及特定于角色的能力?

例如,在我的ability.rb中,我有:

class Ability
  include CanCan::Ability

  def initialize(user)

      alias_action :create, :read, :update, :destroy, :to => :crud


    # Define abilities for the passed in user here. For example:
    #
    user ||= User.new # guest user (not logged in)

      #users who are not signed in can create registration or login 

      # can read publicly available projects, programs and proposals
      can :read, Project, {:active => true, :closed => false, :sweep => { :disclosure => { :allusers => true } } }

      # {:active => true, :closed => false  &&  :Project.sweep.disclosure.allusers => true}
      # if user role is student

      if user_signed_in?
        can :crud, Profile, :user_id => user.id #[for themselves]


      elsif user.try(:profile).present? && user.profile.has_role?(:student)

所以,我希望学生能够阅读客人可以阅读的相同内容。有没有办法说学生可以做新用户和登录用户可以做的所有事情(以及学生的特定能力)?

【问题讨论】:

    标签: ruby-on-rails cancan user-roles cancancan


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      我在这里添加了一个示例能力类以供您理解。您可以轻松理解代码并阅读 cmets。您的代码似乎不太好,我可以指出一件事,您不应该通过profile 管理角色,您应该使用user 分配或管理roles

      如果您想为一组用户提供相同的能力,那么您可以使用这种类型的|| 条件user.has_role?(:role_one) || user.has_role?(:role_two) 并将能力块传递为can :manage, [SomeClassName, SomeClassName]

          class Ability
            include CanCan::Ability
      
            def initialize(user)
      
              user ||= User.new
      
              #Only same user can mange his Profile
              can :manage, [Profile], :user_id => user.id
      
              #Give rule wise permission
              if user.admin?
                can :manage, :all
              elsif user.has_role?(:some_role_name)
                can :manage, [SomeClassName]
              elsif user.has_role?(:role_one) || user.has_role?(:role_two)
                can :manage, [SomeClassName, SomeClassName]
              else
                can :read, :all
              end
      
            end
          end
      

      希望这将帮助您完成任务。

      【讨论】:

      • 您好 Rokibul,恐怕我无法理解您的示例的含义。你是说对于普通能力,我需要指定所有可以拥有该能力的角色,而不是定义该能力并将该能力合并到另一个也可以使用该能力的角色中。我也怕我不明白为什么我不能在profile.rb中拥有能力。这是有意的,因为 1 个用户可以拥有许多个人资料。对不起,我不明白你分享的答案。
      • 很遗憾听到您无法理解代码示例。但有一件事,我没有让你指出一个用户有多个配置文件用于多个角色,你可以直接为单个用户分配多个角色,而不是通过配置文件,我认为这会很乱,反正祝你好运:)跨度>
      【解决方案3】:

      你可以通过这样的函数调用在你的角色中进行一种组合

      class Ability
        include CanCan::Ability
      
        def initialize(user)
          # Define abilities for the passed in user here. For example:
          #
          user ||= User.new # guest user (not logged in)
      
            #users who are not signed in can create registration or login
      
            # can read publicly available projects, programs and proposals
      
            # {:active => true, :closed => false  &&  :Project.sweep.disclosure.allusers => true}
            # if user role is student
      
            if user_signed_in?
              if user.try(:profile).present? && user.profile.has_role?(:student)
                student
              else
                authenticated
              end
            else
              anonymous
            end
        end
      
        def anonymous
            can :read, Project, {:active => true, :closed => false, :sweep => { :disclosure => { :allusers => true } } }
        end
      
        def authenticated
          anonymous
          can :crud, Profile, :user_id => user.id #[for themselves]
        end
      
        def student
          authenticated
          #other student abilities
        end
        #other roles follow the same principal
        def teacher
          authenticated
        end
      end
      

      authenticated 函数将包含任何角色的通用能力,每个需要它的角色都会调用(这是一种继承,任何学生都可以做经过身份验证的用户可以做的事情以及他的能力)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-25
        • 1970-01-01
        • 1970-01-01
        • 2019-06-28
        • 1970-01-01
        • 2015-11-27
        • 2015-11-27
        相关资源
        最近更新 更多