X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_export%2Facct_sql.pm;h=9f1ae7b5c9e34840dde9ec2d1d4a0ed5acc121ef;hb=90393980e5f2859ee1e186fa461f48f5129e803e;hp=631a44c8a536e4a52dcbd868305e1b77e6960181;hpb=c3986b04e040c54afdf14cb39b3daf5210afa739;p=freeside.git diff --git a/FS/FS/part_export/acct_sql.pm b/FS/FS/part_export/acct_sql.pm index 631a44c8a..9f1ae7b5c 100644 --- a/FS/FS/part_export/acct_sql.pm +++ b/FS/FS/part_export/acct_sql.pm @@ -1,6 +1,6 @@ package FS::part_export::acct_sql; -use vars qw(@ISA %info @saltset); +use vars qw(@ISA %info); use Tie::IxHash; #use Digest::MD5 qw(md5_hex); use FS::Record; #qw(qsearchs); @@ -12,74 +12,149 @@ tie my %options, 'Tie::IxHash', 'datasrc' => { label => 'DBI data source' }, 'username' => { label => 'Database username' }, 'password' => { label => 'Database password' }, + 'table' => { label => 'Database table' }, + 'schema' => { label => + 'Database schema mapping to Freeside methods.', + type => 'textarea', + }, + 'static' => { label => + 'Database schema mapping to static values.', + type => 'textarea', + }, + 'primary_key' => { label => 'Database primary key' }, + 'crypt' => { label => 'Password encryption', + type=>'select', options=>[qw(crypt md5)], + default=>'crypt', + }, ; +tie my %vpopmail_map, 'Tie::IxHash', + 'pw_name' => 'username', + 'pw_domain' => 'domain', + 'pw_passwd' => 'crypt_password', + 'pw_uid' => 'uid', + 'pw_gid' => 'gid', + 'pw_gecos' => 'finger', + 'pw_dir' => 'dir', + #'pw_shell' => 'shell', + 'pw_shell' => 'quota', +; +my $vpopmail_map = join('\n', map "$_ $vpopmail_map{$_}", keys %vpopmail_map ); + +tie my %postfix_courierimap_mailbox_map, 'Tie::IxHash', + 'username' => 'email', + 'password' => '_password', + 'crypt' => 'crypt_password', + 'name' => 'finger', + 'maildir' => 'virtual_maildir', + 'domain' => 'domain', + 'svcnum' => 'svcnum', +; +my $postfix_courierimap_mailbox_map = + join('\n', map "$_ $postfix_courierimap_mailbox_map{$_}", + keys %postfix_courierimap_mailbox_map ); + +tie my %postfix_courierimap_alias_map, 'Tie::IxHash', + 'address' => 'email', + 'goto' => 'email', + 'domain' => 'domain', + 'svcnum' => 'svcnum', +; +my $postfix_courierimap_alias_map = + join('\n', map "$_ $postfix_courierimap_alias_map{$_}", + keys %postfix_courierimap_alias_map ); + +tie my %postfix_native_mailbox_map, 'Tie::IxHash', + 'userid' => 'email', + 'uid' => 'uid', + 'gid' => 'gid', + 'password' => 'ldap_password', + 'mail' => 'domain_slash_username', +; +my $postfix_native_mailbox_map = + join('\n', map "$_ $postfix_native_mailbox_map{$_}", + keys %postfix_native_mailbox_map ); + %info = ( 'svc' => 'svc_acct', 'desc' => 'Real-time export of accounts to SQL databases '. - '(Postfix+Courier IMAP, others?)', + '(vpopmail, Postfix+Courier IMAP, others?)', 'options' => \%options, 'nodomain' => '', 'notes' => <
In contrast to sqlmail, this is intended to export just svc_acct +records only, rather than a single export for svc_acct, svc_forward and +svc_domain records, to export in "default" database schemas rather than +configure the MTA or POP/IMAP server for a Freeside-specific schema, and +to be configured for different mail server setups. + +

Use these buttons for some useful presets: + END ); -@saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' ); - -#mapping needs to be configurable... -# export col => freeside col/method or callback -my %map = ( - 'username' => 'email', - 'password' => '_password', - 'crypt' => sub { - my $svc_acct = shift; - #false laziness w/shellcommands.pm - #eventually should check a "password-encoding" field - if ( length($svc_acct->_password) == 13 - || $svc_acct->_password =~ /^\$(1|2a?)\$/ ) { - $svc_acct->_password; - } else { - crypt( - $svc_acct->_password, - $saltset[int(rand(64))].$saltset[int(rand(64))] - ); - } - - }, - 'name' => 'finger', - 'maildir' => sub { $_[0]->domain. '/maildirs/'. $_[0]->username. '/' }, - 'domain' => sub { shift->domain }, - 'svcnum' => 'svcnum', -); - -my $table = 'mailbox'; #also needs to be configurable... +sub _schema_map { shift->_map('schema'); } +sub _static_map { shift->_map('static'); } -my $primary_key = 'username'; +sub _map { + my $self = shift; + map { /^\s*(\S+)\s*(\S+)\s*$/ } split("\n", $self->option(shift) ); +} sub rebless { shift; } sub _export_insert { my($self, $svc_acct) = (shift, shift); + my %schema = $self->_schema_map; + my %static = $self->_static_map; - my %record = map { my $value = $map{$_}; - $_ => ( ref($value) - ? &{$value}($svc_acct) - : $svc_acct->$value() - ); - } keys %map; + my %record = ( + + ( map { $_ => $static{$_} } keys %static ), + + ( map { my $value = $schema{$_}; + my @arg = (); + push @arg, $self->option('crypt') + if $value eq 'crypt_password' && $self->option('crypt'); + $_ => $svc_acct->$value(@arg); + } keys %schema + ), + + ); my $err_or_queue = - $self->acct_sql_queue( $svc_acct->svcnum, 'insert', $table, %record ); + $self->acct_sql_queue( + $svc_acct->svcnum, + 'insert', + $self->option('table'), + %record + ); return $err_or_queue unless ref($err_or_queue); ''; @@ -87,17 +162,72 @@ sub _export_insert { } sub _export_replace { + my($self, $new, $old) = (shift, shift, shift); + + my %schema = $self->_schema_map; + my %static = $self->_static_map; + + my @primary_key = (); + if ( $self->option('primary_key') =~ /,/ ) { + foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) { + my $keymap = $schema{$key}; + push @primary_key, $old->$keymap(); + } + } else { + my $keymap = $schema{$self->option('primary_key')}; + push @primary_key, $old->$keymap(); + } + + my %record = ( + + ( map { $_ => $static{$_} } keys %static ), + + ( map { my $value = $schema{$_}; + my @arg = (); + push @arg, $self->option('crypt') + if $value eq 'crypt_password' && $self->option('crypt'); + $_ => $new->$value(@arg); + } keys %schema + ), + + ); + + my $err_or_queue = $self->acct_sql_queue( + $new->svcnum, + 'replace', + $self->option('table'), + $self->option('primary_key'), @primary_key, + %record, + ); + return $err_or_queue unless ref($err_or_queue); + ''; } sub _export_delete { my ( $self, $svc_acct ) = (shift, shift); - my $keymap = $map{$primary_key}; + + my %schema = $self->_schema_map; + + my %primary_key = (); + if ( $self->option('primary_key') =~ /,/ ) { + foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) { + my $keymap = $schema{$key}; + $primary_key{ $key } = $svc_acct->$keymap(); + } + } else { + my $keymap = $schema{$self->option('primary_key')}; + $primary_key{ $self->option('primary_key') } = $svc_acct->$keymap(), + } + my $err_or_queue = $self->acct_sql_queue( $svc_acct->svcnum, 'delete', - $table, - $primary_key => ref($keymap) ? &{$keymap}($svc_acct) : $svc_acct->$keymap() + $self->option('table'), + %primary_key, + #$self->option('primary_key') => $svc_acct->$keymap(), ); + return $err_or_queue unless ref($err_or_queue); + ''; } sub acct_sql_queue { @@ -123,7 +253,7 @@ sub acct_sql_insert { #subroutine, not method " ) VALUES ( ". join(", ", map '?', keys %record ). " )" ) or die $dbh->errstr; - $sth->execute( map $record{$_}, keys %record ) + $sth->execute( values(%record) ) or die "can't insert into $table table: ". $sth->errstr; $dbh->disconnect; @@ -134,7 +264,7 @@ sub acct_sql_delete { #subroutine, not method my( $table, %record ) = @_; my $sth = $dbh->prepare( - "DELETE FROM $table WHERE ". join(' AND ', map "$_ = ? ", keys %record ) + "DELETE FROM $table WHERE ". join(' AND ', map "$_ = ? ", keys %record ) ) or die $dbh->errstr; $sth->execute( map $record{$_}, keys %record ) @@ -143,6 +273,33 @@ sub acct_sql_delete { #subroutine, not method $dbh->disconnect; } +sub acct_sql_replace { #subroutine, not method + my $dbh = acct_sql_connect(shift, shift, shift); + + my( $table, $pkey ) = ( shift, shift ); + + my %primary_key = (); + if ( $pkey =~ /,/ ) { + foreach my $key ( split(/\s*,\s*/, $pkey ) ) { + $primary_key{$key} = shift; + } + } else { + $primary_key{$pkey} = shift; + } + + my %record = @_; + + my $sth = $dbh->prepare( + "UPDATE $table". + ' SET '. join(', ', map "$_ = ?", keys %record ). + ' WHERE '. join(' AND ', map "$_ = ?", keys %primary_key ) + ) or die $dbh->errstr; + + $sth->execute( values(%record), values(%primary_key) ); + + $dbh->disconnect; +} + sub acct_sql_connect { #my($datasrc, $username, $password) = @_; #DBI->connect($datasrc, $username, $password) or die $DBI::errstr; @@ -151,4 +308,3 @@ sub acct_sql_connect { 1; -