Concierge-Users
view release on metacpan or search on metacpan
lib/Concierge/Users.pm view on Meta::CPAN
my $back = lc $backend_type;
$back eq 'database' ? 'Database' :
$back eq 'file' ? 'File' :
$back eq 'yaml' ? 'YAML' :
croak "Invalid backend type: $backend_type (must be 'database', 'file', or 'yaml')";
};
# Load backend module
my $backend_class = "Concierge::Users::${backend}";
eval "require $backend_class";
return {
success => 0,
message => "Backend '$backend_type' not available: $@"
} if $@;
my $field_meta = Concierge::Users::Meta::init_field_meta($config);
# Merge original config with field_meta for backend configure()
# (backend needs storage_dir and other config options)
my $backend_config = {
%$config,
%$field_meta,
};
# Call backend configure() to create storage
my $configure_result = $backend_class->configure( $backend_config );
return $configure_result unless $configure_result->{success};
# Config file is always: storage_dir/users-config.json
my $config_file = "$storage_dir/users-config.json";
# Build complete config structure for serialization
my $config_to_save = {
version => "$Concierge::Users::VERSION",
generated => Concierge::Users::Meta->current_timestamp(),
backend_module => "Concierge::Users::${backend}",
backend_config => $configure_result->{config},
fields => $field_meta->{fields},
field_definitions => $field_meta->{field_definitions},
storage_initialized => 1,
};
# Serialize and save JSON config
eval {
open my $fh, '>', $config_file or croak "Cannot open $config_file for writing: $!";
print {$fh} encode_json($config_to_save);
close $fh;
};
return {
success => 0,
message => "Failed to write config file: $config_file\nError: $@"
} if $@;
# Generate and save YAML config (human-readable reference)
my $yaml_file = "$storage_dir/users-config.yaml";
my $yaml_content = Concierge::Users::Meta::config_to_yaml($config_to_save, $storage_dir);
eval {
open my $fh, '>', $yaml_file or croak "Cannot open $yaml_file for writing: $!";
print {$fh} $yaml_content;
close $fh;
chmod 0666, $yaml_file; # Writable - allows setup() to overwrite
};
return {
success => 0,
message => "Failed to write YAML config file: $yaml_file\nError: $@"
} if $@;
return {
success => 1,
message => "Users system configured successfully",
config_file => $config_file,
yaml_file => $yaml_file,
};
}
# ==============================================================================
# Constructor - Load from saved config
# ==============================================================================
sub new {
my ($class, $config_file) = @_;
croak "Usage: Concierge::Users->new('/path/to/users-config.json')"
. "\nCall Concierge::Users->setup() first with configuration to create the config file"
unless $config_file && -f $config_file;
# Load and deserialize config
my $config_json;
eval {
open my $fh, '<', $config_file or croak "Cannot open $config_file: $!";
local $/; # slurp mode
$config_json = <$fh>;
close $fh;
};
croak "Failed to read config file: $config_file\nError: $@" if $@;
my $saved_config;
eval {
$saved_config = decode_json($config_json);
};
croak "Failed to parse config file: $config_file\nError: $@" if $@;
# Validate config structure
croak "Invalid config file: missing 'backend_module' or 'fields'"
unless $saved_config->{backend_module} && $saved_config->{fields};
# Load backend module
my $backend_module = $saved_config->{backend_module};
eval "require $backend_module";
croak "Backend '$backend_module' not available: $@" if $@;
# Instantiate backend with its config (no fields needed for runtime)
my $backend_obj = $backend_module->new($saved_config->{backend_config});
# Create Users object - just store what's needed for API operations
my $self = bless {
backend => $backend_obj,
fields => $saved_config->{fields},
field_definitions => $saved_config->{field_definitions},
}, $class;
( run in 0.943 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )