| 1 | package Concierge v0.13.0; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 9 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 9 | our $VERSION = 'v0.13.0'; | |||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Extensible service layer that manages the relationship between an application and its users | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Carp qw<carp croak>; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use JSON::PP qw< encode_json decode_json >; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use Params::Filter qw< make_filter >; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | # === COMPONENT MODULES === | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 9 | ▶ | use Concierge::Desk::User; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 9 | ▶ | use Concierge::Desk::Component; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 9 | ▶ | use Concierge::Auth; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 9 | ▶ | use Concierge::Sessions; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 9 | ▶ | use Concierge::Users; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | # === PARAMETER FILTERS === | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # Shared filters for secure data segregation | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | # Auth filter - ONLY credentials (user_id + password) | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 9 | our $auth_data_filter = make_filter( | |||||||||||||||||||||||||||||||||||||||||
| 25 | [qw(user_id password)], # required credentials | ||||||||||||||||||||||||||||||||||||||||||
| 26 | [], # accepted - nothing else | ||||||||||||||||||||||||||||||||||||||||||
| 27 | [], # excluded - not needed | ||||||||||||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | # User data filter - everything EXCEPT credentials | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # Handles both minimal input (user_id, moniker) and | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # rich input (user_id, moniker, email, phone, bio, etc.) | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 9 | our $user_data_filter = make_filter( | |||||||||||||||||||||||||||||||||||||||||
| 34 | [qw(user_id moniker)], # required minimum | ||||||||||||||||||||||||||||||||||||||||||
| 35 | ['*'], # accept ALL other fields, except: | ||||||||||||||||||||||||||||||||||||||||||
| 36 | [qw(password confirm_password)], # excluded - security boundary | ||||||||||||||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | # Session data filter - for populating session with initial data | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # Accepts user_id (required for new_session) plus any session fields | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # Excludes credentials (never stored in session data) | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 9 | our $session_data_filter = make_filter( | |||||||||||||||||||||||||||||||||||||||||
| 43 | [qw(user_id)], # required for new_session | ||||||||||||||||||||||||||||||||||||||||||
| 44 | ['*'], # accept all other fields, except: | ||||||||||||||||||||||||||||||||||||||||||
| 45 | [qw(password confirm_password)], # excluded - security boundary | ||||||||||||||||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | # User update filter - for updating existing user records | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # No required fields (user_id passed separately as parameter) | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # Excludes user_id (identity field), password (use reset_password instead) | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 9 | our $user_update_filter = make_filter( | |||||||||||||||||||||||||||||||||||||||||
| 52 | [], # no required fields | ||||||||||||||||||||||||||||||||||||||||||
| 53 | ['*'], # accept all fields, except: | ||||||||||||||||||||||||||||||||||||||||||
| 54 | [qw(user_id password confirm_password)], # excluded - never in updates | ||||||||||||||||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 25 | ▶ | sub new_concierge { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_concierge: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 25 | my ($class) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 25 | bless {}, $class; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | # ============================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 63 | # DESK MANAGEMENT - Opening existing desks | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # ============================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 26 | ▶ | sub open_desk ($class, $desk_location) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
open_desk: called CC=24 SCAR=32.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 26 | ▶ | croak "Desk not found: *$desk_location*" unless -d $desk_location; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 25 | my $concierge = Concierge->new_concierge(); # minimal object | |||||||||||||||||||||||||||||||||||||||||
| 69 | 25 | $concierge->{desk_location} = $desk_location; | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | # Instantiate the concierge from config stored in Concierge's config file | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 25 | my $concierge_conf_file = File::Spec->catfile($desk_location, 'concierge.conf'); | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | # Read entire file (pretty JSON spans multiple lines) | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 25 | my ($fh,$json,$concierge_config); | |||||||||||||||||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 25 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 25 | ▶ | open $fh, "<", $concierge_conf_file | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
open $fh, "<", $concierge_conf_file and $json = readline $fh and close $fh
open $fh, "<", $concierge_conf_file and $json = readline $fh
MC/DC0%0/3
open $fh, "<", $concierge_conf_file and $json = readline $fh and close $fh
open $fh, "<", $concierge_conf_file
$json = readline $fh
close $fh
Truth table25%1/4
A and B and C
A= open $fh, "<", $concierge_conf_fileB= $json = readline $fhC= close $fh
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | and | ||||||||||||||||||||||||||||||||||||||||||
| 80 | $json = <$fh> | ||||||||||||||||||||||||||||||||||||||||||
| 81 | and | ||||||||||||||||||||||||||||||||||||||||||
| 82 | close $fh | ||||||||||||||||||||||||||||||||||||||||||
| 83 | or return { success => 0, message => "Error closing session file: $!" }; | ||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 25 | ▶ | unless (defined $json) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | return { success => 0, message => "Config file is empty" }; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 25 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 89 | 25 | $concierge_config = decode_json($json); | |||||||||||||||||||||||||||||||||||||||||
| 90 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 25 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | return { success => 0, message => "Invalid JSON in config file: $@" }; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | # Instantiate sessions manager from $concierge_config | ||||||||||||||||||||||||||||||||||||||||||
| 96 | $concierge->{sessions} = Concierge::Sessions->new( | ||||||||||||||||||||||||||||||||||||||||||
| 97 | storage_dir => $concierge_config->{sessions_dir} || $concierge_config->{storage_dir}, | ||||||||||||||||||||||||||||||||||||||||||
| 98 | backend_class => $concierge_config->{sessions_backend}, | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 24 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$concierge_config->{'sessions_dir'} || $concierge_config->{'storage_dir'}
MC/DC0%0/2
$concierge_config->{'sessions_dir'} || $concierge_config->{'storage_dir'}
$concierge_config->{'sessions_dir'}
$concierge_config->{'storage_dir'}
Truth table33%1/3
A || B
A= $concierge_config->{'sessions_dir'}B= $concierge_config->{'storage_dir'}
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | # Load user_keys mapping from file (or initialize empty for new desk) | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 24 | my $user_keys_file = File::Spec->catfile($desk_location, 'user_keys.json'); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 24 | ▶ | if (-e $user_keys_file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 2 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | my $user_keys_fh; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 2 | ▶ | open $user_keys_fh, "<", $user_keys_file | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | or return { success => 0, message => "Cannot read user_keys file: $!" }; | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 2 | my $user_keys_json = <$user_keys_fh>; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 2 | close $user_keys_fh; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 2 | $concierge->{user_keys} = decode_json($user_keys_json); | |||||||||||||||||||||||||||||||||||||||||
| 111 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 22 | $concierge->{user_keys} = {}; # New desk or first run | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | # Cleanup expired sessions and synchronize user_keys mapping | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 24 | my $cleanup_result = $concierge->{sessions}->cleanup_sessions(); | |||||||||||||||||||||||||||||||||||||||||
| 117 | # Returns: { success => 1, deleted_count => N, active => [session_ids...] } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 24 | ▶ | if ($cleanup_result->{success} && $cleanup_result->{active}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$cleanup_result->{"success"} and $cleanup_result->{"active"}
MC/DC0%0/2
$cleanup_result->{"success"} and $cleanup_result->{"active"}
$cleanup_result->{"success"}
$cleanup_result->{"active"}
Truth table33%1/3
A and B
A= $cleanup_result->{"success"}B= $cleanup_result->{"active"}
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | # Create lookup hash of active session_ids | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 24 | my %active_sessions = map { $_ => 1 } @{$cleanup_result->{active}}; | |||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | # Remove user_keys entries for deleted sessions | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 24 | my $cleaned = 0; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 24 | for my $key (keys %{$concierge->{user_keys}}) { | |||||||||||||||||||||||||||||||||||||||||
| 126 | 4 | my $session_id = $concierge->{user_keys}{$key}{session_id}; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 4 | ▶ | unless ($active_sessions{$session_id}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | delete $concierge->{user_keys}{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | $cleaned++; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | # Save cleaned mapping if any were removed | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 24 | ▶ | if ($cleaned > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | $concierge->save_user_keys(); | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | # Instantiate users and auth from $concierge_config | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 24 | $concierge->{users} = Concierge::Users->new( $concierge_config->{users_config_file} ); | |||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 24 | ▶ | unless ($concierge_config->{auth_backend}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 1 | croak | |||||||||||||||||||||||||||||||||||||||||
| 144 | "This desk must be built again to work with v0.5+ of Concierge::Auth, " | ||||||||||||||||||||||||||||||||||||||||||
| 145 | . "now shipping with Concierge v0.9+. Building the desk with the same " | ||||||||||||||||||||||||||||||||||||||||||
| 146 | . "original configuration will archive existing user data, but delete " | ||||||||||||||||||||||||||||||||||||||||||
| 147 | . "session and any credential storage, and will automatically install " | ||||||||||||||||||||||||||||||||||||||||||
| 148 | . "the default built-in ID-password authentication system. See the " | ||||||||||||||||||||||||||||||||||||||||||
| 149 | . "POD for how to use an alternative approach to user authentication."; | ||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | $concierge->{auth} = Concierge::Auth->new( | ||||||||||||||||||||||||||||||||||||||||||
| 153 | backend_class => $concierge_config->{auth_backend}, | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 23 | ▶ | %{ $concierge_config->{auth_args} || {} }, | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$concierge_config->{'auth_args'} || {}
MC/DC0%0/1
$concierge_config->{'auth_args'} || {}
$concierge_config->{'auth_args'}
Truth table50%1/2
A
A= $concierge_config->{'auth_args'}
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | # Load any additional components declared in concierge.conf's | ||||||||||||||||||||||||||||||||||||||||||
| 158 | # 'components' block (populated at build time by build_desk() -- see | ||||||||||||||||||||||||||||||||||||||||||
| 159 | # Concierge::Desk::Setup). Unlike sessions/auth/users above, this is a | ||||||||||||||||||||||||||||||||||||||||||
| 160 | # generic loop: each component's class is require()d and constructed | ||||||||||||||||||||||||||||||||||||||||||
| 161 | # with the payload build_desk() persisted for it. A required | ||||||||||||||||||||||||||||||||||||||||||
| 162 | # component's new() failure is an uncaught exception -- a desk must | ||||||||||||||||||||||||||||||||||||||||||
| 163 | # never open half-instantiated. An optional component's new() failure | ||||||||||||||||||||||||||||||||||||||||||
| 164 | # is substituted with a Concierge::Desk::UnavailableComponent stand-in | ||||||||||||||||||||||||||||||||||||||||||
| 165 | # instead. See Concierge::Desk::Component for the full contract. | ||||||||||||||||||||||||||||||||||||||||||
| 166 | # | ||||||||||||||||||||||||||||||||||||||||||
| 167 | # A 'defer' entry (always also 'optional' -- enforced at build time) | ||||||||||||||||||||||||||||||||||||||||||
| 168 | # skips the real new($payload) call here entirely. Instead, its class | ||||||||||||||||||||||||||||||||||||||||||
| 169 | # is still require()d eagerly (catching a missing/broken module | ||||||||||||||||||||||||||||||||||||||||||
| 170 | # immediately, at open_desk() time, rather than deferring even that | ||||||||||||||||||||||||||||||||||||||||||
| 171 | # far) and revalidated via the same probe_component() helper used at | ||||||||||||||||||||||||||||||||||||||||||
| 172 | # build time's tier 1b. A passing probe installs a | ||||||||||||||||||||||||||||||||||||||||||
| 173 | # Concierge::Desk::DeferredComponent stand-in, which defers the real, | ||||||||||||||||||||||||||||||||||||||||||
| 174 | # potentially expensive new($payload) call until first actual use. A | ||||||||||||||||||||||||||||||||||||||||||
| 175 | # failing probe resolves immediately to UnavailableComponent, exactly | ||||||||||||||||||||||||||||||||||||||||||
| 176 | # like any other optional component failure. See | ||||||||||||||||||||||||||||||||||||||||||
| 177 | # Concierge::Desk::Component/PROBING for the full tier model. | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 23 | ▶ | for my $name (keys %{ $concierge_config->{components} // {} }) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$concierge_config->{"components"} // {}
MC/DC100%1/1
$concierge_config->{"components"} // {}
$concierge_config->{"components"}
Truth table100%2/2
A
A= $concierge_config->{"components"}
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 8 | my $entry = $concierge_config->{components}{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 180 | 8 | my $comp; | |||||||||||||||||||||||||||||||||||||||||
| 181 | 8 | ▶ | if ($entry->{defer}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 5 | (my $file = $entry->{class}) =~ s{::}{/}g; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 5 | require "$file.pm"; | |||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | my $probe = Concierge::Desk::Component::probe_component( | ||||||||||||||||||||||||||||||||||||||||||
| 186 | $entry->{class}, $entry->{payload}, | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 5 | ); | |||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | 5 | ▶ | if ($probe->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 4 | require Concierge::Desk::DeferredComponent; | |||||||||||||||||||||||||||||||||||||||||
| 191 | $comp = Concierge::Desk::DeferredComponent->new( | ||||||||||||||||||||||||||||||||||||||||||
| 192 | name => $name, class => $entry->{class}, payload => $entry->{payload}, | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||
| 194 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 1 | require Concierge::Desk::UnavailableComponent; | |||||||||||||||||||||||||||||||||||||||||
| 196 | $comp = Concierge::Desk::UnavailableComponent->new( | ||||||||||||||||||||||||||||||||||||||||||
| 197 | name => $name, reason => $probe->{message}, | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 3 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 203 | 3 | (my $file = $entry->{class}) =~ s{::}{/}g; | |||||||||||||||||||||||||||||||||||||||||
| 204 | 3 | require "$file.pm"; | |||||||||||||||||||||||||||||||||||||||||
| 205 | 3 | $comp = $entry->{class}->new($entry->{payload}); | |||||||||||||||||||||||||||||||||||||||||
| 206 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 207 | 3 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 2 | ▶ | if ($entry->{optional}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 1 | require Concierge::Desk::UnavailableComponent; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 1 | $comp = Concierge::Desk::UnavailableComponent->new( | |||||||||||||||||||||||||||||||||||||||||
| 211 | name => $name, reason => $@, | ||||||||||||||||||||||||||||||||||||||||||
| 212 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 213 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 214 | 1 | croak "Failed to load required component '$name' ($entry->{class}): $@"; | |||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 7 | $concierge->{$name} = $comp; | |||||||||||||||||||||||||||||||||||||||||
| 219 | 7 | ▶ | unless ($concierge->can($name)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 9 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 4 | ▶ | *{"Concierge::$name"} = sub { $_[0]->{$name} }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | 22 | return { success => 1, message => 'Welcome!', concierge => $concierge }; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 84 | ▶ | sub sessions { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sessions: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 84 | $_[0]->{sessions}; | |||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 70 | ▶ | sub users { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
users: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 70 | $_[0]->{users}; | |||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 68 | ▶ | sub auth { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
auth: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | 68 | $_[0]->{auth}; | |||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | # ============================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 239 | # CONCIERGE STATE MANAGEMENT | ||||||||||||||||||||||||||||||||||||||||||
| 240 | # ============================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | # Save user_keys mapping to persistent storage | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 37 | ▶ | sub save_user_keys ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
save_user_keys: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 37 | my $user_keys_file = File::Spec->catfile($self->{desk_location}, 'user_keys.json'); | |||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | # my $json = encode_json($self->{user_keys}); | ||||||||||||||||||||||||||||||||||||||||||
| 247 | 37 | my $json = JSON::PP->new->utf8->pretty->encode( $self->{user_keys} ); | |||||||||||||||||||||||||||||||||||||||||
| 248 | 37 | ▶ | open my $fh, ">", $user_keys_file | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | or return { success => 0, message => "Cannot write user_keys file: $!" }; | ||||||||||||||||||||||||||||||||||||||||||
| 250 | 37 | print $fh $json; | |||||||||||||||||||||||||||||||||||||||||
| 251 | 37 | ▶ | close $fh | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | or return { success => 0, message => "Error closing user_keys file: $!" }; | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 37 | return { success => 1 }; | |||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | # ============================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 259 | # USER MANAGEMENT AND AUTHENTICATION | ||||||||||||||||||||||||||||||||||||||||||
| 260 | # ============================================================================= | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | # Add user: register in Users, set password in Auth | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 22 | ▶ | sub add_user ($self, $user_input) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_user: called CC=7 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | # Single input hashref handles both minimal and rich input: | ||||||||||||||||||||||||||||||||||||||||||
| 265 | # Minimal: { user_id => '...', moniker => '...', password => '...' } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | # Rich: { user_id => '...', moniker => '...', email => '...', | ||||||||||||||||||||||||||||||||||||||||||
| 267 | # phone => '...', bio => '...', password => '...' } | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | 22 | ▶ | return { success => 0, message => 'user_input must be a hash reference' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | unless ref $user_input eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | # Filter for Users - gets everything EXCEPT password | ||||||||||||||||||||||||||||||||||||||||||
| 273 | 21 | my $user_data = $user_data_filter->($user_input); | |||||||||||||||||||||||||||||||||||||||||
| 274 | 21 | ▶ | return { success => 0, message => 'Missing required fields: user_id and moniker' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | unless $user_data; | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | # Filter for Auth - gets ONLY user_id and password | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 19 | my $auth_data = $auth_data_filter->($user_input); | |||||||||||||||||||||||||||||||||||||||||
| 279 | 19 | ▶ | return { success => 0, message => 'Missing required field: password' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | unless $auth_data; | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | 18 | my $user_id = $auth_data->{user_id}; | |||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | # Step 1: Register user in Users component (password automatically excluded) | ||||||||||||||||||||||||||||||||||||||||||
| 285 | 18 | my $register_result = $self->users->register_user($user_data); | |||||||||||||||||||||||||||||||||||||||||
| 286 | 18 | ▶ | unless ($register_result->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 1 | return { success => 0, message => $register_result->{message} }; | |||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | # Step 2: Set password in Auth component | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 17 | my $enroll = $self->auth->enroll($user_id, $auth_data->{password}); | |||||||||||||||||||||||||||||||||||||||||
| 292 | 17 | my ($pwd_ok, $pwd_msg) = ($enroll->{success}, $enroll->{message}); | |||||||||||||||||||||||||||||||||||||||||
| 293 | 17 | ▶ | unless ($pwd_ok) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | # Rollback: delete the user record since password failed | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | $self->users->delete_user($user_id); | |||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | ▶ | return { success => 0, message => $pwd_msg || 'Failed to set password' }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$pwd_msg || 'Failed to set password'
MC/DC0%0/1
$pwd_msg || 'Failed to set password'
$pwd_msg
Truth table0%0/2
A
A= $pwd_msg
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 17 | success => 1, | |||||||||||||||||||||||||||||||||||||||||
| 301 | message => "User '$user_id' added successfully", | ||||||||||||||||||||||||||||||||||||||||||
| 302 | user_id => $user_id, | ||||||||||||||||||||||||||||||||||||||||||
| 303 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | # Remove user: delete from all components (Auth, Users, Sessions, concierge mapping) | ||||||||||||||||||||||||||||||||||||||||||
| 307 | 3 | ▶ | sub remove_user ($self, $user_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove_user: called CC=14 SCAR=28.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | # Removes user from all Concierge components | ||||||||||||||||||||||||||||||||||||||||||
| 309 | # Attempts all deletions with graceful handling | ||||||||||||||||||||||||||||||||||||||||||
| 310 | # Application should handle higher-level cleanup (files, assets) before calling this | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | 3 | ▶ | return { success => 0, message => 'user_id is required' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $user_id and length $user_id
MC/DC100%2/2
defined $user_id and length $user_id
defined $user_id
length $user_id
Truth table100%3/3
A and B
A= defined $user_idB= length $user_id
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | unless defined $user_id && length($user_id); | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | 1 | my @deleted_from; | |||||||||||||||||||||||||||||||||||||||||
| 316 | my @warnings; | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | # Step 1: Delete from Users component | ||||||||||||||||||||||||||||||||||||||||||
| 319 | 1 | my $users_result = $self->users->delete_user($user_id); | |||||||||||||||||||||||||||||||||||||||||
| 320 | 1 | ▶ | if ($users_result->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 1 | push @deleted_from, 'Users'; | |||||||||||||||||||||||||||||||||||||||||
| 322 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | push @warnings, "Users: $users_result->{message}"; | |||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | # Step 2: Delete from Auth component | ||||||||||||||||||||||||||||||||||||||||||
| 327 | 1 | my $revoked = $self->auth->revoke($user_id); | |||||||||||||||||||||||||||||||||||||||||
| 328 | 1 | my ($auth_ok, $auth_msg) = ($revoked->{success}, $revoked->{message}); | |||||||||||||||||||||||||||||||||||||||||
| 329 | 1 | ▶ | if ($auth_ok) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | 1 | push @deleted_from, 'Auth'; | |||||||||||||||||||||||||||||||||||||||||
| 331 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | push @warnings, "Auth: " . ($auth_msg || 'deletion failed'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$auth_msg || 'deletion failed'
MC/DC0%0/1
$auth_msg || 'deletion failed'
$auth_msg
Truth table0%0/2
A
A= $auth_msg
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | # Step 3: Find and delete session (if user has active session) | ||||||||||||||||||||||||||||||||||||||||||
| 336 | 1 | my $session_id; | |||||||||||||||||||||||||||||||||||||||||
| 337 | my $user_key_to_delete; | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | # Search user_keys mapping for this user_id | ||||||||||||||||||||||||||||||||||||||||||
| 340 | 1 | for my $key (keys %{$self->{user_keys}}) { | |||||||||||||||||||||||||||||||||||||||||
| 341 | 1 | ▶ | if ($self->{user_keys}{$key}{user_id} eq $user_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | 1 | $session_id = $self->{user_keys}{$key}{session_id}; | |||||||||||||||||||||||||||||||||||||||||
| 343 | 1 | $user_key_to_delete = $key; | |||||||||||||||||||||||||||||||||||||||||
| 344 | 1 | last; | |||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | # Delete session if found | ||||||||||||||||||||||||||||||||||||||||||
| 349 | 1 | ▶ | if ($session_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 350 | 1 | my $session_result = $self->sessions->delete_session($session_id); | |||||||||||||||||||||||||||||||||||||||||
| 351 | 1 | ▶ | if ($session_result->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | 1 | push @deleted_from, 'Sessions'; | |||||||||||||||||||||||||||||||||||||||||
| 353 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | push @warnings, "Sessions: $session_result->{message}"; | |||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | # Step 4: Remove from concierge user_keys mapping | ||||||||||||||||||||||||||||||||||||||||||
| 359 | 1 | ▶ | if ($user_key_to_delete) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 360 | 1 | delete $self->{user_keys}{$user_key_to_delete}; | |||||||||||||||||||||||||||||||||||||||||
| 361 | 1 | my $save_result = $self->save_user_keys(); | |||||||||||||||||||||||||||||||||||||||||
| 362 | 1 | ▶ | if ($save_result->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 1 | push @deleted_from, 'concierge mapping'; | |||||||||||||||||||||||||||||||||||||||||
| 364 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | push @warnings, "Concierge mapping: failed to save"; | |||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | # Build response | ||||||||||||||||||||||||||||||||||||||||||
| 370 | 1 | ▶ | my $message = scalar(@deleted_from) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | ? "User '$user_id' removed from: " . join(', ', @deleted_from) | ||||||||||||||||||||||||||||||||||||||||||
| 372 | : "User '$user_id' not found in any component"; | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 375 | 1 | ▶ | success => 1, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 376 | message => $message, | ||||||||||||||||||||||||||||||||||||||||||
| 377 | user_id => $user_id, | ||||||||||||||||||||||||||||||||||||||||||
| 378 | deleted_from => \@deleted_from, | ||||||||||||||||||||||||||||||||||||||||||
| 379 | (@warnings ? (warnings => \@warnings) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 380 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | # Verify user: check if user exists in both Auth and Users components | ||||||||||||||||||||||||||||||||||||||||||
| 384 | 8 | ▶ | sub verify_user ($self, $user_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
verify_user: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 385 | # Verifies user exists and checks for data consistency | ||||||||||||||||||||||||||||||||||||||||||
| 386 | # User is considered verified only if present in BOTH components | ||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | 8 | ▶ | return { success => 0, message => 'user_id is required' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $user_id and length $user_id
MC/DC100%2/2
defined $user_id and length $user_id
defined $user_id
length $user_id
Truth table100%3/3
A and B
A= defined $user_idB= length $user_id
| |||||||||||||||||||||||||||||||||||||||||||
| 389 | unless defined $user_id && length($user_id); | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | # Check Auth component (password file) | ||||||||||||||||||||||||||||||||||||||||||
| 392 | 6 | my $auth_exists = $self->auth->is_id_known($user_id)->{known}; | |||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | # Check Users component (user data store) | ||||||||||||||||||||||||||||||||||||||||||
| 395 | 6 | my $user_result = $self->users->get_user($user_id); | |||||||||||||||||||||||||||||||||||||||||
| 396 | 6 | my $users_exists = $user_result->{success}; | |||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | # User is verified if in BOTH components | ||||||||||||||||||||||||||||||||||||||||||
| 399 | 6 | ▶ | my $verified = $auth_exists && $users_exists; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$auth_exists && $users_exists
MC/DC100%2/2
$auth_exists && $users_exists
$auth_exists
$users_exists
Truth table100%3/3
A && B
A= $auth_existsB= $users_exists
| |||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | 6 | my $response = { | |||||||||||||||||||||||||||||||||||||||||
| 402 | success => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 403 | verified => $verified, | ||||||||||||||||||||||||||||||||||||||||||
| 404 | exists_in_auth => $auth_exists, | ||||||||||||||||||||||||||||||||||||||||||
| 405 | exists_in_users => $users_exists, | ||||||||||||||||||||||||||||||||||||||||||
| 406 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | # Add user_status if available | ||||||||||||||||||||||||||||||||||||||||||
| 409 | 6 | ▶ | if ($users_exists && exists $user_result->{user}{user_status}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$users_exists and exists $user_result->{"user"}{"user_status"}
MC/DC50%1/2
$users_exists and exists $user_result->{"user"}{"user_status"}
$users_exists
exists $user_result->{"user"}{"user_status"}
Truth table66%2/3
A and B
A= $users_existsB= exists $user_result->{"user"}{"user_status"}
| |||||||||||||||||||||||||||||||||||||||||||
| 410 | 2 | $response->{user_status} = $user_result->{user}{user_status}; | |||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | # Warn if inconsistent (exists in one but not both) | ||||||||||||||||||||||||||||||||||||||||||
| 414 | 6 | ▶ | if ($auth_exists != $users_exists) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 415 | 1 | $response->{warning} = 'User exists in only one component - data inconsistency detected'; | |||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | 6 | return $response; | |||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | # Update user data: modify user record in Users component | ||||||||||||||||||||||||||||||||||||||||||
| 422 | 6 | ▶ | sub update_user_data ($self, $user_id, $update_data) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update_user_data: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 423 | 6 | ▶ | return { success => 0, message => 'user_id is required' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $user_id and length $user_id
MC/DC100%2/2
defined $user_id and length $user_id
defined $user_id
length $user_id
Truth table100%3/3
A and B
A= defined $user_idB= length $user_id
| |||||||||||||||||||||||||||||||||||||||||||
| 424 | unless defined $user_id && length($user_id); | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | 4 | ▶ | return { success => 0, message => 'update_data must be a hash reference' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 427 | unless ref $update_data eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | # Filter update data (excludes user_id, password, confirm_password) | ||||||||||||||||||||||||||||||||||||||||||
| 430 | 3 | my $filtered_updates = $user_update_filter->($update_data); | |||||||||||||||||||||||||||||||||||||||||
| 431 | 3 | ▶ | return { success => 0, message => 'No valid fields to update' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$filtered_updates and keys %$filtered_updates
MC/DC50%1/2
$filtered_updates and keys %$filtered_updates
$filtered_updates
keys %$filtered_updates
Truth table66%2/3
A and B
A= $filtered_updatesB= keys %$filtered_updates
| |||||||||||||||||||||||||||||||||||||||||||
| 432 | unless $filtered_updates && keys %$filtered_updates; | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | # Update user in Users component | ||||||||||||||||||||||||||||||||||||||||||
| 435 | 2 | my $update_result = $self->users->update_user($user_id, $filtered_updates); | |||||||||||||||||||||||||||||||||||||||||
| 436 | 2 | ▶ | unless ($update_result->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | return { success => 0, message => $update_result->{message} }; | |||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 441 | 2 | success => 1, | |||||||||||||||||||||||||||||||||||||||||
| 442 | message => "User '$user_id' updated successfully", | ||||||||||||||||||||||||||||||||||||||||||
| 443 | user_id => $user_id, | ||||||||||||||||||||||||||||||||||||||||||
| 444 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | # Get user data: retrieve user profile from Users component | ||||||||||||||||||||||||||||||||||||||||||
| 448 | 10 | ▶ | sub get_user_data ($self, $user_id, @fields) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_user_data: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 449 | # Retrieves user profile data fields | ||||||||||||||||||||||||||||||||||||||||||
| 450 | # If @fields specified, returns only those fields | ||||||||||||||||||||||||||||||||||||||||||
| 451 | # Otherwise returns all user data | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | 10 | ▶ | return { success => 0, message => 'user_id is required' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $user_id and length $user_id
MC/DC100%2/2
defined $user_id and length $user_id
defined $user_id
length $user_id
Truth table100%3/3
A and B
A= defined $user_idB= length $user_id
| |||||||||||||||||||||||||||||||||||||||||||
| 454 | unless defined $user_id && length($user_id); | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | # Get user from Users component | ||||||||||||||||||||||||||||||||||||||||||
| 457 | 8 | my $user_result = $self->users->get_user($user_id); | |||||||||||||||||||||||||||||||||||||||||
| 458 | return { success => 0, message => $user_result->{message} || 'User not found' } | ||||||||||||||||||||||||||||||||||||||||||
| 459 | 8 | ▶ | unless $user_result->{success}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$user_result->{'message'} || 'User not found'
MC/DC0%0/1
$user_result->{'message'} || 'User not found'
$user_result->{'message'}
Truth table50%1/2
A
A= $user_result->{'message'}
| |||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | 7 | my $user_data = $user_result->{user}; | |||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | # If specific fields requested, return only those that exist | ||||||||||||||||||||||||||||||||||||||||||
| 464 | 7 | ▶ | if (@fields) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 465 | 1 | my %selected; | |||||||||||||||||||||||||||||||||||||||||
| 466 | 1 | for my $field (@fields) { | |||||||||||||||||||||||||||||||||||||||||
| 467 | 2 | ▶ | $selected{$field} = $user_data->{$field} if exists $user_data->{$field}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||||||||||||||
| 469 | 1 | return { success => 1, user => \%selected }; | |||||||||||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | # Otherwise return all data | ||||||||||||||||||||||||||||||||||||||||||
| 473 | 6 | return { success => 1, user => $user_data }; | |||||||||||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||
| 476 | # List users: retrieve list of user_ids, optionally with full data | ||||||||||||||||||||||||||||||||||||||||||
| 477 | 3 | ▶ | sub list_users ($self, $filter='', $options={}) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_users: called CC=10 SCAR=24.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 478 | # Returns user_ids by default | ||||||||||||||||||||||||||||||||||||||||||
| 479 | # With include_data => 1: fetches full user records | ||||||||||||||||||||||||||||||||||||||||||
| 480 | # With fields => [...]: returns only specified fields per user | ||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | # Get user_ids from Users component | ||||||||||||||||||||||||||||||||||||||||||
| 483 | 3 | my $list_result = $self->users->list_users($filter); | |||||||||||||||||||||||||||||||||||||||||
| 484 | return { success => 0, message => $list_result->{message} || 'Failed to list users' } | ||||||||||||||||||||||||||||||||||||||||||
| 485 | 3 | ▶ | unless $list_result->{success}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$list_result->{'message'} || 'Failed to list users'
MC/DC0%0/1
$list_result->{'message'} || 'Failed to list users'
$list_result->{'message'}
Truth table0%0/2
A
A= $list_result->{'message'}
| |||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | 3 | ▶ | my $user_ids = $list_result->{user_ids} // []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$list_result->{"user_ids"} // []
MC/DC0%0/1
$list_result->{"user_ids"} // []
$list_result->{"user_ids"}
Truth table50%1/2
A
A= $list_result->{"user_ids"}
| |||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | # If only IDs requested, return them with count | ||||||||||||||||||||||||||||||||||||||||||
| 490 | 3 | ▶ | unless ($options->{include_data}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 491 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 492 | 2 | success => 1, | |||||||||||||||||||||||||||||||||||||||||
| 493 | user_ids => $user_ids, | ||||||||||||||||||||||||||||||||||||||||||
| 494 | count => scalar(@$user_ids), | ||||||||||||||||||||||||||||||||||||||||||
| 495 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | # Fetch full data for each user | ||||||||||||||||||||||||||||||||||||||||||
| 499 | 1 | my %users; # Hash indexed by user_id for easy lookup | |||||||||||||||||||||||||||||||||||||||||
| 500 | 1 | ▶ | my @fields = $options->{fields} ? $options->{fields}->@* : (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | 1 | for my $user_id (@$user_ids) { | |||||||||||||||||||||||||||||||||||||||||
| 503 | 3 | my $data = $self->get_user_data($user_id, @fields); | |||||||||||||||||||||||||||||||||||||||||
| 504 | 3 | ▶ | $users{$user_id} = $data->{user} if $data->{success}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 505 | } | ||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||
| 507 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 508 | 1 | success => 1, | |||||||||||||||||||||||||||||||||||||||||
| 509 | user_ids => $user_ids, # Preserve order | ||||||||||||||||||||||||||||||||||||||||||
| 510 | users => \%users, # Hash for easy lookup | ||||||||||||||||||||||||||||||||||||||||||
| 511 | count => scalar(keys %users), | ||||||||||||||||||||||||||||||||||||||||||
| 512 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | # Build closures that give a User object backend access to its data store | ||||||||||||||||||||||||||||||||||||||||||
| 516 | # Called by any Concierge method that creates a logged-in User object | ||||||||||||||||||||||||||||||||||||||||||
| 517 | 23 | ▶ | sub _make_user_closures ($self, $user_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_user_closures: called CC=1 SCAR=0.3
| |||||||||||||||||||||||||||||||||||||||||||
| 518 | 2 | ▶ | my $get_user_data = sub (@fields) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||
| 519 | 2 | my $result = $self->users->get_user($user_id); | |||||||||||||||||||||||||||||||||||||||||
| 520 | 2 | ▶ | return $result unless $result->{success}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||
| 522 | 2 | ▶ | if (@fields) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 523 | 0 | my %selected = map { $_ => $result->{user}{$_} } | |||||||||||||||||||||||||||||||||||||||||
| 524 | 0 | grep { exists $result->{user}{$_} } | |||||||||||||||||||||||||||||||||||||||||
| 525 | @fields; | ||||||||||||||||||||||||||||||||||||||||||
| 526 | 0 | return { success => 1, user => \%selected }; | |||||||||||||||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||||||||||||||||
| 528 | 2 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 529 | 23 | }; | |||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||
| 531 | 2 | ▶ | my $update_user_data = sub ($updates) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 532 | 2 | return $self->users->update_user($user_id, $updates); | |||||||||||||||||||||||||||||||||||||||||
| 533 | 23 | }; | |||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||
| 535 | 5 | ▶ | my $verify_password = sub ($password) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 536 | 5 | return $self->verify_password($user_id, $password); | |||||||||||||||||||||||||||||||||||||||||
| 537 | 23 | }; | |||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | 1 | ▶ | my $reset_password = sub ($new_password) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 540 | 1 | return $self->reset_password($user_id, $new_password); | |||||||||||||||||||||||||||||||||||||||||
| 541 | 23 | }; | |||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | 23 | return ($get_user_data, $update_user_data, $verify_password, $reset_password); | |||||||||||||||||||||||||||||||||||||||||
| 544 | } | ||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||
| 546 | # Admit visitor: assign user_key only (no session, no user data) | ||||||||||||||||||||||||||||||||||||||||||
| 547 | 2 | ▶ | sub admit_visitor ($self) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
admit_visitor: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 548 | 2 | ▶ | my $visitor_id = $self->auth->gen_random_string(13) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 549 | or return { success => 0, message => "Couldn't generate visitor ID" }; | ||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||
| 551 | # Create user object for visitor (no session, no user_data, no backend access) | ||||||||||||||||||||||||||||||||||||||||||
| 552 | 2 | my $user = Concierge::Desk::User->enable_user($visitor_id, { | |||||||||||||||||||||||||||||||||||||||||
| 553 | user_key => $visitor_id, # Use visitor_id as user_key | ||||||||||||||||||||||||||||||||||||||||||
| 554 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||
| 556 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 557 | 2 | success => 1, | |||||||||||||||||||||||||||||||||||||||||
| 558 | user => $user, | ||||||||||||||||||||||||||||||||||||||||||
| 559 | is_visitor => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 560 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||||||
| 563 | # Checkin guest: assign a session with no user data backend or authentication | ||||||||||||||||||||||||||||||||||||||||||
| 564 | 7 | ▶ | sub checkin_guest ($self, $session_opts={}) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
checkin_guest: called CC=6 SCAR=18.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 565 | 7 | ▶ | my $guest_id = $self->auth->gen_random_string(13) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 566 | or return { success => 0, message => "Couldn't generate guest ID" }; | ||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||
| 568 | # Shorter timeout for anonymous | ||||||||||||||||||||||||||||||||||||||||||
| 569 | my $timeout = $session_opts->{timeout} | ||||||||||||||||||||||||||||||||||||||||||
| 570 | // $self->{config}{anonymous_timeout} | ||||||||||||||||||||||||||||||||||||||||||
| 571 | 7 | ▶ | // 1800; # 30 minutes default | ||||||||||||||||||||||||||||||||||||||||
|
Condition40%2/5
($session_opts->{"timeout"} // $self->{"config"}{"anonymous_timeout"}) // 1800
$session_opts->{"timeout"} // $self->{"config"}{"anonymous_timeout"}
MC/DC0%0/2
($session_opts->{"timeout"} // $self->{"config"}{"anonymous_timeout"}) // 1800
$session_opts->{"timeout"}
$self->{"config"}{"anonymous_timeout"}
Truth table33%1/3
A // B
A= $session_opts->{"timeout"}B= $self->{"config"}{"anonymous_timeout"}
| |||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||
| 573 | # Create session using Sessions manager | ||||||||||||||||||||||||||||||||||||||||||
| 574 | 7 | my $result = $self->sessions->new_session( | |||||||||||||||||||||||||||||||||||||||||
| 575 | user_id => $guest_id, | ||||||||||||||||||||||||||||||||||||||||||
| 576 | session_timeout => $timeout, | ||||||||||||||||||||||||||||||||||||||||||
| 577 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 578 | return { success => 0, message => $result->{message} } | ||||||||||||||||||||||||||||||||||||||||||
| 579 | 7 | ▶ | unless $result->{success}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | 7 | my $session = $result->{session}; | |||||||||||||||||||||||||||||||||||||||||
| 582 | 7 | my $session_id = $session->session_id(); | |||||||||||||||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||||||||||||||||
| 584 | # Guest has a session, so it can log itself out, even though it has | ||||||||||||||||||||||||||||||||||||||||||
| 585 | # no Auth-backed identity for the password-related closures. | ||||||||||||||||||||||||||||||||||||||||||
| 586 | 7 | ▶ | my $logout = sub { $self->logout_user($session_id) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 587 | 7 | ▶ | my $session_valid = sub { $self->sessions->get_session($session_id) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||
| 589 | # Create user object for guest (no user_data, no data-backend closures) | ||||||||||||||||||||||||||||||||||||||||||
| 590 | 7 | my $user = Concierge::Desk::User->enable_user($guest_id, { | |||||||||||||||||||||||||||||||||||||||||
| 591 | session => $session, | ||||||||||||||||||||||||||||||||||||||||||
| 592 | user_key => $guest_id, # Use guest_id as user_key for simplicity | ||||||||||||||||||||||||||||||||||||||||||
| 593 | _logout => $logout, | ||||||||||||||||||||||||||||||||||||||||||
| 594 | _session_valid => $session_valid, | ||||||||||||||||||||||||||||||||||||||||||
| 595 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||||||||||
| 597 | # Store user_key mapping | ||||||||||||||||||||||||||||||||||||||||||
| 598 | 7 | $self->{user_keys}{$user->user_key()} = { | |||||||||||||||||||||||||||||||||||||||||
| 599 | user_id => $guest_id, | ||||||||||||||||||||||||||||||||||||||||||
| 600 | session_id => $session_id, | ||||||||||||||||||||||||||||||||||||||||||
| 601 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 602 | 7 | $self->save_user_keys(); | |||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||
| 604 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 605 | 7 | success => 1, | |||||||||||||||||||||||||||||||||||||||||
| 606 | user => $user, | ||||||||||||||||||||||||||||||||||||||||||
| 607 | is_guest => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 608 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 609 | } | ||||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||||
| 611 | 3 | ▶ | sub login_guest($self, $user_input, $guest_user_key) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
login_guest: called CC=14 SCAR=27.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 612 | # Convert guest to logged-in user, transferring session data (shopping cart, etc.) | ||||||||||||||||||||||||||||||||||||||||||
| 613 | # $user_input: hashref with user_id, moniker, password (and optional email, etc.) | ||||||||||||||||||||||||||||||||||||||||||
| 614 | # Registers a new user account unless user_id already belongs to a known | ||||||||||||||||||||||||||||||||||||||||||
| 615 | # user, in which case this just logs them in -- transferring the guest's | ||||||||||||||||||||||||||||||||||||||||||
| 616 | # session data either way. | ||||||||||||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||||||||||||
| 618 | # Step 1: Lookup guest's session_id from user_key mapping | ||||||||||||||||||||||||||||||||||||||||||
| 619 | 3 | my $guest_mapping = $self->{user_keys}{$guest_user_key}; | |||||||||||||||||||||||||||||||||||||||||
| 620 | 3 | ▶ | return { success => 0, message => 'Guest user_key not found' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 621 | unless $guest_mapping; | ||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||
| 623 | 3 | my $guest_session_id = $guest_mapping->{session_id}; | |||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||
| 625 | # Step 2: Get guest session and extract its data | ||||||||||||||||||||||||||||||||||||||||||
| 626 | 3 | my $session_result = $self->sessions->get_session($guest_session_id); | |||||||||||||||||||||||||||||||||||||||||
| 627 | return { success => 0, message => 'Guest session not found' } | ||||||||||||||||||||||||||||||||||||||||||
| 628 | 3 | ▶ | unless $session_result->{success}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||||||||
| 630 | 3 | my $guest_session = $session_result->{session}; | |||||||||||||||||||||||||||||||||||||||||
| 631 | 3 | my $guest_data_result = $guest_session->get_data(); | |||||||||||||||||||||||||||||||||||||||||
| 632 | 3 | ▶ | my $guest_data = $guest_data_result->{value} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$guest_data_result->{'value'} || {}
MC/DC0%0/1
$guest_data_result->{'value'} || {}
$guest_data_result->{'value'}
Truth table50%1/2
A
A= $guest_data_result->{'value'}
| |||||||||||||||||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||||||||||||||||
| 634 | # Step 3: Register a new user account (Auth + Users), unless user_id | ||||||||||||||||||||||||||||||||||||||||||
| 635 | # already belongs to a known user -- then skip straight to login. | ||||||||||||||||||||||||||||||||||||||||||
| 636 | 3 | my $user_id = $user_input->{user_id}; | |||||||||||||||||||||||||||||||||||||||||
| 637 | 3 | my $is_known_user = 0; | |||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||
| 639 | 3 | ▶ | if (defined $user_id && length $user_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $user_id and length $user_id
MC/DC0%0/2
defined $user_id and length $user_id
defined $user_id
length $user_id
Truth table33%1/3
A and B
A= defined $user_idB= length $user_id
| |||||||||||||||||||||||||||||||||||||||||||
| 640 | 3 | my $verify = $self->verify_user($user_id); | |||||||||||||||||||||||||||||||||||||||||
| 641 | 3 | ▶ | if ($verify->{exists_in_auth} || $verify->{exists_in_users}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$verify->{"exists_in_auth"} or $verify->{"exists_in_users"}
MC/DC50%1/2
$verify->{"exists_in_auth"} or $verify->{"exists_in_users"}
$verify->{"exists_in_auth"}
$verify->{"exists_in_users"}
Truth table66%2/3
A or B
A= $verify->{"exists_in_auth"}B= $verify->{"exists_in_users"}
| |||||||||||||||||||||||||||||||||||||||||||
| 642 | return { success => 0, message => "Account '$user_id' is in an inconsistent state; contact support" } | ||||||||||||||||||||||||||||||||||||||||||
| 643 | 2 | ▶ | unless $verify->{verified}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 644 | 1 | $is_known_user = 1; | |||||||||||||||||||||||||||||||||||||||||
| 645 | } | ||||||||||||||||||||||||||||||||||||||||||
| 646 | } | ||||||||||||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||||||||||||
| 648 | 2 | ▶ | unless ($is_known_user) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 649 | 1 | my $add_result = $self->add_user($user_input); | |||||||||||||||||||||||||||||||||||||||||
| 650 | 1 | ▶ | return $add_result unless $add_result->{success}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||||||||||||
| 653 | # Step 4: Log in the (new or known) user (authenticate, create session, get User object) | ||||||||||||||||||||||||||||||||||||||||||
| 654 | 2 | my $auth_data = $auth_data_filter->($user_input); | |||||||||||||||||||||||||||||||||||||||||
| 655 | my $login_result = $self->login_user({ | ||||||||||||||||||||||||||||||||||||||||||
| 656 | user_id => $auth_data->{user_id}, | ||||||||||||||||||||||||||||||||||||||||||
| 657 | password => $auth_data->{password}, | ||||||||||||||||||||||||||||||||||||||||||
| 658 | 2 | }); | |||||||||||||||||||||||||||||||||||||||||
| 659 | 2 | ▶ | return $login_result unless $login_result->{success}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||||||||
| 661 | 2 | my $user = $login_result->{user}; | |||||||||||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||||||||||||
| 663 | # Step 5: Transfer guest session data to new user's session | ||||||||||||||||||||||||||||||||||||||||||
| 664 | 2 | ▶ | if (%$guest_data) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 665 | 2 | my $new_session = $user->session(); | |||||||||||||||||||||||||||||||||||||||||
| 666 | 2 | ▶ | if ($new_session) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 667 | 2 | $new_session->set_data($guest_data); | |||||||||||||||||||||||||||||||||||||||||
| 668 | 2 | $new_session->save(); | |||||||||||||||||||||||||||||||||||||||||
| 669 | } | ||||||||||||||||||||||||||||||||||||||||||
| 670 | } | ||||||||||||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||||||||||||
| 672 | # Step 6: Delete guest session | ||||||||||||||||||||||||||||||||||||||||||
| 673 | 2 | $self->sessions->delete_session($guest_session_id); | |||||||||||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||||||||||||
| 675 | # Step 7: Remove guest user_key mapping | ||||||||||||||||||||||||||||||||||||||||||
| 676 | 2 | delete $self->{user_keys}{$guest_user_key}; | |||||||||||||||||||||||||||||||||||||||||
| 677 | 2 | $self->save_user_keys(); | |||||||||||||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||||||||||||
| 679 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 680 | 2 | success => 1, | |||||||||||||||||||||||||||||||||||||||||
| 681 | message => 'Guest converted to logged-in user', | ||||||||||||||||||||||||||||||||||||||||||
| 682 | user => $user, | ||||||||||||||||||||||||||||||||||||||||||
| 683 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||||||||||||||||
| 686 | # Restore user: reconstruct User object from a user_key (e.g., from a cookie) | ||||||||||||||||||||||||||||||||||||||||||
| 687 | 6 | ▶ | sub restore_user ($self, $user_key) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
restore_user: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 688 | 6 | ▶ | return { success => 0, message => 'user_key is required' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $user_key and length $user_key
MC/DC50%1/2
defined $user_key and length $user_key
defined $user_key
length $user_key
Truth table66%2/3
A and B
A= defined $user_keyB= length $user_key
| |||||||||||||||||||||||||||||||||||||||||||
| 689 | unless defined $user_key && length($user_key); | ||||||||||||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||||||||||||
| 691 | # Step 1: Lookup user_key in mapping | ||||||||||||||||||||||||||||||||||||||||||
| 692 | 5 | my $mapping = $self->{user_keys}{$user_key}; | |||||||||||||||||||||||||||||||||||||||||
| 693 | 5 | ▶ | return { success => 0, message => 'user_key not found' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 694 | unless $mapping; | ||||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||
| 696 | 4 | my $user_id = $mapping->{user_id}; | |||||||||||||||||||||||||||||||||||||||||
| 697 | 4 | my $session_id = $mapping->{session_id}; | |||||||||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||||||||||
| 699 | # Step 2: Retrieve session (validates it still exists and hasn't expired) | ||||||||||||||||||||||||||||||||||||||||||
| 700 | 4 | my $session_result = $self->sessions->get_session($session_id); | |||||||||||||||||||||||||||||||||||||||||
| 701 | 4 | ▶ | unless ($session_result->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 702 | # Session gone or expired -- clean up stale mapping | ||||||||||||||||||||||||||||||||||||||||||
| 703 | 1 | delete $self->{user_keys}{$user_key}; | |||||||||||||||||||||||||||||||||||||||||
| 704 | 1 | $self->save_user_keys(); | |||||||||||||||||||||||||||||||||||||||||
| 705 | 1 | return { success => 0, message => 'Session expired' }; | |||||||||||||||||||||||||||||||||||||||||
| 706 | } | ||||||||||||||||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||||||||||||||||
| 708 | 3 | my $session = $session_result->{session}; | |||||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||||||
| 710 | # Step 3: Determine user type -- logged-in or guest | ||||||||||||||||||||||||||||||||||||||||||
| 711 | 3 | my $user_result = $self->users->get_user($user_id); | |||||||||||||||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||||||||||||||||
| 713 | 3 | ▶ | if ($user_result->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 714 | # Logged-in user: rebuild with user data and backend closures | ||||||||||||||||||||||||||||||||||||||||||
| 715 | 2 | my ($get, $update, $verify_password, $reset_password) = $self->_make_user_closures($user_id); | |||||||||||||||||||||||||||||||||||||||||
| 716 | 0 | ▶ | my $logout = sub { $self->logout_user($session_id) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 717 | 2 | ▶ | my $session_valid = sub { $self->sessions->get_session($session_id) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 718 | my $user = Concierge::Desk::User->enable_user($user_id, { | ||||||||||||||||||||||||||||||||||||||||||
| 719 | session => $session, | ||||||||||||||||||||||||||||||||||||||||||
| 720 | user_data => $user_result->{user}, | ||||||||||||||||||||||||||||||||||||||||||
| 721 | 2 | user_key => $user_key, | |||||||||||||||||||||||||||||||||||||||||
| 722 | _get_user_data => $get, | ||||||||||||||||||||||||||||||||||||||||||
| 723 | _update_user_data => $update, | ||||||||||||||||||||||||||||||||||||||||||
| 724 | _verify_password => $verify_password, | ||||||||||||||||||||||||||||||||||||||||||
| 725 | _reset_password => $reset_password, | ||||||||||||||||||||||||||||||||||||||||||
| 726 | _logout => $logout, | ||||||||||||||||||||||||||||||||||||||||||
| 727 | _session_valid => $session_valid, | ||||||||||||||||||||||||||||||||||||||||||
| 728 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||||||||||||
| 730 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 731 | 2 | success => 1, | |||||||||||||||||||||||||||||||||||||||||
| 732 | message => 'User restored', | ||||||||||||||||||||||||||||||||||||||||||
| 733 | user => $user, | ||||||||||||||||||||||||||||||||||||||||||
| 734 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 735 | } | ||||||||||||||||||||||||||||||||||||||||||
| 736 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 737 | # Guest: session only, no user data -- but a session still means | ||||||||||||||||||||||||||||||||||||||||||
| 738 | # it can log itself out. | ||||||||||||||||||||||||||||||||||||||||||
| 739 | 0 | ▶ | my $logout = sub { $self->logout_user($session_id) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 740 | 1 | ▶ | my $session_valid = sub { $self->sessions->get_session($session_id) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 741 | 1 | my $user = Concierge::Desk::User->enable_user($user_id, { | |||||||||||||||||||||||||||||||||||||||||
| 742 | session => $session, | ||||||||||||||||||||||||||||||||||||||||||
| 743 | user_key => $user_key, | ||||||||||||||||||||||||||||||||||||||||||
| 744 | _logout => $logout, | ||||||||||||||||||||||||||||||||||||||||||
| 745 | _session_valid => $session_valid, | ||||||||||||||||||||||||||||||||||||||||||
| 746 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||||||||||||||||
| 748 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 749 | 1 | success => 1, | |||||||||||||||||||||||||||||||||||||||||
| 750 | message => 'Guest restored', | ||||||||||||||||||||||||||||||||||||||||||
| 751 | user => $user, | ||||||||||||||||||||||||||||||||||||||||||
| 752 | is_guest => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 753 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 754 | } | ||||||||||||||||||||||||||||||||||||||||||
| 755 | } | ||||||||||||||||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||||||||||||||||
| 757 | # Login user: authenticate, create session, assign user_key and store external_key mapping | ||||||||||||||||||||||||||||||||||||||||||
| 758 | 23 | ▶ | sub login_user ($self, $credentials, $session_opts={}) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
login_user: called CC=9 SCAR=22.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 759 | # Step 0: Get credentials | ||||||||||||||||||||||||||||||||||||||||||
| 760 | 23 | my $auth_data = $auth_data_filter->($credentials); | |||||||||||||||||||||||||||||||||||||||||
| 761 | 23 | ▶ | return { success => 0, message => 'Missing user_id or password' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 762 | unless $auth_data; | ||||||||||||||||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||||||||||||||||
| 764 | 23 | my $user_id = $auth_data->{user_id}; | |||||||||||||||||||||||||||||||||||||||||
| 765 | 23 | my $password = $auth_data->{password}; | |||||||||||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||||||||||||
| 767 | # Step 1: Get user from database | ||||||||||||||||||||||||||||||||||||||||||
| 768 | 23 | my $user_result = $self->users->get_user($user_id); | |||||||||||||||||||||||||||||||||||||||||
| 769 | return { success => 0, message => 'User not found' } | ||||||||||||||||||||||||||||||||||||||||||
| 770 | 23 | ▶ | unless $user_result->{success}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 771 | |||||||||||||||||||||||||||||||||||||||||||
| 772 | # Step 2: Authenticate with ID & password | ||||||||||||||||||||||||||||||||||||||||||
| 773 | 22 | my $authed = $self->auth->authenticate($user_id, $password); | |||||||||||||||||||||||||||||||||||||||||
| 774 | 22 | my ($auth_ok, $auth_msg) = ($authed->{success}, $authed->{message}); | |||||||||||||||||||||||||||||||||||||||||
| 775 | 22 | ▶ | return { success => 0, message => $auth_msg || 'Authentication failed' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$auth_msg || 'Authentication failed'
MC/DC0%0/1
$auth_msg || 'Authentication failed'
$auth_msg
Truth table50%1/2
A
A= $auth_msg
| |||||||||||||||||||||||||||||||||||||||||||
| 776 | unless $auth_ok; | ||||||||||||||||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||||||||||||||||
| 778 | # Step 3: Create session | ||||||||||||||||||||||||||||||||||||||||||
| 779 | my $session_result = $self->sessions->new_session( | ||||||||||||||||||||||||||||||||||||||||||
| 780 | user_id => $user_id, | ||||||||||||||||||||||||||||||||||||||||||
| 781 | 21 | ▶ | %{ $session_opts || {} }, | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$session_opts || {}
MC/DC0%0/1
$session_opts || {}
$session_opts
Truth table50%1/2
A
A= $session_opts
| |||||||||||||||||||||||||||||||||||||||||||
| 782 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 783 | return { success => 0, message => $session_result->{message} || 'Failed to create session' } | ||||||||||||||||||||||||||||||||||||||||||
| 784 | 21 | ▶ | unless $session_result->{success}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$session_result->{'message'} || 'Failed to create session'
MC/DC0%0/1
$session_result->{'message'} || 'Failed to create session'
$session_result->{'message'}
Truth table0%0/2
A
A= $session_result->{'message'}
| |||||||||||||||||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||||||||||||||||
| 786 | 21 | my $session = $session_result->{session}; | |||||||||||||||||||||||||||||||||||||||||
| 787 | 21 | my $session_id = $session->session_id(); | |||||||||||||||||||||||||||||||||||||||||
| 788 | |||||||||||||||||||||||||||||||||||||||||||
| 789 | # Create user object for logged-in user | ||||||||||||||||||||||||||||||||||||||||||
| 790 | 21 | my ($get, $update, $verify_password, $reset_password) = $self->_make_user_closures($user_id); | |||||||||||||||||||||||||||||||||||||||||
| 791 | 21 | ▶ | my $logout = sub { $self->logout_user($session_id) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 792 | 21 | ▶ | my $session_valid = sub { $self->sessions->get_session($session_id) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 793 | my $user = Concierge::Desk::User->enable_user($user_id, { | ||||||||||||||||||||||||||||||||||||||||||
| 794 | session => $session, | ||||||||||||||||||||||||||||||||||||||||||
| 795 | user_data => $user_result->{user}, | ||||||||||||||||||||||||||||||||||||||||||
| 796 | 21 | _get_user_data => $get, | |||||||||||||||||||||||||||||||||||||||||
| 797 | _update_user_data => $update, | ||||||||||||||||||||||||||||||||||||||||||
| 798 | _verify_password => $verify_password, | ||||||||||||||||||||||||||||||||||||||||||
| 799 | _reset_password => $reset_password, | ||||||||||||||||||||||||||||||||||||||||||
| 800 | _logout => $logout, | ||||||||||||||||||||||||||||||||||||||||||
| 801 | _session_valid => $session_valid, | ||||||||||||||||||||||||||||||||||||||||||
| 802 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 803 | |||||||||||||||||||||||||||||||||||||||||||
| 804 | # Store user_key mapping | ||||||||||||||||||||||||||||||||||||||||||
| 805 | 21 | $self->{user_keys}{$user->user_key()} = { | |||||||||||||||||||||||||||||||||||||||||
| 806 | user_id => $user_id, | ||||||||||||||||||||||||||||||||||||||||||
| 807 | session_id => $session_id, | ||||||||||||||||||||||||||||||||||||||||||
| 808 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 809 | 21 | $self->save_user_keys(); | |||||||||||||||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||||||||||||||||
| 811 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 812 | 21 | success => 1, | |||||||||||||||||||||||||||||||||||||||||
| 813 | message => 'Login successful', | ||||||||||||||||||||||||||||||||||||||||||
| 814 | user => $user, | ||||||||||||||||||||||||||||||||||||||||||
| 815 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 816 | } | ||||||||||||||||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||||||||||||||||
| 818 | # Verify password: check if password is correct for user | ||||||||||||||||||||||||||||||||||||||||||
| 819 | 13 | ▶ | sub verify_password ($self, $user_id, $password) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
verify_password: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 820 | # Verifies if provided password is correct for the user | ||||||||||||||||||||||||||||||||||||||||||
| 821 | # Usually not needed - if user has valid session/user_key, they're already authenticated | ||||||||||||||||||||||||||||||||||||||||||
| 822 | # Use cases: sensitive operations requiring re-authentication, admin verification, etc. | ||||||||||||||||||||||||||||||||||||||||||
| 823 | # Most password resets don't need this - session authentication is sufficient | ||||||||||||||||||||||||||||||||||||||||||
| 824 | |||||||||||||||||||||||||||||||||||||||||||
| 825 | # Minimal validation - application controls when this is called | ||||||||||||||||||||||||||||||||||||||||||
| 826 | 13 | ▶ | return { success => 0, message => 'user_id is required' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $user_id and length $user_id
MC/DC100%2/2
defined $user_id and length $user_id
defined $user_id
length $user_id
Truth table100%3/3
A and B
A= defined $user_idB= length $user_id
| |||||||||||||||||||||||||||||||||||||||||||
| 827 | unless defined $user_id && length($user_id); | ||||||||||||||||||||||||||||||||||||||||||
| 828 | |||||||||||||||||||||||||||||||||||||||||||
| 829 | 11 | ▶ | return { success => 0, message => 'password is required' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $password and length $password
MC/DC100%2/2
defined $password and length $password
defined $password
length $password
Truth table100%3/3
A and B
A= defined $passwordB= length $password
| |||||||||||||||||||||||||||||||||||||||||||
| 830 | unless defined $password && length($password); | ||||||||||||||||||||||||||||||||||||||||||
| 831 | |||||||||||||||||||||||||||||||||||||||||||
| 832 | # Check password via Auth component | ||||||||||||||||||||||||||||||||||||||||||
| 833 | 9 | my $authed = $self->auth->authenticate($user_id, $password); | |||||||||||||||||||||||||||||||||||||||||
| 834 | 9 | my ($pwd_ok, $pwd_msg) = ($authed->{success}, $authed->{message}); | |||||||||||||||||||||||||||||||||||||||||
| 835 | |||||||||||||||||||||||||||||||||||||||||||
| 836 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 837 | 9 | ▶ | success => $pwd_ok ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$pwd_msg || ($pwd_ok ? 'Password verified' : 'Invalid password')
MC/DC0%0/2
$pwd_msg || ($pwd_ok ? 'Password verified' : 'Invalid password')
$pwd_msg
($pwd_ok ? 'Password verified' : 'Invalid password')
Truth table66%2/3
A || B
A= $pwd_msgB= ($pwd_ok ? 'Password verified' : 'Invalid password')
| |||||||||||||||||||||||||||||||||||||||||||
| 838 | message => $pwd_msg || ($pwd_ok ? 'Password verified' : 'Invalid password'), | ||||||||||||||||||||||||||||||||||||||||||
| 839 | user_id => $user_id, | ||||||||||||||||||||||||||||||||||||||||||
| 840 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 841 | } | ||||||||||||||||||||||||||||||||||||||||||
| 842 | |||||||||||||||||||||||||||||||||||||||||||
| 843 | # Reset password: set user's new password | ||||||||||||||||||||||||||||||||||||||||||
| 844 | 6 | ▶ | sub reset_password ($self, $user_id, $new_password) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reset_password: called CC=8 SCAR=21.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 845 | # Changes user password using Auth component | ||||||||||||||||||||||||||||||||||||||||||
| 846 | # Application is responsible for verifying user identity and old password if needed | ||||||||||||||||||||||||||||||||||||||||||
| 847 | |||||||||||||||||||||||||||||||||||||||||||
| 848 | 6 | ▶ | return { success => 0, message => 'user_id is required' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $user_id and length $user_id
MC/DC100%2/2
defined $user_id and length $user_id
defined $user_id
length $user_id
Truth table100%3/3
A and B
A= defined $user_idB= length $user_id
| |||||||||||||||||||||||||||||||||||||||||||
| 849 | unless defined $user_id && length($user_id); | ||||||||||||||||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||||||||||||||||
| 851 | 4 | ▶ | return { success => 0, message => 'new_password is required' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $new_password and length $new_password
MC/DC100%2/2
defined $new_password and length $new_password
defined $new_password
length $new_password
Truth table100%3/3
A and B
A= defined $new_passwordB= length $new_password
| |||||||||||||||||||||||||||||||||||||||||||
| 852 | unless defined $new_password && length($new_password); | ||||||||||||||||||||||||||||||||||||||||||
| 853 | |||||||||||||||||||||||||||||||||||||||||||
| 854 | # Reset existing password using Auth's change_credentials | ||||||||||||||||||||||||||||||||||||||||||
| 855 | # Pass through Auth's messages (Auth provides detailed error messages) | ||||||||||||||||||||||||||||||||||||||||||
| 856 | 2 | my $changed = $self->auth->change_credentials($user_id, $new_password); | |||||||||||||||||||||||||||||||||||||||||
| 857 | 2 | my ($reset_ok, $reset_msg) = ($changed->{success}, $changed->{message}); | |||||||||||||||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||||||||||||||||
| 859 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 860 | 2 | ▶ | success => $reset_ok ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$reset_msg || ($reset_ok ? 'Password reset successful' : 'Password reset failed')
MC/DC0%0/2
$reset_msg || ($reset_ok ? 'Password reset successful' : 'Password reset failed')
$reset_msg
($reset_ok ? 'Password reset successful' : 'Password reset failed')
Truth table33%1/3
A || B
A= $reset_msgB= ($reset_ok ? 'Password reset successful' : 'Password reset failed')
| |||||||||||||||||||||||||||||||||||||||||||
| 861 | message => $reset_msg || ($reset_ok ? 'Password reset successful' : 'Password reset failed'), | ||||||||||||||||||||||||||||||||||||||||||
| 862 | user_id => $user_id, | ||||||||||||||||||||||||||||||||||||||||||
| 863 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 864 | } | ||||||||||||||||||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||||||||||||||||
| 866 | # Logout user: delete session and remove from concierge mapping | ||||||||||||||||||||||||||||||||||||||||||
| 867 | 7 | ▶ | sub logout_user ($self, $session_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
logout_user: called CC=11 SCAR=25.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 868 | # Logs out user by deleting their session | ||||||||||||||||||||||||||||||||||||||||||
| 869 | # Removes from concierge user_keys mapping | ||||||||||||||||||||||||||||||||||||||||||
| 870 | |||||||||||||||||||||||||||||||||||||||||||
| 871 | 7 | ▶ | return { success => 0, message => 'No session provided for logout' } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $session_id and length $session_id
MC/DC100%2/2
defined $session_id and length $session_id
defined $session_id
length $session_id
Truth table100%3/3
A and B
A= defined $session_idB= length $session_id
| |||||||||||||||||||||||||||||||||||||||||||
| 872 | unless defined $session_id && length($session_id); | ||||||||||||||||||||||||||||||||||||||||||
| 873 | |||||||||||||||||||||||||||||||||||||||||||
| 874 | # Step 1: Verify session exists | ||||||||||||||||||||||||||||||||||||||||||
| 875 | 5 | my $session_check = $self->sessions->get_session($session_id); | |||||||||||||||||||||||||||||||||||||||||
| 876 | 5 | ▶ | unless ($session_check->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 877 | 1 | return { success => 0, message => 'Session not found for logout' }; | |||||||||||||||||||||||||||||||||||||||||
| 878 | } | ||||||||||||||||||||||||||||||||||||||||||
| 879 | |||||||||||||||||||||||||||||||||||||||||||
| 880 | # Step 2: Find user_key for this session (to remove from mapping) | ||||||||||||||||||||||||||||||||||||||||||
| 881 | 4 | my $user_key_to_delete; | |||||||||||||||||||||||||||||||||||||||||
| 882 | my $user_id; | ||||||||||||||||||||||||||||||||||||||||||
| 883 | |||||||||||||||||||||||||||||||||||||||||||
| 884 | 4 | for my $key (keys %{$self->{user_keys}}) { | |||||||||||||||||||||||||||||||||||||||||
| 885 | 4 | ▶ | if ($self->{user_keys}{$key}{session_id} eq $session_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 886 | 4 | $user_key_to_delete = $key; | |||||||||||||||||||||||||||||||||||||||||
| 887 | 4 | $user_id = $self->{user_keys}{$key}{user_id}; | |||||||||||||||||||||||||||||||||||||||||
| 888 | 4 | last; | |||||||||||||||||||||||||||||||||||||||||
| 889 | } | ||||||||||||||||||||||||||||||||||||||||||
| 890 | } | ||||||||||||||||||||||||||||||||||||||||||
| 891 | |||||||||||||||||||||||||||||||||||||||||||
| 892 | # Step 3: Delete session from Sessions component | ||||||||||||||||||||||||||||||||||||||||||
| 893 | 4 | my $delete_result = $self->sessions->delete_session($session_id); | |||||||||||||||||||||||||||||||||||||||||
| 894 | 4 | ▶ | unless ($delete_result->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 895 | 0 | ▶ | return { success => 0, message => $delete_result->{message} || 'Failed to delete session' }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$delete_result->{'message'} || 'Failed to delete session'
MC/DC0%0/1
$delete_result->{'message'} || 'Failed to delete session'
$delete_result->{'message'}
Truth table0%0/2
A
A= $delete_result->{'message'}
| |||||||||||||||||||||||||||||||||||||||||||
| 896 | } | ||||||||||||||||||||||||||||||||||||||||||
| 897 | |||||||||||||||||||||||||||||||||||||||||||
| 898 | # Step 4: Remove from concierge user_keys mapping | ||||||||||||||||||||||||||||||||||||||||||
| 899 | 4 | ▶ | if ($user_key_to_delete) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 900 | 4 | delete $self->{user_keys}{$user_key_to_delete}; | |||||||||||||||||||||||||||||||||||||||||
| 901 | 4 | my $save_result = $self->save_user_keys(); | |||||||||||||||||||||||||||||||||||||||||
| 902 | 4 | ▶ | unless ($save_result->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 903 | 0 | return { success => 0, message => 'Session deleted but failed to update mapping' }; | |||||||||||||||||||||||||||||||||||||||||
| 904 | } | ||||||||||||||||||||||||||||||||||||||||||
| 905 | } | ||||||||||||||||||||||||||||||||||||||||||
| 906 | |||||||||||||||||||||||||||||||||||||||||||
| 907 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 908 | 4 | ▶ | success => 1, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 909 | message => 'Logout successful', | ||||||||||||||||||||||||||||||||||||||||||
| 910 | session_id => $session_id, | ||||||||||||||||||||||||||||||||||||||||||
| 911 | ($user_id ? (user_id => $user_id) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 912 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 913 | } | ||||||||||||||||||||||||||||||||||||||||||
| 914 | |||||||||||||||||||||||||||||||||||||||||||
| 915 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||||||||||||||||
| 917 | __END__ | ||||||||||||||||||||||||||||||||||||||||||