<commit_before># frozen_string_literal: true

require 'ditty/controllers/application'
require 'ditty/services/email'
require 'securerandom'

module Ditty
  class Main < Application
    set track_actions: true

    def find_template(views, name, engine, &block)
      super(views, name, engine, &block) # Root
      super(::Ditty::App.view_folder, name, engine, &block) # Basic Plugin
    end

    # Home Page
    get '/' do
      authenticate!
      haml :index, locals: { title: 'Home' }
    end

    # OmniAuth Identity Stuff
    # Log in Page
    get '/auth/identity' do
      # Redirect to the registration page if there's no SA user
      sa = Role.find_or_create(name: 'super_admin')
      if User.where(roles: sa).count == 0
        flash[:info] = 'Please register the super admin user.'
        redirect "#{settings.map_path}/auth/identity/register"
      end
      haml :'identity/login', locals: { title: 'Log In' }
    end

    get '/auth/identity/forgot' do
      haml :'identity/forgot', locals: { title: 'Forgot your password?' }
    end

    post '/auth/identity/forgot' do
      email = params['email']
      identity = Identity[username: email]
      if identity
        # Update record
        token = SecureRandom.hex(16)
        identity.update(reset_token: token, reset_requested: Time.now)
        # Send Email
        reset_url = "#{request.base_url}#{settings.map_path}/auth/identity/reset?token=#{token}"
        Ditty::Services::Email.deliver(
          :forgot_password,
          email,
          locals: { identity: identity, reset_url: reset_url, request: request }
        )
      end
      flash[:info] = 'An email was sent to the email provided with instructions on how to reset your password'
      redirect '/auth/identity'
    end

    get '/auth/identity/reset' do
      identity = Identity[reset_token: params['token']]<commit_msg>refac: Move check for super admin to before filter<commit_after># frozen_string_literal: true

require 'ditty/controllers/application'
require 'ditty/services/email'
require 'securerandom'

module Ditty
  class Main < Application
    set track_actions: true

    def find_template(views, name, engine, &block)
      super(views, name, engine, &block) # Root
      super(::Ditty::App.view_folder, name, engine, &block) # Basic Plugin
    end

    before /.*/ do
      # Redirect to the registration page if there's no SA user
      sa = Role.find_or_create(name: 'super_admin')
      if User.where(roles: sa).count == 0
        flash[:info] = 'Please register the super admin user.'
        redirect "#{settings.map_path}/auth/identity/register"
      end
    end

    # Home Page
    get '/' do
      authenticate!
      haml :index, locals: { title: 'Home' }
    end

    # OmniAuth Identity Stuff
    # Log in Page
    get '/auth/identity' do
      haml :'identity/login', locals: { title: 'Log In' }
    end

    get '/auth/identity/forgot' do
      haml :'identity/forgot', locals: { title: 'Forgot your password?' }
    end

    post '/auth/identity/forgot' do
      email = params['email']
      identity = Identity[username: email]
      if identity
        # Update record
        token = SecureRandom.hex(16)
        identity.update(reset_token: token, reset_requested: Time.now)
        # Send Email
        reset_url = "#{request.base_url}#{settings.map_path}/auth/identity/reset?token=#{token}"
        Ditty::Services::Email.deliver(
          :forgot_password,
          email,
          locals: { identity: identity, reset_url: reset_url, request: request }
        )
      end
      flash[:info] = 'An email was sent to the email provided with instructions on how to reset your password'
      redirect '/auth/identity'
    end

    get '/auth/identity/reset' do
      identity = Identity[reset_token: params['token']]
{'fraction_non_alphanumeric': 0.1107020773073889, 'fraction_numerical': 0.0015777018143570865, 'mean_word_length': 2.733071638861629, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '5082951', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1117, 'n_words': 326}