代码之家  ›  专栏  ›  技术社区  ›  Ryan Bigg Andrés Bonilla

基于用户权限查找论坛

  •  2
  • Ryan Bigg Andrés Bonilla  · 技术社区  · 17 年前

    我正在实施一个名为Rborard的论坛系统。代码可在以下位置查看: http://github.com/radar/rboard

    有关资料如下:

    class Category < ActiveRecord::Base
      has_many :permissions
      has_many :groups, :through => :permissions
      has_many :forums
    end
    

    论坛模式

    class Forum < ActiveRecord::Base
      has_many :permissions
      has_many :groups, :through => :permissions
      belongs_to :category
    end
    

    群模型

    class Group < ActiveRecord::Base
      has_many :group_users
      has_many :users, :through => :group_users
      belongs_to :owner, :class_name => "User"
    end
    

    权限模型

    class Permission < ActiveRecord::Base
      belongs_to :forum
      belongs_to :category
      belongs_to :group
    end
    

    class User < ActiveRecord::Base
      include Rboard::Permissions
      has_many :group_users
      # Note: I am using nested_has_many_through
      has_many :groups, :through => :group_users
      has_many :permissions, :through => :groups
    end
    

    权限模块

    module Rboard::Permissions
      THINGS = ['forum', 'category']
    
      def self.included(klass)
    
        klass.class_eval do
          # Here we can pass an object to check if the user or any the user's groups
          # has permissions on that particular option.
          def overall_permissions(thing = nil)
            conditions = if thing.nil?
              THINGS.map do |t| 
                "permissions.#{t}_id " + (thing.nil? ? " IS NULL" : "= #{thing.id}") + " OR permissions.#{t}_id IS NULL"     
              end.join(" AND ")
            else
              association = thing.class.to_s.downcase
              "permissions.#{association}_id = #{thing.id} OR permissions.#{association}_id IS NULL"
            end
    
            permissions.all(:conditions => conditions)
          end
    
          def can?(action, thing = nil)
            permissions = overall_permissions(thing)
            !!permissions.detect { |p| p.send("can_#{action}") }
          end
        end
      end
    end
    

    希望有了它,您能够找出权限表中的字段如下 can_see_forum forum_id , category_id default (默认值当前未使用)

    1 回复  |  直到 16 年前
        1
  •  0
  •   Steve    17 年前

    看起来你需要像(虚构的)行为那样的东西。一些mixin可以应用于不同类型的对象——在本例中是组和用户——允许您测试授权。一种可能的用法可能是:

    
    class Group
      include Acts::Permissible
      acts_as_permissible
    end
    
    module Acts
      module Permissible
        def self.acts_as_permissible
          begin
            Role.find(:all).each do |role|
              define_method "can_#{role.access_type}?" do
                self.send('has_role?', role.access_type)
              end
            end
          # Since we're possibly running within the scope of Rake, handle the case   
          # where the roles table doesn't exist  
          rescue ActiveRecord::StatementInvalid => e        
            RAILS_DEFAULT_LOGGER.error "Statement invalid while adding Role methods to User. Is the Roles table present in the DB?\n" + e.inspect
          end
        end
      end
    end
    

    警告:这是 航空代码

    推荐文章