<commit_before>class UserSessionsController < ApplicationController

  def login
    if !params[:institution_id].blank?
      session['institution_id'] = params[:institution_id]
    elsif session['institution_id'].blank?
      redirect_to choose_institution_path, flash: {error: 'Please choose your log in institution.'} and return
    end
    @institution = Institution.find(session[:institution_id])
    if !@institution.shib_domain.blank?
      #initiate shibboleth login sequence
      redirect_to OmniAuth::Strategies::Shibboleth.login_path_with_entity(
          Dmptool2::Application.shibboleth_host, @institution.shib_entity_id)
    else
      #just let the original form render
    end
  end

  def create
    redirect_to choose_institution_path if session[:institution_id].blank? and return
    auth = env["omniauth.auth"]
    user = User.from_omniauth(auth, session['institution_id'])
    if auth[:provider] == 'shibboleth' && user.nil?
      redirect_to choose_institution_path, flash: { error: 'Problem with InCommon login.  Your InCommon provider may not be releasing the necessary attributes.'} and return
    end
    if user.nil? || !user.active?
     redirect_to choose_institution_path, flash: { error: "Incorrect username, password or institution" } and return
    end 
    session[:user_id] = user.id
    session[:login_method] = auth[:provider]

    if user.first_name.blank? || user.last_name.blank? || user.prefs.blank?
      redirect_to edit_user_path(user), flash: {error: 'Please complete filling in your profile information.'} and return
    else
      unless session[:return_to].blank?
        r = session[:return_to]
        session.delete(:return_to)
        redirect_to r and return
      end
      redirect_to dashboard_path and return
    end
  end

  def failure
    redirect_to choose_institution_path, flash: { error: "Failure: Incorrect username, password or institution" }
  end

  def destroy
    reset_session
    session[:user_id]
    redirect_to root_path, notice: "Signed out."
  end

  #allow choosing the institution before logging in.
  def institution
     @inst_list = InstitutionsController.institution_select_list
  end
  
  #email username from email address -- get the form, post the submission
  def username_reminder
    if request.post?
      email = params[:email]
      if email.present? && email.match(/^.+\@.+\..+$/)
        users =  User.where(email: email).includes(:authentications).where(active: true).where(authentications: {:provider => 'ldap'})
        if users.length > 0
          uid = users.first.authentications.first.uid
          UsersMailer.username_reminder(uid, email).deliver
          flash[:notice] = "Your username has been emailed to #{email}."
          redirect_to login_path
        else
          flash[:error] = "No user found with email address #{email}."
          redirect_to(:action => 'username_reminder', email: email) and return
        end
      else
        flash[:error] = "You must supply a valid email to obtain a username reminder."
        redirect_to(:action => 'username_reminder', email: email) and return
      end
    end
  end
  
  #reset the password for an email address and mail it -- get is the form, post the submission
  def password_reset
    if request.post?
      email = params[:email]
      if email.present?
        ldap_user = nil
        dmp_user = false
        if email.match(/^.+\@.+\..+$/)
          users =  User.where(email: email).where(active: true)
          begin
            ldap_user = Ldap_User.find_by_email(email)
            dmp_user = ldap_user.objectclass.include?('dmpUser')
          rescue Exception => ex
          end
        else
          users =  User.where(active: true).where(login_id: email)
          begin
            ldap_user = Ldap_User.find_by_id(email)
            dmp_user = ldap_user.objectclass.include?('dmpUser')
          rescue Exception => ex
          end
        end
        if users.length > 0 && !ldap_user.nil? && dmp_user
          user = users.first
          email = user.email
          token = user.ensure_token
          reset_url = complete_password_reset_url(:id => user.id, :token => token, :protocol => 'https')
          UsersMailer.password_reset(user.login_id, email, reset_url).deliver
          
          flash[:notice] = "An email has been sent to #{email} with instructions for resetting your password."
          redirect_to login_path and return
        elsif user.length < 1
          flash[:error] = "No user found with email or username #{email}."
          redirect_to(:action => 'password_reset', email: email) and return
        elsif ldap_user.nil?
          flash[:error] = "You must reset your Shibboleth password through your own institution's password reset mechanism." +
                          "  You do not have an independent login for the DMPTool."
          redirect_to(:action => 'password_reset', email: email) and return<commit_msg>fixing variable name.<commit_after>class UserSessionsController < ApplicationController

  def login
    if !params[:institution_id].blank?
      session['institution_id'] = params[:institution_id]
    elsif session['institution_id'].blank?
      redirect_to choose_institution_path, flash: {error: 'Please choose your log in institution.'} and return
    end
    @institution = Institution.find(session[:institution_id])
    if !@institution.shib_domain.blank?
      #initiate shibboleth login sequence
      redirect_to OmniAuth::Strategies::Shibboleth.login_path_with_entity(
          Dmptool2::Application.shibboleth_host, @institution.shib_entity_id)
    else
      #just let the original form render
    end
  end

  def create
    redirect_to choose_institution_path if session[:institution_id].blank? and return
    auth = env["omniauth.auth"]
    user = User.from_omniauth(auth, session['institution_id'])
    if auth[:provider] == 'shibboleth' && user.nil?
      redirect_to choose_institution_path, flash: { error: 'Problem with InCommon login.  Your InCommon provider may not be releasing the necessary attributes.'} and return
    end
    if user.nil? || !user.active?
     redirect_to choose_institution_path, flash: { error: "Incorrect username, password or institution" } and return
    end 
    session[:user_id] = user.id
    session[:login_method] = auth[:provider]

    if user.first_name.blank? || user.last_name.blank? || user.prefs.blank?
      redirect_to edit_user_path(user), flash: {error: 'Please complete filling in your profile information.'} and return
    else
      unless session[:return_to].blank?
        r = session[:return_to]
        session.delete(:return_to)
        redirect_to r and return
      end
      redirect_to dashboard_path and return
    end
  end

  def failure
    redirect_to choose_institution_path, flash: { error: "Failure: Incorrect username, password or institution" }
  end

  def destroy
    reset_session
    session[:user_id]
    redirect_to root_path, notice: "Signed out."
  end

  #allow choosing the institution before logging in.
  def institution
     @inst_list = InstitutionsController.institution_select_list
  end
  
  #email username from email address -- get the form, post the submission
  def username_reminder
    if request.post?
      email = params[:email]
      if email.present? && email.match(/^.+\@.+\..+$/)
        users =  User.where(email: email).includes(:authentications).where(active: true).where(authentications: {:provider => 'ldap'})
        if users.length > 0
          uid = users.first.authentications.first.uid
          UsersMailer.username_reminder(uid, email).deliver
          flash[:notice] = "Your username has been emailed to #{email}."
          redirect_to login_path
        else
          flash[:error] = "No user found with email address #{email}."
          redirect_to(:action => 'username_reminder', email: email) and return
        end
      else
        flash[:error] = "You must supply a valid email to obtain a username reminder."
        redirect_to(:action => 'username_reminder', email: email) and return
      end
    end
  end
  
  #reset the password for an email address and mail it -- get is the form, post the submission
  def password_reset
    if request.post?
      email = params[:email]
      if email.present?
        ldap_user = nil
        dmp_user = false
        if email.match(/^.+\@.+\..+$/)
          users =  User.where(email: email).where(active: true)
          begin
            ldap_user = Ldap_User.find_by_email(email)
            dmp_user = ldap_user.objectclass.include?('dmpUser')
          rescue Exception => ex
          end
        else
          users =  User.where(active: true).where(login_id: email)
          begin
            ldap_user = Ldap_User.find_by_id(email)
            dmp_user = ldap_user.objectclass.include?('dmpUser')
          rescue Exception => ex
          end
        end
        if users.length > 0 && !ldap_user.nil? && dmp_user
          user = users.first
          email = user.email
          token = user.ensure_token
          reset_url = complete_password_reset_url(:id => user.id, :token => token, :protocol => 'https')
          UsersMailer.password_reset(user.login_id, email, reset_url).deliver
          
          flash[:notice] = "An email has been sent to #{email} with instructions for resetting your password."
          redirect_to login_path and return
        elsif users.length < 1
          flash[:error] = "No user found with email or username #{email}."
          redirect_to(:action => 'password_reset', email: email) and return
        elsif ldap_user.nil?
          flash[:error] = "You must reset your Shibboleth password through your own institution's password reset mechanism." +
                          "  You do not have an independent login for the DMPTool."
          redirect_to(:action => 'password_reset', email: email) and return
{'fraction_non_alphanumeric': 0.0944921677614957, 'fraction_numerical': 0.000808489135927236, 'mean_word_length': 2.8120184899845917, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '516875', 'n_tokens_mistral': 2822, 'n_tokens_neox': 2787, 'n_words': 801}