Sha256: 16945614ecda077ea5c5284a1d8ddf5c4f95c1d1366cff18468a89801ecf9370

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

module PrivatePerson
  module Permitted
    def acts_as_permitted
      class_eval do
        def is_permitted?(permissor, permissible)
          if permissible.nil?
            raise 'Called is_permitted? on nil. Does not compute. Preparing to self destruct.'
          end
          if Permission.find_all_by_permissible(permissible).blocked.exists?
            return false
          end
          wildcards = permissions_by(permissor).find_all_by_wildcard(permissible.class.name).legitimate
          if wildcards.exists?
            return true
          end
          permissions = permissions_by(permissor).find_all_by_permissible(permissible).legitimate
          if permissions.exists?
            return true
          end
          return false
        end

        def permissions_by(permissor)
          Permission.find_all_by_permissor(permissor).find_all_by_relationship_type(relationship_to(permissor))
        end

        def relationship_to(permissor)
          # First make sure we're not a new user
          if self.new_record?
            return 'public'
          end
          # Next check for an efficient method
          for relationship_method in permissor.class.of
            is_method = ('is_' + relationship_method.to_s.singularize + '_of?').to_sym
            if respond_to?(is_method) and self.send(is_method, permissor)
              return relationship_method.to_s
            end
          end
          # Then check for a slow method
          for relationship_method in permissor.class.of
            relationship_members = permissor.send(relationship_method.to_sym)
            if relationship_members.exists?(:id => self.id)
              return relationship_method.to_s
            end
          end
          return 'public'
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
private_person-0.2.4 lib/private_person/models/permitted.rb