6 use FS::UID qw(adminsuidsetup);
7 use FS::Record qw(qsearch qsearchs str2time_sql);
12 my %allowed_types = map { $_ => 1 } qw ( sqlradius sqlradius_withdomain );
13 my $conf = new FS::Conf;
15 my $user = shift or die &usage;
20 if ( !defined($export_x) ) {
21 @part_export = qsearch('part_export', {} );
22 } elsif ( $export_x =~ /^(\d+)$/ ) {
23 @part_export = qsearchs('part_export', { exportnum=>$1 } )
24 or die "exportnum $export_x not found\n";
26 @part_export = qsearch('part_export', { exporttype=>$export_x } )
27 or die "no exports of type $export_x found\n";
30 # gross almost false laziness with FS::part_export::sqlradius::update_svc_acct
31 @part_export = grep { ! $_->option('ignore_accounting') }
32 grep { $allowed_types{$_->exporttype} }
34 or die "No sqlradius exports specified.";
37 foreach my $part_export ( @part_export ) {
38 my $dbh = FS::DBI->connect( map $part_export->option($_),
39 qw ( datasrc username password ) );
41 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
42 my $group = "UserName";
44 if ( ref($part_export) =~ /withdomain/ );
46 my $sth = $dbh->prepare("SELECT UserName, Realm,
47 $str2time max(AcctStartTime)),
48 $str2time max(AcctStopTime))
50 WHERE AcctStartTime != 0 AND AcctStopTime != 0
53 $sth->execute() or die $sth->errstr;
55 while (my $row = $sth->fetchrow_arrayref ) {
56 my ($username, $realm, $start, $stop) = @$row;
58 $username = lc($username) unless $conf->exists('username-uppercase');
60 if ( ref($part_export) =~ /withdomain/ ) {
61 $extra_sql = " And '$realm' = ( SELECT domain FROM svc_domain
62 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
65 my $svc_acct = qsearchs( 'svc_acct',
66 { 'username' => $username },
71 $svc_acct->last_login($start)
72 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
73 $svc_acct->last_logout($stop)
74 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);
81 die "Usage:\n\n freeside-sqlradius-set_lastlog user [ exportnum|exporttype ]\n";
86 freeside-sqlradius-set-lastlog - Command line tool to set last_login and last_logout values from radius tables
90 freeside-sqlradius-set-lastlog user [ exportnum|exporttype ]
94 Sets the last_login and last_logout columns of each svc_acct based on
95 data in the radacct table for the specified export (selected by exportnum
96 or exporttype) or all exports if none are specified.
100 L<freeside-sqlradius-radacctd>, L<FS::part_export>