<commit_before>require 'new_user_mailer'

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :reset_password]
  skip_before_filter :authenticate_user!, only: [:get_verification_question, :answer_verification_question]

  def new_user
    authorize! :edit, current_user.current_provider
    @user = User.new
    @errors = []
  end

  def create_user
    authorize! :edit, current_user.current_provider
    
    @user = User.only_deleted.find_by_username(params[:user][:username])
    @is_user_deleted = @user.try(:deleted_at).present?
    if !@is_user_deleted
      #this user might already be a member of the site, but not of this
      #provider, in which case we ought to just set up the role
      @user = User.find_by_username(params[:user][:username])
      @role = Role.new
      new_password = <PASSWORD>
      new_user = false
      record_valid = false
      User.transaction do
        begin
          new_attrs = user_params
          is_address_blank = check_blank_address
          if is_address_blank
            new_attrs.except!(:user_address_attributes)
          end

          if not @user
            @user = User.new(new_attrs)
            @user.user_address = nil if is_address_blank
            @user.password = User.generate_password
            raw, enc = Devise.token_generator.generate(User, :reset_password_token)
            @user.reset_password_token = enc
            @user.reset_password_sent_at = Time.zone.now.utc
            @user.current_provider = current_provider
            @user.save!
            new_user = true
          end

          @role.user = @user
          @role.provider = current_provider
          @role.level = params[:role][:level]
          @role.save!

          record_valid = true
        rescue => e
          Rails.logger.info(e)
          raise ActiveRecord::Rollback
        end
      end

      if record_valid
        # NewUserMailer doesn't server the purpose by design
        #NewUserMailer.new_user_email(@user, new_password).deliver if new_user

        # send password reset instructions instead
        @user.send_reset_password_instructions  if new_user

        flash.now[:notice] = "%s has been added and the instructions has been emailed" % @user.email
        redirect_to provider_path(current_provider)
      else
        user_errors = @user.valid? ? {} : @user.errors.messages

        role_errors = @role.valid? ? {} : @role.errors.messages
        @errors = user_errors.merge(role_errors)
        render :action => :new_user
      end
    else # deleted user
      flash.now[:alert] = TranslationEngine.translate_text(:user_was_deleted)
      @errors = {}
      render :action => :new_user
    end
  end

  def show
    authorize! :read, @user
  end

  def edit
    authorize! :edit, @user
  end

  def update
    new_attrs = user_params
    is_address_blank = check_blank_address
    if is_address_blank
      prev_address = @user.user_address
      @user.address_id = nil
      new_attrs.except!(:user_address_attributes)
    end
    
    if @user.update_attributes(new_attrs)
      role = Role.find_by(user: @user, provider: current_provider)
      role.update(level: params[:role][:level]) if role
      prev_address.destroy if is_address_blank && prev_address.present?
      edit_verification_questions
      flash.now[:notice] = "User updated."
      redirect_to user_path(@user)
    else
      flash.now[:notice] = "Unable to update user."

      render :edit
    end
  end
<commit_msg>Only update user role when params present [RPII-215]<commit_after>require 'new_user_mailer'

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :reset_password]
  skip_before_filter :authenticate_user!, only: [:get_verification_question, :answer_verification_question]

  def new_user
    authorize! :edit, current_user.current_provider
    @user = User.new
    @errors = []
  end

  def create_user
    authorize! :edit, current_user.current_provider
    
    @user = User.only_deleted.find_by_username(params[:user][:username])
    @is_user_deleted = @user.try(:deleted_at).present?
    if !@is_user_deleted
      #this user might already be a member of the site, but not of this
      #provider, in which case we ought to just set up the role
      @user = User.find_by_username(params[:user][:username])
      @role = Role.new
      new_password = nil
      new_user = false
      record_valid = false
      User.transaction do
        begin
          new_attrs = user_params
          is_address_blank = check_blank_address
          if is_address_blank
            new_attrs.except!(:user_address_attributes)
          end

          if not @user
            @user = User.new(new_attrs)
            @user.user_address = nil if is_address_blank
            @user.password = <PASSWORD>
            raw, enc = Devise.token_generator.generate(User, :reset_password_token)
            @user.reset_password_token = enc
            @user.reset_password_sent_at = Time.zone.now.utc
            @user.current_provider = current_provider
            @user.save!
            new_user = true
          end

          @role.user = @user
          @role.provider = current_provider
          @role.level = params[:role][:level] if params[:role].present? && params[:role][:level].present?
          @role.save!

          record_valid = true
        rescue => e
          Rails.logger.info(e)
          raise ActiveRecord::Rollback
        end
      end

      if record_valid
        # NewUserMailer doesn't server the purpose by design
        #NewUserMailer.new_user_email(@user, new_password).deliver if new_user

        # send password reset instructions instead
        @user.send_reset_password_instructions  if new_user

        flash.now[:notice] = "%s has been added and the instructions has been emailed" % @user.email
        redirect_to provider_path(current_provider)
      else
        user_errors = @user.valid? ? {} : @user.errors.messages

        role_errors = @role.valid? ? {} : @role.errors.messages
        @errors = user_errors.merge(role_errors)
        render :action => :new_user
      end
    else # deleted user
      flash.now[:alert] = TranslationEngine.translate_text(:user_was_deleted)
      @errors = {}
      render :action => :new_user
    end
  end

  def show
    authorize! :read, @user
  end

  def edit
    authorize! :edit, @user
  end

  def update
    new_attrs = user_params
    is_address_blank = check_blank_address
    if is_address_blank
      prev_address = @user.user_address
      @user.address_id = nil
      new_attrs.except!(:user_address_attributes)
    end
    
    if @user.update_attributes(new_attrs)
      role = Role.find_by(user: @user, provider: current_provider)
      if role && params[:role].present? && params[:role][:level].present?
        role.update(level: params[:role][:level]) 
      end

      prev_address.destroy if is_address_blank && prev_address.present?
      edit_verification_questions
      flash.now[:notice] = "User updated."
      redirect_to user_path(@user)
    else
      flash.now[:notice] = "Unable to update user."

      render :edit
    end
  end

{'fraction_non_alphanumeric': 0.0887163848073191, 'fraction_numerical': 0.0004158580537843083, 'mean_word_length': 2.609304652326163, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '2869118', 'n_tokens_mistral': 2145, 'n_tokens_neox': 2114, 'n_words': 495}