Serialization moved to conf modules (to be able to use another serialization)

This commit is contained in:
Xavier Guimard 2009-10-20 13:20:53 +00:00
parent cd60098695
commit 7553d5b95c
5 changed files with 94 additions and 66 deletions

View File

@ -12,6 +12,7 @@ lib/Lemonldap/NG/Common/Conf/DBI.pm
lib/Lemonldap/NG/Common/Conf/File.pm lib/Lemonldap/NG/Common/Conf/File.pm
lib/Lemonldap/NG/Common/Conf/LDAP.pm lib/Lemonldap/NG/Common/Conf/LDAP.pm
lib/Lemonldap/NG/Common/Conf/SOAP.pm lib/Lemonldap/NG/Common/Conf/SOAP.pm
lib/Lemonldap/NG/Common/Conf/Serializer.pm
lib/Lemonldap/NG/Common/Crypto.pm lib/Lemonldap/NG/Common/Crypto.pm
lib/Lemonldap/NG/Common/Safelib.pm lib/Lemonldap/NG/Common/Safelib.pm
Makefile.PL Makefile.PL

View File

@ -9,7 +9,6 @@ package Lemonldap::NG::Common::Conf;
use strict; use strict;
no strict 'refs'; no strict 'refs';
use Data::Dumper;
use Lemonldap::NG::Common::Conf::Constants; #inherits use Lemonldap::NG::Common::Conf::Constants; #inherits
use Lemonldap::NG::Common::Crypto; #link protected cipher Object "cypher" in configuration hash use Lemonldap::NG::Common::Crypto; #link protected cipher Object "cypher" in configuration hash
use Regexp::Assemble; use Regexp::Assemble;
@ -55,9 +54,12 @@ sub new {
unless ( $self->{mdone} ) { unless ( $self->{mdone} ) {
$self->_readConfFile( $self->{confFile} ) unless ( $self->{type} ); $self->_readConfFile( $self->{confFile} ) unless ( $self->{type} );
unless ( $self->{type} ) { unless ( $self->{type} ) {
$msg .= "Error: configStorage: type is not defined\n"; $msg .= 'Error: configStorage: type is not defined';
return 0; return 0;
} }
unless ( $self->{type} =~ /^[\w:]+$/ ) {
$msg .= "Error: configStorage: type is not well formed";
}
$self->{type} = "Lemonldap::NG::Common::Conf::$self->{type}" $self->{type} = "Lemonldap::NG::Common::Conf::$self->{type}"
unless $self->{type} =~ /^Lemonldap::/; unless $self->{type} =~ /^Lemonldap::/;
eval "require $self->{type}"; eval "require $self->{type}";
@ -126,32 +128,9 @@ sub saveConf {
return CONFIG_WAS_CHANGED return CONFIG_WAS_CHANGED
if ( $conf->{cfgNum} != $self->lastCfg or $self->isLocked ); if ( $conf->{cfgNum} != $self->lastCfg or $self->isLocked );
$self->lock or return DATABASE_LOCKED; $self->lock or return DATABASE_LOCKED;
my $fields; $conf->{cfgNum}++;
local $Data::Dumper::Indent = 0; $msg = "Configuration $conf->{cfgNum} stored";
local $Data::Dumper::Varname = "data"; return $self->store($conf);
while ( my ( $k, $v ) = each(%$conf) ) {
next if ( $k =~ /^(?:reVHosts|cipher)$/ );
if ( ref($v) ) {
$fields->{$k} = Dumper($v);
$fields->{$k} =~ s/'/'/g;
$fields->{$k} = "'$fields->{$k}'";
}
elsif ( $v =~ /^\d+$/ ) {
$fields->{$k} = "$v";
}
else {
# mono-line
$v =~ s/[\r\n]/ /gm;
# trim
$v =~ s/^\s*(.*?)\s*$/$1/;
$fields->{$k} = "'$v'";
}
}
$fields->{cfgNum} = $self->lastCfg + 1;
$msg = "Configuration $fields->{cfgNum} stored";
return $self->store($fields);
} }
## @method hashRef getConf(hashRef args) ## @method hashRef getConf(hashRef args)
@ -234,41 +213,7 @@ sub getDBConf {
? ( $a[ $#a + $args->{cfgNum} ] ) ? ( $a[ $#a + $args->{cfgNum} ] )
: $a[0]; : $a[0];
} }
my $fields = $self->load( $args->{cfgNum} ); my $conf = $self->load( $args->{cfgNum} );
my $conf;
while ( my ( $k, $v ) = each(%$fields) ) {
$v =~ s/^'(.*)'$/$1/s;
if ( $k =~
/^(?:exportedVars|locationRules|groups|exportedHeaders|macros|globalStorageOptions)$/
and $v ||= {}
and not ref($v) )
{
$conf->{$k} = {};
if ( defined($v) and $v !~ /^\$/ ) {
print STDERR
"Lemonldap::NG : Warning: configuration is in old format, you've to migrate !\n";
eval { require Storable; require MIME::Base64; };
if ($@) {
$msg = "Error : $@";
return 0;
}
$conf->{$k} = Storable::thaw( MIME::Base64::decode_base64($v) );
}
else {
my $data;
$v =~ s/^\$([_a-zA-Z][_a-zA-Z0-9]*) *=/\$data =/;
$v =~ s/&#?39;/'/g;
eval $v;
print STDERR
"Lemonldap::NG : Error while reading configuration with $k key: $@\n"
if ($@);
$conf->{$k} = $data;
}
}
else {
$conf->{$k} = $v;
}
}
$msg = "Get configuration $conf->{cfgNum}"; $msg = "Get configuration $conf->{cfgNum}";
my $re = Regexp::Assemble->new(); my $re = Regexp::Assemble->new();
foreach ( keys %{ $conf->{locationRules} } ) { foreach ( keys %{ $conf->{locationRules} } ) {

View File

@ -3,7 +3,8 @@ package Lemonldap::NG::Common::Conf::DBI;
use strict; use strict;
use DBI; use DBI;
use MIME::Base64; use MIME::Base64;
use Lemonldap::NG::Common::Conf::Constants; use Lemonldap::NG::Common::Conf::Constants; #inherits
use Lemonldap::NG::Common::Conf::Serializer;
our $VERSION = 0.17; our $VERSION = 0.17;
@ -79,6 +80,7 @@ sub unlock {
sub store { sub store {
my ( $self, $fields ) = @_; my ( $self, $fields ) = @_;
my $fields = $self->serialize($fields);
my $tmp = my $tmp =
$self->dbh->do( "insert into " $self->dbh->do( "insert into "
. $self->{dbiTable} . " (" . $self->{dbiTable} . " ("
@ -107,7 +109,7 @@ sub load {
$self->logError; $self->logError;
return 0; return 0;
} }
return $row; return $self->unserialize($row);
} }
sub delete { sub delete {

View File

@ -2,6 +2,7 @@ package Lemonldap::NG::Common::Conf::File;
use strict; use strict;
use Lemonldap::NG::Common::Conf::Constants; #inherits use Lemonldap::NG::Common::Conf::Constants; #inherits
use Lemonldap::NG::Common::Conf::Serializer;
our $VERSION = 0.23; our $VERSION = 0.23;
@ -63,6 +64,7 @@ sub unlock {
sub store { sub store {
my ( $self, $fields ) = @_; my ( $self, $fields ) = @_;
my $fields = $self->serialize($fields);
my $mask = umask; my $mask = umask;
umask( oct('0027') ); umask( oct('0027') );
unless ( open FILE, unless ( open FILE,
@ -98,7 +100,7 @@ sub load {
} }
} }
close FILE; close FILE;
return $f; return $self->unserialize($f);
} }
sub delete { sub delete {

View File

@ -0,0 +1,78 @@
package Lemonldap::NG::Common::Conf::Serializer;
use Data::Dumper;
BEGIN {
*Lemonldap::NG::Common::Conf::serialize = \&serialize;
*Lemonldap::NG::Common::Conf::unserialize = \&unserialize;
}
sub serialize {
my ( $self, $conf ) = @_;
my $fields;
local $Data::Dumper::Indent = 0;
local $Data::Dumper::Varname = "data";
while ( my ( $k, $v ) = each(%$conf) ) {
next if ( $k =~ /^(?:reVHosts|cipher)$/ );
if ( ref($v) ) {
$fields->{$k} = Dumper($v);
$fields->{$k} =~ s/'/'/g;
$fields->{$k} = "'$fields->{$k}'";
}
elsif ( $v =~ /^\d+$/ ) {
$fields->{$k} = "$v";
}
else {
# mono-line
$v =~ s/[\r\n]/ /gm;
# trim
$v =~ s/^\s*(.*?)\s*$/$1/;
$fields->{$k} = "'$v'";
}
}
return $fields;
}
sub unserialize {
my ( $self, $fields ) = @_;
my $conf;
while ( my ( $k, $v ) = each(%$fields) ) {
$v =~ s/^'(.*)'$/$1/s;
if ( $k =~
/^(?:exportedVars|locationRules|groups|exportedHeaders|macros|globalStorageOptions)$/
and $v ||= {}
and not ref($v) )
{
$conf->{$k} = {};
if ( defined($v) and $v !~ /^\$/ ) {
print STDERR
"Lemonldap::NG : Warning: configuration is in old format, you've to migrate !\n";
eval { require Storable; require MIME::Base64; };
if ($@) {
$msg = "Error : $@";
return 0;
}
$conf->{$k} = Storable::thaw( MIME::Base64::decode_base64($v) );
}
else {
my $data;
$v =~ s/^\$([_a-zA-Z][_a-zA-Z0-9]*) *=/\$data =/;
$v =~ s/&#?39;/'/g;
eval $v;
print STDERR
"Lemonldap::NG : Error while reading configuration with $k key: $@\n"
if ($@);
$conf->{$k} = $data;
}
}
else {
$conf->{$k} = $v;
}
}
return $conf;
}
1;
__END__