From a29ca7854db66e3fa41ad6973aab65a148cb9145 Mon Sep 17 00:00:00 2001 From: jeff Date: Mon, 7 May 2007 06:35:55 +0000 Subject: [PATCH] sqlradius usergroup tools --- FS/bin/freeside-sqlradius-dedup-group | 82 +++++++++++++++++++++++++++++++++++ FS/bin/freeside-sqlradius-reset | 7 +++ 2 files changed, 89 insertions(+) create mode 100755 FS/bin/freeside-sqlradius-dedup-group diff --git a/FS/bin/freeside-sqlradius-dedup-group b/FS/bin/freeside-sqlradius-dedup-group new file mode 100755 index 000000000..441d50f62 --- /dev/null +++ b/FS/bin/freeside-sqlradius-dedup-group @@ -0,0 +1,82 @@ +#!/usr/bin/perl -w + +use strict; +use vars qw( %seen @dups ); +use Getopt::Std; +use FS::UID qw(adminsuidsetup); +use FS::Record qw(qsearch qsearchs); +use FS::part_export; + +my %allowed_types = map { $_ => 1 } qw ( sqlradius sqlradius_withdomain ); + +my $user = shift or die &usage; +adminsuidsetup $user; + +my $export_x = shift; +my @part_export; +if ( !defined($export_x) ) { + @part_export = qsearch('part_export', {} ); +} elsif ( $export_x =~ /^(\d+)$/ ) { + @part_export = qsearchs('part_export', { exportnum=>$1 } ) + or die "exportnum $export_x not found\n"; +} else { + @part_export = qsearch('part_export', { exporttype=>$export_x } ) + or die "no exports of type $export_x found\n"; +} + +@part_export = grep { $allowed_types{$_->exporttype} } @part_export + or die "No sqlradius exports specified."; + +foreach my $part_export ( @part_export ) { + my $dbh = DBI->connect( map $part_export->option($_), + qw ( datasrc username password ) ); + + my $sth = $dbh->prepare("SELECT id,username,groupname + FROM usergroup ORDER By username,groupname,id") + or die $dbh->errstr; + $sth->execute() or die $sth->errstr; + + @dups = (); %seen = (); + while (my $row = $sth->fetchrow_arrayref ) { + my ($userid, $username, $groupname) = @$row; + unless ( exists($seen{$username}{$groupname}) ) { + $seen{$username}{$groupname} = $userid; + next; + } + push @dups, $userid; + } + + $sth = $dbh->prepare("DELETE FROM usergroup WHERE id = ?") + or die $dbh->errstr; + + foreach (@dups) { + $sth->execute($_) or die $sth->errstr; + } + +} + + +sub usage { + die "Usage:\n\n freeside-sqlradius-dedup-group user [ exportnum|exporttype ]\n"; +} + +=head1 NAME + +freeside-sqlradius-dedup-group - Command line tool to eliminate duplicate usergroup entries from radius tables + +=head1 SYNOPSIS + + freeside-sqlradius-dedup-group user [ exportnum|exporttype ] + +=head1 DESCRIPTION + + Removes all but one username groupname pair when duplicate entries exist + for the specified export (selected by exportnum or exporttype) or all + exports if none are specified. + +=head1 SEE ALSO + +L, L, L + +=cut + diff --git a/FS/bin/freeside-sqlradius-reset b/FS/bin/freeside-sqlradius-reset index 2ac5012d4..a2da7452e 100755 --- a/FS/bin/freeside-sqlradius-reset +++ b/FS/bin/freeside-sqlradius-reset @@ -39,6 +39,7 @@ foreach my $export ( @exports ) { foreach my $export ( @exports ) { #my @svcparts = map { $_->svcpart } $export->export_svc; + my $overlimit_groups = $export->option('overlimit_groups'); my @svc_acct = map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) } @@ -51,6 +52,12 @@ foreach my $export ( @exports ) { $svc_acct->check; #set any fixed usergroup so it'll export even if all #svc_acct records don't have the group yet + if ($overlimit_groups && $svc_acct->overlimit) { + $svc_acct->usergroup( &{ $svc_acct->_fieldhandlers->{'usergroup'} } + ($svc_acct, $overlimit_groups) + ); + } + #false laziness with FS::svc_acct::insert (like it matters) my $error = $export->export_insert($svc_acct); die $error if $error; -- 2.11.0