| 1 | package Concierge::Desk::User 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: User object enabled by Concierge | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 9 | ▶ | use File::Spec; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 9 | ▶ | use File::Path qw/make_path/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | # === COMPONENT MODULES === | ||||||||||||||||||||
| 12 | 9 | ▶ | use Concierge::Auth::Generators qw(gen_random_string); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 9 | ▶ | use Concierge::Sessions; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 9 | ▶ | use Concierge::Users; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 34 | ▶ | sub enable_user { | ||||||||||||||||||
|
Subroutine
enable_user: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||
| 17 | 34 | my ($class, $user_id, $options) = @_; | |||||||||||||||||||
| 18 | 34 | ▶ | $options //= {}; # Default to empty hashref if not provided | ||||||||||||||||||
|
Condition100%2/2
$options //= {}
MC/DC100%1/1
$options //= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # $options = { | ||||||||||||||||||||
| 21 | # session => $session_obj, # optional - for guest/logged-in | ||||||||||||||||||||
| 22 | # user_data => $user_data_hash, # optional - for logged-in only | ||||||||||||||||||||
| 23 | # user_key => $external_key, # optional - reuse existing or generate new | ||||||||||||||||||||
| 24 | # _get_user_data => $closure, # optional - for logged-in users | ||||||||||||||||||||
| 25 | # _update_user_data => $closure, # optional - for logged-in users | ||||||||||||||||||||
| 26 | # _verify_password => $closure, # optional - for logged-in users | ||||||||||||||||||||
| 27 | # _reset_password => $closure, # optional - for logged-in users | ||||||||||||||||||||
| 28 | # _logout => $closure, # optional - for guest/logged-in (any user with a session) | ||||||||||||||||||||
| 29 | # _session_valid => $closure, # optional - for guest/logged-in (any user with a session) | ||||||||||||||||||||
| 30 | # } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | my $self = bless { | ||||||||||||||||||||
| 33 | user_id => $user_id, | ||||||||||||||||||||
| 34 | 34 | ▶ | user_key => $options->{user_key} || scalar(gen_random_string(13)), | ||||||||||||||||||
|
Condition100%2/2
$options->{'user_key'} || scalar gen_random_string(13)
MC/DC100%1/1
$options->{'user_key'} || scalar gen_random_string(13)
$options->{'user_key'}
Truth table100%2/2
A
A= $options->{'user_key'}
| |||||||||||||||||||||
| 35 | }, $class; | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | # Store session reference and ID if provided | ||||||||||||||||||||
| 38 | 34 | ▶ | if ($options->{session}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | 31 | $self->{session} = $options->{session}; | |||||||||||||||||||
| 40 | 31 | $self->{session_id} = $options->{session}->session_id(); | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | # Store user_data snapshot in memory if provided (logged-in users) | ||||||||||||||||||||
| 44 | 34 | ▶ | $self->{user_data} = $options->{user_data} if $options->{user_data}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | # Store closures for backend access (if logged-in user) | ||||||||||||||||||||
| 47 | 34 | ▶ | $self->{_get_user_data} = $options->{_get_user_data} if $options->{_get_user_data}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | 34 | ▶ | $self->{_update_user_data} = $options->{_update_user_data} if $options->{_update_user_data}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 34 | ▶ | $self->{_verify_password} = $options->{_verify_password} if $options->{_verify_password}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | 34 | ▶ | $self->{_reset_password} = $options->{_reset_password} if $options->{_reset_password}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 34 | ▶ | $self->{_logout} = $options->{_logout} if $options->{_logout}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 34 | ▶ | $self->{_session_valid} = $options->{_session_valid} if $options->{_session_valid}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | # Determine user type (for status methods) | ||||||||||||||||||||
| 55 | 34 | ▶ | $self->{is_visitor} = !$options->{session} && !$options->{user_data}; | ||||||||||||||||||
|
Condition66%2/3
$options->{'session'} || $options->{'user_data'}
MC/DC50%1/2
$options->{'session'} || $options->{'user_data'}
$options->{'session'}
$options->{'user_data'}
Truth table66%2/3
A || B
A= $options->{'session'}B= $options->{'user_data'}
| |||||||||||||||||||||
| 56 | 34 | ▶ | $self->{is_guest} = $options->{session} && !$options->{user_data}; | ||||||||||||||||||
|
Condition100%3/3
$options->{'session'} && !$options->{'user_data'}
MC/DC100%2/2
$options->{'session'} && !$options->{'user_data'}
$options->{'session'}
!$options->{'user_data'}
Truth table100%3/3
A && not(B)
A= $options->{'session'}B= !$options->{'user_data'}
| |||||||||||||||||||||
| 57 | 34 | ▶ | $self->{is_logged_in} = $options->{session} && $options->{user_data}; | ||||||||||||||||||
|
Condition100%3/3
$options->{'session'} && $options->{'user_data'}
MC/DC100%2/2
$options->{'session'} && $options->{'user_data'}
$options->{'session'}
$options->{'user_data'}
Truth table100%3/3
A && B
A= $options->{'session'}B= $options->{'user_data'}
| |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 34 | return $self; # Return blessed object directly (not wrapped) | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | # ============================================================================= | ||||||||||||||||||||
| 63 | # IDENTITY & METADATA - Direct scalar returns | ||||||||||||||||||||
| 64 | # ============================================================================= | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 9 | ▶ | sub user_id ($self) { | ||||||||||||||||||
|
Subroutine
user_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 67 | 9 | return $self->{user_id}; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 46 | ▶ | sub user_key ($self) { | ||||||||||||||||||
|
Subroutine
user_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 71 | 46 | return $self->{user_key}; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 18 | ▶ | sub session_id ($self) { | ||||||||||||||||||
|
Subroutine
session_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 75 | 18 | return $self->{session_id}; # undef if visitor/no session | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | # ============================================================================= | ||||||||||||||||||||
| 79 | # STATUS METHODS - Direct boolean returns | ||||||||||||||||||||
| 80 | # ============================================================================= | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 6 | ▶ | sub is_visitor ($self) { | ||||||||||||||||||
|
Subroutine
is_visitor: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 83 | 6 | ▶ | return $self->{is_visitor} ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 6 | ▶ | sub is_guest ($self) { | ||||||||||||||||||
|
Subroutine
is_guest: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 87 | 6 | ▶ | return $self->{is_guest} ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 9 | ▶ | sub is_logged_in ($self) { | ||||||||||||||||||
|
Subroutine
is_logged_in: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 91 | 9 | ▶ | return $self->{is_logged_in} ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | # ============================================================================= | ||||||||||||||||||||
| 95 | # SESSION ACCESS - Returns session object | ||||||||||||||||||||
| 96 | # ============================================================================= | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 9 | ▶ | sub session ($self) { | ||||||||||||||||||
|
Subroutine
session: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 99 | 9 | return $self->{session}; # undef if no session (visitor) | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | # Internal: is this object's session still live on the backend right now? | ||||||||||||||||||||
| 103 | # Deliberately a fresh backend check via the _session_valid closure, not a | ||||||||||||||||||||
| 104 | # check of cached in-memory state -- a *different* Concierge::Desk::User | ||||||||||||||||||||
| 105 | # instance for the same identity (e.g. from a second restore_user() call) | ||||||||||||||||||||
| 106 | # can still hold a populated (but now stale) session field after this | ||||||||||||||||||||
| 107 | # object's own logout() ran, and cached session status wouldn't catch that. | ||||||||||||||||||||
| 108 | 26 | ▶ | sub _session_ok ($self) { | ||||||||||||||||||
|
Subroutine
_session_ok: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 109 | 26 | ▶ | return 0 unless $self->{_session_valid}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 26 | my $result = $self->{_session_valid}->(); | |||||||||||||||||||
| 111 | 26 | ▶ | return $result->{success} ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 7 | ▶ | sub get_session_data ($self) { | ||||||||||||||||||
|
Subroutine
get_session_data: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 115 | 7 | ▶ | return undef unless $self->{session}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 6 | ▶ | return undef unless $self->_session_ok; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 5 | my $result = $self->{session}->get_data(); | |||||||||||||||||||
| 118 | 5 | ▶ | return $result->{value} // {}; | ||||||||||||||||||
|
Condition50%1/2
$result->{"value"} // {}
MC/DC0%0/1
$result->{"value"} // {}
$result->{"value"}
Truth table50%1/2
A
A= $result->{"value"}
| |||||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 6 | ▶ | sub update_session_data ($self, $updates) { | ||||||||||||||||||
|
Subroutine
update_session_data: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||
| 122 | 6 | ▶ | return undef unless $self->{session}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 6 | ▶ | return undef unless $self->_session_ok; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 5 | my $result = $self->{session}->get_data(); | |||||||||||||||||||
| 126 | 5 | ▶ | my $current = $result->{value} // {}; | ||||||||||||||||||
|
Condition50%1/2
$result->{"value"} // {}
MC/DC0%0/1
$result->{"value"} // {}
$result->{"value"}
Truth table50%1/2
A
A= $result->{"value"}
| |||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | # Merge updates into current data | ||||||||||||||||||||
| 129 | 5 | for my $key (keys %$updates) { | |||||||||||||||||||
| 130 | 6 | $current->{$key} = $updates->{$key}; | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 5 | $self->{session}->set_data($current); | |||||||||||||||||||
| 134 | 5 | $self->{session}->save(); | |||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 5 | return 1; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | # ============================================================================= | ||||||||||||||||||||
| 140 | # USER DATA - Quick access from memory snapshot | ||||||||||||||||||||
| 141 | # ============================================================================= | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 3 | ▶ | sub moniker ($self) { | ||||||||||||||||||
|
Subroutine
moniker: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 144 | 3 | return $self->{user_data}{moniker}; | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 1 | ▶ | sub email ($self) { | ||||||||||||||||||
|
Subroutine
email: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 148 | 1 | return $self->{user_data}{email}; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 1 | ▶ | sub user_status ($self) { | ||||||||||||||||||
|
Subroutine
user_status: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 152 | 1 | return $self->{user_data}{user_status}; | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 1 | ▶ | sub access_level ($self) { | ||||||||||||||||||
|
Subroutine
access_level: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 156 | 1 | return $self->{user_data}{access_level}; | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 3 | ▶ | sub get_user_field ($self, $field) { | ||||||||||||||||||
|
Subroutine
get_user_field: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 160 | 3 | return $self->{user_data}{$field}; | |||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | # ============================================================================= | ||||||||||||||||||||
| 164 | # USER DATA - Backend operations via closures | ||||||||||||||||||||
| 165 | # ============================================================================= | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | 4 | ▶ | sub refresh_user_data ($self) { | ||||||||||||||||||
|
Subroutine
refresh_user_data: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 168 | # Fetch fresh data from backend, update memory snapshot | ||||||||||||||||||||
| 169 | 4 | ▶ | return undef unless $self->{_get_user_data}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 170 | 3 | ▶ | return undef unless $self->_session_ok; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 2 | my $result = $self->{_get_user_data}->(); | |||||||||||||||||||
| 173 | 2 | ▶ | return undef unless $result->{success}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | 2 | $self->{user_data} = $result->{user}; | |||||||||||||||||||
| 176 | 2 | return 1; | |||||||||||||||||||
| 177 | } | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | 4 | ▶ | sub update_user_data ($self, $updates) { | ||||||||||||||||||
|
Subroutine
update_user_data: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 180 | # Update backend AND memory snapshot | ||||||||||||||||||||
| 181 | 4 | ▶ | return undef unless $self->{_update_user_data}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 182 | 3 | ▶ | return undef unless $self->_session_ok; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 2 | my $result = $self->{_update_user_data}->($updates); | |||||||||||||||||||
| 185 | 2 | ▶ | return undef unless $result->{success}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | # Update memory snapshot with new values | ||||||||||||||||||||
| 188 | 2 | for my $field (keys %$updates) { | |||||||||||||||||||
| 189 | 2 | $self->{user_data}{$field} = $updates->{$field}; | |||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 2 | return 1; | |||||||||||||||||||
| 193 | } | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | # ============================================================================= | ||||||||||||||||||||
| 196 | # PASSWORD & LOGOUT - Mirror operations via closures | ||||||||||||||||||||
| 197 | # ============================================================================= | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 9 | ▶ | sub verify_password ($self, $password) { | ||||||||||||||||||
|
Subroutine
verify_password: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 200 | 9 | ▶ | return undef unless $self->{_verify_password}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 201 | 6 | ▶ | return undef unless $self->_session_ok; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | 5 | my $result = $self->{_verify_password}->($password); | |||||||||||||||||||
| 204 | 5 | ▶ | return $result->{success} ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 205 | } | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | 5 | ▶ | sub reset_password ($self, $new_password) { | ||||||||||||||||||
|
Subroutine
reset_password: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 208 | 5 | ▶ | return undef unless $self->{_reset_password}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 209 | 2 | ▶ | return undef unless $self->_session_ok; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | 1 | my $result = $self->{_reset_password}->($new_password); | |||||||||||||||||||
| 212 | 1 | ▶ | return $result->{success} ? 1 : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 213 | } | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | 6 | ▶ | sub logout ($self) { | ||||||||||||||||||
|
Subroutine
logout: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 216 | 6 | ▶ | return undef unless $self->{_logout}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | 3 | my $result = $self->{_logout}->(); | |||||||||||||||||||
| 219 | 3 | ▶ | return undef unless $result->{success}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | # Concierge has already done the real work (session deleted, user_key | ||||||||||||||||||||
| 222 | # mapping removed). Clean up this object in place so any reference to | ||||||||||||||||||||
| 223 | # it the application is still holding degrades to visitor-equivalent | ||||||||||||||||||||
| 224 | # status rather than silently retaining backend-write capability or | ||||||||||||||||||||
| 225 | # reporting stale identity/session state. user_id and user_key are | ||||||||||||||||||||
| 226 | # left intact -- they're inert identity strings, not capabilities. | ||||||||||||||||||||
| 227 | 3 | delete $self->{$_} for qw( | |||||||||||||||||||
| 228 | _get_user_data _update_user_data | ||||||||||||||||||||
| 229 | _verify_password _reset_password _logout _session_valid | ||||||||||||||||||||
| 230 | session session_id user_data | ||||||||||||||||||||
| 231 | ); | ||||||||||||||||||||
| 232 | 3 | $self->{is_logged_in} = 0; | |||||||||||||||||||
| 233 | 3 | $self->{is_guest} = 0; | |||||||||||||||||||
| 234 | 3 | $self->{is_visitor} = 1; | |||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | 3 | return 1; | |||||||||||||||||||
| 237 | } | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | 9 | 1; | |||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | __END__ | ||||||||||||||||||||