<commit_before>class UsersController < ApplicationController

  before_action :require_login, except: [:create, :finish_signup, :finish_signup_update, :new]

  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]
  before_action :require_logout, only: [:new]

  # GET /users
  # GET /users.json
  def index
    @admin_acr = params[:admin_acr]

    @all_users = params[:all_users]
    @all_institutions = params[:all_institutions]

    @q = params[:q]

    @users = User.order(:first_name, :last_name)
    @institutions = Institution.order(full_name: :asc)

    if (!@q.blank? && !@q.nil?)
      @users = @users.search_terms(@q)
    end

    case @all_users
      when "all"
        @users = @users.page(params[:page]).per(9999)
      else
        @users = @users.page(params[:page]).per(10)
    end

    case @all_institutions
      when "all"
        @institutions = @institutions.page(params[:page]).per(9999)
      else
        @institutions = @institutions.page(params[:page]).per(10)
    end

  end

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

  # GET /users/new
  def new
    @user = User.new
    @user.institution_id = params[:institution_id] if params[:institution_id]
    @institution_list = InstitutionsController.institution_select_list
  end


  # GET /users/1/edit
  def edit

    @user = User.find(params[:id])
    @my_institution = @user.institution
    if user_role_in?(:dmp_admin)
      @institution_list = InstitutionsController.institution_select_list
    else
      @institution_list = @my_institution.root.subtree.collect {|i| ["#{'-' * i.depth} #{i.full_name}", i.id] }
    end

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

  end

  # POST /users
  # POST /users.json
  def create
    @institution_list = InstitutionsController.institution_select_list
    @user = User.new(user_params)
    @user.ldap_create = true

    @user.skip_email_uniqueness_validation = true
    if [@user.valid?].all?
      # if they already have a matching email in ldap then make the account the same login_id as that one
      ldap_user_by_email = nil
      begin
        ldap_user_by_email = Ldap_User::LDAP.fetch_by_email(@user.email)
      rescue LdapMixin::LdapException
      end
      @user.login_id = ldap_user_by_email[:uid].first if ldap_user_by_email

      begin
        results = Ldap_User::LDAP.fetch(@user.login_id)

        #fix up DMP account for in LDAP so that it has dmpUser class, if needed
        unless results['objectclass'].include?('dmpUser')
          cl = Ldap_User::LDAP.fetch_attribute(@user.login_id, 'objectclass')
          cl = cl + ['dmpUser']
          Ldap_User::LDAP.replace_attribute(@user.login_id, 'objectclass', cl)
          results = Ldap_User::LDAP.fetch(@user.login_id)
        end
        existing_user = User.find_by_login_id(@user.login_id)
        if existing_user.blank?
          #add user to DMP users table and redirect to login
          @user.save
          redirect_to login_path, notice: "You've been added to the DMP Tool with your existing UC3 username, \"#{@user.login_id}\".  Please log in with your UCS username and password to continue." and return
        else
          #redirct to login, their record already exists.
          redirect_to login_path, notice: "You already have a DMP Tool account. Your username is \"#{@user.login_id}\".  Please log in with your username and password to continue." and return
        end
        @user.skip_email_uniqueness_validation = false
      rescue LdapMixin::LdapException => detail
        @user.skip_email_uniqueness_validation = false
        # add record
        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>."<commit_msg>Tweak to new account form to notify when people are doing idiotic stuff.<commit_after>class UsersController < ApplicationController

  before_action :require_login, except: [:create, :finish_signup, :finish_signup_update, :new]

  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]
  before_action :require_logout, only: [:new]

  # GET /users
  # GET /users.json
  def index
    @admin_acr = params[:admin_acr]

    @all_users = params[:all_users]
    @all_institutions = params[:all_institutions]

    @q = params[:q]

    @users = User.order(:first_name, :last_name)
    @institutions = Institution.order(full_name: :asc)

    if (!@q.blank? && !@q.nil?)
      @users = @users.search_terms(@q)
    end

    case @all_users
      when "all"
        @users = @users.page(params[:page]).per(9999)
      else
        @users = @users.page(params[:page]).per(10)
    end

    case @all_institutions
      when "all"
        @institutions = @institutions.page(params[:page]).per(9999)
      else
        @institutions = @institutions.page(params[:page]).per(10)
    end

  end

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

  # GET /users/new
  def new
    @user = User.new
    @user.institution_id = params[:institution_id] if params[:institution_id]
    @institution_list = InstitutionsController.institution_select_list
  end


  # GET /users/1/edit
  def edit

    @user = User.find(params[:id])
    @my_institution = @user.institution
    if user_role_in?(:dmp_admin)
      @institution_list = InstitutionsController.institution_select_list
    else
      @institution_list = @my_institution.root.subtree.collect {|i| ["#{'-' * i.depth} #{i.full_name}", i.id] }
    end

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

  end

  # POST /users
  # POST /users.json
  def create
    @institution_list = InstitutionsController.institution_select_list
    @user = User.new(user_params)
    @user.ldap_create = true

    existing_user = User.find_by_login_id(@user.login_id)
    unless existing_user.blank?
      redirect_to login_path, notice: "You already have a DMP Tool account. Please log in with your current username and password to continue." and return
    end

    @user.skip_email_uniqueness_validation = true
    if [@user.valid?].all?
      # if they already have a matching email in ldap then make the account the same login_id as that one
      ldap_user_by_email = nil
      begin
        ldap_user_by_email = Ldap_User::LDAP.fetch_by_email(@user.email)
      rescue LdapMixin::LdapException
      end
      @user.login_id = ldap_user_by_email[:uid].first if ldap_user_by_email

      begin
        results = Ldap_User::LDAP.fetch(@user.login_id)

        #fix up DMP account for in LDAP so that it has dmpUser class, if needed
        unless results['objectclass'].include?('dmpUser')
          cl = Ldap_User::LDAP.fetch_attribute(@user.login_id, 'objectclass')
          cl = cl + ['dmpUser']
          Ldap_User::LDAP.replace_attribute(@user.login_id, 'objectclass', cl)
          results = Ldap_User::LDAP.fetch(@user.login_id)
        end
        existing_user = User.find_by_login_id(@user.login_id)
        if existing_user.blank?
          #add user to DMP users table and redirect to login
          @user.save
          redirect_to login_path, notice: "You've been added to the DMP Tool with your existing UC3 username, \"#{@user.login_id}\".  Please log in to continue." and return
        else
          #redirct to login, their record already exists.
          redirect_to login_path, notice: "You already have a DMP Tool account. Your username is \"#{@user.login_id}\".  Please log in with your current password to continue." and return
        end
        @user.skip_email_uniqueness_validation = false
      rescue LdapMixin::LdapException => detail
        @user.skip_email_uniqueness_validation = false
        # add record
        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>."
{'fraction_non_alphanumeric': 0.106515116006562, 'fraction_numerical': 0.0037497070541363956, 'mean_word_length': 3.091562799616491, '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': '7040098', 'n_tokens_mistral': 2781, 'n_tokens_neox': 2733, 'n_words': 690}