From 6254e3bad89cc0b45fa8d73944349c0d5c227b42 Mon Sep 17 00:00:00 2001 From: Max Kadel Date: Wed, 3 Apr 2024 16:21:47 -0400 Subject: [PATCH] Remove Ldap dependency Closes #3962 --- Gemfile | 1 - Gemfile.lock | 2 -- app/controllers/patron_controller.rb | 1 - app/models/ldap.rb | 38 ---------------------- spec/controllers/patron_controller_spec.rb | 23 +------------ spec/models/ldap_spec.rb | 36 -------------------- 6 files changed, 1 insertion(+), 100 deletions(-) delete mode 100644 app/models/ldap.rb delete mode 100644 spec/models/ldap_spec.rb diff --git a/Gemfile b/Gemfile index d44e885b..fdc69a59 100644 --- a/Gemfile +++ b/Gemfile @@ -38,7 +38,6 @@ gem 'marc', '~> 1.0' gem 'marc_cleanup', github: "pulibrary/marc_cleanup", tag: 'v0.9.1' gem 'multi_json' gem 'net-imap', require: false -gem 'net-ldap' gem 'net-pop', require: false gem 'net-sftp' gem 'net-smtp', require: false diff --git a/Gemfile.lock b/Gemfile.lock index 2d5b65e8..84c5845e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -368,7 +368,6 @@ GEM net-imap (0.3.7) date net-protocol - net-ldap (0.17.1) net-pop (0.1.2) net-protocol net-protocol (0.2.2) @@ -684,7 +683,6 @@ DEPENDENCIES marc_cleanup! multi_json net-imap - net-ldap net-pop net-sftp net-smtp diff --git a/app/controllers/patron_controller.rb b/app/controllers/patron_controller.rb index 1dcf8d54..f2d6d77d 100644 --- a/app/controllers/patron_controller.rb +++ b/app/controllers/patron_controller.rb @@ -10,7 +10,6 @@ def patron_info patron_access = CampusAccess.where(uid: patron_id).first || CampusAccess.new(uid: patron_id, category: "none") info[:campus_authorized] = patron_access.access? info[:campus_authorized_category] = patron_access.category - info[:ldap] = Ldap.find_by_netid(patron_id) if params[:ldap].present? && sanitize(params[:ldap]) == "true" respond_to do |wants| wants.json { render json: MultiJson.dump(info) } end diff --git a/app/models/ldap.rb b/app/models/ldap.rb deleted file mode 100644 index cf52adff..00000000 --- a/app/models/ldap.rb +++ /dev/null @@ -1,38 +0,0 @@ -require 'net/ldap' - -class Ldap - class << self - def find_by_netid(net_id, ldap_connection: default_connection) - filter = Net::LDAP::Filter.eq("uid", net_id) - result = ldap_connection.search(filter:).first - return {} if result.blank? - attributes(result) - end - - private - - def attributes(result) - { - netid: result[:uid]&.first, - department: result[:purescollege]&.first || result[:ou]&.first, - address: result[:puinterofficeaddress]&.first, - telephone: result[:telephonenumber]&.first, - givenname: result[:givenname]&.first, - surname: result[:sn]&.first, - email: result[:mail]&.first, - status: result[:edupersonprimaryaffiliation]&.first, - pustatus: result[:pustatus]&.first, - universityid: result[:universityid]&.first, - title: result[:title]&.first - } - end - - def default_connection - @default_connection ||= Net::LDAP.new host: "ldap.princeton.edu", base: "o=Princeton University,c=US", port: 636, - encryption: { - method: :simple_tls, - tls_options: OpenSSL::SSL::SSLContext::DEFAULT_PARAMS - } - end - end -end diff --git a/spec/controllers/patron_controller_spec.rb b/spec/controllers/patron_controller_spec.rb index 3481699f..bc36c66f 100644 --- a/spec/controllers/patron_controller_spec.rb +++ b/spec/controllers/patron_controller_spec.rb @@ -79,29 +79,8 @@ expect(JSON.parse(response.body)["patron_group"]).to eq "staff" end - it "allows authenticated users to access patron info and ldap data when desired" do - expect(Ldap).to receive(:find_by_netid).with(patron_identifier).and_return(ldap_data: "is here") - get :patron_info, params: { patron_id: patron_identifier, ldap: true, format: :json } - expect(response).to have_http_status(200) - expect(JSON.parse(response.body)).to eq( - "netid" => "bbird", - "first_name" => "Big", - "last_name" => "Bird", - "barcode" => "00000000000000", - "university_id" => "100000000", - "patron_id" => "100000000", - "patron_group" => "staff", - "patron_group_desc" => "P Faculty & Professional", - "campus_authorized" => false, - "campus_authorized_category" => "none", - "ldap" => { "ldap_data" => "is here" }, - "active_email" => "bbird@SCRUBBED_princeton.edu" - ) - end - it "allows authenticated users to access just patron info when desired" do - expect(Ldap).not_to receive(:find_by_netid) - get :patron_info, params: { patron_id: patron_identifier, ldap: 'other', format: :json } + get :patron_info, params: { patron_id: patron_identifier, format: :json } expect(response).to have_http_status(200) expect(JSON.parse(response.body)).to eq( "netid" => "bbird", diff --git a/spec/models/ldap_spec.rb b/spec/models/ldap_spec.rb deleted file mode 100644 index c003695b..00000000 --- a/spec/models/ldap_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -require 'rails_helper' - -RSpec.describe Ldap, type: :model do - subject(:data) { described_class.find_by_netid('abc123', ldap_connection:) } - let(:ldap_connection) { Net::LDAP.new } - let(:valid_ldap_response) do - [{ dn: ["uid=abc123,o=princeton university,c=us"], telephonenumber: ["111-222-3333"], edupersonaffiliation: ["member", "staff", "employee"], puhomedepartmentnumber: ["99999"], sn: ["Smith"], - objectclass: ["inetorgperson", "organizationalPerson", "person", "top", "puPerson", "nsMessagingServerUser", "inetUser", "ipUser", "inetMailUser", "inetLocalMailRecipient", "nManagedPerson", "userPresenceProfile", "oblixorgperson", "oblixPersonPwdPolicy", "eduPerson", "posixAccount"], - givenname: ["Sally"], uid: ["abc123"], displayname: ["Sally Smith"], ou: ["Library Information Technology"], pudisplayname: ["Smith, Sally"], edupersonprincipalname: ["abc123@princeton.edu"], pustatus: ["stf"], edupersonprimaryaffiliation: ["staff"], cn: ["Sally Smith"], universityid: ["999999999"], - loginshell: ["/bin/no login"], mail: ["sally.smith@princeton.edu"], edupersonentitlement: ["urn:mace:dir:entitlement:common-lib-terms"], puinterofficeaddress: ["Firestone Library$Library Information Technology"], title: ["Staff, Library - Information Technology."], street: ["B-1H-1 Firestone Library"] }] - end - - # rubocop:disable RSpec/ExampleLength - describe '#find_by_netid' do - it 'parses a valid ldap response' do - allow(ldap_connection).to receive(:search).with(filter: Net::LDAP::Filter.eq("uid", 'abc123')).and_return(valid_ldap_response) - expect(data[:netid]).to eq('abc123') - expect(data[:department]).to eq('Library Information Technology') - expect(data[:address]).to eq('Firestone Library$Library Information Technology') - expect(data[:telephone]).to eq('111-222-3333') - expect(data[:givenname]).to eq('Sally') - expect(data[:surname]).to eq('Smith') - expect(data[:email]).to eq('sally.smith@princeton.edu') - expect(data[:status]).to eq('staff') - expect(data[:pustatus]).to eq('stf') - expect(data[:universityid]).to eq('999999999') - expect(data[:title]).to eq('Staff, Library - Information Technology.') - end - # rubocop:enable RSpec/ExampleLength - - it 'returns a blank hash for an invalid ldap response' do - allow(ldap_connection).to receive(:search).with(filter: Net::LDAP::Filter.eq("uid", 'abc123')).and_return([]) - expect(data).to be_blank - end - end -end