<commit_before>class UsersController < ApplicationController
  include InstitutionsHelper

  before_action :set_user, only: [:show, :edit, :update, :destroy]
  before_action :check_dmp_admin_access, only: [:index, :edit_user_roles, :update_user_roles, :destroy]


  # GET /users
  # GET /users.json
  def index
    @users = User.all.page(params[:page]).per(10)
    @institutions = Institution.all

  end

  # GET /users/1
  # GET /users/1.json
  def show
  end

  # GET /users/new
  def new
    @user = User.new
    @institution_list = Institution.all
  end

  # GET /users/1/edit
  def edit

    @user = User.find(params[:id])
    @my_institution = @user.institution

    @institution_list = my_profile_institution_list(@my_institution)

    @roles = @user.roles.map {|r| r.name}.join(' | ')

  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)
    @user.ldap_create = true
    if [@user.valid?, valid_password(user_params[:password], user_params[:password_confirmation])].all?
      begin
        results = Ldap_User::LDAP.fetch(@user.login_id)
        if results['objectclass'].include?('dmpUser')
          @display_text = "A DMPTool account with this login already exists.  Please login."
        else
          @display_text = "Please contact <EMAIL> to add your DMPTool account manually."
        end
      rescue LdapMixin::LdapException => detail
        if detail.message.include? 'does not exist'
          # Add the user, spaces are in place of the first/last name as LDAP requires these.
          User.transaction do
            if !Ldap_User.add(@user.login_id, user_params[:password], ' ', ' ', @user.email)
              @display_text = "There were problems adding this user to the LDAP directory. Please contact <EMAIL>."
            elsif @user.save
              @user.ensure_ldap_authentication(@user.login_id)
              @display_text = "This DMPTool account has been created."
            end
          end
        end
      end
    end

    respond_to do |format|
      if !@user.errors.any? && @user.save
        session[:user_id] = @user.id
        format.html { redirect_to edit_user_path(@user), notice: 'User was successfully created.' }
        format.json { render action: 'show', status: :created, location: @user }
      else
        format.html { render action: 'new' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json

  def update
    password = user_params[:password]
    password_confirmation = user_params[:password_confirmation]

    if !password.empty?
      if valid_password(password, password_confirmation)
        begin
          reset_ldap_password(@user, password)
        rescue
          flash[:notice] = "Problem updating password in LDAP. Please retry."
          render :edit and return
        end
      else
        render :edit and return
      end
    end


    User.transaction do
      @user.institution_id = params[:user].delete(:institution_id)
      if @user.update_attributes(user_params)

        update_notifications(params[:prefs])
        # LDAP will not except for these two fields to be empty.
        user_params[:first_name] = " " if user_params[:first_name].empty?
        user_params[:last_name] = " " if user_params[:last_name].empty?

        update_ldap_if_necessary(@user, user_params)
        flash[:notice] = 'User information updated.'
        redirect_to edit_user_path(@user)
      else
        render 'edit'
      end
    end
  rescue LdapMixin::LdapException
    flash[:notice] = 'Error updating LDAP. Local update also cancelled.'
    render 'edit'
  rescue Exception => e
    puts e.to_s
    flash[:notice] = 'Unknown error updating user information.'
    render 'edit'
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|<commit_msg>Fixing improper nesting and also was rendering the edit screen without redirecting and not loading all variables.<commit_after>class UsersController < ApplicationController
  include InstitutionsHelper

  before_action :set_user, only: [:show, :edit, :update, :destroy]
  before_action :check_dmp_admin_access, only: [:index, :edit_user_roles, :update_user_roles, :destroy]


  # GET /users
  # GET /users.json
  def index
    @users = User.all.page(params[:page]).per(10)
    @institutions = Institution.all

  end

  # GET /users/1
  # GET /users/1.json
  def show
  end

  # GET /users/new
  def new
    @user = User.new
    @institution_list = Institution.all
  end

  # GET /users/1/edit
  def edit

    @user = User.find(params[:id])
    @my_institution = @user.institution

    @institution_list = my_profile_institution_list(@my_institution)

    @roles = @user.roles.map {|r| r.name}.join(' | ')

  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)
    @user.ldap_create = true
    if [@user.valid?, valid_password(user_params[:password], user_params[:password_confirmation])].all?
      begin
        results = Ldap_User::LDAP.fetch(@user.login_id)
        if results['objectclass'].include?('dmpUser')
          @display_text = "A DMPTool account with this login already exists.  Please login."
        else
          @display_text = "Please contact <EMAIL> to add your DMPTool account manually."
        end
      rescue LdapMixin::LdapException => detail
        if detail.message.include? 'does not exist'
          # Add the user, spaces are in place of the first/last name as LDAP requires these.
          User.transaction do
            if !Ldap_User.add(@user.login_id, user_params[:password], ' ', ' ', @user.email)
              @display_text = "There were problems adding this user to the LDAP directory. Please contact <EMAIL>."
            elsif @user.save
              @user.ensure_ldap_authentication(@user.login_id)
              @display_text = "This DMPTool account has been created."
            end
          end
        end
      end
    end

    respond_to do |format|
      if !@user.errors.any? && @user.save
        session[:user_id] = @user.id
        format.html { redirect_to edit_user_path(@user), notice: 'User was successfully created.' }
        format.json { render action: 'show', status: :created, location: @user }
      else
        format.html { render action: 'new' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json

  def update
    password = user_params[:password]
    password_confirmation = user_params[:password_confirmation]

    if !password.empty?
      if valid_password(password, password_confirmation)
        begin
          reset_ldap_password(@user, password)
        rescue
          flash[:notice] = "Problem updating password in LDAP. Please retry."
          redirect_to edit_user_path(@user) and return
        end
      else
        redirect_to edit_user_path(@user) and return
      end
    end


    User.transaction do
      @user.institution_id = params[:user].delete(:institution_id)
      if @user.update_attributes(user_params)

        update_notifications(params[:prefs])
        # LDAP will not except for these two fields to be empty.
        user_params[:first_name] = " " if user_params[:first_name].empty?
        user_params[:last_name] = " " if user_params[:last_name].empty?

        update_ldap_if_necessary(@user, user_params)
        flash[:notice] = 'User information updated.'
        redirect_to edit_user_path(@user)
      else
        redirect_to edit_user_path(@user)
      end
    end
  #rescue LdapMixin::LdapException
  #  flash[:notice] = 'Error updating LDAP. Local update also cancelled.'
  #  redirect_to edit_user_path(@user)
  #rescue Exception => e
  #  puts e.to_s
  #  flash[:notice] = 'Unknown error updating user information.'
  #  redirect_to edit_user_path(@user)
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
{'fraction_non_alphanumeric': 0.10182314275083716, 'fraction_numerical': 0.0022324196949026417, 'mean_word_length': 2.7333333333333334, 'pattern_counts': {'":': 0, '<': 9, '<?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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '910933', 'n_tokens_mistral': 2484, 'n_tokens_neox': 2444, 'n_words': 637}