<commit_before>#
#  Views: ("*" - login required, "R" - root required))
#     signup
#     welcome
#
#     verify
#     reverify
#     send_verify
#
#     login
#     logout_user
#     email_new_password
#
#   * prefs
#   * profile
#   * remove_image
#
#     no_feature_email
#     no_question_email
#     no_commercial_email
#     no_comment_email
#
#  Admin Tools:
#   R delete
#
#  Test Views:
#     test_verify
#
#  Helpers:
#    hide_params(obj, out, prefix)
#    login_check(id)
#
################################################################################

class AccountController < ApplicationController
  before_filter :login_required, :except => [
    :signup,
    :welcome,
    :verify,
    :reverify,
    :send_verify,
    :login,
    :logout_user,
    :email_new_password,
    :no_feature_email,
    :no_question_email,
    :no_commercial_email,
    :no_comment_email
  ]

  def login
    case request.method
      when :get
        @login = ""
        @remember = true
      when :post
        user = User.authenticate(params['user_login'], params['user_password'])
        @remember = params['user'] && params['user']['remember_me'] == "1"
        if set_session_user(user)
          logger.warn("%s, %s, %s" % [user.login, params['user_login'], params['user_password']])
          flash_notice :login_success.t
          user.last_login = Time.now
          user.save
          if @remember
            set_autologin_cookie(user)
          else
            clear_autologin_cookie
          end
          flash[:params] = params
          redirect_back_or_default(:action => "welcome")
        else
          @login = params['user_login']
          flash_error :login_failed.t
        end
    end
    @hiddens = []
    if flash[:params]
      flash[:params].each do |obj,val|
        hide_params(val, @hiddens, obj.to_s) if val.is_a?(Hash)
      end
    end
  end

  # Parameters from page that redirected to login are preserved in
  # flash[:params].  I store them in the login page as hidden fields.
  # When user logs in, it puts these params back into flash[:params]
  # and redirects to the original page.  The login_required "before"
  # filter will then merge them back into params[] automatically.
  # This method flattens the params structure into a simple array.
  def hide_params(obj, out, prefix)
    if !obj.is_a?(Hash)
      id    = prefix.gsub(/\[/, "_").gsub(/\]/, "")
      name  = prefix
      value = obj.to_s
      out << [id, name, value]
    else
      obj.each do |k,v|
        hide_params(v, out, "#{prefix}[#{k.to_s}]")
      end
    end
  end

  def signup
    case request.method
      when :get
        @new_user = User.new
      when :post
        theme = params['new_user']['theme']
        login = params['new_user']['login']
        valid_themes = CSS + ["NULL"]
        if valid_themes.member?(theme) and (login != 'test_denied')
          @new_user = User.new(params['new_user'])
          @new_user.created = Time.now
          @new_user.last_login = @new_user.created
          @new_user.change_rows(5)
          @new_user.change_columns(3)
          @new_user.mailing_address = ''
          @new_user.notes = ''
          if @new_user.save
            user = User.authenticate(@new_user.login, params['new_user']['password'])
            set_session_user(user)
            flash_notice :signup_success.t
            AccountMailer.deliver_verify(@new_user)
            redirect_back_or_default(:action => "welcome")
          else
            flash_object_errors(@new_user)
          end
        else
          AccountMailer.deliver_denied(params['new_user'])
          redirect_back_or_default(:action => "welcome")
        end
    end
  end

  def email_new_password
    case request.method
      when :get
        @new_user = User.new
      when :post
        @login = params['new_user']['login']
        @new_user = User.find(:first, :conditions => ["login = ?", @login])
        if @new_user.nil?
          flash_error :email_new_password_failed.t(:user => @login)
        else
          password = <PASSWORD>(10)
          @new_user.change_password(password)
          if @new_user.save
            user = User.authenticate(@new_user.login, params['new_user']['password'])
            set_session_user(user)
            flash_notice :email_new_password_success.t
            AccountMailer.deliver_new_password(@new_user, password)
            @hiddens = []
            render(:action => "login")
          else
            flash_object_errors(@new_user)
          end
        end
    end
  end

  def prefs
    if @user
      @licenses = License.current_names_and_ids(@user.license)
      case request.method
        when :get
          @user.reload # Make sure we have the latest version of the user

        when :post
          error = false

          @user.login = params['user']['login']
          @user.change_email(params['user']['email'])
          @user.html_email = params['user']['html_email']
          @user.feature_email = params['user']['feature_email']
          @user.comment_email = params['user']['comment_email']
          @user.commercial_email = params['user']['commercial_email']
          @user.question_email = params['user']['question_email']
          @user.change_theme(params['user']['theme'])
          @user.change_rows(params['user']['rows'])
          @user.change_columns(params['user']['columns'])
          @user.alternate_rows = params['user']['alternate_rows']
          @user.alternate_columns = params['user']['alternate_columns']
          @user.vertical_layout = params['user']['vertical_layout']
          @user.license_id = params['user']['license_id'].to_i

          password = params['user']['password']
          if password
            if password == params['user']['password_confirmation']
              @user.change_password(password)
            else
              error = true
              flash_error :prefs_password_no_match.t
            end
          end

          if error
          elsif !@user.save
            flash_object_errors(@user)
          else
            flash_notice :prefs_success.t
            redirect_back_or_default(:action => "welcome")
          end
      end
    else
      store_location
      access_denied<commit_msg>Oops, account/prefs wasn't saving locale.<commit_after>#
#  Views: ("*" - login required, "R" - root required))
#     signup
#     welcome
#
#     verify
#     reverify
#     send_verify
#
#     login
#     logout_user
#     email_new_password
#
#   * prefs
#   * profile
#   * remove_image
#
#     no_feature_email
#     no_question_email
#     no_commercial_email
#     no_comment_email
#
#  Admin Tools:
#   R delete
#
#  Test Views:
#     test_verify
#
#  Helpers:
#    hide_params(obj, out, prefix)
#    login_check(id)
#
################################################################################

class AccountController < ApplicationController
  before_filter :login_required, :except => [
    :signup,
    :welcome,
    :verify,
    :reverify,
    :send_verify,
    :login,
    :logout_user,
    :email_new_password,
    :no_feature_email,
    :no_question_email,
    :no_commercial_email,
    :no_comment_email
  ]

  def login
    case request.method
      when :get
        @login = ""
        @remember = true
      when :post
        user = User.authenticate(params['user_login'], params['user_password'])
        @remember = params['user'] && params['user']['remember_me'] == "1"
        if set_session_user(user)
          logger.warn("%s, %s, %s" % [user.login, params['user_login'], params['user_password']])
          flash_notice :login_success.t
          user.last_login = Time.now
          user.save
          if @remember
            set_autologin_cookie(user)
          else
            clear_autologin_cookie
          end
          flash[:params] = params
          redirect_back_or_default(:action => "welcome")
        else
          @login = params['user_login']
          flash_error :login_failed.t
        end
    end
    @hiddens = []
    if flash[:params]
      flash[:params].each do |obj,val|
        hide_params(val, @hiddens, obj.to_s) if val.is_a?(Hash)
      end
    end
  end

  # Parameters from page that redirected to login are preserved in
  # flash[:params].  I store them in the login page as hidden fields.
  # When user logs in, it puts these params back into flash[:params]
  # and redirects to the original page.  The login_required "before"
  # filter will then merge them back into params[] automatically.
  # This method flattens the params structure into a simple array.
  def hide_params(obj, out, prefix)
    if !obj.is_a?(Hash)
      id    = prefix.gsub(/\[/, "_").gsub(/\]/, "")
      name  = prefix
      value = obj.to_s
      out << [id, name, value]
    else
      obj.each do |k,v|
        hide_params(v, out, "#{prefix}[#{k.to_s}]")
      end
    end
  end

  def signup
    case request.method
      when :get
        @new_user = User.new
      when :post
        theme = params['new_user']['theme']
        login = params['new_user']['login']
        valid_themes = CSS + ["NULL"]
        if valid_themes.member?(theme) and (login != 'test_denied')
          @new_user = User.new(params['new_user'])
          @new_user.created = Time.now
          @new_user.last_login = @new_user.created
          @new_user.change_rows(5)
          @new_user.change_columns(3)
          @new_user.mailing_address = ''
          @new_user.notes = ''
          if @new_user.save
            user = User.authenticate(@new_user.login, params['new_user']['password'])
            set_session_user(user)
            flash_notice :signup_success.t
            AccountMailer.deliver_verify(@new_user)
            redirect_back_or_default(:action => "welcome")
          else
            flash_object_errors(@new_user)
          end
        else
          AccountMailer.deliver_denied(params['new_user'])
          redirect_back_or_default(:action => "welcome")
        end
    end
  end

  def email_new_password
    case request.method
      when :get
        @new_user = User.new
      when :post
        @login = params['new_user']['login']
        @new_user = User.find(:first, :conditions => ["login = ?", @login])
        if @new_user.nil?
          flash_error :email_new_password_failed.t(:user => @login)
        else
          password = <PASSWORD>)
          @new_user.change_password(password)
          if @new_user.save
            user = User.authenticate(@new_user.login, params['new_user']['password'])
            set_session_user(user)
            flash_notice :email_new_password_success.t
            AccountMailer.deliver_new_password(@new_user, password)
            @hiddens = []
            render(:action => "login")
          else
            flash_object_errors(@new_user)
          end
        end
    end
  end

  def prefs
    if @user
      @licenses = License.current_names_and_ids(@user.license)
      case request.method
        when :get
          @user.reload # Make sure we have the latest version of the user

        when :post
          error = false

          @user.login = params['user']['login']
          @user.change_email(params['user']['email'])
          @user.html_email = params['user']['html_email']
          @user.feature_email = params['user']['feature_email']
          @user.comment_email = params['user']['comment_email']
          @user.commercial_email = params['user']['commercial_email']
          @user.question_email = params['user']['question_email']
          @user.change_theme(params['user']['theme'])
          @user.change_rows(params['user']['rows'])
          @user.change_columns(params['user']['columns'])
          @user.alternate_rows = params['user']['alternate_rows']
          @user.alternate_columns = params['user']['alternate_columns']
          @user.vertical_layout = params['user']['vertical_layout']
          @user.license_id = params['user']['license_id'].to_i
          @user.locale = params['user']['locale'].to_s

          password = params['user']['password']
          if password
            if password == params['user']['password_confirmation']
              @user.change_password(password)
            else
              error = true
              flash_error :prefs_password_no_match.t
            end
          end

          if error
          elsif !@user.save
            flash_object_errors(@user)
          else
            flash_notice :prefs_success.t
            redirect_back_or_default(:action => "welcome")
          end
      end
    else
      store_location
      access_denied
{'fraction_non_alphanumeric': 0.1287277918781726, 'fraction_numerical': 0.0006345177664974619, 'mean_word_length': 2.2716658017644007, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, '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': '1065484', 'n_tokens_mistral': 3761, 'n_tokens_neox': 3710, 'n_words': 761}