4 use vars qw( @ISA $DEBUG $me $conf $skip_fuzzyfiles
5 $dir_prefix @shells $usernamemin
6 $usernamemax $passwordmin $passwordmax
7 $username_ampersand $username_letter $username_letterfirst
8 $username_noperiod $username_nounderscore $username_nodash
9 $username_uppercase $username_percent $username_colon
10 $password_noampersand $password_noexclamation
11 $warning_template $warning_from $warning_subject $warning_mimetype
14 $radius_password $radius_ip
17 use Scalar::Util qw( blessed );
21 use Crypt::PasswdMD5 1.2;
24 use Authen::Passphrase;
25 use FS::UID qw( datasrc driver_name );
27 use FS::Record qw( qsearch qsearchs fields dbh dbdef );
28 use FS::Msgcat qw(gettext);
29 use FS::UI::bytecount;
35 use FS::cust_main_invoice;
39 use FS::radius_usergroup;
46 @ISA = qw( FS::svc_Common );
49 $me = '[FS::svc_acct]';
51 #ask FS::UID to run this stuff for us later
52 FS::UID->install_callback( sub {
54 $dir_prefix = $conf->config('home');
55 @shells = $conf->config('shells');
56 $usernamemin = $conf->config('usernamemin') || 2;
57 $usernamemax = $conf->config('usernamemax');
58 $passwordmin = $conf->config('passwordmin') || 6;
59 $passwordmax = $conf->config('passwordmax') || 8;
60 $username_letter = $conf->exists('username-letter');
61 $username_letterfirst = $conf->exists('username-letterfirst');
62 $username_noperiod = $conf->exists('username-noperiod');
63 $username_nounderscore = $conf->exists('username-nounderscore');
64 $username_nodash = $conf->exists('username-nodash');
65 $username_uppercase = $conf->exists('username-uppercase');
66 $username_ampersand = $conf->exists('username-ampersand');
67 $username_percent = $conf->exists('username-percent');
68 $username_colon = $conf->exists('username-colon');
69 $password_noampersand = $conf->exists('password-noexclamation');
70 $password_noexclamation = $conf->exists('password-noexclamation');
71 $dirhash = $conf->config('dirhash') || 0;
72 if ( $conf->exists('warning_email') ) {
73 $warning_template = new Text::Template (
75 SOURCE => [ map "$_\n", $conf->config('warning_email') ]
76 ) or warn "can't create warning email template: $Text::Template::ERROR";
77 $warning_from = $conf->config('warning_email-from'); # || 'your-isp-is-dum'
78 $warning_subject = $conf->config('warning_email-subject') || 'Warning';
79 $warning_mimetype = $conf->config('warning_email-mimetype') || 'text/plain';
80 $warning_cc = $conf->config('warning_email-cc');
82 $warning_template = '';
84 $warning_subject = '';
85 $warning_mimetype = '';
88 $smtpmachine = $conf->config('smtpmachine');
89 $radius_password = $conf->config('radius-password') || 'Password';
90 $radius_ip = $conf->config('radius-ip') || 'Framed-IP-Address';
91 @pw_set = ( 'A'..'Z' ) if $conf->exists('password-generated-allcaps');
95 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
96 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
100 my ( $hashref, $cache ) = @_;
101 if ( $hashref->{'svc_acct_svcnum'} ) {
102 $self->{'_domsvc'} = FS::svc_domain->new( {
103 'svcnum' => $hashref->{'domsvc'},
104 'domain' => $hashref->{'svc_acct_domain'},
105 'catchall' => $hashref->{'svc_acct_catchall'},
112 FS::svc_acct - Object methods for svc_acct records
118 $record = new FS::svc_acct \%hash;
119 $record = new FS::svc_acct { 'column' => 'value' };
121 $error = $record->insert;
123 $error = $new_record->replace($old_record);
125 $error = $record->delete;
127 $error = $record->check;
129 $error = $record->suspend;
131 $error = $record->unsuspend;
133 $error = $record->cancel;
135 %hash = $record->radius;
137 %hash = $record->radius_reply;
139 %hash = $record->radius_check;
141 $domain = $record->domain;
143 $svc_domain = $record->svc_domain;
145 $email = $record->email;
147 $seconds_since = $record->seconds_since($timestamp);
151 An FS::svc_acct object represents an account. FS::svc_acct inherits from
152 FS::svc_Common. The following fields are currently supported:
156 =item svcnum - primary key (assigned automatcially for new accounts)
160 =item _password - generated if blank
162 =item _password_encoding - plain, crypt, ldap (or empty for autodetection)
164 =item sec_phrase - security phrase
166 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
174 =item dir - set automatically if blank (and uid is not)
178 =item quota - (unimplementd)
180 =item slipip - IP address
190 =item domsvc - svcnum from svc_domain
192 =item radius_I<Radius_Attribute> - I<Radius-Attribute> (reply)
194 =item rc_I<Radius_Attribute> - I<Radius-Attribute> (check)
204 Creates a new account. To add the account to the database, see L<"insert">.
211 'longname_plural' => 'Access accounts and mailboxes',
212 'sorts' => [ 'username', 'uid', 'seconds', 'last_login' ],
213 'display_weight' => 10,
214 'cancel_weight' => 50,
216 'dir' => 'Home directory',
219 def_info => 'set to fixed and blank for no UIDs',
222 'slipip' => 'IP address',
223 # 'popnum' => qq!<A HREF="$p/browse/svc_acct_pop.cgi/">POP number</A>!,
225 label => 'Access number',
227 select_table => 'svc_acct_pop',
228 select_key => 'popnum',
229 select_label => 'city',
235 disable_default => 1,
242 disable_inventory => 1,
245 '_password' => 'Password',
248 def_info => 'when blank, defaults to UID',
253 def_info => 'set to blank for no shell tracking',
255 #select_list => [ $conf->config('shells') ],
256 select_list => [ $conf ? $conf->config('shells') : () ],
257 disable_inventory => 1,
260 'finger' => 'Real name', # (GECOS)',
264 select_table => 'svc_domain',
265 select_key => 'svcnum',
266 select_label => 'domain',
267 disable_inventory => 1,
271 label => 'RADIUS groups',
272 type => 'radius_usergroup_selector',
273 disable_inventory => 1,
276 'seconds' => { label => 'Seconds',
277 label_sort => 'with Time Remaining',
279 disable_inventory => 1,
281 disable_part_svc_column => 1,
283 'upbytes' => { label => 'Upload',
285 disable_inventory => 1,
287 'format' => \&FS::UI::bytecount::display_bytecount,
288 'parse' => \&FS::UI::bytecount::parse_bytecount,
289 disable_part_svc_column => 1,
291 'downbytes' => { label => 'Download',
293 disable_inventory => 1,
295 'format' => \&FS::UI::bytecount::display_bytecount,
296 'parse' => \&FS::UI::bytecount::parse_bytecount,
297 disable_part_svc_column => 1,
299 'totalbytes'=> { label => 'Total up and download',
301 disable_inventory => 1,
303 'format' => \&FS::UI::bytecount::display_bytecount,
304 'parse' => \&FS::UI::bytecount::parse_bytecount,
305 disable_part_svc_column => 1,
307 'seconds_threshold' => { label => 'Seconds threshold',
309 disable_inventory => 1,
311 disable_part_svc_column => 1,
313 'upbytes_threshold' => { label => 'Upload threshold',
315 disable_inventory => 1,
317 'format' => \&FS::UI::bytecount::display_bytecount,
318 'parse' => \&FS::UI::bytecount::parse_bytecount,
319 disable_part_svc_column => 1,
321 'downbytes_threshold' => { label => 'Download threshold',
323 disable_inventory => 1,
325 'format' => \&FS::UI::bytecount::display_bytecount,
326 'parse' => \&FS::UI::bytecount::parse_bytecount,
327 disable_part_svc_column => 1,
329 'totalbytes_threshold'=> { label => 'Total up and download threshold',
331 disable_inventory => 1,
333 'format' => \&FS::UI::bytecount::display_bytecount,
334 'parse' => \&FS::UI::bytecount::parse_bytecount,
335 disable_part_svc_column => 1,
338 label => 'Last login',
342 label => 'Last logout',
349 sub table { 'svc_acct'; }
351 sub table_dupcheck_fields { ( 'username', 'domsvc' ); }
355 #false laziness with edit/svc_acct.cgi
357 my( $self, $groups ) = @_;
358 if ( ref($groups) eq 'ARRAY' ) {
360 } elsif ( length($groups) ) {
361 [ split(/\s*,\s*/, $groups) ];
370 shift->_lastlog('in', @_);
374 shift->_lastlog('out', @_);
378 my( $self, $op, $time ) = @_;
380 if ( defined($time) ) {
381 warn "$me last_log$op called on svcnum ". $self->svcnum.
382 ' ('. $self->email. "): $time\n"
387 my $sql = "UPDATE svc_acct SET last_log$op = ? WHERE svcnum = ?";
391 my $sth = $dbh->prepare( $sql )
392 or die "Error preparing $sql: ". $dbh->errstr;
393 my $rv = $sth->execute($time, $self->svcnum);
394 die "Error executing $sql: ". $sth->errstr
396 die "Can't update last_log$op for svcnum". $self->svcnum
399 $self->{'Hash'}->{"last_log$op"} = $time;
401 $self->getfield("last_log$op");
405 =item search_sql STRING
407 Class method which returns an SQL fragment to search for the given string.
412 my( $class, $string ) = @_;
413 if ( $string =~ /^([^@]+)@([^@]+)$/ ) {
414 my( $username, $domain ) = ( $1, $2 );
415 my $q_username = dbh->quote($username);
416 my @svc_domain = qsearch('svc_domain', { 'domain' => $domain } );
418 "svc_acct.username = $q_username AND ( ".
419 join( ' OR ', map { "svc_acct.domsvc = ". $_->svcnum; } @svc_domain ).
424 } elsif ( $string =~ /^(\d{1,3}\.){3}\d{1,3}$/ ) {
426 $class->search_sql_field('slipip', $string ).
428 $class->search_sql_field('username', $string ).
431 $class->search_sql_field('username', $string);
435 =item label [ END_TIMESTAMP [ START_TIMESTAMP ] ]
437 Returns the "username@domain" string for this account.
439 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
449 =item label_long [ END_TIMESTAMP [ START_TIMESTAMP ] ]
451 Returns a longer string label for this acccount ("Real Name <username@domain>"
452 if available, or "username@domain").
454 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
461 my $label = $self->label(@_);
462 my $finger = $self->finger;
463 return $label unless $finger =~ /\S/;
464 my $maxlen = 40 - length($label) - length($self->cust_svc->part_svc->svc);
465 $finger = substr($finger, 0, $maxlen-3).'...' if length($finger) > $maxlen;
469 =item insert [ , OPTION => VALUE ... ]
471 Adds this account to the database. If there is an error, returns the error,
472 otherwise returns false.
474 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
475 defined. An FS::cust_svc record will be created and inserted.
477 The additional field I<usergroup> can optionally be defined; if so it should
478 contain an arrayref of group names. See L<FS::radius_usergroup>.
480 The additional field I<child_objects> can optionally be defined; if so it
481 should contain an arrayref of FS::tablename objects. They will have their
482 svcnum fields set and will be inserted after this record, but before any
483 exports are run. Each element of the array can also optionally be a
484 two-element array reference containing the child object and the name of an
485 alternate field to be filled in with the newly-inserted svcnum, for example
486 C<[ $svc_forward, 'srcsvc' ]>
488 Currently available options are: I<depend_jobnum>
490 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
491 jobnums), all provisioning jobs will have a dependancy on the supplied
492 jobnum(s) (they will not run until the specific job(s) complete(s)).
494 (TODOC: L<FS::queue> and L<freeside-queued>)
496 (TODOC: new exports!)
505 warn "[$me] insert called on $self: ". Dumper($self).
506 "\nwith options: ". Dumper(%options);
509 local $SIG{HUP} = 'IGNORE';
510 local $SIG{INT} = 'IGNORE';
511 local $SIG{QUIT} = 'IGNORE';
512 local $SIG{TERM} = 'IGNORE';
513 local $SIG{TSTP} = 'IGNORE';
514 local $SIG{PIPE} = 'IGNORE';
516 my $oldAutoCommit = $FS::UID::AutoCommit;
517 local $FS::UID::AutoCommit = 0;
520 my $error = $self->check;
521 return $error if $error;
523 if ( $self->svcnum && qsearchs('cust_svc',{'svcnum'=>$self->svcnum}) ) {
524 my $cust_svc = qsearchs('cust_svc',{'svcnum'=>$self->svcnum});
525 unless ( $cust_svc ) {
526 $dbh->rollback if $oldAutoCommit;
527 return "no cust_svc record found for svcnum ". $self->svcnum;
529 $self->pkgnum($cust_svc->pkgnum);
530 $self->svcpart($cust_svc->svcpart);
533 # set usage fields and thresholds if unset but set in a package def
534 if ( $self->pkgnum ) {
535 my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
536 my $part_pkg = $cust_pkg->part_pkg if $cust_pkg;
537 if ( $part_pkg && $part_pkg->can('usage_valuehash') ) {
539 my %values = $part_pkg->usage_valuehash;
540 my $multiplier = $conf->exists('svc_acct-usage_threshold')
541 ? 1 - $conf->config('svc_acct-usage_threshold')/100
544 foreach ( keys %values ) {
545 next if $self->getfield($_);
546 $self->setfield( $_, $values{$_} );
547 $self->setfield( $_. '_threshold', int( $values{$_} * $multiplier ) );
554 $error = $self->SUPER::insert(
555 'jobnums' => \@jobnums,
556 'child_objects' => $self->child_objects,
560 $dbh->rollback if $oldAutoCommit;
564 if ( $self->usergroup ) {
565 foreach my $groupname ( @{$self->usergroup} ) {
566 my $radius_usergroup = new FS::radius_usergroup ( {
567 svcnum => $self->svcnum,
568 groupname => $groupname,
570 my $error = $radius_usergroup->insert;
572 $dbh->rollback if $oldAutoCommit;
578 unless ( $skip_fuzzyfiles ) {
579 $error = $self->queue_fuzzyfiles_update;
581 $dbh->rollback if $oldAutoCommit;
582 return "updating fuzzy search cache: $error";
586 my $cust_pkg = $self->cust_svc->cust_pkg;
589 my $cust_main = $cust_pkg->cust_main;
590 my $agentnum = $cust_main->agentnum;
592 if ( $conf->exists('emailinvoiceautoalways')
593 || $conf->exists('emailinvoiceauto')
594 && ! $cust_main->invoicing_list_emailonly
596 my @invoicing_list = $cust_main->invoicing_list;
597 push @invoicing_list, $self->email;
598 $cust_main->invoicing_list(\@invoicing_list);
602 my ($to,$welcome_template,$welcome_from,$welcome_subject,$welcome_subject_template,$welcome_mimetype)
603 = ('','','','','','');
605 if ( $conf->exists('welcome_email', $agentnum) ) {
606 $welcome_template = new Text::Template (
608 SOURCE => [ map "$_\n", $conf->config('welcome_email', $agentnum) ]
609 ) or warn "can't create welcome email template: $Text::Template::ERROR";
610 $welcome_from = $conf->config('welcome_email-from', $agentnum);
611 # || 'your-isp-is-dum'
612 $welcome_subject = $conf->config('welcome_email-subject', $agentnum)
614 $welcome_subject_template = new Text::Template (
616 SOURCE => $welcome_subject,
617 ) or warn "can't create welcome email subject template: $Text::Template::ERROR";
618 $welcome_mimetype = $conf->config('welcome_email-mimetype', $agentnum)
621 if ( $welcome_template && $cust_pkg ) {
622 my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
626 'custnum' => $self->custnum,
627 'username' => $self->username,
628 'password' => $self->_password,
629 'first' => $cust_main->first,
630 'last' => $cust_main->getfield('last'),
631 'pkg' => $cust_pkg->part_pkg->pkg,
633 my $wqueue = new FS::queue {
634 'svcnum' => $self->svcnum,
635 'job' => 'FS::svc_acct::send_email'
637 my $error = $wqueue->insert(
639 'from' => $welcome_from,
640 'subject' => $welcome_subject_template->fill_in( HASH => \%hash, ),
641 'mimetype' => $welcome_mimetype,
642 'body' => $welcome_template->fill_in( HASH => \%hash, ),
645 $dbh->rollback if $oldAutoCommit;
646 return "error queuing welcome email: $error";
649 if ( $options{'depend_jobnum'} ) {
650 warn "$me depend_jobnum found; adding to welcome email dependancies"
652 if ( ref($options{'depend_jobnum'}) ) {
653 warn "$me adding jobs ". join(', ', @{$options{'depend_jobnum'}} ).
654 "to welcome email dependancies"
656 push @jobnums, @{ $options{'depend_jobnum'} };
658 warn "$me adding job $options{'depend_jobnum'} ".
659 "to welcome email dependancies"
661 push @jobnums, $options{'depend_jobnum'};
665 foreach my $jobnum ( @jobnums ) {
666 my $error = $wqueue->depend_insert($jobnum);
668 $dbh->rollback if $oldAutoCommit;
669 return "error queuing welcome email job dependancy: $error";
679 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
685 Deletes this account from the database. If there is an error, returns the
686 error, otherwise returns false.
688 The corresponding FS::cust_svc record will be deleted as well.
690 (TODOC: new exports!)
697 return "can't delete system account" if $self->_check_system;
699 return "Can't delete an account which is a (svc_forward) source!"
700 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
702 return "Can't delete an account which is a (svc_forward) destination!"
703 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
705 return "Can't delete an account with (svc_www) web service!"
706 if qsearch( 'svc_www', { 'usersvc' => $self->svcnum } );
708 # what about records in session ? (they should refer to history table)
710 local $SIG{HUP} = 'IGNORE';
711 local $SIG{INT} = 'IGNORE';
712 local $SIG{QUIT} = 'IGNORE';
713 local $SIG{TERM} = 'IGNORE';
714 local $SIG{TSTP} = 'IGNORE';
715 local $SIG{PIPE} = 'IGNORE';
717 my $oldAutoCommit = $FS::UID::AutoCommit;
718 local $FS::UID::AutoCommit = 0;
721 foreach my $cust_main_invoice (
722 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
724 unless ( defined($cust_main_invoice) ) {
725 warn "WARNING: something's wrong with qsearch";
728 my %hash = $cust_main_invoice->hash;
729 $hash{'dest'} = $self->email;
730 my $new = new FS::cust_main_invoice \%hash;
731 my $error = $new->replace($cust_main_invoice);
733 $dbh->rollback if $oldAutoCommit;
738 foreach my $svc_domain (
739 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
741 my %hash = new FS::svc_domain->hash;
742 $hash{'catchall'} = '';
743 my $new = new FS::svc_domain \%hash;
744 my $error = $new->replace($svc_domain);
746 $dbh->rollback if $oldAutoCommit;
751 my $error = $self->SUPER::delete;
753 $dbh->rollback if $oldAutoCommit;
757 foreach my $radius_usergroup (
758 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } )
760 my $error = $radius_usergroup->delete;
762 $dbh->rollback if $oldAutoCommit;
767 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
771 =item replace OLD_RECORD
773 Replaces OLD_RECORD with this one in the database. If there is an error,
774 returns the error, otherwise returns false.
776 The additional field I<usergroup> can optionally be defined; if so it should
777 contain an arrayref of group names. See L<FS::radius_usergroup>.
785 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
789 warn "$me replacing $old with $new\n" if $DEBUG;
793 return "can't modify system account" if $old->_check_system;
796 #no warnings 'numeric'; #alas, a 5.006-ism
799 foreach my $xid (qw( uid gid )) {
801 return "Can't change $xid!"
802 if ! $conf->exists("svc_acct-edit_$xid")
803 && $old->$xid() != $new->$xid()
804 && $new->cust_svc->part_svc->part_svc_column($xid)->columnflag ne 'F'
809 #change homdir when we change username
810 $new->setfield('dir', '') if $old->username ne $new->username;
812 local $SIG{HUP} = 'IGNORE';
813 local $SIG{INT} = 'IGNORE';
814 local $SIG{QUIT} = 'IGNORE';
815 local $SIG{TERM} = 'IGNORE';
816 local $SIG{TSTP} = 'IGNORE';
817 local $SIG{PIPE} = 'IGNORE';
819 my $oldAutoCommit = $FS::UID::AutoCommit;
820 local $FS::UID::AutoCommit = 0;
823 # redundant, but so $new->usergroup gets set
824 $error = $new->check;
825 return $error if $error;
827 $old->usergroup( [ $old->radius_groups ] );
829 warn $old->email. " old groups: ". join(' ',@{$old->usergroup}). "\n";
830 warn $new->email. "new groups: ". join(' ',@{$new->usergroup}). "\n";
832 if ( $new->usergroup ) {
833 #(sorta) false laziness with FS::part_export::sqlradius::_export_replace
834 my @newgroups = @{$new->usergroup};
835 foreach my $oldgroup ( @{$old->usergroup} ) {
836 if ( grep { $oldgroup eq $_ } @newgroups ) {
837 @newgroups = grep { $oldgroup ne $_ } @newgroups;
840 my $radius_usergroup = qsearchs('radius_usergroup', {
841 svcnum => $old->svcnum,
842 groupname => $oldgroup,
844 my $error = $radius_usergroup->delete;
846 $dbh->rollback if $oldAutoCommit;
847 return "error deleting radius_usergroup $oldgroup: $error";
851 foreach my $newgroup ( @newgroups ) {
852 my $radius_usergroup = new FS::radius_usergroup ( {
853 svcnum => $new->svcnum,
854 groupname => $newgroup,
856 my $error = $radius_usergroup->insert;
858 $dbh->rollback if $oldAutoCommit;
859 return "error adding radius_usergroup $newgroup: $error";
865 $error = $new->SUPER::replace($old, @_);
867 $dbh->rollback if $oldAutoCommit;
868 return $error if $error;
871 if ( $new->username ne $old->username && ! $skip_fuzzyfiles ) {
872 $error = $new->queue_fuzzyfiles_update;
874 $dbh->rollback if $oldAutoCommit;
875 return "updating fuzzy search cache: $error";
879 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
883 =item queue_fuzzyfiles_update
885 Used by insert & replace to update the fuzzy search cache
889 sub queue_fuzzyfiles_update {
892 local $SIG{HUP} = 'IGNORE';
893 local $SIG{INT} = 'IGNORE';
894 local $SIG{QUIT} = 'IGNORE';
895 local $SIG{TERM} = 'IGNORE';
896 local $SIG{TSTP} = 'IGNORE';
897 local $SIG{PIPE} = 'IGNORE';
899 my $oldAutoCommit = $FS::UID::AutoCommit;
900 local $FS::UID::AutoCommit = 0;
903 my $queue = new FS::queue {
904 'svcnum' => $self->svcnum,
905 'job' => 'FS::svc_acct::append_fuzzyfiles'
907 my $error = $queue->insert($self->username);
909 $dbh->rollback if $oldAutoCommit;
910 return "queueing job (transaction rolled back): $error";
913 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
921 Suspends this account by calling export-specific suspend hooks. If there is
922 an error, returns the error, otherwise returns false.
924 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
930 return "can't suspend system account" if $self->_check_system;
931 $self->SUPER::suspend(@_);
936 Unsuspends this account by by calling export-specific suspend hooks. If there
937 is an error, returns the error, otherwise returns false.
939 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
945 my %hash = $self->hash;
946 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
947 $hash{_password} = $1;
948 my $new = new FS::svc_acct ( \%hash );
949 my $error = $new->replace($self);
950 return $error if $error;
953 $self->SUPER::unsuspend(@_);
958 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
960 If the B<auto_unset_catchall> configuration option is set, this method will
961 automatically remove any references to the canceled service in the catchall
962 field of svc_domain. This allows packages that contain both a svc_domain and
963 its catchall svc_acct to be canceled in one step.
968 # Only one thing to do at this level
970 foreach my $svc_domain (
971 qsearch( 'svc_domain', { catchall => $self->svcnum } ) ) {
972 if($conf->exists('auto_unset_catchall')) {
973 my %hash = $svc_domain->hash;
974 $hash{catchall} = '';
975 my $new = new FS::svc_domain ( \%hash );
976 my $error = $new->replace($svc_domain);
977 return $error if $error;
979 return "cannot unprovision svc_acct #".$self->svcnum.
980 " while assigned as catchall for svc_domain #".$svc_domain->svcnum;
984 $self->SUPER::cancel(@_);
990 Checks all fields to make sure this is a valid service. If there is an error,
991 returns the error, otherwise returns false. Called by the insert and replace
994 Sets any fixed values; see L<FS::part_svc>.
1001 my($recref) = $self->hashref;
1003 my $x = $self->setfixed( $self->_fieldhandlers );
1004 return $x unless ref($x);
1007 if ( $part_svc->part_svc_column('usergroup')->columnflag eq "F" ) {
1009 [ split(',', $part_svc->part_svc_column('usergroup')->columnvalue) ] );
1012 my $error = $self->ut_numbern('svcnum')
1013 #|| $self->ut_number('domsvc')
1014 || $self->ut_foreign_key('domsvc', 'svc_domain', 'svcnum' )
1015 || $self->ut_textn('sec_phrase')
1016 || $self->ut_snumbern('seconds')
1017 || $self->ut_snumbern('upbytes')
1018 || $self->ut_snumbern('downbytes')
1019 || $self->ut_snumbern('totalbytes')
1020 || $self->ut_enum( '_password_encoding',
1021 [ '', qw( plain crypt ldap ) ]
1024 return $error if $error;
1026 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
1027 if ( $username_uppercase ) {
1028 $recref->{username} =~ /^([a-z0-9_\-\.\&\%\:]{$usernamemin,$ulen})$/i
1029 or return gettext('illegal_username'). " ($usernamemin-$ulen): ". $recref->{username};
1030 $recref->{username} = $1;
1032 $recref->{username} =~ /^([a-z0-9_\-\.\&\%\:]{$usernamemin,$ulen})$/
1033 or return gettext('illegal_username'). " ($usernamemin-$ulen): ". $recref->{username};
1034 $recref->{username} = $1;
1037 if ( $username_letterfirst ) {
1038 $recref->{username} =~ /^[a-z]/ or return gettext('illegal_username');
1039 } elsif ( $username_letter ) {
1040 $recref->{username} =~ /[a-z]/ or return gettext('illegal_username');
1042 if ( $username_noperiod ) {
1043 $recref->{username} =~ /\./ and return gettext('illegal_username');
1045 if ( $username_nounderscore ) {
1046 $recref->{username} =~ /_/ and return gettext('illegal_username');
1048 if ( $username_nodash ) {
1049 $recref->{username} =~ /\-/ and return gettext('illegal_username');
1051 unless ( $username_ampersand ) {
1052 $recref->{username} =~ /\&/ and return gettext('illegal_username');
1054 unless ( $username_percent ) {
1055 $recref->{username} =~ /\%/ and return gettext('illegal_username');
1057 unless ( $username_colon ) {
1058 $recref->{username} =~ /\:/ and return gettext('illegal_username');
1061 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
1062 $recref->{popnum} = $1;
1063 return "Unknown popnum" unless
1064 ! $recref->{popnum} ||
1065 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
1067 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
1069 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
1070 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
1072 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
1073 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
1074 #not all systems use gid=uid
1075 #you can set a fixed gid in part_svc
1077 return "Only root can have uid 0"
1078 if $recref->{uid} == 0
1079 && $recref->{username} !~ /^(root|toor|smtp)$/;
1081 unless ( $recref->{username} eq 'sync' ) {
1082 if ( grep $_ eq $recref->{shell}, @shells ) {
1083 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
1085 return "Illegal shell \`". $self->shell. "\'; ".
1086 "shells configuration value contains: @shells";
1089 $recref->{shell} = '/bin/sync';
1093 $recref->{gid} ne '' ?
1094 return "Can't have gid without uid" : ( $recref->{gid}='' );
1095 #$recref->{dir} ne '' ?
1096 # return "Can't have directory without uid" : ( $recref->{dir}='' );
1097 $recref->{shell} ne '' ?
1098 return "Can't have shell without uid" : ( $recref->{shell}='' );
1101 unless ( $part_svc->part_svc_column('dir')->columnflag eq 'F' ) {
1103 $recref->{dir} =~ /^([\/\w\-\.\&]*)$/
1104 or return "Illegal directory: ". $recref->{dir};
1105 $recref->{dir} = $1;
1106 return "Illegal directory"
1107 if $recref->{dir} =~ /(^|\/)\.+(\/|$)/; #no .. component
1108 return "Illegal directory"
1109 if $recref->{dir} =~ /\&/ && ! $username_ampersand;
1110 unless ( $recref->{dir} ) {
1111 $recref->{dir} = $dir_prefix . '/';
1112 if ( $dirhash > 0 ) {
1113 for my $h ( 1 .. $dirhash ) {
1114 $recref->{dir} .= substr($recref->{username}, $h-1, 1). '/';
1116 } elsif ( $dirhash < 0 ) {
1117 for my $h ( reverse $dirhash .. -1 ) {
1118 $recref->{dir} .= substr($recref->{username}, $h, 1). '/';
1121 $recref->{dir} .= $recref->{username};
1127 # $error = $self->ut_textn('finger');
1128 # return $error if $error;
1129 if ( $self->getfield('finger') eq '' ) {
1130 my $cust_pkg = $self->svcnum
1131 ? $self->cust_svc->cust_pkg
1132 : qsearchs('cust_pkg', { 'pkgnum' => $self->getfield('pkgnum') } );
1134 my $cust_main = $cust_pkg->cust_main;
1135 $self->setfield('finger', $cust_main->first.' '.$cust_main->get('last') );
1138 $self->getfield('finger') =~
1139 /^([\w \t\!\@\#\$\%\&\(\)\-\+\;\'\"\,\.\?\/\*\<\>]*)$/
1140 or return "Illegal finger: ". $self->getfield('finger');
1141 $self->setfield('finger', $1);
1143 $recref->{quota} =~ /^(\w*)$/ or return "Illegal quota";
1144 $recref->{quota} = $1;
1146 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
1147 if ( $recref->{slipip} eq '' ) {
1148 $recref->{slipip} = '';
1149 } elsif ( $recref->{slipip} eq '0e0' ) {
1150 $recref->{slipip} = '0e0';
1152 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
1153 or return "Illegal slipip: ". $self->slipip;
1154 $recref->{slipip} = $1;
1159 #arbitrary RADIUS stuff; allow ut_textn for now
1160 foreach ( grep /^radius_/, fields('svc_acct') ) {
1161 $self->ut_textn($_);
1164 if ( $recref->{_password_encoding} eq 'ldap' ) {
1166 if ( $recref->{_password} =~ /^(\{[\w\-]+\})(!?.{0,64})$/ ) {
1167 $recref->{_password} = uc($1).$2;
1169 return 'Illegal (ldap-encoded) password: '. $recref->{_password};
1172 } elsif ( $recref->{_password_encoding} eq 'crypt' ) {
1174 if ( $recref->{_password} =~
1175 #/^(\$\w+\$.*|[\w\+\/]{13}|_[\w\+\/]{19}|\*)$/
1176 /^(!!?)?(\$\w+\$.*|[\w\+\/\.]{13}|_[\w\+\/\.]{19}|\*)$/
1179 $recref->{_password} = ( defined($1) ? $1 : '' ). $2;
1182 return 'Illegal (crypt-encoded) password: '. $recref->{_password};
1185 } elsif ( $recref->{_password_encoding} eq 'plain' ) {
1187 #generate a password if it is blank
1188 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
1189 unless length( $recref->{_password} );
1191 if ( $recref->{_password} =~ /^([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1192 $recref->{_password} = $1;
1194 return gettext('illegal_password'). " $passwordmin-$passwordmax ".
1195 FS::Msgcat::_gettext('illegal_password_characters').
1196 ": ". $recref->{_password};
1199 if ( $password_noampersand ) {
1200 $recref->{_password} =~ /\&/ and return gettext('illegal_password');
1202 if ( $password_noexclamation ) {
1203 $recref->{_password} =~ /\!/ and return gettext('illegal_password');
1208 #carp "warning: _password_encoding unspecified\n";
1210 #generate a password if it is blank
1211 unless ( length( $recref->{_password} ) ) {
1213 $recref->{_password} =
1214 join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) );
1215 $recref->{_password_encoding} = 'plain';
1219 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
1220 if ( $recref->{_password} =~ /^((\*SUSPENDED\* |!!?)?)([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1221 $recref->{_password} = $1.$3;
1222 $recref->{_password_encoding} = 'plain';
1223 } elsif ( $recref->{_password} =~
1224 /^((\*SUSPENDED\* |!!?)?)([\w\.\/\$\;\+]{13,64})$/
1226 $recref->{_password} = $1.$3;
1227 $recref->{_password_encoding} = 'crypt';
1228 } elsif ( $recref->{_password} eq '*' ) {
1229 $recref->{_password} = '*';
1230 $recref->{_password_encoding} = 'crypt';
1231 } elsif ( $recref->{_password} eq '!' ) {
1232 $recref->{_password_encoding} = 'crypt';
1233 $recref->{_password} = '!';
1234 } elsif ( $recref->{_password} eq '!!' ) {
1235 $recref->{_password} = '!!';
1236 $recref->{_password_encoding} = 'crypt';
1238 #return "Illegal password";
1239 return gettext('illegal_password'). " $passwordmin-$passwordmax ".
1240 FS::Msgcat::_gettext('illegal_password_characters').
1241 ": ". $recref->{_password};
1248 $self->SUPER::check;
1254 Internal function to check the username against the list of system usernames
1255 from the I<system_usernames> configuration value. Returns true if the username
1256 is listed on the system username list.
1262 scalar( grep { $self->username eq $_ || $self->email eq $_ }
1263 $conf->config('system_usernames')
1267 =item _check_duplicate
1269 Internal method to check for duplicates usernames, username@domain pairs and
1272 If the I<global_unique-username> configuration value is set to B<username> or
1273 B<username@domain>, enforces global username or username@domain uniqueness.
1275 In all cases, check for duplicate uids and usernames or username@domain pairs
1276 per export and with identical I<svcpart> values.
1280 sub _check_duplicate {
1283 my $global_unique = $conf->config('global_unique-username') || 'none';
1284 return '' if $global_unique eq 'disabled';
1288 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
1289 unless ( $part_svc ) {
1290 return 'unknown svcpart '. $self->svcpart;
1293 my @dup_user = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1294 qsearch( 'svc_acct', { 'username' => $self->username } );
1295 return gettext('username_in_use')
1296 if $global_unique eq 'username' && @dup_user;
1298 my @dup_userdomain = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1299 qsearch( 'svc_acct', { 'username' => $self->username,
1300 'domsvc' => $self->domsvc } );
1301 return gettext('username_in_use')
1302 if $global_unique eq 'username@domain' && @dup_userdomain;
1305 if ( $part_svc->part_svc_column('uid')->columnflag ne 'F'
1306 && $self->username !~ /^(toor|(hyla)?fax)$/ ) {
1307 @dup_uid = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1308 qsearch( 'svc_acct', { 'uid' => $self->uid } );
1313 if ( @dup_user || @dup_userdomain || @dup_uid ) {
1314 my $exports = FS::part_export::export_info('svc_acct');
1315 my %conflict_user_svcpart;
1316 my %conflict_userdomain_svcpart = ( $self->svcpart => 'SELF', );
1318 foreach my $part_export ( $part_svc->part_export ) {
1320 #this will catch to the same exact export
1321 my @svcparts = map { $_->svcpart } $part_export->export_svc;
1323 #this will catch to exports w/same exporthost+type ???
1324 #my @other_part_export = qsearch('part_export', {
1325 # 'machine' => $part_export->machine,
1326 # 'exporttype' => $part_export->exporttype,
1328 #foreach my $other_part_export ( @other_part_export ) {
1329 # push @svcparts, map { $_->svcpart }
1330 # qsearch('export_svc', { 'exportnum' => $part_export->exportnum });
1333 #my $nodomain = $exports->{$part_export->exporttype}{'nodomain'};
1334 #silly kludge to avoid uninitialized value errors
1335 my $nodomain = exists( $exports->{$part_export->exporttype}{'nodomain'} )
1336 ? $exports->{$part_export->exporttype}{'nodomain'}
1338 if ( $nodomain =~ /^Y/i ) {
1339 $conflict_user_svcpart{$_} = $part_export->exportnum
1342 $conflict_userdomain_svcpart{$_} = $part_export->exportnum
1347 foreach my $dup_user ( @dup_user ) {
1348 my $dup_svcpart = $dup_user->cust_svc->svcpart;
1349 if ( exists($conflict_user_svcpart{$dup_svcpart}) ) {
1350 return "duplicate username ". $self->username.
1351 ": conflicts with svcnum ". $dup_user->svcnum.
1352 " via exportnum ". $conflict_user_svcpart{$dup_svcpart};
1356 foreach my $dup_userdomain ( @dup_userdomain ) {
1357 my $dup_svcpart = $dup_userdomain->cust_svc->svcpart;
1358 if ( exists($conflict_userdomain_svcpart{$dup_svcpart}) ) {
1359 return "duplicate username\@domain ". $self->email.
1360 ": conflicts with svcnum ". $dup_userdomain->svcnum.
1361 " via exportnum ". $conflict_userdomain_svcpart{$dup_svcpart};
1365 foreach my $dup_uid ( @dup_uid ) {
1366 my $dup_svcpart = $dup_uid->cust_svc->svcpart;
1367 if ( exists($conflict_user_svcpart{$dup_svcpart})
1368 || exists($conflict_userdomain_svcpart{$dup_svcpart}) ) {
1369 return "duplicate uid ". $self->uid.
1370 ": conflicts with svcnum ". $dup_uid->svcnum.
1372 ( $conflict_user_svcpart{$dup_svcpart}
1373 || $conflict_userdomain_svcpart{$dup_svcpart} );
1385 Depriciated, use radius_reply instead.
1390 carp "FS::svc_acct::radius depriciated, use radius_reply";
1391 $_[0]->radius_reply;
1396 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1397 reply attributes of this record.
1399 Note that this is now the preferred method for reading RADIUS attributes -
1400 accessing the columns directly is discouraged, as the column names are
1401 expected to change in the future.
1408 return %{ $self->{'radius_reply'} }
1409 if exists $self->{'radius_reply'};
1414 my($column, $attrib) = ($1, $2);
1415 #$attrib =~ s/_/\-/g;
1416 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1417 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
1419 if ( $self->slipip && $self->slipip ne '0e0' ) {
1420 $reply{$radius_ip} = $self->slipip;
1423 if ( $self->seconds !~ /^$/ ) {
1424 $reply{'Session-Timeout'} = $self->seconds;
1432 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1433 check attributes of this record.
1435 Note that this is now the preferred method for reading RADIUS attributes -
1436 accessing the columns directly is discouraged, as the column names are
1437 expected to change in the future.
1444 return %{ $self->{'radius_check'} }
1445 if exists $self->{'radius_check'};
1450 my($column, $attrib) = ($1, $2);
1451 #$attrib =~ s/_/\-/g;
1452 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1453 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table );
1456 my($pw_attrib, $password) = $self->radius_password;
1457 $check{$pw_attrib} = $password;
1459 my $cust_svc = $self->cust_svc;
1460 die "FATAL: no cust_svc record for svc_acct.svcnum ". $self->svcnum. "\n"
1462 my $cust_pkg = $cust_svc->cust_pkg;
1463 if ( $cust_pkg && $cust_pkg->part_pkg->is_prepaid && $cust_pkg->bill ) {
1464 $check{'Expiration'} = time2str('%B %e %Y %T', $cust_pkg->bill ); #http://lists.cistron.nl/pipermail/freeradius-users/2005-January/040184.html
1471 =item radius_password
1473 Returns a key/value pair containing the RADIUS attribute name and value
1478 sub radius_password {
1481 my($pw_attrib, $password);
1482 if ( $self->_password_encoding eq 'ldap' ) {
1484 $pw_attrib = 'Password-With-Header';
1485 $password = $self->_password;
1487 } elsif ( $self->_password_encoding eq 'crypt' ) {
1489 $pw_attrib = 'Crypt-Password';
1490 $password = $self->_password;
1492 } elsif ( $self->_password_encoding eq 'plain' ) {
1494 $pw_attrib = $radius_password; #Cleartext-Password? man rlm_pap
1495 $password = $self->_password;
1499 $pw_attrib = length($password) <= 12 ? $radius_password : 'Crypt-Password';
1500 $password = $self->_password;
1504 ($pw_attrib, $password);
1510 This method instructs the object to "snapshot" or freeze RADIUS check and
1511 reply attributes to the current values.
1515 #bah, my english is too broken this morning
1516 #Of note is the "Expiration" attribute, which, for accounts in prepaid packages, is typically defined on-the-fly as the associated packages cust_pkg.bill. (This is used by
1517 #the FS::cust_pkg's replace method to trigger the correct export updates when
1518 #package dates change)
1523 $self->{$_} = { $self->$_() }
1524 foreach qw( radius_reply radius_check );
1528 =item forget_snapshot
1530 This methos instructs the object to forget any previously snapshotted
1531 RADIUS check and reply attributes.
1535 sub forget_snapshot {
1539 foreach qw( radius_reply radius_check );
1543 =item domain [ END_TIMESTAMP [ START_TIMESTAMP ] ]
1545 Returns the domain associated with this account.
1547 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
1554 die "svc_acct.domsvc is null for svcnum ". $self->svcnum unless $self->domsvc;
1555 my $svc_domain = $self->svc_domain(@_)
1556 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
1557 $svc_domain->domain;
1562 Returns the FS::svc_domain record for this account's domain (see
1567 # FS::h_svc_acct has a history-aware svc_domain override
1572 ? $self->{'_domsvc'}
1573 : qsearchs( 'svc_domain', { 'svcnum' => $self->domsvc } );
1578 Returns the FS::cust_svc record for this account (see L<FS::cust_svc>).
1582 #inherited from svc_Common
1584 =item email [ END_TIMESTAMP [ START_TIMESTAMP ] ]
1586 Returns an email address associated with the account.
1588 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
1595 $self->username. '@'. $self->domain(@_);
1600 Returns an array of FS::acct_snarf records associated with the account.
1601 If the acct_snarf table does not exist or there are no associated records,
1602 an empty list is returned
1608 return () unless dbdef->table('acct_snarf');
1609 eval "use FS::acct_snarf;";
1611 qsearch('acct_snarf', { 'svcnum' => $self->svcnum } );
1614 =item decrement_upbytes OCTETS
1616 Decrements the I<upbytes> field of this record by the given amount. If there
1617 is an error, returns the error, otherwise returns false.
1621 sub decrement_upbytes {
1622 shift->_op_usage('-', 'upbytes', @_);
1625 =item increment_upbytes OCTETS
1627 Increments the I<upbytes> field of this record by the given amount. If there
1628 is an error, returns the error, otherwise returns false.
1632 sub increment_upbytes {
1633 shift->_op_usage('+', 'upbytes', @_);
1636 =item decrement_downbytes OCTETS
1638 Decrements the I<downbytes> field of this record by the given amount. If there
1639 is an error, returns the error, otherwise returns false.
1643 sub decrement_downbytes {
1644 shift->_op_usage('-', 'downbytes', @_);
1647 =item increment_downbytes OCTETS
1649 Increments the I<downbytes> field of this record by the given amount. If there
1650 is an error, returns the error, otherwise returns false.
1654 sub increment_downbytes {
1655 shift->_op_usage('+', 'downbytes', @_);
1658 =item decrement_totalbytes OCTETS
1660 Decrements the I<totalbytes> field of this record by the given amount. If there
1661 is an error, returns the error, otherwise returns false.
1665 sub decrement_totalbytes {
1666 shift->_op_usage('-', 'totalbytes', @_);
1669 =item increment_totalbytes OCTETS
1671 Increments the I<totalbytes> field of this record by the given amount. If there
1672 is an error, returns the error, otherwise returns false.
1676 sub increment_totalbytes {
1677 shift->_op_usage('+', 'totalbytes', @_);
1680 =item decrement_seconds SECONDS
1682 Decrements the I<seconds> field of this record by the given amount. If there
1683 is an error, returns the error, otherwise returns false.
1687 sub decrement_seconds {
1688 shift->_op_usage('-', 'seconds', @_);
1691 =item increment_seconds SECONDS
1693 Increments the I<seconds> field of this record by the given amount. If there
1694 is an error, returns the error, otherwise returns false.
1698 sub increment_seconds {
1699 shift->_op_usage('+', 'seconds', @_);
1707 my %op2condition = (
1708 '-' => sub { my($self, $column, $amount) = @_;
1709 $self->$column - $amount <= 0;
1711 '+' => sub { my($self, $column, $amount) = @_;
1712 ($self->$column || 0) + $amount > 0;
1715 my %op2warncondition = (
1716 '-' => sub { my($self, $column, $amount) = @_;
1717 my $threshold = $column . '_threshold';
1718 $self->$column - $amount <= $self->$threshold + 0;
1720 '+' => sub { my($self, $column, $amount) = @_;
1721 ($self->$column || 0) + $amount > 0;
1726 my( $self, $op, $column, $amount ) = @_;
1728 warn "$me _op_usage called for $column on svcnum ". $self->svcnum.
1729 ' ('. $self->email. "): $op $amount\n"
1732 return '' unless $amount;
1734 local $SIG{HUP} = 'IGNORE';
1735 local $SIG{INT} = 'IGNORE';
1736 local $SIG{QUIT} = 'IGNORE';
1737 local $SIG{TERM} = 'IGNORE';
1738 local $SIG{TSTP} = 'IGNORE';
1739 local $SIG{PIPE} = 'IGNORE';
1741 my $oldAutoCommit = $FS::UID::AutoCommit;
1742 local $FS::UID::AutoCommit = 0;
1745 my $sql = "UPDATE svc_acct SET $column = ".
1746 " CASE WHEN $column IS NULL THEN 0 ELSE $column END ". #$column||0
1747 " $op ? WHERE svcnum = ?";
1751 my $sth = $dbh->prepare( $sql )
1752 or die "Error preparing $sql: ". $dbh->errstr;
1753 my $rv = $sth->execute($amount, $self->svcnum);
1754 die "Error executing $sql: ". $sth->errstr
1755 unless defined($rv);
1756 die "Can't update $column for svcnum". $self->svcnum
1759 my $action = $op2action{$op};
1761 if ( &{$op2condition{$op}}($self, $column, $amount) &&
1762 ( $action eq 'suspend' && !$self->overlimit
1763 || $action eq 'unsuspend' && $self->overlimit )
1765 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
1766 if ($part_export->option('overlimit_groups')) {
1768 my $other = new FS::svc_acct $self->hashref;
1769 my $groups = &{ $self->_fieldhandlers->{'usergroup'} }
1770 ($self, $part_export->option('overlimit_groups'));
1771 $other->usergroup( $groups );
1772 if ($action eq 'suspend'){
1773 $new = $other; $old = $self;
1775 $new = $self; $old = $other;
1777 my $error = $part_export->export_replace($new, $old);
1778 $error ||= $self->overlimit($action);
1780 $dbh->rollback if $oldAutoCommit;
1781 return "Error replacing radius groups in export, ${op}: $error";
1787 if ( $conf->exists("svc_acct-usage_$action")
1788 && &{$op2condition{$op}}($self, $column, $amount) ) {
1789 #my $error = $self->$action();
1790 my $error = $self->cust_svc->cust_pkg->$action();
1791 # $error ||= $self->overlimit($action);
1793 $dbh->rollback if $oldAutoCommit;
1794 return "Error ${action}ing: $error";
1798 if ($warning_template && &{$op2warncondition{$op}}($self, $column, $amount)) {
1799 my $wqueue = new FS::queue {
1800 'svcnum' => $self->svcnum,
1801 'job' => 'FS::svc_acct::reached_threshold',
1806 $to = $warning_cc if &{$op2condition{$op}}($self, $column, $amount);
1810 my $error = $wqueue->insert(
1811 'svcnum' => $self->svcnum,
1813 'column' => $column,
1817 $dbh->rollback if $oldAutoCommit;
1818 return "Error queuing threshold activity: $error";
1822 warn "$me update successful; committing\n"
1824 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1830 my( $self, $valueref, %options ) = @_;
1832 warn "$me set_usage called for svcnum ". $self->svcnum.
1833 ' ('. $self->email. "): ".
1834 join(', ', map { "$_ => " . $valueref->{$_}} keys %$valueref) . "\n"
1837 local $SIG{HUP} = 'IGNORE';
1838 local $SIG{INT} = 'IGNORE';
1839 local $SIG{QUIT} = 'IGNORE';
1840 local $SIG{TERM} = 'IGNORE';
1841 local $SIG{TSTP} = 'IGNORE';
1842 local $SIG{PIPE} = 'IGNORE';
1844 local $FS::svc_Common::noexport_hack = 1;
1845 my $oldAutoCommit = $FS::UID::AutoCommit;
1846 local $FS::UID::AutoCommit = 0;
1851 if ( $options{null} ) {
1852 %handyhash = ( map { ( $_ => 'NULL', $_."_threshold" => 'NULL' ) }
1853 qw( seconds upbytes downbytes totalbytes )
1856 foreach my $field (keys %$valueref){
1857 $reset = 1 if $valueref->{$field};
1858 $self->setfield($field, $valueref->{$field});
1859 $self->setfield( $field.'_threshold',
1860 int($self->getfield($field)
1861 * ( $conf->exists('svc_acct-usage_threshold')
1862 ? 1 - $conf->config('svc_acct-usage_threshold')/100
1867 $handyhash{$field} = $self->getfield($field);
1868 $handyhash{$field.'_threshold'} = $self->getfield($field.'_threshold');
1870 #my $error = $self->replace; #NO! we avoid the call to ->check for
1871 #die $error if $error; #services not explicity changed via the UI
1873 my $sql = "UPDATE svc_acct SET " .
1874 join (',', map { "$_ = $handyhash{$_}" } (keys %handyhash) ).
1875 " WHERE svcnum = ". $self->svcnum;
1880 if (scalar(keys %handyhash)) {
1881 my $sth = $dbh->prepare( $sql )
1882 or die "Error preparing $sql: ". $dbh->errstr;
1883 my $rv = $sth->execute();
1884 die "Error executing $sql: ". $sth->errstr
1885 unless defined($rv);
1886 die "Can't update usage for svcnum ". $self->svcnum
1893 if ($self->overlimit) {
1894 $error = $self->overlimit('unsuspend');
1895 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
1896 if ($part_export->option('overlimit_groups')) {
1897 my $old = new FS::svc_acct $self->hashref;
1898 my $groups = &{ $self->_fieldhandlers->{'usergroup'} }
1899 ($self, $part_export->option('overlimit_groups'));
1900 $old->usergroup( $groups );
1901 $error ||= $part_export->export_replace($self, $old);
1906 if ( $conf->exists("svc_acct-usage_unsuspend")) {
1907 $error ||= $self->cust_svc->cust_pkg->unsuspend;
1910 $dbh->rollback if $oldAutoCommit;
1911 return "Error unsuspending: $error";
1915 warn "$me update successful; committing\n"
1917 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1923 =item recharge HASHREF
1925 Increments usage columns by the amount specified in HASHREF as
1926 column=>amount pairs.
1931 my ($self, $vhash) = @_;
1934 warn "[$me] recharge called on $self: ". Dumper($self).
1935 "\nwith vhash: ". Dumper($vhash);
1938 my $oldAutoCommit = $FS::UID::AutoCommit;
1939 local $FS::UID::AutoCommit = 0;
1943 foreach my $column (keys %$vhash){
1944 $error ||= $self->_op_usage('+', $column, $vhash->{$column});
1948 $dbh->rollback if $oldAutoCommit;
1950 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1955 =item is_rechargeable
1957 Returns true if this svc_account can be "recharged" and false otherwise.
1961 sub is_rechargable {
1963 $self->seconds ne ''
1964 || $self->upbytes ne ''
1965 || $self->downbytes ne ''
1966 || $self->totalbytes ne '';
1969 =item seconds_since TIMESTAMP
1971 Returns the number of seconds this account has been online since TIMESTAMP,
1972 according to the session monitor (see L<FS::Session>).
1974 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
1975 L<Time::Local> and L<Date::Parse> for conversion functions.
1979 #note: POD here, implementation in FS::cust_svc
1982 $self->cust_svc->seconds_since(@_);
1985 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
1987 Returns the numbers of seconds this account has been online between
1988 TIMESTAMP_START (inclusive) and TIMESTAMP_END (exclusive), according to an
1989 external SQL radacct table, specified via sqlradius export. Sessions which
1990 started in the specified range but are still open are counted from session
1991 start to the end of the range (unless they are over 1 day old, in which case
1992 they are presumed missing their stop record and not counted). Also, sessions
1993 which end in the range but started earlier are counted from the start of the
1994 range to session end. Finally, sessions which start before the range but end
1995 after are counted for the entire range.
1997 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
1998 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
2003 #note: POD here, implementation in FS::cust_svc
2004 sub seconds_since_sqlradacct {
2006 $self->cust_svc->seconds_since_sqlradacct(@_);
2009 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
2011 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
2012 in this package for sessions ending between TIMESTAMP_START (inclusive) and
2013 TIMESTAMP_END (exclusive).
2015 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
2016 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
2021 #note: POD here, implementation in FS::cust_svc
2022 sub attribute_since_sqlradacct {
2024 $self->cust_svc->attribute_since_sqlradacct(@_);
2027 =item get_session_history TIMESTAMP_START TIMESTAMP_END
2029 Returns an array of hash references of this customers login history for the
2030 given time range. (document this better)
2034 sub get_session_history {
2036 $self->cust_svc->get_session_history(@_);
2039 =item last_login_text
2041 Returns text describing the time of last login.
2045 sub last_login_text {
2047 $self->last_login ? ctime($self->last_login) : 'unknown';
2050 =item get_cdrs TIMESTAMP_START TIMESTAMP_END [ 'OPTION' => 'VALUE ... ]
2055 my($self, $start, $end, %opt ) = @_;
2057 my $did = $self->username; #yup
2059 my $prefix = $opt{'default_prefix'}; #convergent.au '+61'
2061 my $for_update = $opt{'for_update'} ? 'FOR UPDATE' : '';
2063 #SELECT $for_update * FROM cdr
2064 # WHERE calldate >= $start #need a conversion
2065 # AND calldate < $end #ditto
2066 # AND ( charged_party = "$did"
2067 # OR charged_party = "$prefix$did" #if length($prefix);
2068 # OR ( ( charged_party IS NULL OR charged_party = '' )
2070 # ( src = "$did" OR src = "$prefix$did" ) # if length($prefix)
2073 # AND ( freesidestatus IS NULL OR freesidestatus = '' )
2076 if ( length($prefix) ) {
2078 " AND ( charged_party = '$did'
2079 OR charged_party = '$prefix$did'
2080 OR ( ( charged_party IS NULL OR charged_party = '' )
2082 ( src = '$did' OR src = '$prefix$did' )
2088 " AND ( charged_party = '$did'
2089 OR ( ( charged_party IS NULL OR charged_party = '' )
2099 'select' => "$for_update *",
2102 #( freesidestatus IS NULL OR freesidestatus = '' )
2103 'freesidestatus' => '',
2105 'extra_sql' => $charged_or_src,
2113 Returns all RADIUS groups for this account (see L<FS::radius_usergroup>).
2119 if ( $self->usergroup ) {
2120 confess "explicitly specified usergroup not an arrayref: ". $self->usergroup
2121 unless ref($self->usergroup) eq 'ARRAY';
2122 #when provisioning records, export callback runs in svc_Common.pm before
2123 #radius_usergroup records can be inserted...
2124 @{$self->usergroup};
2126 map { $_->groupname }
2127 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } );
2131 =item clone_suspended
2133 Constructor used by FS::part_export::_export_suspend fallback. Document
2138 sub clone_suspended {
2140 my %hash = $self->hash;
2141 $hash{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) );
2142 new FS::svc_acct \%hash;
2145 =item clone_kludge_unsuspend
2147 Constructor used by FS::part_export::_export_unsuspend fallback. Document
2152 sub clone_kludge_unsuspend {
2154 my %hash = $self->hash;
2155 $hash{_password} = '';
2156 new FS::svc_acct \%hash;
2159 =item check_password
2161 Checks the supplied password against the (possibly encrypted) password in the
2162 database. Returns true for a successful authentication, false for no match.
2164 Currently supported encryptions are: classic DES crypt() and MD5
2168 sub check_password {
2169 my($self, $check_password) = @_;
2171 #remove old-style SUSPENDED kludge, they should be allowed to login to
2172 #self-service and pay up
2173 ( my $password = $self->_password ) =~ s/^\*SUSPENDED\* //;
2175 if ( $self->_password_encoding eq 'ldap' ) {
2177 my $auth = from_rfc2307 Authen::Passphrase $self->_password;
2178 return $auth->match($check_password);
2180 } elsif ( $self->_password_encoding eq 'crypt' ) {
2182 my $auth = from_crypt Authen::Passphrase $self->_password;
2183 return $auth->match($check_password);
2185 } elsif ( $self->_password_encoding eq 'plain' ) {
2187 return $check_password eq $password;
2191 #XXX this could be replaced with Authen::Passphrase stuff
2193 if ( $password =~ /^(\*|!!?)$/ ) { #no self-service login
2195 } elsif ( length($password) < 13 ) { #plaintext
2196 $check_password eq $password;
2197 } elsif ( length($password) == 13 ) { #traditional DES crypt
2198 crypt($check_password, $password) eq $password;
2199 } elsif ( $password =~ /^\$1\$/ ) { #MD5 crypt
2200 unix_md5_crypt($check_password, $password) eq $password;
2201 } elsif ( $password =~ /^\$2a?\$/ ) { #Blowfish
2202 warn "Can't check password: Blowfish encryption not yet supported, ".
2203 "svcnum ". $self->svcnum. "\n";
2206 warn "Can't check password: Unrecognized encryption for svcnum ".
2207 $self->svcnum. "\n";
2215 =item crypt_password [ DEFAULT_ENCRYPTION_TYPE ]
2217 Returns an encrypted password, either by passing through an encrypted password
2218 in the database or by encrypting a plaintext password from the database.
2220 The optional DEFAULT_ENCRYPTION_TYPE parameter can be set to I<crypt> (classic
2221 UNIX DES crypt), I<md5> (md5 crypt supported by most modern Linux and BSD
2222 distrubtions), or (eventually) I<blowfish> (blowfish hashing supported by
2223 OpenBSD, SuSE, other Linux distibutions with pam_unix2, etc.). The default
2224 encryption type is only used if the password is not already encrypted in the
2229 sub crypt_password {
2232 if ( $self->_password_encoding eq 'ldap' ) {
2234 if ( $self->_password =~ /^\{(PLAIN|CLEARTEXT)\}(.+)$/ ) {
2237 #XXX this could be replaced with Authen::Passphrase stuff
2239 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2240 if ( $encryption eq 'crypt' ) {
2243 $saltset[int(rand(64))].$saltset[int(rand(64))]
2245 } elsif ( $encryption eq 'md5' ) {
2246 unix_md5_crypt( $self->_password );
2247 } elsif ( $encryption eq 'blowfish' ) {
2248 croak "unknown encryption method $encryption";
2250 croak "unknown encryption method $encryption";
2253 } elsif ( $self->_password =~ /^\{CRYPT\}(.+)$/ ) {
2257 } elsif ( $self->_password_encoding eq 'crypt' ) {
2259 return $self->_password;
2261 } elsif ( $self->_password_encoding eq 'plain' ) {
2263 #XXX this could be replaced with Authen::Passphrase stuff
2265 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2266 if ( $encryption eq 'crypt' ) {
2269 $saltset[int(rand(64))].$saltset[int(rand(64))]
2271 } elsif ( $encryption eq 'md5' ) {
2272 unix_md5_crypt( $self->_password );
2273 } elsif ( $encryption eq 'blowfish' ) {
2274 croak "unknown encryption method $encryption";
2276 croak "unknown encryption method $encryption";
2281 if ( length($self->_password) == 13
2282 || $self->_password =~ /^\$(1|2a?)\$/
2283 || $self->_password =~ /^(\*|NP|\*LK\*|!!?)$/
2289 #XXX this could be replaced with Authen::Passphrase stuff
2291 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2292 if ( $encryption eq 'crypt' ) {
2295 $saltset[int(rand(64))].$saltset[int(rand(64))]
2297 } elsif ( $encryption eq 'md5' ) {
2298 unix_md5_crypt( $self->_password );
2299 } elsif ( $encryption eq 'blowfish' ) {
2300 croak "unknown encryption method $encryption";
2302 croak "unknown encryption method $encryption";
2311 =item ldap_password [ DEFAULT_ENCRYPTION_TYPE ]
2313 Returns an encrypted password in "LDAP" format, with a curly-bracked prefix
2314 describing the format, for example, "{PLAIN}himom", "{CRYPT}94pAVyK/4oIBk" or
2315 "{MD5}5426824942db4253f87a1009fd5d2d4".
2317 The optional DEFAULT_ENCRYPTION_TYPE is not yet used, but the idea is for it
2318 to work the same as the B</crypt_password> method.
2324 #eventually should check a "password-encoding" field
2326 if ( $self->_password_encoding eq 'ldap' ) {
2328 return $self->_password;
2330 } elsif ( $self->_password_encoding eq 'crypt' ) {
2332 if ( length($self->_password) == 13 ) { #crypt
2333 return '{CRYPT}'. $self->_password;
2334 } elsif ( $self->_password =~ /^\$1\$(.*)$/ && length($1) == 31 ) { #passwdMD5
2336 #} elsif ( $self->_password =~ /^\$2a?\$(.*)$/ ) { #Blowfish
2337 # die "Blowfish encryption not supported in this context, svcnum ".
2338 # $self->svcnum. "\n";
2340 warn "encryption method not (yet?) supported in LDAP context";
2341 return '{CRYPT}*'; #unsupported, should not auth
2344 } elsif ( $self->_password_encoding eq 'plain' ) {
2346 return '{PLAIN}'. $self->_password;
2348 #return '{CLEARTEXT}'. $self->_password; #?
2352 if ( length($self->_password) == 13 ) { #crypt
2353 return '{CRYPT}'. $self->_password;
2354 } elsif ( $self->_password =~ /^\$1\$(.*)$/ && length($1) == 31 ) { #passwdMD5
2356 } elsif ( $self->_password =~ /^\$2a?\$(.*)$/ ) { #Blowfish
2357 warn "Blowfish encryption not supported in this context, svcnum ".
2358 $self->svcnum. "\n";
2361 #are these two necessary anymore?
2362 } elsif ( $self->_password =~ /^(\w{48})$/ ) { #LDAP SSHA
2363 return '{SSHA}'. $1;
2364 } elsif ( $self->_password =~ /^(\w{64})$/ ) { #LDAP NS-MTA-MD5
2365 return '{NS-MTA-MD5}'. $1;
2368 return '{PLAIN}'. $self->_password;
2370 #return '{CLEARTEXT}'. $self->_password; #?
2372 #XXX this could be replaced with Authen::Passphrase stuff if it gets used
2373 #my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2374 #if ( $encryption eq 'crypt' ) {
2375 # return '{CRYPT}'. crypt(
2377 # $saltset[int(rand(64))].$saltset[int(rand(64))]
2379 #} elsif ( $encryption eq 'md5' ) {
2380 # unix_md5_crypt( $self->_password );
2381 #} elsif ( $encryption eq 'blowfish' ) {
2382 # croak "unknown encryption method $encryption";
2384 # croak "unknown encryption method $encryption";
2392 =item domain_slash_username
2394 Returns $domain/$username/
2398 sub domain_slash_username {
2400 $self->domain. '/'. $self->username. '/';
2403 =item virtual_maildir
2405 Returns $domain/maildirs/$username/
2409 sub virtual_maildir {
2411 $self->domain. '/maildirs/'. $self->username. '/';
2422 This is the FS::svc_acct job-queue-able version. It still uses
2423 FS::Misc::send_email under-the-hood.
2430 eval "use FS::Misc qw(send_email)";
2433 $opt{mimetype} ||= 'text/plain';
2434 $opt{mimetype} .= '; charset="iso-8859-1"' unless $opt{mimetype} =~ /charset/;
2436 my $error = send_email(
2437 'from' => $opt{from},
2439 'subject' => $opt{subject},
2440 'content-type' => $opt{mimetype},
2441 'body' => [ map "$_\n", split("\n", $opt{body}) ],
2443 die $error if $error;
2446 =item check_and_rebuild_fuzzyfiles
2450 sub check_and_rebuild_fuzzyfiles {
2451 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
2452 -e "$dir/svc_acct.username"
2453 or &rebuild_fuzzyfiles;
2456 =item rebuild_fuzzyfiles
2460 sub rebuild_fuzzyfiles {
2462 use Fcntl qw(:flock);
2464 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
2468 open(USERNAMELOCK,">>$dir/svc_acct.username")
2469 or die "can't open $dir/svc_acct.username: $!";
2470 flock(USERNAMELOCK,LOCK_EX)
2471 or die "can't lock $dir/svc_acct.username: $!";
2473 my @all_username = map $_->getfield('username'), qsearch('svc_acct', {});
2475 open (USERNAMECACHE,">$dir/svc_acct.username.tmp")
2476 or die "can't open $dir/svc_acct.username.tmp: $!";
2477 print USERNAMECACHE join("\n", @all_username), "\n";
2478 close USERNAMECACHE or die "can't close $dir/svc_acct.username.tmp: $!";
2480 rename "$dir/svc_acct.username.tmp", "$dir/svc_acct.username";
2490 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
2491 open(USERNAMECACHE,"<$dir/svc_acct.username")
2492 or die "can't open $dir/svc_acct.username: $!";
2493 my @array = map { chomp; $_; } <USERNAMECACHE>;
2494 close USERNAMECACHE;
2498 =item append_fuzzyfiles USERNAME
2502 sub append_fuzzyfiles {
2503 my $username = shift;
2505 &check_and_rebuild_fuzzyfiles;
2507 use Fcntl qw(:flock);
2509 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
2511 open(USERNAME,">>$dir/svc_acct.username")
2512 or die "can't open $dir/svc_acct.username: $!";
2513 flock(USERNAME,LOCK_EX)
2514 or die "can't lock $dir/svc_acct.username: $!";
2516 print USERNAME "$username\n";
2518 flock(USERNAME,LOCK_UN)
2519 or die "can't unlock $dir/svc_acct.username: $!";
2527 =item radius_usergroup_selector GROUPS_ARRAYREF [ SELECTNAME ]
2531 sub radius_usergroup_selector {
2532 my $sel_groups = shift;
2533 my %sel_groups = map { $_=>1 } @$sel_groups;
2535 my $selectname = shift || 'radius_usergroup';
2538 my $sth = $dbh->prepare(
2539 'SELECT DISTINCT(groupname) FROM radius_usergroup ORDER BY groupname'
2540 ) or die $dbh->errstr;
2541 $sth->execute() or die $sth->errstr;
2542 my @all_groups = map { $_->[0] } @{$sth->fetchall_arrayref};
2546 function ${selectname}_doadd(object) {
2547 var myvalue = object.${selectname}_add.value;
2548 var optionName = new Option(myvalue,myvalue,false,true);
2549 var length = object.$selectname.length;
2550 object.$selectname.options[length] = optionName;
2551 object.${selectname}_add.value = "";
2554 <SELECT MULTIPLE NAME="$selectname">
2557 foreach my $group ( @all_groups ) {
2558 $html .= qq(<OPTION VALUE="$group");
2559 if ( $sel_groups{$group} ) {
2560 $html .= ' SELECTED';
2561 $sel_groups{$group} = 0;
2563 $html .= ">$group</OPTION>\n";
2565 foreach my $group ( grep { $sel_groups{$_} } keys %sel_groups ) {
2566 $html .= qq(<OPTION VALUE="$group" SELECTED>$group</OPTION>\n);
2568 $html .= '</SELECT>';
2570 $html .= qq!<BR><INPUT TYPE="text" NAME="${selectname}_add">!.
2571 qq!<INPUT TYPE="button" VALUE="Add new group" onClick="${selectname}_doadd(this.form)">!;
2576 =item reached_threshold
2578 Performs some activities when svc_acct thresholds (such as number of seconds
2579 remaining) are reached.
2583 sub reached_threshold {
2586 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $opt{'svcnum'} } );
2587 die "Cannot find svc_acct with svcnum " . $opt{'svcnum'} unless $svc_acct;
2589 if ( $opt{'op'} eq '+' ){
2590 $svc_acct->setfield( $opt{'column'}.'_threshold',
2591 int($svc_acct->getfield($opt{'column'})
2592 * ( $conf->exists('svc_acct-usage_threshold')
2593 ? $conf->config('svc_acct-usage_threshold')/100
2598 my $error = $svc_acct->replace;
2599 die $error if $error;
2600 }elsif ( $opt{'op'} eq '-' ){
2602 my $threshold = $svc_acct->getfield( $opt{'column'}.'_threshold' );
2603 return '' if ($threshold eq '' );
2605 $svc_acct->setfield( $opt{'column'}.'_threshold', 0 );
2606 my $error = $svc_acct->replace;
2607 die $error if $error; # email next time, i guess
2609 if ( $warning_template ) {
2610 eval "use FS::Misc qw(send_email)";
2613 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
2614 my $cust_main = $cust_pkg->cust_main;
2616 my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ }
2617 $cust_main->invoicing_list,
2618 ($opt{'to'} ? $opt{'to'} : ())
2621 my $mimetype = $warning_mimetype;
2622 $mimetype .= '; charset="iso-8859-1"' unless $opt{mimetype} =~ /charset/;
2624 my $body = $warning_template->fill_in( HASH => {
2625 'custnum' => $cust_main->custnum,
2626 'username' => $svc_acct->username,
2627 'password' => $svc_acct->_password,
2628 'first' => $cust_main->first,
2629 'last' => $cust_main->getfield('last'),
2630 'pkg' => $cust_pkg->part_pkg->pkg,
2631 'column' => $opt{'column'},
2632 'amount' => $opt{'column'} =~/bytes/
2633 ? FS::UI::bytecount::display_bytecount($svc_acct->getfield($opt{'column'}))
2634 : $svc_acct->getfield($opt{'column'}),
2635 'threshold' => $opt{'column'} =~/bytes/
2636 ? FS::UI::bytecount::display_bytecount($threshold)
2641 my $error = send_email(
2642 'from' => $warning_from,
2644 'subject' => $warning_subject,
2645 'content-type' => $mimetype,
2646 'body' => [ map "$_\n", split("\n", $body) ],
2648 die $error if $error;
2651 die "unknown op: " . $opt{'op'};
2659 The $recref stuff in sub check should be cleaned up.
2661 The suspend, unsuspend and cancel methods update the database, but not the
2662 current object. This is probably a bug as it's unexpected and
2665 radius_usergroup_selector? putting web ui components in here? they should
2666 probably live somewhere else...
2668 insertion of RADIUS group stuff in insert could be done with child_objects now
2669 (would probably clean up export of them too)
2673 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,
2674 export.html from the base documentation, L<FS::Record>, L<FS::Conf>,
2675 L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>,
2676 L<freeside-queued>), L<FS::svc_acct_pop>,
2677 schema.html from the base documentation.
2681 =item domain_select_hash %OPTIONS
2683 Returns a hash SVCNUM => DOMAIN ... representing the domains this customer
2684 may at present purchase.
2686 Currently available options are: I<pkgnum> I<svcpart>
2690 sub domain_select_hash {
2691 my ($self, %options) = @_;
2697 $part_svc = $self->part_svc;
2698 $cust_pkg = $self->cust_svc->cust_pkg
2702 $part_svc = qsearchs('part_svc', { 'svcpart' => $options{svcpart} })
2703 if $options{'svcpart'};
2705 $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $options{pkgnum} })
2706 if $options{'pkgnum'};
2708 if ($part_svc && ( $part_svc->part_svc_column('domsvc')->columnflag eq 'S'
2709 || $part_svc->part_svc_column('domsvc')->columnflag eq 'F')) {
2710 %domains = map { $_->svcnum => $_->domain }
2711 map { qsearchs('svc_domain', { 'svcnum' => $_ }) }
2712 split(',', $part_svc->part_svc_column('domsvc')->columnvalue);
2713 }elsif ($cust_pkg && !$conf->exists('svc_acct-alldomains') ) {
2714 %domains = map { $_->svcnum => $_->domain }
2715 map { qsearchs('svc_domain', { 'svcnum' => $_->svcnum }) }
2716 map { qsearch('cust_svc', { 'pkgnum' => $_->pkgnum } ) }
2717 qsearch('cust_pkg', { 'custnum' => $cust_pkg->custnum });
2719 %domains = map { $_->svcnum => $_->domain } qsearch('svc_domain', {} );
2722 if ($part_svc && $part_svc->part_svc_column('domsvc')->columnflag eq 'D') {
2723 my $svc_domain = qsearchs('svc_domain',
2724 { 'svcnum' => $part_svc->part_svc_column('domsvc')->columnvalue } );
2725 if ( $svc_domain ) {
2726 $domains{$svc_domain->svcnum} = $svc_domain->domain;
2728 warn "unknown svc_domain.svcnum for part_svc_column domsvc: ".
2729 $part_svc->part_svc_column('domsvc')->columnvalue;