Ruby on rails 基于用户查找论坛';s权限
我正在实施一个名为Rborard的论坛系统。代码可在上查看。我一直在尝试实现的权限代码陷入了僵局,我决定求助于无所不知、无忧无虑的堆栈溢出来解决这个问题 有关资料如下: 类别模型Ruby on rails 基于用户查找论坛';s权限,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在实施一个名为Rborard的论坛系统。代码可在上查看。我一直在尝试实现的权限代码陷入了僵局,我决定求助于无所不知、无忧无虑的堆栈溢出来解决这个问题 有关资料如下: 类别模型 class Category < ActiveRecord::Base has_many :permissions has_many :groups, :through => :permissions has_many :forums end class Forum < ActiveRe
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
希望通过这个,您能够找出权限表中的字段,如can\u see\u forum
等等。额外字段包括forum\u id
、category\u id
和default
(默认值当前未使用)
我想知道的是,我怎样才能找到一个小组能看到的所有论坛?通常,如果设置了论坛id,则该权限适用。如果该组只有一个权限,但没有指定论坛id或类别id,则该组将被视为所有内容的全局权限。我在这里完全不知所措。看起来你需要(虚构的)行为。一些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
警告:这是航空代码!从未测试过。但是,您可以将类似的内容混合到您的用户中,并针对与您的组模型相同的角色进行授权。看起来您需要类似于(虚构的)行为的内容。一些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
警告:这是航空代码!从未测试过。但是您可以将类似的内容混合到您的用户中,并针对与您的组模型相同的角色进行授权
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