<commit_before># coding: utf-8
require_dependency 'google_plus_api'
require_dependency 'google_plus_config'
require_dependency 'carto/controller_helper'

class Admin::OrganizationUsersController < Admin::AdminController
  include OrganizationUsersHelper

  # Organization actions
  ssl_required  :new, :create, :edit, :update, :destroy
  # Data of single users
  ssl_required  :profile, :account, :oauth, :api_key, :regenerate_api_key

  before_filter :get_config
  before_filter :login_required, :check_permissions
  before_filter :get_user, only: [:edit, :update, :destroy, :regenerate_api_key]
  before_filter :initialize_google_plus_config, only: [:edit, :update]

  layout 'application'

  def new
    @user = ::User.new
    @user.quota_in_bytes = (current_user.organization.unassigned_quota < 100.megabytes ? current_user.organization.unassigned_quota : 100.megabytes)

    @user.soft_geocoding_limit = current_user.soft_geocoding_limit
    @user.soft_here_isolines_limit = current_user.soft_here_isolines_limit
    @user.soft_twitter_datasource_limit = current_user.soft_twitter_datasource_limit

    respond_to do |format|
      format.html { render 'new' }
    end
  end

  def edit
    set_flash_flags
    respond_to do |format|
      format.html { render 'edit' }
    end
  end

  def create
    @user = ::User.new

    # Validation is done on params to allow checking the change of the value.
    # The error is deferred to display values in the form in the error scenario.
    validation_failure = !soft_limits_validation(@user, params[:user], current_user.organization.owner)

    @user.set_fields(
      params[:user],
      [
        :username, :email, :password, :quota_in_bytes, :password_confirmation,
        :twitter_datasource_enabled, :soft_geocoding_limit, :soft_here_isolines_limit])
    @user.organization = current_user.organization
    @user.username = @user.username
    current_user.copy_account_features(@user)

    raise Carto::UnprocesableEntityError.new("Soft limits validation error") if validation_failure

    @user.save(raise_on_failure: true)
    @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
    redirect_to CartoDB.url(self, 'organization', {}, current_user), flash: { success: "New user created successfully" }
  rescue Carto::UnprocesableEntityError => e
    CartoDB.report_exception(e, "Validation error", request: request, user: current_user)
    set_flash_flags
    flash.now[:error] = e.user_message
    render 'new', status: 422
  rescue CartoDB::CentralCommunicationFailure => e
    CartoDB.report_exception(e)
    begin
      @user.destroy<commit_msg>Removed redundant assignment<commit_after># coding: utf-8
require_dependency 'google_plus_api'
require_dependency 'google_plus_config'
require_dependency 'carto/controller_helper'

class Admin::OrganizationUsersController < Admin::AdminController
  include OrganizationUsersHelper

  # Organization actions
  ssl_required  :new, :create, :edit, :update, :destroy
  # Data of single users
  ssl_required  :profile, :account, :oauth, :api_key, :regenerate_api_key

  before_filter :get_config
  before_filter :login_required, :check_permissions
  before_filter :get_user, only: [:edit, :update, :destroy, :regenerate_api_key]
  before_filter :initialize_google_plus_config, only: [:edit, :update]

  layout 'application'

  def new
    @user = ::User.new
    @user.quota_in_bytes = (current_user.organization.unassigned_quota < 100.megabytes ? current_user.organization.unassigned_quota : 100.megabytes)

    @user.soft_geocoding_limit = current_user.soft_geocoding_limit
    @user.soft_here_isolines_limit = current_user.soft_here_isolines_limit
    @user.soft_twitter_datasource_limit = current_user.soft_twitter_datasource_limit

    respond_to do |format|
      format.html { render 'new' }
    end
  end

  def edit
    set_flash_flags
    respond_to do |format|
      format.html { render 'edit' }
    end
  end

  def create
    @user = ::User.new

    # Validation is done on params to allow checking the change of the value.
    # The error is deferred to display values in the form in the error scenario.
    validation_failure = !soft_limits_validation(@user, params[:user], current_user.organization.owner)

    @user.set_fields(
      params[:user],
      [
        :username, :email, :password, :quota_in_bytes, :password_confirmation,
        :twitter_datasource_enabled, :soft_geocoding_limit, :soft_here_isolines_limit])
    @user.organization = current_user.organization
    current_user.copy_account_features(@user)

    raise Carto::UnprocesableEntityError.new("Soft limits validation error") if validation_failure

    @user.save(raise_on_failure: true)
    @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
    redirect_to CartoDB.url(self, 'organization', {}, current_user), flash: { success: "New user created successfully" }
  rescue Carto::UnprocesableEntityError => e
    CartoDB.report_exception(e, "Validation error", request: request, user: current_user)
    set_flash_flags
    flash.now[:error] = e.user_message
    render 'new', status: 422
  rescue CartoDB::CentralCommunicationFailure => e
    CartoDB.report_exception(e)
    begin
      @user.destroy
{'fraction_non_alphanumeric': 0.0915182507494269, 'fraction_numerical': 0.0035267148651031564, 'mean_word_length': 4.945492662473795, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6715939', 'n_tokens_mistral': 1841, 'n_tokens_neox': 1742, 'n_words': 369}