4 use vars qw( @ISA $noexport_hack $conf
5 $dir_prefix @shells $usernamemin
6 $usernamemax $passwordmin $passwordmax
7 $username_ampersand $username_letter $username_letterfirst
8 $username_noperiod $username_nounderscore $username_nodash
11 $welcome_template $welcome_from $welcome_subject $welcome_mimetype
17 use FS::UID qw( datasrc );
19 use FS::Record qw( qsearch qsearchs fields dbh );
25 use FS::cust_main_invoice;
29 use FS::radius_usergroup;
32 use FS::Msgcat qw(gettext);
34 @ISA = qw( FS::svc_Common );
36 #ask FS::UID to run this stuff for us later
37 $FS::UID::callback{'FS::svc_acct'} = sub {
39 $dir_prefix = $conf->config('home');
40 @shells = $conf->config('shells');
41 $usernamemin = $conf->config('usernamemin') || 2;
42 $usernamemax = $conf->config('usernamemax');
43 $passwordmin = $conf->config('passwordmin') || 6;
44 $passwordmax = $conf->config('passwordmax') || 8;
45 $username_letter = $conf->exists('username-letter');
46 $username_letterfirst = $conf->exists('username-letterfirst');
47 $username_noperiod = $conf->exists('username-noperiod');
48 $username_nounderscore = $conf->exists('username-nounderscore');
49 $username_nodash = $conf->exists('username-nodash');
50 $username_uppercase = $conf->exists('username-uppercase');
51 $username_ampersand = $conf->exists('username-ampersand');
52 $mydomain = $conf->config('domain');
53 $dirhash = $conf->config('dirhash') || 0;
54 if ( $conf->exists('welcome_email') ) {
55 $welcome_template = new Text::Template (
57 SOURCE => [ map "$_\n", $conf->config('welcome_email') ]
58 ) or warn "can't create welcome email template: $Text::Template::ERROR";
59 $welcome_from = $conf->config('welcome_email-from'); # || 'your-isp-is-dum'
60 $welcome_subject = $conf->config('welcome_email-subject') || 'Welcome';
61 $welcome_mimetype = $conf->config('welcome_email-mimetype') || 'text/plain';
63 $welcome_template = '';
65 $smtpmachine = $conf->config('smtpmachine');
68 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
69 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
73 my ( $hashref, $cache ) = @_;
74 if ( $hashref->{'svc_acct_svcnum'} ) {
75 $self->{'_domsvc'} = FS::svc_domain->new( {
76 'svcnum' => $hashref->{'domsvc'},
77 'domain' => $hashref->{'svc_acct_domain'},
78 'catchall' => $hashref->{'svc_acct_catchall'},
85 FS::svc_acct - Object methods for svc_acct records
91 $record = new FS::svc_acct \%hash;
92 $record = new FS::svc_acct { 'column' => 'value' };
94 $error = $record->insert;
96 $error = $new_record->replace($old_record);
98 $error = $record->delete;
100 $error = $record->check;
102 $error = $record->suspend;
104 $error = $record->unsuspend;
106 $error = $record->cancel;
108 %hash = $record->radius;
110 %hash = $record->radius_reply;
112 %hash = $record->radius_check;
114 $domain = $record->domain;
116 $svc_domain = $record->svc_domain;
118 $email = $record->email;
120 $seconds_since = $record->seconds_since($timestamp);
124 An FS::svc_acct object represents an account. FS::svc_acct inherits from
125 FS::svc_Common. The following fields are currently supported:
129 =item svcnum - primary key (assigned automatcially for new accounts)
133 =item _password - generated if blank
135 =item sec_phrase - security phrase
137 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
145 =item dir - set automatically if blank (and uid is not)
149 =item quota - (unimplementd)
151 =item slipip - IP address
155 =item domsvc - svcnum from svc_domain
157 =item radius_I<Radius_Attribute> - I<Radius-Attribute>
167 Creates a new account. To add the account to the database, see L<"insert">.
171 sub table { 'svc_acct'; }
175 Adds this account to the database. If there is an error, returns the error,
176 otherwise returns false.
178 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
179 defined. An FS::cust_svc record will be created and inserted.
181 The additional field I<usergroup> can optionally be defined; if so it should
182 contain an arrayref of group names. See L<FS::radius_usergroup>. (used in
183 sqlradius export only)
185 (TODOC: L<FS::queue> and L<freeside-queued>)
187 (TODOC: new exports! $noexport_hack)
195 local $SIG{HUP} = 'IGNORE';
196 local $SIG{INT} = 'IGNORE';
197 local $SIG{QUIT} = 'IGNORE';
198 local $SIG{TERM} = 'IGNORE';
199 local $SIG{TSTP} = 'IGNORE';
200 local $SIG{PIPE} = 'IGNORE';
202 my $oldAutoCommit = $FS::UID::AutoCommit;
203 local $FS::UID::AutoCommit = 0;
206 $error = $self->check;
207 return $error if $error;
209 #no, duplicate checking just got a whole lot more complicated
210 #(perhaps keep this check with a config option to turn on?)
212 #return gettext('username_in_use'). ": ". $self->username
213 # if qsearchs( 'svc_acct', { 'username' => $self->username,
214 # 'domsvc' => $self->domsvc,
217 if ( $self->svcnum ) {
218 my $cust_svc = qsearchs('cust_svc',{'svcnum'=>$self->svcnum});
219 unless ( $cust_svc ) {
220 $dbh->rollback if $oldAutoCommit;
221 return "no cust_svc record found for svcnum ". $self->svcnum;
223 $self->pkgnum($cust_svc->pkgnum);
224 $self->svcpart($cust_svc->svcpart);
227 #new duplicate username checking
229 my @dup_user = qsearch( 'svc_acct', { 'username' => $self->username } );
230 my @dup_userdomain = qsearchs( 'svc_acct', { 'username' => $self->username,
231 'domsvc' => $self->domsvc } );
233 if ( @dup_user || @dup_userdomain ) {
234 my $exports = FS::part_export::export_info('svc_acct');
235 my( %conflict_user_svcpart, %conflict_userdomain_svcpart );
237 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
238 unless ( $part_svc ) {
239 $dbh->rollback if $oldAutoCommit;
240 return 'unknown svcpart '. $self->svcpart;
243 foreach my $part_export ( $part_svc->part_export ) {
245 #this will catch to the same exact export
246 my @svcparts = map { $_->svcpart }
247 qsearch('export_svc', { 'exportnum' => $part_export->exportnum });
249 #this will catch to exports w/same exporthost+type ???
250 #my @other_part_export = qsearch('part_export', {
251 # 'machine' => $part_export->machine,
252 # 'exporttype' => $part_export->exporttype,
254 #foreach my $other_part_export ( @other_part_export ) {
255 # push @svcparts, map { $_->svcpart }
256 # qsearch('export_svc', { 'exportnum' => $part_export->exportnum });
259 my $nodomain = $exports->{$part_export->exporttype}{'nodomain'};
260 if ( $nodomain =~ /^Y/i ) {
261 $conflict_user_svcpart{$_} = $part_export->exportnum
264 $conflict_userdomain_svcpart{$_} = $part_export->exportnum
269 foreach my $dup_user ( @dup_user ) {
270 my $dup_svcpart = $dup_user->cust_svc->svcpart;
271 if ( exists($conflict_user_svcpart{$dup_svcpart}) ) {
272 return "duplicate username: conflicts with svcnum ". $dup_user->svcnum.
273 " via exportnum ". $conflict_user_svcpart{$dup_svcpart};
277 foreach my $dup_userdomain ( @dup_userdomain ) {
278 my $dup_svcpart = $dup_userdomain->cust_svc->svcpart;
279 if ( exists($conflict_user_svcpart{$dup_svcpart}) ) {
280 return "duplicate username\@domain: conflicts with svcnum ".
281 $dup_userdomain->svcnum. " via exportnum ".
282 $conflict_user_svcpart{$dup_svcpart};
288 #see? i told you it was more complicated
290 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
291 return "Unknown svcpart" unless $part_svc;
292 return "uid ". $self->uid. " in use"
293 if $part_svc->part_svc_column('uid')->columnflag ne 'F'
294 && qsearchs( 'svc_acct', { 'uid' => $self->uid } )
295 && $self->username !~ /^(hyla)?fax$/
296 && $self->username !~ /^toor$/ #FreeBSD
300 $error = $self->SUPER::insert(\@jobnums);
302 $dbh->rollback if $oldAutoCommit;
306 if ( $self->usergroup ) {
307 foreach my $groupname ( @{$self->usergroup} ) {
308 my $radius_usergroup = new FS::radius_usergroup ( {
309 svcnum => $self->svcnum,
310 groupname => $groupname,
312 my $error = $radius_usergroup->insert;
314 $dbh->rollback if $oldAutoCommit;
320 #false laziness with sub replace (and cust_main)
321 my $queue = new FS::queue {
322 'svcnum' => $self->svcnum,
323 'job' => 'FS::svc_acct::append_fuzzyfiles'
325 $error = $queue->insert($self->username);
327 $dbh->rollback if $oldAutoCommit;
328 return "queueing job (transaction rolled back): $error";
332 my $cust_pkg = $self->cust_svc->cust_pkg;
333 my( $cust_main, $to ) = ( '', '' );
334 if ( $welcome_template && $cust_pkg ) {
335 my $cust_main = $cust_pkg->cust_main;
336 my $to = join(', ', grep { $_ ne 'POST' } $cust_main->invoicing_list );
338 my $wqueue = new FS::queue {
339 'svcnum' => $self->svcnum,
340 'job' => 'FS::svc_acct::send_email'
342 warn "attempting to queue email to $to";
343 my $error = $wqueue->insert(
345 'from' => $welcome_from,
346 'subject' => $welcome_subject,
347 'mimetype' => $welcome_mimetype,
348 'body' => $welcome_template->fill_in( HASH => {
349 'username' => $self->username,
350 'password' => $self->_password,
351 'first' => $cust_main->first,
352 'last' => $cust_main->getfield('last'),
353 'pkg' => $cust_pkg->part_pkg->pkg,
357 $dbh->rollback if $oldAutoCommit;
358 return "queuing welcome email: $error";
361 foreach my $jobnum ( @jobnums ) {
362 my $error = $wqueue->depend_insert($jobnum);
364 $dbh->rollback if $oldAutoCommit;
365 return "queuing welcome email job dependancy: $error";
373 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
379 Deletes this account from the database. If there is an error, returns the
380 error, otherwise returns false.
382 The corresponding FS::cust_svc record will be deleted as well.
384 (TODOC: new exports! $noexport_hack)
391 if ( defined( $FS::Record::dbdef->table('svc_acct_sm') ) ) {
392 return "Can't delete an account which has (svc_acct_sm) mail aliases!"
393 if $self->uid && qsearch( 'svc_acct_sm', { 'domuid' => $self->uid } );
396 return "Can't delete an account which is a (svc_forward) source!"
397 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
399 return "Can't delete an account which is a (svc_forward) destination!"
400 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
402 return "Can't delete an account with (svc_www) web service!"
403 if qsearch( 'svc_www', { 'usersvc' => $self->usersvc } );
405 # what about records in session ? (they should refer to history table)
407 local $SIG{HUP} = 'IGNORE';
408 local $SIG{INT} = 'IGNORE';
409 local $SIG{QUIT} = 'IGNORE';
410 local $SIG{TERM} = 'IGNORE';
411 local $SIG{TSTP} = 'IGNORE';
412 local $SIG{PIPE} = 'IGNORE';
414 my $oldAutoCommit = $FS::UID::AutoCommit;
415 local $FS::UID::AutoCommit = 0;
418 foreach my $cust_main_invoice (
419 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
421 unless ( defined($cust_main_invoice) ) {
422 warn "WARNING: something's wrong with qsearch";
425 my %hash = $cust_main_invoice->hash;
426 $hash{'dest'} = $self->email;
427 my $new = new FS::cust_main_invoice \%hash;
428 my $error = $new->replace($cust_main_invoice);
430 $dbh->rollback if $oldAutoCommit;
435 foreach my $svc_domain (
436 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
438 my %hash = new FS::svc_domain->hash;
439 $hash{'catchall'} = '';
440 my $new = new FS::svc_domain \%hash;
441 my $error = $new->replace($svc_domain);
443 $dbh->rollback if $oldAutoCommit;
448 foreach my $radius_usergroup (
449 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } )
451 my $error = $radius_usergroup->delete;
453 $dbh->rollback if $oldAutoCommit;
458 my $error = $self->SUPER::delete;
460 $dbh->rollback if $oldAutoCommit;
464 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
468 =item replace OLD_RECORD
470 Replaces OLD_RECORD with this one in the database. If there is an error,
471 returns the error, otherwise returns false.
473 The additional field I<usergroup> can optionally be defined; if so it should
474 contain an arrayref of group names. See L<FS::radius_usergroup>. (used in
475 sqlradius export only)
480 my ( $new, $old ) = ( shift, shift );
483 return "Username in use"
484 if $old->username ne $new->username &&
485 qsearchs( 'svc_acct', { 'username' => $new->username,
486 'domsvc' => $new->domsvc,
489 #no warnings 'numeric'; #alas, a 5.006-ism
491 return "Can't change uid!" if $old->uid != $new->uid;
494 #change homdir when we change username
495 $new->setfield('dir', '') if $old->username ne $new->username;
497 local $SIG{HUP} = 'IGNORE';
498 local $SIG{INT} = 'IGNORE';
499 local $SIG{QUIT} = 'IGNORE';
500 local $SIG{TERM} = 'IGNORE';
501 local $SIG{TSTP} = 'IGNORE';
502 local $SIG{PIPE} = 'IGNORE';
504 my $oldAutoCommit = $FS::UID::AutoCommit;
505 local $FS::UID::AutoCommit = 0;
508 $old->usergroup( [ $old->radius_groups ] );
509 if ( $new->usergroup ) {
510 #(sorta) false laziness with FS::part_export::sqlradius::_export_replace
511 my @newgroups = @{$new->usergroup};
512 foreach my $oldgroup ( @{$old->usergroup} ) {
513 if ( grep { $oldgroup eq $_ } @newgroups ) {
514 @newgroups = grep { $oldgroup ne $_ } @newgroups;
517 my $radius_usergroup = qsearchs('radius_usergroup', {
518 svcnum => $old->svcnum,
519 groupname => $oldgroup,
521 my $error = $radius_usergroup->delete;
523 $dbh->rollback if $oldAutoCommit;
524 return "error deleting radius_usergroup $oldgroup: $error";
528 foreach my $newgroup ( @newgroups ) {
529 my $radius_usergroup = new FS::radius_usergroup ( {
530 svcnum => $new->svcnum,
531 groupname => $newgroup,
533 my $error = $radius_usergroup->insert;
535 $dbh->rollback if $oldAutoCommit;
536 return "error adding radius_usergroup $newgroup: $error";
542 $error = $new->SUPER::replace($old);
544 $dbh->rollback if $oldAutoCommit;
545 return $error if $error;
548 #false laziness with sub insert (and cust_main)
549 my $queue = new FS::queue {
550 'svcnum' => $new->svcnum,
551 'job' => 'FS::svc_acct::append_fuzzyfiles'
553 $error = $queue->insert($new->username);
555 $dbh->rollback if $oldAutoCommit;
556 return "queueing job (transaction rolled back): $error";
560 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
566 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
567 error, returns the error, otherwise returns false.
569 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
575 my %hash = $self->hash;
576 unless ( $hash{_password} =~ /^\*SUSPENDED\* /
577 || $hash{_password} eq '*'
579 $hash{_password} = '*SUSPENDED* '.$hash{_password};
580 my $new = new FS::svc_acct ( \%hash );
581 my $error = $new->replace($self);
582 return $error if $error;
585 $self->SUPER::suspend;
590 Unsuspends this account by removing *SUSPENDED* from the password. If there is
591 an error, returns the error, otherwise returns false.
593 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
599 my %hash = $self->hash;
600 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
601 $hash{_password} = $1;
602 my $new = new FS::svc_acct ( \%hash );
603 my $error = $new->replace($self);
604 return $error if $error;
607 $self->SUPER::unsuspend;
612 Just returns false (no error) for now.
614 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
618 Checks all fields to make sure this is a valid service. If there is an error,
619 returns the error, otherwise returns false. Called by the insert and replace
622 Sets any fixed values; see L<FS::part_svc>.
629 my($recref) = $self->hashref;
631 my $x = $self->setfixed;
632 return $x unless ref($x);
635 if ( $part_svc->part_svc_column('usergroup')->columnflag eq "F" ) {
637 [ split(',', $part_svc->part_svc_column('usergroup')->columnvalue) ] );
640 my $error = $self->ut_numbern('svcnum')
641 || $self->ut_number('domsvc')
642 || $self->ut_textn('sec_phrase')
644 return $error if $error;
646 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
647 if ( $username_uppercase ) {
648 $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/i
649 or return gettext('illegal_username'). " ($usernamemin-$ulen): ". $recref->{username};
650 $recref->{username} = $1;
652 $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/
653 or return gettext('illegal_username'). " ($usernamemin-$ulen): ". $recref->{username};
654 $recref->{username} = $1;
657 if ( $username_letterfirst ) {
658 $recref->{username} =~ /^[a-z]/ or return gettext('illegal_username');
659 } elsif ( $username_letter ) {
660 $recref->{username} =~ /[a-z]/ or return gettext('illegal_username');
662 if ( $username_noperiod ) {
663 $recref->{username} =~ /\./ and return gettext('illegal_username');
665 if ( $username_nounderscore ) {
666 $recref->{username} =~ /_/ and return gettext('illegal_username');
668 if ( $username_nodash ) {
669 $recref->{username} =~ /\-/ and return gettext('illegal_username');
671 unless ( $username_ampersand ) {
672 $recref->{username} =~ /\&/ and return gettext('illegal_username');
675 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
676 $recref->{popnum} = $1;
677 return "Unknown popnum" unless
678 ! $recref->{popnum} ||
679 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
681 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
683 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
684 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
686 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
687 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
688 #not all systems use gid=uid
689 #you can set a fixed gid in part_svc
691 return "Only root can have uid 0"
692 if $recref->{uid} == 0
693 && $recref->{username} ne 'root'
694 && $recref->{username} ne 'toor';
696 # $error = $self->ut_textn('finger');
697 # return $error if $error;
698 $self->getfield('finger') =~
699 /^([\w \t\!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\*\<\>]*)$/
700 or return "Illegal finger: ". $self->getfield('finger');
701 $self->setfield('finger', $1);
703 $recref->{dir} =~ /^([\/\w\-\.\&]*)$/
704 or return "Illegal directory";
706 return "Illegal directory"
707 if $recref->{dir} =~ /(^|\/)\.+(\/|$)/; #no .. component
708 return "Illegal directory"
709 if $recref->{dir} =~ /\&/ && ! $username_ampersand;
710 unless ( $recref->{dir} ) {
711 $recref->{dir} = $dir_prefix . '/';
712 if ( $dirhash > 0 ) {
713 for my $h ( 1 .. $dirhash ) {
714 $recref->{dir} .= substr($recref->{username}, $h-1, 1). '/';
716 } elsif ( $dirhash < 0 ) {
717 for my $h ( reverse $dirhash .. -1 ) {
718 $recref->{dir} .= substr($recref->{username}, $h, 1). '/';
721 $recref->{dir} .= $recref->{username};
725 unless ( $recref->{username} eq 'sync' ) {
726 if ( grep $_ eq $recref->{shell}, @shells ) {
727 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
729 return "Illegal shell \`". $self->shell. "\'; ".
730 $conf->dir. "/shells contains: @shells";
733 $recref->{shell} = '/bin/sync';
736 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
737 $recref->{quota} = $1;
740 $recref->{gid} ne '' ?
741 return "Can't have gid without uid" : ( $recref->{gid}='' );
742 $recref->{finger} ne '' ?
743 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
744 $recref->{dir} ne '' ?
745 return "Can't have directory without uid" : ( $recref->{dir}='' );
746 $recref->{shell} ne '' ?
747 return "Can't have shell without uid" : ( $recref->{shell}='' );
748 $recref->{quota} ne '' ?
749 return "Can't have quota without uid" : ( $recref->{quota}='' );
752 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
753 unless ( $recref->{slipip} eq '0e0' ) {
754 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
755 or return "Illegal slipip". $self->slipip;
756 $recref->{slipip} = $1;
758 $recref->{slipip} = '0e0';
763 #arbitrary RADIUS stuff; allow ut_textn for now
764 foreach ( grep /^radius_/, fields('svc_acct') ) {
768 #generate a password if it is blank
769 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
770 unless ( $recref->{_password} );
772 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
773 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{$passwordmin,$passwordmax})$/ ) {
774 $recref->{_password} = $1.$3;
775 #uncomment this to encrypt password immediately upon entry, or run
776 #bin/crypt_pw in cron to give new users a window during which their
777 #password is available to techs, for faxing, etc. (also be aware of
779 #$recref->{password} = $1.
780 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
782 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/\$]{13,34})$/ ) {
783 $recref->{_password} = $1.$3;
784 } elsif ( $recref->{_password} eq '*' ) {
785 $recref->{_password} = '*';
786 } elsif ( $recref->{_password} eq '!!' ) {
787 $recref->{_password} = '!!';
789 #return "Illegal password";
790 return gettext('illegal_password'). "$passwordmin-$passwordmax".
791 FS::Msgcat::_gettext('illegal_password_characters').
792 ": ". $recref->{_password};
800 Depriciated, use radius_reply instead.
805 carp "FS::svc_acct::radius depriciated, use radius_reply";
811 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
812 reply attributes of this record.
814 Note that this is now the preferred method for reading RADIUS attributes -
815 accessing the columns directly is discouraged, as the column names are
816 expected to change in the future.
825 my($column, $attrib) = ($1, $2);
826 #$attrib =~ s/_/\-/g;
827 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
828 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
829 if ( $self->ip && $self->ip ne '0e0' ) {
830 $reply{'Framed-IP-Address'} = $self->ip;
837 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
838 check attributes of this record.
840 Note that this is now the preferred method for reading RADIUS attributes -
841 accessing the columns directly is discouraged, as the column names are
842 expected to change in the future.
848 ( 'Password' => $self->_password,
851 my($column, $attrib) = ($1, $2);
852 #$attrib =~ s/_/\-/g;
853 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
854 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table )
860 Returns the domain associated with this account.
866 if ( $self->domsvc ) {
867 #$self->svc_domain->domain;
868 my $svc_domain = $self->svc_domain
869 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
872 $mydomain or die "svc_acct.domsvc is null and no legacy domain config file";
878 Returns the FS::svc_domain record for this account's domain (see
887 : qsearchs( 'svc_domain', { 'svcnum' => $self->domsvc } );
892 Returns the FS::cust_svc record for this account (see L<FS::cust_svc>).
896 qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
901 Returns an email address associated with the account.
907 $self->username. '@'. $self->domain;
910 =item seconds_since TIMESTAMP
912 Returns the number of seconds this account has been online since TIMESTAMP.
915 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
916 L<Time::Local> and L<Date::Parse> for conversion functions.
920 #note: POD here, implementation in FS::cust_svc
923 $self->cust_svc->seconds_since(@_);
928 Returns all RADIUS groups for this account (see L<FS::radius_usergroup>).
934 if ( $self->usergroup ) {
935 #when provisioning records, export callback runs in svc_Common.pm before
936 #radius_usergroup records can be inserted...
939 map { $_->groupname }
940 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } );
958 use Mail::Internet 1.44;
961 $opt{mimetype} ||= 'text/plain';
962 $opt{mimetype} .= '; charset="iso-8859-1"' unless $opt{mimetype} =~ /charset/;
964 $ENV{MAILADDRESS} = $opt{from};
965 my $header = new Mail::Header ( [
968 "Sender: $opt{from}",
969 "Reply-To: $opt{from}",
970 "Date: ". time2str("%a, %d %b %Y %X %z", time),
971 "Subject: $opt{subject}",
972 "Content-Type: $opt{mimetype}",
974 my $message = new Mail::Internet (
976 'Body' => [ map "$_\n", split("\n", $opt{body}) ],
979 $message->smtpsend( Host => $smtpmachine )
980 or $message->smtpsend( Host => $smtpmachine, Debug => 1 )
981 or die "can't send email to $opt{to} via $smtpmachine with SMTP: $!";
984 =item check_and_rebuild_fuzzyfiles
988 sub check_and_rebuild_fuzzyfiles {
989 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
990 -e "$dir/svc_acct.username"
991 or &rebuild_fuzzyfiles;
994 =item rebuild_fuzzyfiles
998 sub rebuild_fuzzyfiles {
1000 use Fcntl qw(:flock);
1002 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
1006 open(USERNAMELOCK,">>$dir/svc_acct.username")
1007 or die "can't open $dir/svc_acct.username: $!";
1008 flock(USERNAMELOCK,LOCK_EX)
1009 or die "can't lock $dir/svc_acct.username: $!";
1011 my @all_username = map $_->getfield('username'), qsearch('svc_acct', {});
1013 open (USERNAMECACHE,">$dir/svc_acct.username.tmp")
1014 or die "can't open $dir/svc_acct.username.tmp: $!";
1015 print USERNAMECACHE join("\n", @all_username), "\n";
1016 close USERNAMECACHE or die "can't close $dir/svc_acct.username.tmp: $!";
1018 rename "$dir/svc_acct.username.tmp", "$dir/svc_acct.username";
1028 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
1029 open(USERNAMECACHE,"<$dir/svc_acct.username")
1030 or die "can't open $dir/svc_acct.username: $!";
1031 my @array = map { chomp; $_; } <USERNAMECACHE>;
1032 close USERNAMECACHE;
1036 =item append_fuzzyfiles USERNAME
1040 sub append_fuzzyfiles {
1041 my $username = shift;
1043 &check_and_rebuild_fuzzyfiles;
1045 use Fcntl qw(:flock);
1047 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
1049 open(USERNAME,">>$dir/svc_acct.username")
1050 or die "can't open $dir/svc_acct.username: $!";
1051 flock(USERNAME,LOCK_EX)
1052 or die "can't lock $dir/svc_acct.username: $!";
1054 print USERNAME "$username\n";
1056 flock(USERNAME,LOCK_UN)
1057 or die "can't unlock $dir/svc_acct.username: $!";
1065 =item radius_usergroup_selector GROUPS_ARRAYREF [ SELECTNAME ]
1069 sub radius_usergroup_selector {
1070 my $sel_groups = shift;
1071 my %sel_groups = map { $_=>1 } @$sel_groups;
1073 my $selectname = shift || 'radius_usergroup';
1076 my $sth = $dbh->prepare(
1077 'SELECT DISTINCT(groupname) FROM radius_usergroup ORDER BY groupname'
1078 ) or die $dbh->errstr;
1079 $sth->execute() or die $sth->errstr;
1080 my @all_groups = map { $_->[0] } @{$sth->fetchall_arrayref};
1084 function ${selectname}_doadd(object) {
1085 var myvalue = object.${selectname}_add.value;
1086 var optionName = new Option(myvalue,myvalue,false,true);
1087 var length = object.$selectname.length;
1088 object.$selectname.options[length] = optionName;
1089 object.${selectname}_add.value = "";
1092 <SELECT MULTIPLE NAME="$selectname">
1095 foreach my $group ( @all_groups ) {
1097 if ( $sel_groups{$group} ) {
1098 $html .= ' SELECTED';
1099 $sel_groups{$group} = 0;
1101 $html .= ">$group</OPTION>\n";
1103 foreach my $group ( grep { $sel_groups{$_} } keys %sel_groups ) {
1104 $html .= "<OPTION SELECTED>$group</OPTION>\n";
1106 $html .= '</SELECT>';
1108 $html .= qq!<BR><INPUT TYPE="text" NAME="${selectname}_add">!.
1109 qq!<INPUT TYPE="button" VALUE="Add new group" onClick="${selectname}_doadd(this.form)">!;
1118 The $recref stuff in sub check should be cleaned up.
1120 The suspend, unsuspend and cancel methods update the database, but not the
1121 current object. This is probably a bug as it's unexpected and
1124 radius_usergroup_selector? putting web ui components in here? they should
1125 probably live somewhere else...
1129 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,
1130 export.html from the base documentation, L<FS::Record>, L<FS::Conf>,
1131 L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>,
1132 L<freeside-queued>), L<Net::SSH>, L<ssh>, L<FS::svc_acct_pop>,
1133 schema.html from the base documentation.