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 $object ( $self->contact_phone, $self->contact_email ) {
211 my $error = $object->delete;
213 $dbh->rollback if $oldAutoCommit;
218 my $error = $self->SUPER::delete;
220 $dbh->rollback if $oldAutoCommit;
224 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
229 =item replace OLD_RECORD
231 Replaces the OLD_RECORD with this one in the database. If there is an error,
232 returns the error, otherwise returns false.
239 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
241 : $self->replace_old;
243 $self->$_( $self->$_ || $old->$_ ) for qw( _password _password_encoding );
245 local $SIG{INT} = 'IGNORE';
246 local $SIG{QUIT} = 'IGNORE';
247 local $SIG{TERM} = 'IGNORE';
248 local $SIG{TSTP} = 'IGNORE';
249 local $SIG{PIPE} = 'IGNORE';
251 my $oldAutoCommit = $FS::UID::AutoCommit;
252 local $FS::UID::AutoCommit = 0;
255 my $error = $self->SUPER::replace($old);
257 $dbh->rollback if $oldAutoCommit;
261 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) }
262 keys %{ $self->hashref } ) {
263 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
264 my $phonetypenum = $1;
266 my %cp = ( 'contactnum' => $self->contactnum,
267 'phonetypenum' => $phonetypenum,
269 my $contact_phone = qsearchs('contact_phone', \%cp)
270 || new FS::contact_phone \%cp;
272 my %cpd = _parse_phonestring( $self->get($pf) );
273 $contact_phone->set( $_ => $cpd{$_} ) foreach keys %cpd;
275 my $method = $contact_phone->contactphonenum ? 'replace' : 'insert';
277 $error = $contact_phone->$method;
279 $dbh->rollback if $oldAutoCommit;
284 if ( defined($self->hashref->{'emailaddress'}) ) {
286 #ineffecient but whatever, how many email addresses can there be?
288 foreach my $contact_email ( $self->contact_email ) {
289 my $error = $contact_email->delete;
291 $dbh->rollback if $oldAutoCommit;
296 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
298 my $contact_email = new FS::contact_email {
299 'contactnum' => $self->contactnum,
300 'emailaddress' => $email,
302 $error = $contact_email->insert;
304 $dbh->rollback if $oldAutoCommit;
312 #unless ( $import || $skip_fuzzyfiles ) {
313 #warn " queueing fuzzyfiles update\n"
315 $error = $self->queue_fuzzyfiles_update;
317 $dbh->rollback if $oldAutoCommit;
318 return "updating fuzzy search cache: $error";
322 if ( ( $old->selfservice_access eq '' && $self->selfservice_access
323 && ! $self->_password
328 my $error = $self->send_reset_email( queue=>1 );
330 $dbh->rollback if $oldAutoCommit;
335 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
341 #i probably belong in contact_phone.pm
342 sub _parse_phonestring {
345 my($countrycode, $extension) = ('1', '');
348 if ( $value =~ s/^\s*\+\s*(\d+)// ) {
354 if ( $value =~ s/\s*(ext|x)\s*(\d+)\s*$//i ) {
358 ( 'countrycode' => $countrycode,
359 'phonenum' => $value,
360 'extension' => $extension,
364 =item queue_fuzzyfiles_update
366 Used by insert & replace to update the fuzzy search cache
370 use FS::cust_main::Search;
371 sub queue_fuzzyfiles_update {
374 local $SIG{HUP} = 'IGNORE';
375 local $SIG{INT} = 'IGNORE';
376 local $SIG{QUIT} = 'IGNORE';
377 local $SIG{TERM} = 'IGNORE';
378 local $SIG{TSTP} = 'IGNORE';
379 local $SIG{PIPE} = 'IGNORE';
381 my $oldAutoCommit = $FS::UID::AutoCommit;
382 local $FS::UID::AutoCommit = 0;
385 foreach my $field ( 'first', 'last' ) {
386 my $queue = new FS::queue {
387 'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
389 my @args = "contact.$field", $self->get($field);
390 my $error = $queue->insert( @args );
392 $dbh->rollback if $oldAutoCommit;
393 return "queueing job (transaction rolled back): $error";
397 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
404 Checks all fields to make sure this is a valid example. If there is
405 an error, returns the error, otherwise returns false. Called by the insert
410 # the check method should currently be supplied - FS::Record contains some
411 # data checking routines
416 if ( $self->selfservice_access eq 'R' ) {
417 $self->selfservice_access('Y');
422 $self->ut_numbern('contactnum')
423 || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
424 || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
425 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
426 || $self->ut_foreign_keyn('classnum', 'contact_class', 'classnum')
427 || $self->ut_namen('last')
428 || $self->ut_namen('first')
429 || $self->ut_textn('title')
430 || $self->ut_textn('comment')
431 || $self->ut_enum('selfservice_access', [ '', 'Y' ])
432 || $self->ut_textn('_password')
433 || $self->ut_enum('_password_encoding', [ '', 'bcrypt'])
434 || $self->ut_enum('disabled', [ '', 'Y' ])
436 return $error if $error;
438 return "No prospect or customer!" unless $self->prospectnum || $self->custnum;
439 return "Prospect and customer!" if $self->prospectnum && $self->custnum;
441 return "One of first name, last name, or title must have a value"
442 if ! grep $self->$_(), qw( first last title);
449 my $data = $self->first. ' '. $self->last;
450 $data .= ', '. $self->title
452 $data .= ' ('. $self->comment. ')'
457 sub contact_classname {
459 my $contact_class = $self->contact_class or return '';
460 $contact_class->classname;
463 sub by_selfservice_email {
464 my($class, $email) = @_;
466 my $contact_email = qsearchs({
467 'table' => 'contact_email',
468 'addl_from' => ' LEFT JOIN contact USING ( contactnum ) ',
469 'hashref' => { 'emailaddress' => $email, },
470 'extra_sql' => " AND selfservice_access = 'Y' ".
471 " AND ( disabled IS NULL OR disabled = '' )",
474 $contact_email->contact;
478 #these three functions are very much false laziness w/FS/FS/Auth/internal.pm
479 # and should maybe be libraried in some way for other password needs
481 use Crypt::Eksblowfish::Bcrypt qw( bcrypt_hash en_base64 de_base64);
483 sub authenticate_password {
484 my($self, $check_password) = @_;
486 if ( $self->_password_encoding eq 'bcrypt' ) {
488 my( $cost, $salt, $hash ) = split(',', $self->_password);
490 my $check_hash = en_base64( bcrypt_hash( { key_nul => 1,
492 salt => de_base64($salt),
498 $hash eq $check_hash;
502 return 0 if $self->_password eq '';
504 $self->_password eq $check_password;
510 sub change_password {
511 my($self, $new_password) = @_;
513 $self->change_password_fields( $new_password );
519 sub change_password_fields {
520 my($self, $new_password) = @_;
522 $self->_password_encoding('bcrypt');
526 my $salt = pack( 'C*', map int(rand(256)), 1..16 );
528 my $hash = bcrypt_hash( { key_nul => 1,
536 join(',', $cost, en_base64($salt), en_base64($hash) )
541 # end of false laziness w/FS/FS/Auth/internal.pm
544 #false laziness w/ClientAPI/MyAccount/reset_passwd
545 use Digest::SHA qw(sha512_hex);
547 use FS::ClientAPI_SessionCache;
548 sub send_reset_email {
549 my( $self, %opt ) = @_;
551 my @contact_email = $self->contact_email or return '';
553 my $reset_session = {
554 'contactnum' => $self->contactnum,
555 'svcnum' => $opt{'svcnum'},
558 my $timeout = '24 hours'; #?
560 my $reset_session_id;
562 $reset_session_id = sha512_hex(time(). {}. rand(). $$)
563 } until ( ! defined $self->myaccount_cache->get("reset_passwd_$reset_session_id") );
566 $self->myaccount_cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
570 my $conf = new FS::Conf;
572 my $cust_main = $self->cust_main
573 or die "no customer"; #reset a password for a prospect contact? someday
575 my $msgnum = $conf->config('selfservice-password_reset_msgnum', $cust_main->agentnum);
576 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
577 return { 'error' => "selfservice-password_reset_msgnum unset" } unless $msgnum;
578 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
580 'to' => join(',', map $_->emailaddress, @contact_email ),
581 'cust_main' => $cust_main,
583 'substitutions' => { 'session_id' => $reset_session_id }
586 if ( $opt{'queue'} ) { #or should queueing just be the default?
588 my $queue = new FS::queue {
589 'job' => 'FS::Misc::process_send_email',
590 'custnum' => $cust_main->custnum,
592 $queue->insert( $msg_template->prepare( %msg_template ) );
596 $msg_template->send( %msg_template );
602 use vars qw( $myaccount_cache );
603 sub myaccount_cache {
605 $myaccount_cache ||= new FS::ClientAPI_SessionCache( {
606 'namespace' => 'FS::ClientAPI::MyAccount',
616 L<FS::Record>, schema.html from the base documentation.