2 use base qw( FS::Record );
5 use Scalar::Util qw( blessed );
6 use FS::Record qw( qsearchs dbh ); # qw( qsearch qsearchs dbh );
13 FS::contact - Object methods for contact records
19 $record = new FS::contact \%hash;
20 $record = new FS::contact { 'column' => 'value' };
22 $error = $record->insert;
24 $error = $new_record->replace($old_record);
26 $error = $record->delete;
28 $error = $record->check;
32 An FS::contact object represents an example. FS::contact inherits from
33 FS::Record. The following fields are currently supported:
69 =item selfservice_access
75 =item _password_encoding
92 Creates a new example. To add the example to the database, see L<"insert">.
94 Note that this stores the hash reference, not a distinct copy of the hash it
95 points to. You can ask the object for a copy with the I<hash> method.
99 # the new method can be inherited from FS::Record, if a table method is defined
101 sub table { 'contact'; }
105 Adds this record to the database. If there is an error, returns the error,
106 otherwise returns false.
113 local $SIG{INT} = 'IGNORE';
114 local $SIG{QUIT} = 'IGNORE';
115 local $SIG{TERM} = 'IGNORE';
116 local $SIG{TSTP} = 'IGNORE';
117 local $SIG{PIPE} = 'IGNORE';
119 my $oldAutoCommit = $FS::UID::AutoCommit;
120 local $FS::UID::AutoCommit = 0;
123 my $error = $self->SUPER::insert;
125 $dbh->rollback if $oldAutoCommit;
129 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) =~ /\S/ }
130 keys %{ $self->hashref } ) {
131 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
132 my $phonetypenum = $1;
134 my $contact_phone = new FS::contact_phone {
135 'contactnum' => $self->contactnum,
136 'phonetypenum' => $phonetypenum,
137 _parse_phonestring( $self->get($pf) ),
139 $error = $contact_phone->insert;
141 $dbh->rollback if $oldAutoCommit;
146 if ( $self->get('emailaddress') =~ /\S/ ) {
148 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
150 my $contact_email = new FS::contact_email {
151 'contactnum' => $self->contactnum,
152 'emailaddress' => $email,
154 $error = $contact_email->insert;
156 $dbh->rollback if $oldAutoCommit;
164 #unless ( $import || $skip_fuzzyfiles ) {
165 #warn " queueing fuzzyfiles update\n"
167 $error = $self->queue_fuzzyfiles_update;
169 $dbh->rollback if $oldAutoCommit;
170 return "updating fuzzy search cache: $error";
174 if ( $self->selfservice_access ) {
175 my $error = $self->send_reset_email( queue=>1 );
177 $dbh->rollback if $oldAutoCommit;
182 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
190 Delete this record from the database.
194 # the delete method can be inherited from FS::Record
199 local $SIG{HUP} = 'IGNORE';
200 local $SIG{INT} = 'IGNORE';
201 local $SIG{QUIT} = 'IGNORE';
202 local $SIG{TERM} = 'IGNORE';
203 local $SIG{TSTP} = 'IGNORE';
204 local $SIG{PIPE} = 'IGNORE';
206 my $oldAutoCommit = $FS::UID::AutoCommit;
207 local $FS::UID::AutoCommit = 0;
210 foreach my $cust_pkg ( $self->cust_pkg ) {
211 $cust_pkg->contactnum('');
212 my $error = $cust_pkg->replace;
214 $dbh->rollback if $oldAutoCommit;
219 foreach my $object ( $self->contact_phone, $self->contact_email ) {
220 my $error = $object->delete;
222 $dbh->rollback if $oldAutoCommit;
227 my $error = $self->SUPER::delete;
229 $dbh->rollback if $oldAutoCommit;
233 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
238 =item replace OLD_RECORD
240 Replaces the OLD_RECORD with this one in the database. If there is an error,
241 returns the error, otherwise returns false.
248 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
250 : $self->replace_old;
252 $self->$_( $self->$_ || $old->$_ ) for qw( _password _password_encoding );
254 local $SIG{INT} = 'IGNORE';
255 local $SIG{QUIT} = 'IGNORE';
256 local $SIG{TERM} = 'IGNORE';
257 local $SIG{TSTP} = 'IGNORE';
258 local $SIG{PIPE} = 'IGNORE';
260 my $oldAutoCommit = $FS::UID::AutoCommit;
261 local $FS::UID::AutoCommit = 0;
264 my $error = $self->SUPER::replace($old);
266 $dbh->rollback if $oldAutoCommit;
270 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) }
271 keys %{ $self->hashref } ) {
272 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
273 my $phonetypenum = $1;
275 my %cp = ( 'contactnum' => $self->contactnum,
276 'phonetypenum' => $phonetypenum,
278 my $contact_phone = qsearchs('contact_phone', \%cp)
279 || new FS::contact_phone \%cp;
281 my %cpd = _parse_phonestring( $self->get($pf) );
282 $contact_phone->set( $_ => $cpd{$_} ) foreach keys %cpd;
284 my $method = $contact_phone->contactphonenum ? 'replace' : 'insert';
286 $error = $contact_phone->$method;
288 $dbh->rollback if $oldAutoCommit;
293 if ( defined($self->hashref->{'emailaddress'}) ) {
295 #ineffecient but whatever, how many email addresses can there be?
297 foreach my $contact_email ( $self->contact_email ) {
298 my $error = $contact_email->delete;
300 $dbh->rollback if $oldAutoCommit;
305 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
307 my $contact_email = new FS::contact_email {
308 'contactnum' => $self->contactnum,
309 'emailaddress' => $email,
311 $error = $contact_email->insert;
313 $dbh->rollback if $oldAutoCommit;
321 #unless ( $import || $skip_fuzzyfiles ) {
322 #warn " queueing fuzzyfiles update\n"
324 $error = $self->queue_fuzzyfiles_update;
326 $dbh->rollback if $oldAutoCommit;
327 return "updating fuzzy search cache: $error";
331 if ( ( $old->selfservice_access eq '' && $self->selfservice_access
332 && ! $self->_password
337 my $error = $self->send_reset_email( queue=>1 );
339 $dbh->rollback if $oldAutoCommit;
344 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
350 #i probably belong in contact_phone.pm
351 sub _parse_phonestring {
354 my($countrycode, $extension) = ('1', '');
357 if ( $value =~ s/^\s*\+\s*(\d+)// ) {
363 if ( $value =~ s/\s*(ext|x)\s*(\d+)\s*$//i ) {
367 ( 'countrycode' => $countrycode,
368 'phonenum' => $value,
369 'extension' => $extension,
373 =item queue_fuzzyfiles_update
375 Used by insert & replace to update the fuzzy search cache
379 use FS::cust_main::Search;
380 sub queue_fuzzyfiles_update {
383 local $SIG{HUP} = 'IGNORE';
384 local $SIG{INT} = 'IGNORE';
385 local $SIG{QUIT} = 'IGNORE';
386 local $SIG{TERM} = 'IGNORE';
387 local $SIG{TSTP} = 'IGNORE';
388 local $SIG{PIPE} = 'IGNORE';
390 my $oldAutoCommit = $FS::UID::AutoCommit;
391 local $FS::UID::AutoCommit = 0;
394 foreach my $field ( 'first', 'last' ) {
395 my $queue = new FS::queue {
396 'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
398 my @args = "contact.$field", $self->get($field);
399 my $error = $queue->insert( @args );
401 $dbh->rollback if $oldAutoCommit;
402 return "queueing job (transaction rolled back): $error";
406 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
413 Checks all fields to make sure this is a valid example. If there is
414 an error, returns the error, otherwise returns false. Called by the insert
419 # the check method should currently be supplied - FS::Record contains some
420 # data checking routines
425 if ( $self->selfservice_access eq 'R' ) {
426 $self->selfservice_access('Y');
431 $self->ut_numbern('contactnum')
432 || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
433 || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
434 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
435 || $self->ut_foreign_keyn('classnum', 'contact_class', 'classnum')
436 || $self->ut_namen('last')
437 || $self->ut_namen('first')
438 || $self->ut_textn('title')
439 || $self->ut_textn('comment')
440 || $self->ut_enum('selfservice_access', [ '', 'Y' ])
441 || $self->ut_textn('_password')
442 || $self->ut_enum('_password_encoding', [ '', 'bcrypt'])
443 || $self->ut_enum('disabled', [ '', 'Y' ])
445 return $error if $error;
447 return "No prospect or customer!" unless $self->prospectnum || $self->custnum;
448 return "Prospect and customer!" if $self->prospectnum && $self->custnum;
450 return "One of first name, last name, or title must have a value"
451 if ! grep $self->$_(), qw( first last title);
458 my $data = $self->first. ' '. $self->last;
459 $data .= ', '. $self->title
461 $data .= ' ('. $self->comment. ')'
466 sub contact_classname {
468 my $contact_class = $self->contact_class or return '';
469 $contact_class->classname;
472 sub by_selfservice_email {
473 my($class, $email) = @_;
475 my $contact_email = qsearchs({
476 'table' => 'contact_email',
477 'addl_from' => ' LEFT JOIN contact USING ( contactnum ) ',
478 'hashref' => { 'emailaddress' => $email, },
479 'extra_sql' => " AND selfservice_access = 'Y' ".
480 " AND ( disabled IS NULL OR disabled = '' )",
483 $contact_email->contact;
487 #these three functions are very much false laziness w/FS/FS/Auth/internal.pm
488 # and should maybe be libraried in some way for other password needs
490 use Crypt::Eksblowfish::Bcrypt qw( bcrypt_hash en_base64 de_base64);
492 sub authenticate_password {
493 my($self, $check_password) = @_;
495 if ( $self->_password_encoding eq 'bcrypt' ) {
497 my( $cost, $salt, $hash ) = split(',', $self->_password);
499 my $check_hash = en_base64( bcrypt_hash( { key_nul => 1,
501 salt => de_base64($salt),
507 $hash eq $check_hash;
511 return 0 if $self->_password eq '';
513 $self->_password eq $check_password;
519 sub change_password {
520 my($self, $new_password) = @_;
522 $self->change_password_fields( $new_password );
528 sub change_password_fields {
529 my($self, $new_password) = @_;
531 $self->_password_encoding('bcrypt');
535 my $salt = pack( 'C*', map int(rand(256)), 1..16 );
537 my $hash = bcrypt_hash( { key_nul => 1,
545 join(',', $cost, en_base64($salt), en_base64($hash) )
550 # end of false laziness w/FS/FS/Auth/internal.pm
553 #false laziness w/ClientAPI/MyAccount/reset_passwd
554 use Digest::SHA qw(sha512_hex);
556 use FS::ClientAPI_SessionCache;
557 sub send_reset_email {
558 my( $self, %opt ) = @_;
560 my @contact_email = $self->contact_email or return '';
562 my $reset_session = {
563 'contactnum' => $self->contactnum,
564 'svcnum' => $opt{'svcnum'},
567 my $timeout = '24 hours'; #?
569 my $reset_session_id;
571 $reset_session_id = sha512_hex(time(). {}. rand(). $$)
572 } until ( ! defined $self->myaccount_cache->get("reset_passwd_$reset_session_id") );
575 $self->myaccount_cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
579 my $conf = new FS::Conf;
581 my $cust_main = $self->cust_main
582 or die "no customer"; #reset a password for a prospect contact? someday
584 my $msgnum = $conf->config('selfservice-password_reset_msgnum', $cust_main->agentnum);
585 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
586 return { 'error' => "selfservice-password_reset_msgnum unset" } unless $msgnum;
587 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
589 'to' => join(',', map $_->emailaddress, @contact_email ),
590 'cust_main' => $cust_main,
592 'substitutions' => { 'session_id' => $reset_session_id }
595 if ( $opt{'queue'} ) { #or should queueing just be the default?
597 my $queue = new FS::queue {
598 'job' => 'FS::Misc::process_send_email',
599 'custnum' => $cust_main->custnum,
601 $queue->insert( $msg_template->prepare( %msg_template ) );
605 $msg_template->send( %msg_template );
611 use vars qw( $myaccount_cache );
612 sub myaccount_cache {
614 $myaccount_cache ||= new FS::ClientAPI_SessionCache( {
615 'namespace' => 'FS::ClientAPI::MyAccount',
625 L<FS::Record>, schema.html from the base documentation.