2014-01-20 44 views
0

我在我的項目中使用Rails 4,並在我的應用中使用不同的角色,因此我必須爲不同的角色授予不同的訪問權限,因爲我使用的是gem 'six'。但無法配置它。無法在Rails中配置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 

我ability.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 

`

如果選擇角色是老師,她只能訪問測試控制器而不是任何其他控制器。我能怎麼做。有人請幫助我。在此先感謝

回答