ECP-170 Remove login requirement for Trition. Switch to using predefined passwords stored in the database for district login.

This commit is contained in:
rebuilt 2025-06-11 13:54:56 -07:00
parent 72e38f5ee8
commit 2068758ae4
15 changed files with 146 additions and 16 deletions

View file

@ -10,7 +10,7 @@ class SqmApplicationController < ApplicationController
private
def authenticate_district
authenticate(district_name, "#{district_name}!")
authenticate(@district.username, @district.password)
end
def district_name
@ -35,6 +35,8 @@ class SqmApplicationController < ApplicationController
end
def authenticate(username, password)
return unless @district.login_required
authenticate_or_request_with_http_basic do |u, p|
u == username && p == password
end

View file

@ -147,6 +147,10 @@ class Seeder
EspLoader.load_data(filepath: esp_file)
end
def seed_district_credentials(file:)
CredentialsLoader.load_credentials(file:)
end
private
def value_from(pattern:, row:)
matches = row.headers.select do |header|

View file

@ -2,6 +2,7 @@
class District < ApplicationRecord
has_many :schools
encrypts :password
validates :name, presence: true

View file

@ -0,0 +1,45 @@
require "csv"
class CredentialsLoader
def self.load_credentials(file:)
credentials = []
CSV.parse(file, headers: true) do |row|
values = CredentialRowValues.new(row:)
next unless values.district.present?
credentials << values.district
end
District.import(credentials, batch_size: 100, on_duplicate_key_update: [:username, :password, :login_required])
end
end
class CredentialRowValues
attr_reader :row
def initialize(row:)
@row = row
end
def district
@district ||= begin
name = row["Districts"]&.strip
district = District.find_or_initialize_by(name:)
district.username = username
district.password = password
district.login_required = login_required?
district
end
end
def username
row["Username"]&.strip
end
def password
row["PW"]&.strip
end
def login_required?
row["Login Required"]&.strip == "Y"
end
end

17
app/services/sftp/file.rb Normal file
View file

@ -0,0 +1,17 @@
require 'net/sftp'
require 'uri'
module Sftp
class File
def self.open(filepath:, &block)
sftp_url = ENV['SFTP_URL']
uri = URI.parse(sftp_url)
Net::SFTP.start(uri.host, uri.user, password: uri.password) do |sftp|
sftp.file.open(filepath, 'r', &block)
end
rescue Net::SFTP::StatusException => e
puts "Error opening file: #{e.message}"
nil
end
end
end