From 67faf66aa3da7102bac014f62127eb625fb9d864 Mon Sep 17 00:00:00 2001 From: "Brian \"Moses\" Hall" Date: Wed, 29 Nov 2023 09:12:38 -0500 Subject: [PATCH] TTO-153 Logged-in user doesn't see collections despite matching username (#30) * TTO-153 Logged-in user doesn't see collections despite matching username - `Utils::Get_Remote_User_Names` modified to include case-preserved `REMOTE_USER` and `eppn` components. --- Utils.pm | 12 +++++++++++- t/Utils.t | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 t/Utils.t diff --git a/Utils.pm b/Utils.pm index 3c375ed..31e8cc5 100644 --- a/Utils.pm +++ b/Utils.pm @@ -103,10 +103,20 @@ sub Get_Legacy_Remote_User { return $remote_user; } +# Returns an array of unique (Get_Remote_User, case-preserving REMOTE_USER if it exists, +# EPPN components in case-preserving and lowercase-only forms). +# Note: returns the empty string when user is not logged in, i.e., returns ('') sub Get_Remote_User_Names { my @usernames = ( Get_Remote_User() ); + if ( exists $ENV{REMOTE_USER} && defined $ENV{REMOTE_USER} && $ENV{REMOTE_USER} ) { + my $value = $ENV{REMOTE_USER}; + push @usernames, $value unless grep(/^$value$/, @usernames); + } if ( defined $ENV{eppn} && $ENV{eppn} ) { - foreach my $value ( split(/;/, lc $ENV{eppn} ) ) { + foreach my $value ( split(/;/, $ENV{eppn} ) ) { + next unless $value; + push @usernames, $value unless ( grep(/^$value$/, @usernames) ); + $value = lc $value; push @usernames, $value unless ( grep(/^$value$/, @usernames) ); } } diff --git a/t/Utils.t b/t/Utils.t new file mode 100644 index 0000000..ca0bd44 --- /dev/null +++ b/t/Utils.t @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More; + +use Utils; + +local %ENV = %ENV; + +subtest "Get_Remote_User_Names" => sub { + my $save_remote_user = $ENV{REMOTE_USER}; + my $save_eppn = $ENV{eppn}; + + subtest "with logged-in user" => sub { + $ENV{eppn} = 'EPPN@default.invalid'; + $ENV{REMOTE_USER} = 'REMOTE_USER@default.invalid'; + my @names = Utils::Get_Remote_User_Names(); + ok(1 <= scalar grep(/remote_user/, @names), "contains lowercase REMOTE_USER"); + ok(1 <= scalar grep(/REMOTE_USER/, @names), "contains case-preserved REMOTE_USER"); + ok(1 <= scalar grep(/eppn/, @names), "contains lowercase EPPN"); + ok(1 <= scalar grep(/EPPN/, @names), "contains case-preserved EPPN"); + }; + + subtest "with logged-out user" => sub { + delete $ENV{REMOTE_USER}; + delete $ENV{eppn}; + my @names = Utils::Get_Remote_User_Names(); + ok(1 == scalar @names, "there is one name"); + ok('' eq $names[0], "name is empty string"); + }; + + $ENV{REMOTE_USER} = $save_remote_user; + $ENV{eppn} = $save_eppn; +}; + +done_testing();