| 1 | package Concierge::Users v0.9.4; | ||||||||||||||||||||||||||||||||||||||
| 2 | 8 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use Carp qw/ croak carp /; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use JSON::PP qw/ encode_json decode_json /; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use File::Path qw/ make_path /; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use parent qw/ Concierge::Users::Meta /; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||
| 10 | # ============================================================================== | ||||||||||||||||||||||||||||||||||||||
| 11 | # Setup Method - One-time configuration | ||||||||||||||||||||||||||||||||||||||
| 12 | # ============================================================================== | ||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | 92 | ▶ | sub setup { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
setup: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 15 | 92 | my ($class, $config) = @_; | |||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||
| 17 | 92 | ▶ | croak "Configuration must be a hash reference" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 18 | unless ref $config eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||
| 20 | # storage_dir is required - validate/create it FIRST before backend operations | ||||||||||||||||||||||||||||||||||||||
| 21 | my $storage_dir = $config->{storage_dir} | ||||||||||||||||||||||||||||||||||||||
| 22 | 91 | ▶ | or croak "Configuration must include 'storage_dir' parameter"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 23 | 89 | ▶ | unless (-d $storage_dir) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 24 | 20 | eval { make_path($storage_dir) }; | |||||||||||||||||||||||||||||||||||||
| 25 | 20 | ▶ | croak "Cannot create storage directory: $storage_dir\nError: $@" if $@; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||
| 28 | # Explicit backend selection is required | ||||||||||||||||||||||||||||||||||||||
| 29 | my $backend_class = $config->{backend_class} | ||||||||||||||||||||||||||||||||||||||
| 30 | 89 | ▶ | or croak "Configuration must include 'backend_class' parameter"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||
| 32 | # Load backend module | ||||||||||||||||||||||||||||||||||||||
| 33 | 87 | eval "require $backend_class"; | |||||||||||||||||||||||||||||||||||||
| 34 | return { | ||||||||||||||||||||||||||||||||||||||
| 35 | 87 | ▶ | success => 0, | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 36 | message => "Backend '$backend_class' not available: $@" | ||||||||||||||||||||||||||||||||||||||
| 37 | } if $@; | ||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||
| 39 | 85 | my $field_meta = Concierge::Users::Meta::init_field_meta($config); | |||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||
| 41 | # Merge original config with field_meta for backend configure() | ||||||||||||||||||||||||||||||||||||||
| 42 | # (backend needs storage_dir and other config options) | ||||||||||||||||||||||||||||||||||||||
| 43 | 85 | my $backend_config = { | |||||||||||||||||||||||||||||||||||||
| 44 | %$config, | ||||||||||||||||||||||||||||||||||||||
| 45 | %$field_meta, | ||||||||||||||||||||||||||||||||||||||
| 46 | }; | ||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||
| 48 | # Call backend configure() to create storage | ||||||||||||||||||||||||||||||||||||||
| 49 | 85 | my $configure_result = $backend_class->configure( $backend_config ); | |||||||||||||||||||||||||||||||||||||
| 50 | 85 | ▶ | return $configure_result unless $configure_result->{success}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||
| 52 | # Config file is always: storage_dir/users-config.json | ||||||||||||||||||||||||||||||||||||||
| 53 | 83 | my $config_file = "$storage_dir/users-config.json"; | |||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||
| 55 | # Build complete config structure for serialization | ||||||||||||||||||||||||||||||||||||||
| 56 | my $config_to_save = { | ||||||||||||||||||||||||||||||||||||||
| 57 | version => "$Concierge::Users::VERSION", | ||||||||||||||||||||||||||||||||||||||
| 58 | generated => Concierge::Users::Meta->current_timestamp(), | ||||||||||||||||||||||||||||||||||||||
| 59 | backend_module => $backend_class, | ||||||||||||||||||||||||||||||||||||||
| 60 | backend_config => $configure_result->{config}, | ||||||||||||||||||||||||||||||||||||||
| 61 | fields => $field_meta->{fields}, | ||||||||||||||||||||||||||||||||||||||
| 62 | field_definitions => $field_meta->{field_definitions}, | ||||||||||||||||||||||||||||||||||||||
| 63 | 83 | storage_initialized => 1, | |||||||||||||||||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||
| 66 | # Serialize and save JSON config | ||||||||||||||||||||||||||||||||||||||
| 67 | 83 | eval { | |||||||||||||||||||||||||||||||||||||
| 68 | 83 | ▶ | open my $fh, '>', $config_file or croak "Cannot open $config_file for writing: $!"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 69 | 83 | print {$fh} encode_json($config_to_save); | |||||||||||||||||||||||||||||||||||||
| 70 | 83 | close $fh; | |||||||||||||||||||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||||||||||||||||||||
| 72 | return { | ||||||||||||||||||||||||||||||||||||||
| 73 | 83 | ▶ | success => 0, | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 74 | message => "Failed to write config file: $config_file\nError: $@" | ||||||||||||||||||||||||||||||||||||||
| 75 | } if $@; | ||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||
| 77 | # Generate and save YAML config (human-readable reference) | ||||||||||||||||||||||||||||||||||||||
| 78 | 83 | my $yaml_file = "$storage_dir/users-config.yaml"; | |||||||||||||||||||||||||||||||||||||
| 79 | 83 | my $yaml_content = Concierge::Users::Meta::config_to_yaml($config_to_save, $storage_dir); | |||||||||||||||||||||||||||||||||||||
| 80 | 83 | eval { | |||||||||||||||||||||||||||||||||||||
| 81 | 83 | ▶ | open my $fh, '>', $yaml_file or croak "Cannot open $yaml_file for writing: $!"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 82 | 83 | print {$fh} $yaml_content; | |||||||||||||||||||||||||||||||||||||
| 83 | 83 | close $fh; | |||||||||||||||||||||||||||||||||||||
| 84 | 83 | chmod 0666, $yaml_file; # Writable - allows setup() to overwrite | |||||||||||||||||||||||||||||||||||||
| 85 | }; | ||||||||||||||||||||||||||||||||||||||
| 86 | return { | ||||||||||||||||||||||||||||||||||||||
| 87 | 83 | ▶ | success => 0, | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 88 | message => "Failed to write YAML config file: $yaml_file\nError: $@" | ||||||||||||||||||||||||||||||||||||||
| 89 | } if $@; | ||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||
| 91 | return { | ||||||||||||||||||||||||||||||||||||||
| 92 | 83 | success => 1, | |||||||||||||||||||||||||||||||||||||
| 93 | message => "Users system configured successfully", | ||||||||||||||||||||||||||||||||||||||
| 94 | config_file => $config_file, | ||||||||||||||||||||||||||||||||||||||
| 95 | yaml_file => $yaml_file, | ||||||||||||||||||||||||||||||||||||||
| 96 | }; | ||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||
| 99 | # ============================================================================== | ||||||||||||||||||||||||||||||||||||||
| 100 | # Constructor - Load from saved config | ||||||||||||||||||||||||||||||||||||||
| 101 | # ============================================================================== | ||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||
| 103 | 86 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 104 | 86 | my ($class, $config_file) = @_; | |||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||
| 106 | 86 | ▶ | croak "Usage: Concierge::Users->new('/path/to/users-config.json')" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$config_file and -f $config_file
MC/DC100%2/2
$config_file and -f $config_file
$config_file
-f $config_file
Truth table100%3/3
A and B
A= $config_fileB= -f $config_file
| |||||||||||||||||||||||||||||||||||||||
| 107 | . "\nCall Concierge::Users->setup() first with configuration to create the config file" | ||||||||||||||||||||||||||||||||||||||
| 108 | unless $config_file && -f $config_file; | ||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||
| 110 | # Load and deserialize config | ||||||||||||||||||||||||||||||||||||||
| 111 | 84 | my $config_json; | |||||||||||||||||||||||||||||||||||||
| 112 | 84 | eval { | |||||||||||||||||||||||||||||||||||||
| 113 | 84 | ▶ | open my $fh, '<', $config_file or croak "Cannot open $config_file: $!"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 114 | 84 | local $/; # slurp mode | |||||||||||||||||||||||||||||||||||||
| 115 | 84 | $config_json = <$fh>; | |||||||||||||||||||||||||||||||||||||
| 116 | 84 | close $fh; | |||||||||||||||||||||||||||||||||||||
| 117 | }; | ||||||||||||||||||||||||||||||||||||||
| 118 | 84 | ▶ | croak "Failed to read config file: $config_file\nError: $@" if $@; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||
| 120 | 84 | my $saved_config; | |||||||||||||||||||||||||||||||||||||
| 121 | 84 | eval { | |||||||||||||||||||||||||||||||||||||
| 122 | 84 | $saved_config = decode_json($config_json); | |||||||||||||||||||||||||||||||||||||
| 123 | }; | ||||||||||||||||||||||||||||||||||||||
| 124 | 84 | ▶ | croak "Failed to parse config file: $config_file\nError: $@" if $@; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||
| 126 | # Validate config structure | ||||||||||||||||||||||||||||||||||||||
| 127 | croak "Invalid config file: missing 'backend_module' or 'fields'" | ||||||||||||||||||||||||||||||||||||||
| 128 | 83 | ▶ | unless $saved_config->{backend_module} && $saved_config->{fields}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$saved_config->{"backend_module"} and $saved_config->{"fields"}
MC/DC50%1/2
$saved_config->{"backend_module"} and $saved_config->{"fields"}
$saved_config->{"backend_module"}
$saved_config->{"fields"}
Truth table66%2/3
A and B
A= $saved_config->{"backend_module"}B= $saved_config->{"fields"}
| |||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||
| 130 | # Load backend module | ||||||||||||||||||||||||||||||||||||||
| 131 | 82 | my $backend_module = $saved_config->{backend_module}; | |||||||||||||||||||||||||||||||||||||
| 132 | 82 | eval "require $backend_module"; | |||||||||||||||||||||||||||||||||||||
| 133 | 82 | ▶ | croak "Backend '$backend_module' not available: $@" if $@; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||
| 135 | # Instantiate backend with its config (no fields needed for runtime) | ||||||||||||||||||||||||||||||||||||||
| 136 | 82 | my $backend_obj = $backend_module->new($saved_config->{backend_config}); | |||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | # Create Users object - just store what's needed for API operations | ||||||||||||||||||||||||||||||||||||||
| 139 | my $self = bless { | ||||||||||||||||||||||||||||||||||||||
| 140 | backend => $backend_obj, | ||||||||||||||||||||||||||||||||||||||
| 141 | fields => $saved_config->{fields}, | ||||||||||||||||||||||||||||||||||||||
| 142 | field_definitions => $saved_config->{field_definitions}, | ||||||||||||||||||||||||||||||||||||||
| 143 | 82 | }, $class; | |||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||
| 145 | 82 | return $self; | |||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||
| 148 | # ============================================================================== | ||||||||||||||||||||||||||||||||||||||
| 149 | # Public API Methods | ||||||||||||||||||||||||||||||||||||||
| 150 | # ============================================================================== | ||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||
| 152 | # Register a new user | ||||||||||||||||||||||||||||||||||||||
| 153 | 149 | ▶ | sub register_user { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
register_user: called CC=18 SCAR=29.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 154 | 149 | my ($self, $user_data) = @_; | |||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||
| 156 | 149 | ▶ | return { success => 0, message => "User data must be a hash reference" } | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 157 | unless ref $user_data eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||
| 159 | # Clone user_data to avoid modifying caller's hashref | ||||||||||||||||||||||||||||||||||||||
| 160 | 147 | my $data = { %$user_data }; | |||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||
| 162 | # 0. Clean $data | ||||||||||||||||||||||||||||||||||||||
| 163 | # Delete any data for system timestamps | ||||||||||||||||||||||||||||||||||||||
| 164 | 147 | delete $data->{$_} for qw/created_date last_mod_date/; | |||||||||||||||||||||||||||||||||||||
| 165 | # Define undefined values and remove leading and trailing whitespace | ||||||||||||||||||||||||||||||||||||||
| 166 | 147 | for my $f (keys $data->%*) { | |||||||||||||||||||||||||||||||||||||
| 167 | 471 | ▶ | $data->{$f} //= ''; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$data->{$f} //= ""
MC/DC100%1/1
$data->{$f} //= ""
$data->{$f}
Truth table100%2/2
A
A= $data->{$f}
| |||||||||||||||||||||||||||||||||||||||
| 168 | 471 | $data->{$f} =~ s/^\s*|\s*$//g; | |||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||
| 171 | # 1. Validate user_id, including allowing email address as ID | ||||||||||||||||||||||||||||||||||||||
| 172 | return { success => 0, message => "user_id is required as 2-30 characters, email OK, no spaces" } | ||||||||||||||||||||||||||||||||||||||
| 173 | unless $data->{user_id} | ||||||||||||||||||||||||||||||||||||||
| 174 | 147 | ▶ | && $data->{user_id} =~ /^[a-zA-Z0-9._@-]{2,30}$/; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$data->{"user_id"} and $data->{"user_id"} =~ /^[a-zA-Z0-9._\@-]{2,30}$/u
MC/DC0%0/2
$data->{"user_id"} and $data->{"user_id"} =~ /^[a-zA-Z0-9._\@-]{2,30}$/u
$data->{"user_id"}
$data->{"user_id"} =~ /^[a-zA-Z0-9._\@-]{2,30}$/u
Truth table33%1/3
A and B
A= $data->{"user_id"}B= $data->{"user_id"} =~ /^[a-zA-Z0-9._\@-]{2,30}$/u
| |||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||
| 176 | # 2. Validate moniker | ||||||||||||||||||||||||||||||||||||||
| 177 | return { success => 0, message => "moniker is required as 2-24 alphanumeric characters, no spaces" } | ||||||||||||||||||||||||||||||||||||||
| 178 | unless $data->{moniker} | ||||||||||||||||||||||||||||||||||||||
| 179 | 147 | ▶ | && $data->{moniker} =~ /^[a-zA-Z0-9]{2,24}$/; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$data->{"moniker"} and $data->{"moniker"} =~ /^[a-zA-Z0-9]{2,24}$/u
MC/DC100%2/2
$data->{"moniker"} and $data->{"moniker"} =~ /^[a-zA-Z0-9]{2,24}$/u
$data->{"moniker"}
$data->{"moniker"} =~ /^[a-zA-Z0-9]{2,24}$/u
Truth table100%3/3
A and B
A= $data->{"moniker"}B= $data->{"moniker"} =~ /^[a-zA-Z0-9]{2,24}$/u
| |||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||
| 181 | # 3. Check if user already exists | ||||||||||||||||||||||||||||||||||||||
| 182 | 144 | my $existing = $self->get_user($data->{user_id}); | |||||||||||||||||||||||||||||||||||||
| 183 | return { success => 0, message => "User '$data->{user_id}' already exists" } | ||||||||||||||||||||||||||||||||||||||
| 184 | 144 | ▶ | if $existing->{success}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||
| 186 | # 4. Store user_id and moniker, then remove from data for further processing | ||||||||||||||||||||||||||||||||||||||
| 187 | 142 | my $new_user_id = delete $data->{user_id}; | |||||||||||||||||||||||||||||||||||||
| 188 | my $user_init_record = { | ||||||||||||||||||||||||||||||||||||||
| 189 | user_id => $new_user_id, | ||||||||||||||||||||||||||||||||||||||
| 190 | moniker => delete $data->{moniker}, | ||||||||||||||||||||||||||||||||||||||
| 191 | 142 | }; | |||||||||||||||||||||||||||||||||||||
| 192 | 142 | for my $field (@{$self->{fields}}) { | |||||||||||||||||||||||||||||||||||||
| 193 | # Skip user_id and moniker - already set | ||||||||||||||||||||||||||||||||||||||
| 194 | 1390 | ▶ | next if $field eq 'user_id' || $field eq 'moniker'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$field eq "user_id" or $field eq "moniker"
MC/DC100%2/2
$field eq "user_id" or $field eq "moniker"
$field eq "user_id"
$field eq "moniker"
Truth table100%3/3
A or B
A= $field eq "user_id"B= $field eq "moniker"
| |||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||
| 196 | # Get field definition | ||||||||||||||||||||||||||||||||||||||
| 197 | 1106 | my $def = $self->{field_definitions}->{$field}; | |||||||||||||||||||||||||||||||||||||
| 198 | # Apply default for new records if it is defined | ||||||||||||||||||||||||||||||||||||||
| 199 | 1106 | ▶ | if (defined $def->{default}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 200 | 1103 | $user_init_record->{$field} = $def->{default}; | |||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||
| 202 | # Otherwise apply null_value for record initialization | ||||||||||||||||||||||||||||||||||||||
| 203 | elsif (defined $def->{null_value}) { | ||||||||||||||||||||||||||||||||||||||
| 204 | 3 | $user_init_record->{$field} = $def->{null_value}; | |||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||
| 206 | else { | ||||||||||||||||||||||||||||||||||||||
| 207 | 0 | $user_init_record->{$field} = ''; | |||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||
| 210 | 142 | my $result = $self->{backend}->add( $new_user_id, $user_init_record ); | |||||||||||||||||||||||||||||||||||||
| 211 | 142 | ▶ | return $result unless $result->{success}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||
| 213 | # 5. Validate | ||||||||||||||||||||||||||||||||||||||
| 214 | 142 | my $validation = $self->validate_user_data( $data ); | |||||||||||||||||||||||||||||||||||||
| 215 | 142 | ▶ | return $validation unless $validation->{success}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 216 | # Proceed only with validated data | ||||||||||||||||||||||||||||||||||||||
| 217 | 138 | my $validated_user_data = $validation->{valid_data}; | |||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||
| 219 | # 6. Populate the record with validated user data | ||||||||||||||||||||||||||||||||||||||
| 220 | 138 | $result = $self->{backend}->update( $new_user_id, $validated_user_data ); | |||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||
| 222 | # Override message to indicate creation rather than update | ||||||||||||||||||||||||||||||||||||||
| 223 | 138 | $result->{message} = "User '$new_user_id' created"; | |||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||
| 225 | # Add warnings to result if any | ||||||||||||||||||||||||||||||||||||||
| 226 | 138 | ▶ | $result->{warnings} = $validation->{warnings} if $validation->{warnings}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||
| 228 | 138 | return $result; | |||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||
| 231 | # Get user by ID | ||||||||||||||||||||||||||||||||||||||
| 232 | 229 | ▶ | sub get_user { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_user: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 233 | 229 | my ($self, $user_id, $options) = @_; | |||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||
| 235 | 229 | ▶ | return { success => 0, message => "user_id is required" } | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$user_id and $user_id =~ /\S/u
MC/DC100%2/2
$user_id and $user_id =~ /\S/u
$user_id
$user_id =~ /\S/u
Truth table100%3/3
A and B
A= $user_idB= $user_id =~ /\S/u
| |||||||||||||||||||||||||||||||||||||||
| 236 | unless $user_id && $user_id =~ /\S/; | ||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||
| 238 | 226 | ▶ | $options ||= {}; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options ||= {}
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||
| 240 | 226 | my $fetch_result = $self->{backend}->fetch($user_id); | |||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||
| 242 | 226 | ▶ | unless ($fetch_result->{success}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 243 | 156 | return { success => 0, message => $fetch_result->{message} }; | |||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||
| 246 | 70 | my $user_data = $fetch_result->{data}; | |||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||
| 248 | # Handle field selection | ||||||||||||||||||||||||||||||||||||||
| 249 | 70 | ▶ | if ($options->{fields} && ref $options->{fields} eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$options->{"fields"} and ref $options->{"fields"} eq "ARRAY"
MC/DC50%1/2
$options->{"fields"} and ref $options->{"fields"} eq "ARRAY"
$options->{"fields"}
ref $options->{"fields"} eq "ARRAY"
Truth table66%2/3
A and B
A= $options->{"fields"}B= ref $options->{"fields"} eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||
| 250 | 3 | my %selected; | |||||||||||||||||||||||||||||||||||||
| 251 | 3 | $selected{$_} = $user_data->{$_} for @{$options->{fields}}; | |||||||||||||||||||||||||||||||||||||
| 252 | 3 | $selected{user_id} = $user_data->{user_id}; # Always include user_id | |||||||||||||||||||||||||||||||||||||
| 253 | 3 | $user_data = \%selected; | |||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||
| 256 | return { | ||||||||||||||||||||||||||||||||||||||
| 257 | 70 | success => 1, | |||||||||||||||||||||||||||||||||||||
| 258 | user_id => $user_id, | ||||||||||||||||||||||||||||||||||||||
| 259 | user => $user_data | ||||||||||||||||||||||||||||||||||||||
| 260 | }; | ||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||
| 263 | # Update user | ||||||||||||||||||||||||||||||||||||||
| 264 | 21 | ▶ | sub update_user { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
update_user: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 265 | 21 | my ($self, $user_id, $updates) = @_; | |||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||
| 267 | 21 | ▶ | return { success => 0, message => "user_id is required" } | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$user_id and $user_id =~ /\S/u
MC/DC50%1/2
$user_id and $user_id =~ /\S/u
$user_id
$user_id =~ /\S/u
Truth table66%2/3
A and B
A= $user_idB= $user_id =~ /\S/u
| |||||||||||||||||||||||||||||||||||||||
| 268 | unless $user_id && $user_id =~ /\S/; | ||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||
| 270 | 20 | ▶ | return { success => 0, message => "Updates must be a hash reference" } | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 271 | unless ref $updates eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||
| 273 | # Check if user exists | ||||||||||||||||||||||||||||||||||||||
| 274 | 18 | my $existing = $self->get_user($user_id); | |||||||||||||||||||||||||||||||||||||
| 275 | 18 | ▶ | unless ($existing->{success}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 276 | 4 | return { success => 0, message => "User '$user_id' not found" }; | |||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||
| 279 | # 0. Clean $updates | ||||||||||||||||||||||||||||||||||||||
| 280 | # Delete any data for user_id and system timestamps | ||||||||||||||||||||||||||||||||||||||
| 281 | 14 | delete $updates->{$_} for qw/user_id created_date last_mod_date/; | |||||||||||||||||||||||||||||||||||||
| 282 | # Define undefined values and remove leading and trailing whitespace | ||||||||||||||||||||||||||||||||||||||
| 283 | 14 | for my $f (keys $updates->%*) { | |||||||||||||||||||||||||||||||||||||
| 284 | 19 | ▶ | $updates->{$f} //= ''; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$updates->{$f} //= ""
MC/DC0%0/1
$updates->{$f} //= ""
$updates->{$f}
Truth table50%1/2
A
A= $updates->{$f}
| |||||||||||||||||||||||||||||||||||||||
| 285 | 19 | $updates->{$f} =~ s/^\s*|\s*$//g; | |||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||
| 288 | # 1. Validate | ||||||||||||||||||||||||||||||||||||||
| 289 | 14 | my $validation = $self->validate_user_data( $updates ); | |||||||||||||||||||||||||||||||||||||
| 290 | 14 | ▶ | return $validation unless $validation->{success}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 291 | # Proceed only with validated data | ||||||||||||||||||||||||||||||||||||||
| 292 | 13 | my $validated_updates = $validation->{valid_data}; | |||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||
| 294 | # 2. Populate the record with user data | ||||||||||||||||||||||||||||||||||||||
| 295 | 13 | my $result = $self->{backend}->update( $user_id, $validated_updates ); | |||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||
| 297 | # Add warnings to result if any | ||||||||||||||||||||||||||||||||||||||
| 298 | 13 | ▶ | if ($validation->{warnings}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 299 | 1 | $result->{warnings} = $validation->{warnings}; | |||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||
| 302 | 13 | return $result; | |||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||
| 305 | # List users - only returns user_ids with optional filtering | ||||||||||||||||||||||||||||||||||||||
| 306 | 20 | ▶ | sub list_users { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
list_users: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 307 | 20 | my ($self, $filter_string) = @_; | |||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||
| 309 | # Parse filter string if provided | ||||||||||||||||||||||||||||||||||||||
| 310 | 20 | my $filters = {}; | |||||||||||||||||||||||||||||||||||||
| 311 | 20 | ▶ | if ($filter_string && $filter_string =~ /\S/) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$filter_string and $filter_string =~ /\S/u
MC/DC50%1/2
$filter_string and $filter_string =~ /\S/u
$filter_string
$filter_string =~ /\S/u
Truth table66%2/3
A and B
A= $filter_stringB= $filter_string =~ /\S/u
| |||||||||||||||||||||||||||||||||||||||
| 312 | 5 | $filters = $self->parse_filter_string($filter_string); | |||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||
| 315 | 20 | my $users = $self->{backend}->list($filters, {}); | |||||||||||||||||||||||||||||||||||||
| 316 | 20 | ▶ | my @user_ids = map { $_->{user_id} } @{$users->{data} || []}; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$users->{'data'} || []
MC/DC0%0/1
$users->{'data'} || []
$users->{'data'}
Truth table50%1/2
A
A= $users->{'data'}
| |||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||
| 318 | return { | ||||||||||||||||||||||||||||||||||||||
| 319 | success => 1, | ||||||||||||||||||||||||||||||||||||||
| 320 | user_ids => \@user_ids, | ||||||||||||||||||||||||||||||||||||||
| 321 | 20 | ▶ | total_count => $users->{total_count} || 0, | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
$users->{'total_count'} || 0
$filter_string and $filter_string =~ /\S/u
MC/DC100%1/1
$users->{'total_count'} || 0
$users->{'total_count'}
MC/DC50%1/2
$filter_string and $filter_string =~ /\S/u
$filter_string
$filter_string =~ /\S/u
Truth table100%2/2
A
A= $users->{'total_count'}
Truth table66%2/3
A and B
A= $filter_stringB= $filter_string =~ /\S/u
| |||||||||||||||||||||||||||||||||||||||
| 322 | filter_applied => ($filter_string && $filter_string =~ /\S/) ? $filter_string : '', | ||||||||||||||||||||||||||||||||||||||
| 323 | }; | ||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||
| 326 | # Delete user | ||||||||||||||||||||||||||||||||||||||
| 327 | 10 | ▶ | sub delete_user { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_user: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 328 | 10 | my ($self, $user_id) = @_; | |||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||
| 330 | 10 | ▶ | return { success => 0, message => "user_id is required" } | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$user_id and $user_id =~ /\S/u
MC/DC50%1/2
$user_id and $user_id =~ /\S/u
$user_id
$user_id =~ /\S/u
Truth table66%2/3
A and B
A= $user_idB= $user_id =~ /\S/u
| |||||||||||||||||||||||||||||||||||||||
| 331 | unless $user_id && $user_id =~ /\S/; | ||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||
| 333 | # Check if user exists | ||||||||||||||||||||||||||||||||||||||
| 334 | 8 | my $existing = $self->get_user($user_id); | |||||||||||||||||||||||||||||||||||||
| 335 | 8 | ▶ | unless ($existing->{success}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 336 | 3 | return { success => 0, message => "User '$user_id' not found" }; | |||||||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||
| 339 | # Delete using backend | ||||||||||||||||||||||||||||||||||||||
| 340 | 5 | my $result = $self->{backend}->delete($user_id); | |||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||
| 342 | 5 | return $result; | |||||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||
| 345 | # Utility methods | ||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||
| 347 | # Cleanup | ||||||||||||||||||||||||||||||||||||||
| 348 | 87 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||
| 349 | 87 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||
| 351 | # Disconnect backend if it has a disconnect method | ||||||||||||||||||||||||||||||||||||||
| 352 | 87 | ▶ | if ($self->{backend} && $self->{backend}->can('disconnect')) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"backend"} and $self->{"backend"}->can("disconnect")
MC/DC100%2/2
$self->{"backend"} and $self->{"backend"}->can("disconnect")
$self->{"backend"}
$self->{"backend"}->can("disconnect")
Truth table100%3/3
A and B
A= $self->{"backend"}B= $self->{"backend"}->can("disconnect")
| |||||||||||||||||||||||||||||||||||||||
| 353 | 82 | $self->{backend}->disconnect(); | |||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||
| 357 | 8 | 1; | |||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||
| 359 | __END__ | ||||||||||||||||||||||||||||||||||||||