2 use base qw( FS::Record );
5 use Scalar::Util qw( blessed );
6 use FS::Record qw( qsearch qsearchs dbh );
10 use FS::cust_location;
11 use FS::contact_phone;
12 use FS::contact_email;
18 FS::contact - Object methods for contact records
24 $record = new FS::contact \%hash;
25 $record = new FS::contact { 'column' => 'value' };
27 $error = $record->insert;
29 $error = $new_record->replace($old_record);
31 $error = $record->delete;
33 $error = $record->check;
37 An FS::contact object represents an example. FS::contact inherits from
38 FS::Record. The following fields are currently supported:
74 =item selfservice_access
80 =item _password_encoding
97 Creates a new example. To add the example to the database, see L<"insert">.
99 Note that this stores the hash reference, not a distinct copy of the hash it
100 points to. You can ask the object for a copy with the I<hash> method.
104 # the new method can be inherited from FS::Record, if a table method is defined
106 sub table { 'contact'; }
110 Adds this record to the database. If there is an error, returns the error,
111 otherwise returns false.
118 local $SIG{INT} = 'IGNORE';
119 local $SIG{QUIT} = 'IGNORE';
120 local $SIG{TERM} = 'IGNORE';
121 local $SIG{TSTP} = 'IGNORE';
122 local $SIG{PIPE} = 'IGNORE';
124 my $oldAutoCommit = $FS::UID::AutoCommit;
125 local $FS::UID::AutoCommit = 0;
128 my $error = $self->SUPER::insert;
130 $dbh->rollback if $oldAutoCommit;
134 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) =~ /\S/ }
135 keys %{ $self->hashref } ) {
136 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
137 my $phonetypenum = $1;
139 my $contact_phone = new FS::contact_phone {
140 'contactnum' => $self->contactnum,
141 'phonetypenum' => $phonetypenum,
142 _parse_phonestring( $self->get($pf) ),
144 $error = $contact_phone->insert;
146 $dbh->rollback if $oldAutoCommit;
151 if ( $self->get('emailaddress') =~ /\S/ ) {
153 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
155 my $contact_email = new FS::contact_email {
156 'contactnum' => $self->contactnum,
157 'emailaddress' => $email,
159 $error = $contact_email->insert;
161 $dbh->rollback if $oldAutoCommit;
169 #unless ( $import || $skip_fuzzyfiles ) {
170 #warn " queueing fuzzyfiles update\n"
172 $error = $self->queue_fuzzyfiles_update;
174 $dbh->rollback if $oldAutoCommit;
175 return "updating fuzzy search cache: $error";
179 if ( $self->selfservice_access ) {
180 my $error = $self->send_reset_email( queue=>1 );
182 $dbh->rollback if $oldAutoCommit;
187 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
195 Delete this record from the database.
199 # the delete method can be inherited from FS::Record
204 local $SIG{HUP} = 'IGNORE';
205 local $SIG{INT} = 'IGNORE';
206 local $SIG{QUIT} = 'IGNORE';
207 local $SIG{TERM} = 'IGNORE';
208 local $SIG{TSTP} = 'IGNORE';
209 local $SIG{PIPE} = 'IGNORE';
211 my $oldAutoCommit = $FS::UID::AutoCommit;
212 local $FS::UID::AutoCommit = 0;
215 foreach my $cust_pkg ( $self->cust_pkg ) {
216 $cust_pkg->contactnum('');
217 my $error = $cust_pkg->replace;
219 $dbh->rollback if $oldAutoCommit;
224 foreach my $object ( $self->contact_phone, $self->contact_email ) {
225 my $error = $object->delete;
227 $dbh->rollback if $oldAutoCommit;
232 my $error = $self->SUPER::delete;
234 $dbh->rollback if $oldAutoCommit;
238 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
243 =item replace OLD_RECORD
245 Replaces the OLD_RECORD with this one in the database. If there is an error,
246 returns the error, otherwise returns false.
253 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
255 : $self->replace_old;
257 $self->$_( $self->$_ || $old->$_ ) for qw( _password _password_encoding );
259 local $SIG{INT} = 'IGNORE';
260 local $SIG{QUIT} = 'IGNORE';
261 local $SIG{TERM} = 'IGNORE';
262 local $SIG{TSTP} = 'IGNORE';
263 local $SIG{PIPE} = 'IGNORE';
265 my $oldAutoCommit = $FS::UID::AutoCommit;
266 local $FS::UID::AutoCommit = 0;
269 my $error = $self->SUPER::replace($old);
271 $dbh->rollback if $oldAutoCommit;
275 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) }
276 keys %{ $self->hashref } ) {
277 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
278 my $phonetypenum = $1;
280 my %cp = ( 'contactnum' => $self->contactnum,
281 'phonetypenum' => $phonetypenum,
283 my $contact_phone = qsearchs('contact_phone', \%cp)
284 || new FS::contact_phone \%cp;
286 my %cpd = _parse_phonestring( $self->get($pf) );
287 $contact_phone->set( $_ => $cpd{$_} ) foreach keys %cpd;
289 my $method = $contact_phone->contactphonenum ? 'replace' : 'insert';
291 $error = $contact_phone->$method;
293 $dbh->rollback if $oldAutoCommit;
298 if ( defined($self->hashref->{'emailaddress'}) ) {
300 #ineffecient but whatever, how many email addresses can there be?
302 foreach my $contact_email ( $self->contact_email ) {
303 my $error = $contact_email->delete;
305 $dbh->rollback if $oldAutoCommit;
310 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
312 my $contact_email = new FS::contact_email {
313 'contactnum' => $self->contactnum,
314 'emailaddress' => $email,
316 $error = $contact_email->insert;
318 $dbh->rollback if $oldAutoCommit;
326 #unless ( $import || $skip_fuzzyfiles ) {
327 #warn " queueing fuzzyfiles update\n"
329 $error = $self->queue_fuzzyfiles_update;
331 $dbh->rollback if $oldAutoCommit;
332 return "updating fuzzy search cache: $error";
336 if ( ( $old->selfservice_access eq '' && $self->selfservice_access
337 && ! $self->_password
342 my $error = $self->send_reset_email( queue=>1 );
344 $dbh->rollback if $oldAutoCommit;
349 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
355 #i probably belong in contact_phone.pm
356 sub _parse_phonestring {
359 my($countrycode, $extension) = ('1', '');
362 if ( $value =~ s/^\s*\+\s*(\d+)// ) {
368 if ( $value =~ s/\s*(ext|x)\s*(\d+)\s*$//i ) {
372 ( 'countrycode' => $countrycode,
373 'phonenum' => $value,
374 'extension' => $extension,
378 =item queue_fuzzyfiles_update
380 Used by insert & replace to update the fuzzy search cache
384 use FS::cust_main::Search;
385 sub queue_fuzzyfiles_update {
388 local $SIG{HUP} = 'IGNORE';
389 local $SIG{INT} = 'IGNORE';
390 local $SIG{QUIT} = 'IGNORE';
391 local $SIG{TERM} = 'IGNORE';
392 local $SIG{TSTP} = 'IGNORE';
393 local $SIG{PIPE} = 'IGNORE';
395 my $oldAutoCommit = $FS::UID::AutoCommit;
396 local $FS::UID::AutoCommit = 0;
399 foreach my $field ( 'first', 'last' ) {
400 my $queue = new FS::queue {
401 'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
403 my @args = "contact.$field", $self->get($field);
404 my $error = $queue->insert( @args );
406 $dbh->rollback if $oldAutoCommit;
407 return "queueing job (transaction rolled back): $error";
411 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
418 Checks all fields to make sure this is a valid example. If there is
419 an error, returns the error, otherwise returns false. Called by the insert
424 # the check method should currently be supplied - FS::Record contains some
425 # data checking routines
430 if ( $self->selfservice_access eq 'R' ) {
431 $self->selfservice_access('Y');
436 $self->ut_numbern('contactnum')
437 || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
438 || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
439 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
440 || $self->ut_foreign_keyn('classnum', 'contact_class', 'classnum')
441 || $self->ut_namen('last')
442 || $self->ut_namen('first')
443 || $self->ut_textn('title')
444 || $self->ut_textn('comment')
445 || $self->ut_enum('selfservice_access', [ '', 'Y' ])
446 || $self->ut_textn('_password')
447 || $self->ut_enum('_password_encoding', [ '', 'bcrypt'])
448 || $self->ut_enum('disabled', [ '', 'Y' ])
450 return $error if $error;
452 return "No prospect or customer!" unless $self->prospectnum || $self->custnum;
453 return "Prospect and customer!" if $self->prospectnum && $self->custnum;
455 return "One of first name, last name, or title must have a value"
456 if ! grep $self->$_(), qw( first last title);
463 my $data = $self->first. ' '. $self->last;
464 $data .= ', '. $self->title
466 $data .= ' ('. $self->comment. ')'
473 return '' unless $self->locationnum;
474 qsearchs('cust_location', { 'locationnum' => $self->locationnum } );
479 return '' unless $self->classnum;
480 qsearchs('contact_class', { 'classnum' => $self->classnum } );
483 sub contact_classname {
485 my $contact_class = $self->contact_class or return '';
486 $contact_class->classname;
491 qsearch('contact_phone', { 'contactnum' => $self->contactnum } );
496 qsearch('contact_email', { 'contactnum' => $self->contactnum } );
501 qsearchs('cust_main', { 'custnum' => $self->custnum } );
506 qsearch('cust_pkg', { 'contactnum' => $self->contactnum } );
510 sub by_selfservice_email {
511 my($class, $email) = @_;
513 my $contact_email = qsearchs({
514 'table' => 'contact_email',
515 'addl_from' => ' LEFT JOIN contact USING ( contactnum ) ',
516 'hashref' => { 'emailaddress' => $email, },
517 'extra_sql' => " AND selfservice_access = 'Y' ".
518 " AND ( disabled IS NULL OR disabled = '' )",
521 $contact_email->contact;
525 #these three functions are very much false laziness w/FS/FS/Auth/internal.pm
526 # and should maybe be libraried in some way for other password needs
528 use Crypt::Eksblowfish::Bcrypt qw( bcrypt_hash en_base64 de_base64);
530 sub authenticate_password {
531 my($self, $check_password) = @_;
533 if ( $self->_password_encoding eq 'bcrypt' ) {
535 my( $cost, $salt, $hash ) = split(',', $self->_password);
537 my $check_hash = en_base64( bcrypt_hash( { key_nul => 1,
539 salt => de_base64($salt),
545 $hash eq $check_hash;
549 return 0 if $self->_password eq '';
551 $self->_password eq $check_password;
557 sub change_password {
558 my($self, $new_password) = @_;
560 $self->change_password_fields( $new_password );
566 sub change_password_fields {
567 my($self, $new_password) = @_;
569 $self->_password_encoding('bcrypt');
573 my $salt = pack( 'C*', map int(rand(256)), 1..16 );
575 my $hash = bcrypt_hash( { key_nul => 1,
583 join(',', $cost, en_base64($salt), en_base64($hash) )
588 # end of false laziness w/FS/FS/Auth/internal.pm
591 #false laziness w/ClientAPI/MyAccount/reset_passwd
592 use Digest::SHA qw(sha512_hex);
594 use FS::ClientAPI_SessionCache;
595 sub send_reset_email {
596 my( $self, %opt ) = @_;
598 my @contact_email = $self->contact_email or return '';
600 my $reset_session = {
601 'contactnum' => $self->contactnum,
602 'svcnum' => $opt{'svcnum'},
605 my $timeout = '24 hours'; #?
607 my $reset_session_id;
609 $reset_session_id = sha512_hex(time(). {}. rand(). $$)
610 } until ( ! defined $self->myaccount_cache->get("reset_passwd_$reset_session_id") );
613 $self->myaccount_cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
617 my $conf = new FS::Conf;
619 my $cust_main = $self->cust_main
620 or die "no customer"; #reset a password for a prospect contact? someday
622 my $msgnum = $conf->config('selfservice-password_reset_msgnum', $cust_main->agentnum);
623 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
624 return { 'error' => "selfservice-password_reset_msgnum unset" } unless $msgnum;
625 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
627 'to' => join(',', map $_->emailaddress, @contact_email ),
628 'cust_main' => $cust_main,
630 'substitutions' => { 'session_id' => $reset_session_id }
633 if ( $opt{'queue'} ) { #or should queueing just be the default?
635 my $queue = new FS::queue {
636 'job' => 'FS::Misc::process_send_email',
637 'custnum' => $cust_main->custnum,
639 $queue->insert( $msg_template->prepare( %msg_template ) );
643 $msg_template->send( %msg_template );
649 use vars qw( $myaccount_cache );
650 sub myaccount_cache {
652 $myaccount_cache ||= new FS::ClientAPI_SessionCache( {
653 'namespace' => 'FS::ClientAPI::MyAccount',
663 L<FS::Record>, schema.html from the base documentation.