From 4a43f602b5847629fa45a04fae1320ed26024ea3 Mon Sep 17 00:00:00 2001 From: Markus Doits Date: Wed, 5 Jun 2019 19:39:43 +0200 Subject: [PATCH] always use base class when saving roles on resources --- lib/rolify.rb | 3 +++ lib/rolify/adapters/active_record/role_adapter.rb | 14 +++++++------- lib/rolify/adapters/mongoid/role_adapter.rb | 14 +++++++------- lib/rolify/role.rb | 2 +- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/rolify.rb b/lib/rolify.rb index 5c02eb5e..684d6688 100644 --- a/lib/rolify.rb +++ b/lib/rolify.rb @@ -73,4 +73,7 @@ def self.resource_types @@resource_types end + def self.base_class_for(klass) + klass.respond_to?(:base_class) ? klass.base_class : klass + end end diff --git a/lib/rolify/adapters/active_record/role_adapter.rb b/lib/rolify/adapters/active_record/role_adapter.rb index 011a0411..5df7ff2c 100644 --- a/lib/rolify/adapters/active_record/role_adapter.rb +++ b/lib/rolify/adapters/active_record/role_adapter.rb @@ -11,9 +11,9 @@ def where(relation, *args) def where_strict(relation, args) return relation.where(:name => args[:name]) if args[:resource].blank? resource = if args[:resource].is_a?(Class) - {class: args[:resource].to_s, id: nil} + {class: Rolify.base_class_for(args[:resource]).to_s, id: nil} else - {class: args[:resource].class.name, id: args[:resource].id} + {class: Rolify.base_class_for(args[:resource].class).name, id: args[:resource].id} end relation.where(:name => args[:name], :resource_type => resource[:class], :resource_id => resource[:id]) @@ -21,7 +21,7 @@ def where_strict(relation, args) def find_cached(relation, args) resource_id = (args[:resource].nil? || args[:resource].is_a?(Class) || args[:resource] == :any) ? nil : args[:resource].id - resource_type = args[:resource].is_a?(Class) ? args[:resource].to_s : args[:resource].class.name + resource_type = args[:resource].is_a?(Class) ? Rolify.base_class_for(args[:resource]).to_s : Rolify.base_class_for(args[:resource].class).name return relation.find_all { |role| role.name == args[:name].to_s } if args[:resource] == :any @@ -34,7 +34,7 @@ def find_cached(relation, args) def find_cached_strict(relation, args) resource_id = (args[:resource].nil? || args[:resource].is_a?(Class)) ? nil : args[:resource].id - resource_type = args[:resource].is_a?(Class) ? args[:resource].to_s : args[:resource].class.name + resource_type = args[:resource].is_a?(Class) ? Rolify.base_class_for(args[:resource]).to_s : Rolify.base_class_for(args[:resource].class).name relation.find_all do |role| role.resource_id == resource_id && role.resource_type == resource_type && role.name == args[:name].to_s @@ -51,7 +51,7 @@ def add(relation, role) def remove(relation, role_name, resource = nil) cond = { :name => role_name } - cond[:resource_type] = (resource.is_a?(Class) ? resource.to_s : resource.class.name) if resource + cond[:resource_type] = (resource.is_a?(Class) ? Rolify.base_class_for(resource).to_s : Rolify.base_class_for(resource.class).name) if resource cond[:resource_id] = resource.id if resource && !resource.is_a?(Class) roles = relation.roles.where(cond) if roles @@ -104,10 +104,10 @@ def build_query(role, resource = nil) if resource query.insert(0, "(") query += " OR ((#{role_table}.name = ?) AND (#{role_table}.resource_type = ?) AND (#{role_table}.resource_id IS NULL))" - values << role << (resource.is_a?(Class) ? resource.to_s : resource.class.name) + values << role << (resource.is_a?(Class) ? Rolify.base_class_for(resource).to_s : Rolify.base_class_for(resource.class).name) if !resource.is_a? Class query += " OR ((#{role_table}.name = ?) AND (#{role_table}.resource_type = ?) AND (#{role_table}.resource_id = ?))" - values << role << resource.class.name << resource.id + values << role << Rolify.base_class_for(resource.class).name << resource.id end query += ")" end diff --git a/lib/rolify/adapters/mongoid/role_adapter.rb b/lib/rolify/adapters/mongoid/role_adapter.rb index 042a8b73..0da2d8f7 100644 --- a/lib/rolify/adapters/mongoid/role_adapter.rb +++ b/lib/rolify/adapters/mongoid/role_adapter.rb @@ -11,9 +11,9 @@ def where(relation, *args) def where_strict(relation, args) return relation.where(:name => args[:name]) if args[:resource].blank? resource = if args[:resource].is_a?(Class) - {class: args[:resource].to_s, id: nil} + {class: Rolify.base_class_for(args[:resource]).to_s, id: nil} else - {class: args[:resource].class.name, id: args[:resource].id} + {class: Rolify.base_class_for(args[:resource].class).name, id: args[:resource].id} end relation.where(:name => args[:name], :resource_type => resource[:class], :resource_id => resource[:id]) @@ -21,7 +21,7 @@ def where_strict(relation, args) def find_cached(relation, args) resource_id = (args[:resource].nil? || args[:resource].is_a?(Class) || args[:resource] == :any) ? nil : args[:resource].id - resource_type = args[:resource].is_a?(Class) ? args[:resource].to_s : args[:resource].class.name + resource_type = args[:resource].is_a?(Class) ? Rolify.base_class_for(args[:resource]).to_s : Rolify.base_class_for(args[:resource].class).name return relation.find_all { |role| role.name == args[:name].to_s } if args[:resource] == :any @@ -34,7 +34,7 @@ def find_cached(relation, args) def find_cached_strict(relation, args) resource_id = (args[:resource].nil? || args[:resource].is_a?(Class)) ? nil : args[:resource].id - resource_type = args[:resource].is_a?(Class) ? args[:resource].to_s : args[:resource].class.name + resource_type = args[:resource].is_a?(Class) ? Rolify.base_class_for(args[:resource]).to_s : Rolify.base_class_for(args[:resource].class).name relation.find_all do |role| role.resource_id == resource_id && role.resource_type == resource_type && role.name == args[:name].to_s @@ -67,7 +67,7 @@ def remove(relation, role_name, resource = nil) # role.destroy if role.send(user_class.to_s.tableize.to_sym).empty? #end cond = { :name => role_name } - cond[:resource_type] = (resource.is_a?(Class) ? resource.to_s : resource.class.name) if resource + cond[:resource_type] = (resource.is_a?(Class) ? Rolify.base_class_for(resource).to_s : Rolify.base_class_for(resource.class).name) if resource cond[:resource_id] = resource.id if resource && !resource.is_a?(Class) roles = relation.roles.where(cond) roles.each do |role| @@ -116,9 +116,9 @@ def build_query(role, resource = nil) return [{ :name => role }] if resource == :any query = [{ :name => role, :resource_type => nil, :resource_id => nil }] if resource - query << { :name => role, :resource_type => (resource.is_a?(Class) ? resource.to_s : resource.class.name), :resource_id => nil } + query << { :name => role, :resource_type => (resource.is_a?(Class) ? Rolify.base_class_for(resource).to_s : Rolify.base_class_for(resource.class).name), :resource_id => nil } if !resource.is_a? Class - query << { :name => role, :resource_type => resource.class.name, :resource_id => resource.id } + query << { :name => role, :resource_type => Rolify.base_class_for(resource.class).name, :resource_id => resource.id } end end query diff --git a/lib/rolify/role.rb b/lib/rolify/role.rb index 34f6f13a..2ad4dd10 100644 --- a/lib/rolify/role.rb +++ b/lib/rolify/role.rb @@ -11,7 +11,7 @@ def self.included(base) def add_role(role_name, resource = nil) role = self.class.adapter.find_or_create_by(role_name.to_s, - (resource.is_a?(Class) ? resource.to_s : resource.class.name if resource), + (resource.is_a?(Class) ? Rolify.base_class_for(resource).to_s : Rolify.base_class_for(resource.class).name if resource), (resource.id if resource && !resource.is_a?(Class))) if !roles.include?(role)