<commit_before># encoding: UTF-8

require_relative '../../../helpers/carto/uuidhelper'

module Carto
  module Api
    class OverlaysController < ::Api::ApplicationController
      include Carto::UUIDHelper

      ssl_required :index, :show
      before_filter :check_owner_by_vis, only: [ :index ]
      before_filter :check_owner_by_id, only: [ :show ]

      def index
        collection = Carto::Overlay.where(visualization_id: params.fetch('visualization_id')).map { |overlay|
          Carto::Api::OverlayPresenter.new(overlay).to_poro
        }
        render_jsonp(collection)
      rescue KeyError
        head :not_found
      end

      def show
        member = Carto::Overlay.where(id: params.fetch('id')).first
        render_jsonp(member.attributes)
      rescue KeyError
        head :not_found
      end

      protected

      def check_owner_by_id
        head 401 and return if current_user.nil?
        head 401 and return unless is_uuid?(params.fetch('id'))

        member = CartoDB::Overlay::Member.new(id: params.fetch('id')).fetch
        head 401 and return if member.nil?

        vis = Carto::Visualization.where(id: member.visualization_id).first
        head 403 and return if vis.user_id != current_user.id
      end

      def check_owner_by_vis
        head 401 and return if current_user.nil?
        head 401 and return unless is_uuid?(params.fetch('visualization_id'))

        vis = Carto::Visualization.where(id: params.fetch('visualization_id')).first
        head 401 and return if vis.nil?

        head 403 and return if vis.user_id != current_user.id && !vis.has_permission?(
            current_user, 
            Carto::Permission::PERMISSION_READWRITE
          )
      end

    end
  end
end<commit_msg>better naming<commit_after># encoding: UTF-8

require_relative '../../../helpers/carto/uuidhelper'

module Carto
  module Api
    class OverlaysController < ::Api::ApplicationController
      include Carto::UUIDHelper

      ssl_required :index, :show
      before_filter :check_current_user_has_permissions_on_vis, only: [ :index ]
      before_filter :check_current_user_owns_vis, only: [ :show ]

      def index
        collection = Carto::Overlay.where(visualization_id: params.fetch('visualization_id')).map { |overlay|
          Carto::Api::OverlayPresenter.new(overlay).to_poro
        }
        render_jsonp(collection)
      rescue KeyError
        head :not_found
      end

      def show
        member = Carto::Overlay.where(id: params.fetch('id')).first
        render_jsonp(member.attributes)
      rescue KeyError
        head :not_found
      end

      protected

      def check_current_user_owns_vis
        head 401 and return if current_user.nil?
        head 401 and return unless is_uuid?(params.fetch('id'))

        member = CartoDB::Overlay::Member.new(id: params.fetch('id')).fetch
        head 401 and return if member.nil?

        vis = Carto::Visualization.where(id: member.visualization_id).first
        head 403 and return if vis.user_id != current_user.id
      end

      def check_current_user_has_permissions_on_vis
        head 401 and return if current_user.nil?
        head 401 and return unless is_uuid?(params.fetch('visualization_id'))

        vis = Carto::Visualization.where(id: params.fetch('visualization_id')).first
        head 401 and return if vis.nil?

        head 403 and return if vis.user_id != current_user.id && !vis.has_permission?(
            current_user, 
            Carto::Permission::PERMISSION_READWRITE
          )
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.09366236679753225, 'fraction_numerical': 0.014021312394840156, 'mean_word_length': 2.7002074688796682, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '5556073', 'n_tokens_mistral': 1102, 'n_tokens_neox': 1047, 'n_words': 242}