<commit_before># encoding: utf-8

require_relative './user_presenter'

module Carto
  module Api
    class OrganizationUsersController < ::Api::ApplicationController
      include OrganizationsHelper

      ssl_required

      before_filter :load_organization

      def create
        render_jsonp({}, 401) && return unless current_viewer_is_owner?

        user = ::User.new(create_params)
        user.organization_id = @organization.id
        current_viewer.copy_account_features(user)

        render_jsonp(user.errors.full_messages, 410) && return unless user.save

        user.create_in_central
        common_data_url = CartoDB::Visualization::CommonDataService.build_url(self)
        ::Resque.enqueue(::Resque::UserJobs::CommonData::LoadCommonData, user.id, common_data_url)

        user.notify_new_organization_user
        user.organization.notify_if_seat_limit_reached
        render_jsonp Carto::Api::UserPresenter.new(user).to_poro, 200
      rescue CartoDB::CentralCommunicationFailure => e
        Rollbar.report_exception(e)
        begin
          @user.destroy
        rescue => ee
          Rollbar.report_exception(ee)
        end

        render_jsonp 'Central comunication failure', 500
      end

      def destroy
        render_jsonp({}, 401) && return unless current_viewer_is_owner?

        user = ::User.where(delete_params.symbolize_keys).first

        render_jsonp("No user with #{delete_params}", 404) && return if user.nil?
        render_jsonp("Can't delete user. #{'Has shared entities' if user.has_shared_entities?}", 410) unless user.can_delete

        user.delete_in_central
        user.destroy

        render_jsonp 'User deleted', 200
      rescue CartoDB::CentralCommunicationFailure => e
        Rollbar.report_exception(e)
        if e.user_message =~ /No user found with username/
          user.destroy
          render_jsonp 'User deleted', 200
        else
          render_jsonp "User couldn't be deleted", 500
        end
      end

      private

      # TODO: Use native strong params when in Rails 4+
      def create_params
        permit(:email, :username, :password, :password_confirmation, :quota_in_bytes)
      end

      # TODO: Use native strong params when in Rails 4+
      def delete_params
        permit(:email, :username)
      end
    end
  end
end
<commit_msg>Adds update functionality to org users controller<commit_after># encoding: utf-8

require_relative './user_presenter'

module Carto
  module Api
    class OrganizationUsersController < ::Api::ApplicationController
      include OrganizationsHelper

      ssl_required

      before_filter :load_organization

      UPDATE_PARAMS_MAP = { new_email: 'email', new_username: 'username' }

      def create
        render_jsonp({}, 401) && return unless current_viewer_is_owner?

        user = ::User.new(create_params)
        user.organization_id = @organization.id
        current_viewer.copy_account_features(user)

        render_jsonp(user.errors.full_messages, 410) && return unless user.save

        user.create_in_central
        common_data_url = CartoDB::Visualization::CommonDataService.build_url(self)
        ::Resque.enqueue(::Resque::UserJobs::CommonData::LoadCommonData, user.id, common_data_url)

        user.notify_new_organization_user
        user.organization.notify_if_seat_limit_reached
        render_jsonp Carto::Api::UserPresenter.new(user, current_viewer: current_viewer).to_poro, 200
      rescue CartoDB::CentralCommunicationFailure => e
        Rollbar.report_exception(e)
        begin
          @user.destroy
        rescue => ee
          Rollbar.report_exception(ee)
        end

        render_jsonp 'Central comunication failure', 500
      end

      def destroy
        render_jsonp({}, 401) && return unless current_viewer_is_owner?

        user = ::User.where(delete_params).first

        render_jsonp("No user with #{delete_params}", 404) && return if user.nil?
        render_jsonp("Can't delete user. #{'Has shared entities' if user.has_shared_entities?}", 410) unless user.can_delete

        user.delete_in_central
        user.destroy

        render_jsonp 'User deleted', 200
      rescue CartoDB::CentralCommunicationFailure => e
        Rollbar.report_exception(e)
        if e.user_message =~ /No user found with username/
          user.destroy
          render_jsonp 'User deleted', 200
        else
          render_jsonp "User couldn't be deleted", 500
        end
      end

      def update
        render_jsonp({}, 401) && return unless current_viewer_is_owner?
        render_jsonp('No update params provided', 200) && return if update_params.empty?

        user = ::User.where(delete_params).first
        render_jsonp("No user with #{delete_params}", 404) && return if user.nil?

        user.update(Hash[update_params.map { |k, v| [UPDATE_PARAMS_MAP[k].to_sym || k, v] }])

        render_jsonp(user.errors.full_messages, 410) && return unless user.save

        user.update_in_central

        render_jsonp Carto::Api::UserPresenter.new(user, current_viewer: current_viewer).to_poro, 200
      rescue CartoDB::CentralCommunicationFailure => e
        Rollbar.report_exception(e)

        render_jsonp 'Central comunication failure', 500
      end

      private

      # TODO: Use native strong params when in Rails 4+
      def create_params
        permit(:email, :username, :password, :password_confirmation, :quota_in_bytes, :soft_geocoding_limit)
      end

      # TODO: Use native strong params when in Rails 4+
      def delete_params
        permit(:email, :username)
      end

      # TODO: Use native strong params when in Rails 4+
      def update_params
        permit(:new_email, :new_username, :password, :password_confirmation, :quota_in_bytes, :soft_geocoding_limit)
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.08463788061242043, 'fraction_numerical': 0.014622398073284018, 'mean_word_length': 2.9336941813261164, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '934231', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1651, 'n_words': 394}