5 use FS::UID qw(adminsuidsetup);
6 use FS::Record qw(qsearch qsearchs str2time_sql);
11 my %allowed_types = map { $_ => 1 } qw ( sqlradius sqlradius_withdomain );
12 my $conf = new FS::Conf;
14 my $user = shift or die &usage;
19 if ( !defined($export_x) ) {
20 @part_export = qsearch('part_export', {} );
21 } elsif ( $export_x =~ /^(\d+)$/ ) {
22 @part_export = qsearchs('part_export', { exportnum=>$1 } )
23 or die "exportnum $export_x not found\n";
25 @part_export = qsearch('part_export', { exporttype=>$export_x } )
26 or die "no exports of type $export_x found\n";
29 # gross almost false laziness with FS::part_export::sqlradius::update_svc_acct
30 @part_export = grep { ! $_->option('ignore_accounting') }
31 grep { $allowed_types{$_->exporttype} }
33 or die "No sqlradius exports specified.";
36 foreach my $part_export ( @part_export ) {
37 my $dbh = DBI->connect( map $part_export->option($_),
38 qw ( datasrc username password ) );
40 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
41 my $group = "UserName";
43 if ( ref($part_export) =~ /withdomain/ );
45 my $sth = $dbh->prepare("SELECT UserName, Realm,
46 $str2time max(AcctStartTime)),
47 $str2time max(AcctStopTime))
49 WHERE AcctStartTime != 0 AND AcctStopTime != 0
52 $sth->execute() or die $sth->errstr;
54 while (my $row = $sth->fetchrow_arrayref ) {
55 my ($username, $realm, $start, $stop) = @$row;
57 $username = lc($username) unless $conf->exists('username-uppercase');
59 if ( ref($part_export) =~ /withdomain/ ) {
60 $extra_sql = " And '$realm' = ( SELECT domain FROM svc_domain
61 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
64 my $svc_acct = qsearchs( 'svc_acct',
65 { 'username' => $username },
70 $svc_acct->last_login($start)
71 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
72 $svc_acct->last_logout($stop)
73 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);
80 die "Usage:\n\n freeside-sqlradius-set_lastlog user [ exportnum|exporttype ]\n";
85 freeside-sqlradius-set-lastlog - Command line tool to set last_login and last_logout values from radius tables
89 freeside-sqlradius-set-lastlog user [ exportnum|exporttype ]
93 Sets the last_login and last_logout columns of each svc_acct based on
94 data in the radacct table for the specified export (selected by exportnum
95 or exporttype) or all exports if none are specified.
99 L<freeside-sqlradius-radacctd>, L<FS::part_export>