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;
17 FS::contact - Object methods for contact records
23 $record = new FS::contact \%hash;
24 $record = new FS::contact { 'column' => 'value' };
26 $error = $record->insert;
28 $error = $new_record->replace($old_record);
30 $error = $record->delete;
32 $error = $record->check;
36 An FS::contact object represents an example. FS::contact inherits from
37 FS::Record. The following fields are currently supported:
73 =item selfservice_access
79 =item _password_encoding
96 Creates a new example. To add the example to the database, see L<"insert">.
98 Note that this stores the hash reference, not a distinct copy of the hash it
99 points to. You can ask the object for a copy with the I<hash> method.
103 # the new method can be inherited from FS::Record, if a table method is defined
105 sub table { 'contact'; }
109 Adds this record to the database. If there is an error, returns the error,
110 otherwise returns false.
117 local $SIG{INT} = 'IGNORE';
118 local $SIG{QUIT} = 'IGNORE';
119 local $SIG{TERM} = 'IGNORE';
120 local $SIG{TSTP} = 'IGNORE';
121 local $SIG{PIPE} = 'IGNORE';
123 my $oldAutoCommit = $FS::UID::AutoCommit;
124 local $FS::UID::AutoCommit = 0;
127 my $error = $self->SUPER::insert;
129 $dbh->rollback if $oldAutoCommit;
133 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) =~ /\S/ }
134 keys %{ $self->hashref } ) {
135 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
136 my $phonetypenum = $1;
138 my $contact_phone = new FS::contact_phone {
139 'contactnum' => $self->contactnum,
140 'phonetypenum' => $phonetypenum,
141 _parse_phonestring( $self->get($pf) ),
143 $error = $contact_phone->insert;
145 $dbh->rollback if $oldAutoCommit;
150 if ( $self->get('emailaddress') =~ /\S/ ) {
152 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
154 my $contact_email = new FS::contact_email {
155 'contactnum' => $self->contactnum,
156 'emailaddress' => $email,
158 $error = $contact_email->insert;
160 $dbh->rollback if $oldAutoCommit;
168 #unless ( $import || $skip_fuzzyfiles ) {
169 #warn " queueing fuzzyfiles update\n"
171 $error = $self->queue_fuzzyfiles_update;
173 $dbh->rollback if $oldAutoCommit;
174 return "updating fuzzy search cache: $error";
178 if ( $self->selfservice_access ) {
179 my $error = $self->send_reset_email( queue=>1 );
181 $dbh->rollback if $oldAutoCommit;
186 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
194 Delete this record from the database.
198 # the delete method can be inherited from FS::Record
203 local $SIG{HUP} = 'IGNORE';
204 local $SIG{INT} = 'IGNORE';
205 local $SIG{QUIT} = 'IGNORE';
206 local $SIG{TERM} = 'IGNORE';
207 local $SIG{TSTP} = 'IGNORE';
208 local $SIG{PIPE} = 'IGNORE';
210 my $oldAutoCommit = $FS::UID::AutoCommit;
211 local $FS::UID::AutoCommit = 0;
214 foreach my $object ( $self->contact_phone, $self->contact_email ) {
215 my $error = $object->delete;
217 $dbh->rollback if $oldAutoCommit;
222 my $error = $self->SUPER::delete;
224 $dbh->rollback if $oldAutoCommit;
228 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
233 =item replace OLD_RECORD
235 Replaces the OLD_RECORD with this one in the database. If there is an error,
236 returns the error, otherwise returns false.
243 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
245 : $self->replace_old;
247 $self->$_( $self->$_ || $old->$_ ) for qw( _password _password_encoding );
249 local $SIG{INT} = 'IGNORE';
250 local $SIG{QUIT} = 'IGNORE';
251 local $SIG{TERM} = 'IGNORE';
252 local $SIG{TSTP} = 'IGNORE';
253 local $SIG{PIPE} = 'IGNORE';
255 my $oldAutoCommit = $FS::UID::AutoCommit;
256 local $FS::UID::AutoCommit = 0;
259 my $error = $self->SUPER::replace($old);
261 $dbh->rollback if $oldAutoCommit;
265 foreach my $pf ( grep { /^phonetypenum(\d+)$/ && $self->get($_) }
266 keys %{ $self->hashref } ) {
267 $pf =~ /^phonetypenum(\d+)$/ or die "wtf (daily, the)";
268 my $phonetypenum = $1;
270 my %cp = ( 'contactnum' => $self->contactnum,
271 'phonetypenum' => $phonetypenum,
273 my $contact_phone = qsearchs('contact_phone', \%cp)
274 || new FS::contact_phone \%cp;
276 my %cpd = _parse_phonestring( $self->get($pf) );
277 $contact_phone->set( $_ => $cpd{$_} ) foreach keys %cpd;
279 my $method = $contact_phone->contactphonenum ? 'replace' : 'insert';
281 $error = $contact_phone->$method;
283 $dbh->rollback if $oldAutoCommit;
288 if ( defined($self->hashref->{'emailaddress'}) ) {
290 #ineffecient but whatever, how many email addresses can there be?
292 foreach my $contact_email ( $self->contact_email ) {
293 my $error = $contact_email->delete;
295 $dbh->rollback if $oldAutoCommit;
300 foreach my $email ( split(/\s*,\s*/, $self->get('emailaddress') ) ) {
302 my $contact_email = new FS::contact_email {
303 'contactnum' => $self->contactnum,
304 'emailaddress' => $email,
306 $error = $contact_email->insert;
308 $dbh->rollback if $oldAutoCommit;
316 #unless ( $import || $skip_fuzzyfiles ) {
317 #warn " queueing fuzzyfiles update\n"
319 $error = $self->queue_fuzzyfiles_update;
321 $dbh->rollback if $oldAutoCommit;
322 return "updating fuzzy search cache: $error";
326 if ( ( $old->selfservice_access eq '' && $self->selfservice_access
327 && ! $self->_password
332 my $error = $self->send_reset_email( queue=>1 );
334 $dbh->rollback if $oldAutoCommit;
339 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
345 #i probably belong in contact_phone.pm
346 sub _parse_phonestring {
349 my($countrycode, $extension) = ('1', '');
352 if ( $value =~ s/^\s*\+\s*(\d+)// ) {
358 if ( $value =~ s/\s*(ext|x)\s*(\d+)\s*$//i ) {
362 ( 'countrycode' => $countrycode,
363 'phonenum' => $value,
364 'extension' => $extension,
368 =item queue_fuzzyfiles_update
370 Used by insert & replace to update the fuzzy search cache
374 use FS::cust_main::Search;
375 sub queue_fuzzyfiles_update {
378 local $SIG{HUP} = 'IGNORE';
379 local $SIG{INT} = 'IGNORE';
380 local $SIG{QUIT} = 'IGNORE';
381 local $SIG{TERM} = 'IGNORE';
382 local $SIG{TSTP} = 'IGNORE';
383 local $SIG{PIPE} = 'IGNORE';
385 my $oldAutoCommit = $FS::UID::AutoCommit;
386 local $FS::UID::AutoCommit = 0;
389 foreach my $field ( 'first', 'last' ) {
390 my $queue = new FS::queue {
391 'job' => 'FS::cust_main::Search::append_fuzzyfiles_fuzzyfield'
393 my @args = "contact.$field", $self->get($field);
394 my $error = $queue->insert( @args );
396 $dbh->rollback if $oldAutoCommit;
397 return "queueing job (transaction rolled back): $error";
401 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
408 Checks all fields to make sure this is a valid example. If there is
409 an error, returns the error, otherwise returns false. Called by the insert
414 # the check method should currently be supplied - FS::Record contains some
415 # data checking routines
420 if ( $self->selfservice_access eq 'R' ) {
421 $self->selfservice_access('Y');
426 $self->ut_numbern('contactnum')
427 || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
428 || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
429 || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
430 || $self->ut_foreign_keyn('classnum', 'contact_class', 'classnum')
431 || $self->ut_namen('last')
432 || $self->ut_namen('first')
433 || $self->ut_textn('title')
434 || $self->ut_textn('comment')
435 || $self->ut_enum('selfservice_access', [ '', 'Y' ])
436 || $self->ut_textn('_password')
437 || $self->ut_enum('_password_encoding', [ '', 'bcrypt'])
438 || $self->ut_enum('disabled', [ '', 'Y' ])
440 return $error if $error;
442 return "No prospect or customer!" unless $self->prospectnum || $self->custnum;
443 return "Prospect and customer!" if $self->prospectnum && $self->custnum;
445 return "One of first name, last name, or title must have a value"
446 if ! grep $self->$_(), qw( first last title);
453 my $data = $self->first. ' '. $self->last;
454 $data .= ', '. $self->title
456 $data .= ' ('. $self->comment. ')'
463 return '' unless $self->locationnum;
464 qsearchs('cust_location', { 'locationnum' => $self->locationnum } );
469 return '' unless $self->classnum;
470 qsearchs('contact_class', { 'classnum' => $self->classnum } );
473 sub contact_classname {
475 my $contact_class = $self->contact_class or return '';
476 $contact_class->classname;
481 qsearch('contact_phone', { 'contactnum' => $self->contactnum } );
486 qsearch('contact_email', { 'contactnum' => $self->contactnum } );
491 qsearchs('cust_main', { 'custnum' => $self->custnum } );
494 sub by_selfservice_email {
495 my($class, $email) = @_;
497 my $contact_email = qsearchs({
498 'table' => 'contact_email',
499 'addl_from' => ' LEFT JOIN contact USING ( contactnum ) ',
500 'hashref' => { 'emailaddress' => $email, },
501 'extra_sql' => " AND selfservice_access = 'Y' ".
502 " AND ( disabled IS NULL OR disabled = '' )",
505 $contact_email->contact;
509 #these three functions are very much false laziness w/FS/FS/Auth/internal.pm
510 # and should maybe be libraried in some way for other password needs
512 use Crypt::Eksblowfish::Bcrypt qw( bcrypt_hash en_base64 de_base64);
514 sub authenticate_password {
515 my($self, $check_password) = @_;
517 if ( $self->_password_encoding eq 'bcrypt' ) {
519 my( $cost, $salt, $hash ) = split(',', $self->_password);
521 my $check_hash = en_base64( bcrypt_hash( { key_nul => 1,
523 salt => de_base64($salt),
529 $hash eq $check_hash;
533 return 0 if $self->_password eq '';
535 $self->_password eq $check_password;
541 sub change_password {
542 my($self, $new_password) = @_;
544 $self->change_password_fields( $new_password );
550 sub change_password_fields {
551 my($self, $new_password) = @_;
553 $self->_password_encoding('bcrypt');
557 my $salt = pack( 'C*', map int(rand(256)), 1..16 );
559 my $hash = bcrypt_hash( { key_nul => 1,
567 join(',', $cost, en_base64($salt), en_base64($hash) )
572 # end of false laziness w/FS/FS/Auth/internal.pm
575 #false laziness w/ClientAPI/MyAccount/reset_passwd
576 use Digest::SHA qw(sha512_hex);
578 use FS::ClientAPI_SessionCache;
579 sub send_reset_email {
580 my( $self, %opt ) = @_;
582 my @contact_email = $self->contact_email or return '';
584 my $reset_session = {
585 'contactnum' => $self->contactnum,
586 'svcnum' => $opt{'svcnum'},
589 my $timeout = '24 hours'; #?
591 my $reset_session_id;
593 $reset_session_id = sha512_hex(time(). {}. rand(). $$)
594 } until ( ! defined $self->myaccount_cache->get("reset_passwd_$reset_session_id") );
597 $self->myaccount_cache->set( "reset_passwd_$reset_session_id", $reset_session, $timeout );
601 my $conf = new FS::Conf;
603 my $cust_main = $self->cust_main
604 or die "no customer"; #reset a password for a prospect contact? someday
606 my $msgnum = $conf->config('selfservice-password_reset_msgnum', $cust_main->agentnum);
607 #die "selfservice-password_reset_msgnum unset" unless $msgnum;
608 return { 'error' => "selfservice-password_reset_msgnum unset" } unless $msgnum;
609 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum } );
611 'to' => join(',', map $_->emailaddress, @contact_email ),
612 'cust_main' => $cust_main,
614 'substitutions' => { 'session_id' => $reset_session_id }
617 if ( $opt{'queue'} ) { #or should queueing just be the default?
619 my $queue = new FS::queue {
620 'job' => 'FS::Misc::process_send_email',
621 'custnum' => $cust_main->custnum,
623 $queue->insert( $msg_template->prepare( %msg_template ) );
627 $msg_template->send( %msg_template );
633 use vars qw( $myaccount_cache );
634 sub myaccount_cache {
636 $myaccount_cache ||= new FS::ClientAPI_SessionCache( {
637 'namespace' => 'FS::ClientAPI::MyAccount',
647 L<FS::Record>, schema.html from the base documentation.