无法在Rails中配置gem'one'

时间:2014-01-20 04:52:08

标签: ruby devise ruby-on-rails-4

我在我的项目中使用Rails 4,在我的应用程序中使用不同的角色,必须为我使用gem 'six'的不同角色提供不同的访问权限。但无法配置它。

我的user Model已定义

ROLES = %w[ ADMIN STUDENT TEACHER ]

我的应用程序控制器

 before_action :add_abilities
 helper_method :abilities, :can?

 def abilities
   @abilities ||= Six.new
 end

 private
 def can?(object, action, subject)
   abilities.allowed?(object, action, subject)
 end
 def add_abilities
   abilities << Ability
 end

我的能力.rb

`

class Ability
  class << self
    def allowed(user, subject)
      return [] unless user.kind_of?(User)

      case subject.class.name
      when "School" then school_abilities(user, subject)
      when "Standard" then standard_abilities(user, subject)
      when "Section" then section_abilities(user, subject)
      when "Subject" then subject_abailities(user, subject)
      when "Test" then test_abilities(user, subject)
      when "Teach" then teach_abitities(user,subject)
      when "Study" then Study_abilities(user,subject)
      when "Participate" then participate_abilities(user, subject)
      else []
      end.concat(global_abilities(user))
    end

    def global_abilities(user)
      rules = []
      rules << :create_school
      rules
    end

    def school_abilities(user, school)
      rules = []

      # if User::ROLES.first == "ADMIN"
      #   rules += school_admin_rules
      # end
      # if User::ROLES.last == "TEACHER"
      #   rule += school_teacher_rules
      # end
      # if User::ROLES.second == "STUDENT"
      #   rule += school_student_rules
      # end
      if user.role == "ADMIN"
        rules += school_admin_rules
      end
      if user.role == "TEACHER"
        rule += school_teacher_rules
      end
      if user.role == "STUDENT"
        rule += school_student_rules
      end

      rules.flatten
    end

    def school_student_rules
      [
        :read_test
      ]
   end

    def school_teacher_rules
      [
        :read_test,
        :write_test,
        :modify_test,
        :create_test
      ]
    end

    def school_admin_rules
      [
        :admin_school,
        :change_school,
        :rename_school,
        :remove_school,
        :modify_school,
        :write_school
      ]

    end

    [:issue, :note, :snippet, :merge_request].each do |name|
      define_method "#{name}_abilities" do |user, subject|
        if subject.school == user
          [
            :"read_#{name}",
            :"write_#{name}",
            :"modify_#{name}",
            :"admin_#{name}"
          ]
        elsif subject.respond_to?(:teach) && subject.teach == user
          [
            :"read_#{name}",
            :"write_#{name}",
            :"modify_#{name}"
          ]
        elsif subject.respond_to?(:study) && subject.study == user
          [
            :"read_#{name}",
            :"write_#{name}"
          ]
        else
          subject.respond_to?(:school) ? school_abilities(user, subject.school) : []
        end
      end
    end

  end
end

`

如果select role是老师,她只能访问测试控制器,而不能访问任何其他控制器。我能怎么做。有人请帮帮我。提前致谢