<commit_before>require 'project_compat'
module Acts
  module Authorized
    module PolicyBasedAuthorization
      def self.included klass
        attr_accessor :permission_for
        klass.extend ClassMethods
        klass.extend AuthLookupClassMethods
        klass.class_eval do
          belongs_to :contributor, :polymorphic => true unless method_defined? :contributor
          after_initialize :contributor_or_default_if_new

          #checks a policy exists, and if missing resorts to using a private policy
          after_initialize :policy_or_default_if_new

          include ProjectCompat unless method_defined? :projects

          belongs_to :policy, :required_access_to_owner => :manage, :autosave => true

          before_validation :temporary_policy_while_waiting_for_publishing_approval, :publishing_auth, :unless => "Seek::Config.is_virtualliver"
          after_save :queue_update_auth_table
          after_destroy :remove_from_lookup_table
          before_save :update_timestamp_if_policy_was_saved, :if => "Seek::Config.is_virtualliver"

          def update_timestamp_if_policy_was_saved
            #autosaved belongs_to associations get saved before the parent, so to check if it has changed, see if it has a newer updated_at
            update_timestamp if updated_at && policy.updated_at > updated_at
          end


        end
      end

      module ClassMethods

      end

      module AuthLookupClassMethods

        #returns all the authorised items for a given action and optionally a user and set of projects. If user is nil, the items authorised for an
        #anonymous user are returned. If one or more projects are provided, then only the assets linked to those projects are included.
        def all_authorized_for action, user=User.current_user, projects=nil
          projects=Array(projects) unless projects.nil?
          user_id = user.nil? ? 0 : user.id
          assets = []
          programatic_project_filter = !projects.nil? && (!Seek::Config.auth_lookup_enabled || (self==Assay || self==Study))
          if Seek::Config.auth_lookup_enabled
            if (lookup_table_consistent?(user_id))
              Rails.logger.info("Lookup table #{lookup_table_name} is complete for user_id = #{user_id}")
              assets = lookup_for_action_and_user action, user_id, projects
            else
              Rails.logger.info("Lookup table #{lookup_table_name} is incomplete for user_id = #{user_id} - doing things the slow way")
              assets = all.select { |df| df.send("can_#{action}?") }
              programatic_project_filter = !projects.nil?
            end
          else
            assets = all.select { |df| df.send("can_#{action}?") }
          end
          if programatic_project_filter
            assets.select { |a| !(a.projects & projects).empty? }
          else
            assets
          end
        end

        #returns the authorised items from the array of the same class items for a given action and optionally a user. If user is nil, the items authorised for an
        #anonymous user are returned.
        def authorized_partial_asset_collection partial_asset_collection, action, user=User.current_user
          user_id = user.nil? ? 0 : user.id
          if Seek::Config.auth_lookup_enabled && self.lookup_table_consistent?(user_id)
            ids=partial_asset_collection.collect{|asset| asset.id}
            clause = "asset_id IN (#{ids.join(',')})"
            sql =  "SELECT asset_id from #{lookup_table_name} WHERE user_id = #{user_id} AND (#{clause}) AND can_#{action}=#{ActiveRecord::Base.connection.quoted_true}"
            ids = ActiveRecord::Base.connection.select_all(sql).collect{|k| k["asset_id"]}
            authorized_partial_asset_collection = partial_asset_collection.select{|asset| ids.include?(asset.id)}
          else
            authorized_partial_asset_collection = partial_asset_collection.select{|a| a.send("can_#{action}?")}
          end
          authorized_partial_asset_collection
        end
<commit_msg>needs to use the id as a string when checking if it is included in the authorised ids<commit_after>require 'project_compat'
module Acts
  module Authorized
    module PolicyBasedAuthorization
      def self.included klass
        attr_accessor :permission_for
        klass.extend ClassMethods
        klass.extend AuthLookupClassMethods
        klass.class_eval do
          belongs_to :contributor, :polymorphic => true unless method_defined? :contributor
          after_initialize :contributor_or_default_if_new

          #checks a policy exists, and if missing resorts to using a private policy
          after_initialize :policy_or_default_if_new

          include ProjectCompat unless method_defined? :projects

          belongs_to :policy, :required_access_to_owner => :manage, :autosave => true

          before_validation :temporary_policy_while_waiting_for_publishing_approval, :publishing_auth, :unless => "Seek::Config.is_virtualliver"
          after_save :queue_update_auth_table
          after_destroy :remove_from_lookup_table
          before_save :update_timestamp_if_policy_was_saved, :if => "Seek::Config.is_virtualliver"

          def update_timestamp_if_policy_was_saved
            #autosaved belongs_to associations get saved before the parent, so to check if it has changed, see if it has a newer updated_at
            update_timestamp if updated_at && policy.updated_at > updated_at
          end


        end
      end

      module ClassMethods

      end

      module AuthLookupClassMethods

        #returns all the authorised items for a given action and optionally a user and set of projects. If user is nil, the items authorised for an
        #anonymous user are returned. If one or more projects are provided, then only the assets linked to those projects are included.
        def all_authorized_for action, user=User.current_user, projects=nil
          projects=Array(projects) unless projects.nil?
          user_id = user.nil? ? 0 : user.id
          assets = []
          programatic_project_filter = !projects.nil? && (!Seek::Config.auth_lookup_enabled || (self==Assay || self==Study))
          if Seek::Config.auth_lookup_enabled
            if (lookup_table_consistent?(user_id))
              Rails.logger.info("Lookup table #{lookup_table_name} is complete for user_id = #{user_id}")
              assets = lookup_for_action_and_user action, user_id, projects
            else
              Rails.logger.info("Lookup table #{lookup_table_name} is incomplete for user_id = #{user_id} - doing things the slow way")
              assets = all.select { |df| df.send("can_#{action}?") }
              programatic_project_filter = !projects.nil?
            end
          else
            assets = all.select { |df| df.send("can_#{action}?") }
          end
          if programatic_project_filter
            assets.select { |a| !(a.projects & projects).empty? }
          else
            assets
          end
        end

        #returns the authorised items from the array of the same class items for a given action and optionally a user. If user is nil, the items authorised for an
        #anonymous user are returned.
        def authorized_partial_asset_collection partial_asset_collection, action, user=User.current_user
          user_id = user.nil? ? 0 : user.id
          if Seek::Config.auth_lookup_enabled && self.lookup_table_consistent?(user_id)
            ids=partial_asset_collection.collect{|asset| asset.id}
            clause = "asset_id IN (#{ids.join(',')})"
            sql =  "SELECT asset_id from #{lookup_table_name} WHERE user_id = #{user_id} AND (#{clause}) AND can_#{action}=#{ActiveRecord::Base.connection.quoted_true}"
            ids = ActiveRecord::Base.connection.select_all(sql).collect{|k| k["asset_id"]}
            authorized_partial_asset_collection = partial_asset_collection.select{|asset| ids.include?(asset.id.to_s)}
          else
            authorized_partial_asset_collection = partial_asset_collection.select{|a| a.send("can_#{action}?")}
          end
          authorized_partial_asset_collection
        end

{'fraction_non_alphanumeric': 0.07596024052030924, 'fraction_numerical': 0.0004908577739599951, 'mean_word_length': 2.8461538461538463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2994135', 'n_tokens_mistral': 2214, 'n_tokens_neox': 2180, 'n_words': 615}