Skip to content

Commit

Permalink
added support for polymorphic associations
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitri-d authored and ehelms committed Sep 9, 2014
1 parent c2f52de commit 4c7a129
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 2 additions & 0 deletions app/models/host/managed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Host::Managed < Host::Base
belongs_to :owner, :polymorphic => true
belongs_to :compute_resource
belongs_to :image
belongs_to :domain
belongs_to :subnet

belongs_to :location
belongs_to :organization
Expand Down
10 changes: 5 additions & 5 deletions app/validators/authorize_associations_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ def validate(record)

record.class.reflect_on_all_associations.each do |association|

if check_authorization?(association, exemptions)
if check_authorization?(record, association, exemptions)

permission = exemptions[association.name] || "view_#{association.klass.name.downcase.pluralize}"
check_association(record, association, permission)

end
end
end
rescue NameError => e
end

def check_association(record, association, permission)
Expand Down Expand Up @@ -97,10 +96,11 @@ def organization?(association_name)
association_name.to_s.singularize == 'organization'
end

def check_authorization?(association, exemptions)
def check_authorization?(record, association, exemptions)
klass = association.options.include?(:polymorphic) ? record.send(association.foreign_type) : association.klass
!exemptions[association.name] &&
association.klass &&
association.klass.respond_to?(:authorized) &&
klass &&
klass.respond_to?(:authorized) &&
association.name != 'taxonomies'
end

Expand Down

0 comments on commit 4c7a129

Please sign in to comment.