Commit 73d7c67e authored by Nigel Kukard's avatar Nigel Kukard

IMPROVEMENT: Improved the user of user.ID in SQL queries

parent a2be6ff2
...@@ -734,6 +734,8 @@ sub process_request { ...@@ -734,6 +734,8 @@ sub process_request {
$self->log(LOG_DEBUG,"[SMRADIUS] FIND: Username found with '".$module->{'Name'}."'"); $self->log(LOG_DEBUG,"[SMRADIUS] FIND: Username found with '".$module->{'Name'}."'");
$user->{'_UserDB'} = $module; $user->{'_UserDB'} = $module;
$user->{'_UserDB_Data'} = $userdb_data; $user->{'_UserDB_Data'} = $userdb_data;
# The user ID is supposed to be global unique, on the same level as the username
$user->{'ID'} = $user->{'_UserDB_Data'}->{'ID'};
last; last;
# Or a negative result # Or a negative result
......
...@@ -333,7 +333,10 @@ sub getUsage ...@@ -333,7 +333,10 @@ sub getUsage
foreach my $attr ($packet->attributes) { foreach my $attr ($packet->attributes) {
$template->{'request'}->{$attr} = $packet->rawattr($attr) $template->{'request'}->{$attr} = $packet->rawattr($attr)
} }
$template->{'user'} = $user;
# Add user details
$template->{'user'}->{'ID'} = $user->{'ID'};
$template->{'user'}->{'Username'} = $user->{'Username'};
# Current PeriodKey # Current PeriodKey
my $now = DateTime->now->set_time_zone($server->{'smradius'}->{'event_timezone'}); my $now = DateTime->now->set_time_zone($server->{'smradius'}->{'event_timezone'});
...@@ -438,8 +441,9 @@ sub acct_log ...@@ -438,8 +441,9 @@ sub acct_log
# Fix event timestamp # Fix event timestamp
$template->{'request'}->{'Timestamp'} = $user->{'_Internal'}->{'Timestamp'}; $template->{'request'}->{'Timestamp'} = $user->{'_Internal'}->{'Timestamp'};
# Add user # Add user details
$template->{'user'} = $user; $template->{'user'}->{'ID'} = $user->{'ID'};
$template->{'user'}->{'Username'} = $user->{'Username'};
# Current PeriodKey # Current PeriodKey
my $now = DateTime->now->set_time_zone($server->{'smradius'}->{'event_timezone'}); my $now = DateTime->now->set_time_zone($server->{'smradius'}->{'event_timezone'});
......
# Test accounting database # Test accounting database
# Copyright (C) 2007-2015, AllWorldIT # Copyright (C) 2007-2016, AllWorldIT
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or # the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License along # You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., # with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
...@@ -99,7 +99,10 @@ Acct-Delay-Time: %{accounting.Acct-Delay-Time} ...@@ -99,7 +99,10 @@ Acct-Delay-Time: %{accounting.Acct-Delay-Time}
foreach my $attr ($packet->attributes) { foreach my $attr ($packet->attributes) {
$template->{'accounting'}->{$attr} = $packet->attr($attr) $template->{'accounting'}->{$attr} = $packet->attr($attr)
} }
$template->{'user'} = $user;
# Add user details
$template->{'user'}->{'ID'} = $user->{'ID'};
$template->{'user'}->{'Username'} = $user->{'Username'};
if ($packet->rawattr('Acct-Status-Type') eq "1") { if ($packet->rawattr('Acct-Status-Type') eq "1") {
$server->log(LOG_DEBUG,"Start Packet: ".$packet->dump()); $server->log(LOG_DEBUG,"Start Packet: ".$packet->dump());
......
# Support for updating of user stats # Support for updating of user stats
# Copyright (C) 2007-2011, AllWorldIT # Copyright (C) 2007-2016, AllWorldIT
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or # the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License along # You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., # with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
...@@ -119,7 +119,10 @@ sub updateUserStats ...@@ -119,7 +119,10 @@ sub updateUserStats
foreach my $attr ($packet->attributes) { foreach my $attr ($packet->attributes) {
$template->{'request'}->{$attr} = $packet->rawattr($attr) $template->{'request'}->{$attr} = $packet->rawattr($attr)
} }
$template->{'user'} = $user;
# Add user details
$template->{'user'}->{'ID'} = $user->{'ID'};
$template->{'user'}->{'Username'} = $user->{'Username'};
# Current PeriodKey # Current PeriodKey
my $now = DateTime->now->set_time_zone($server->{'smradius'}->{'event_timezone'}); my $now = DateTime->now->set_time_zone($server->{'smradius'}->{'event_timezone'});
......
...@@ -130,7 +130,8 @@ sub find ...@@ -130,7 +130,8 @@ sub find
foreach my $attr ($packet->attributes) { foreach my $attr ($packet->attributes) {
$template->{'request'}->{$attr} = $packet->rawattr($attr) $template->{'request'}->{$attr} = $packet->rawattr($attr)
} }
$template->{'user'} = $user;
# Add MAC address details
$template->{'user'}->{'MACAddress'} = $macAddress; $template->{'user'}->{'MACAddress'} = $macAddress;
# Replace template entries # Replace template entries
......
...@@ -91,7 +91,7 @@ sub init ...@@ -91,7 +91,7 @@ sub init
FROM FROM
@TP@group_attributes, @TP@users_to_groups @TP@group_attributes, @TP@users_to_groups
WHERE WHERE
@TP@users_to_groups.UserID = %{userdb.ID} @TP@users_to_groups.UserID = %{user.ID}
AND @TP@group_attributes.GroupID = @TP@users_to_groups.GroupID AND @TP@group_attributes.GroupID = @TP@users_to_groups.GroupID
AND @TP@group_attributes.Disabled = 0 AND @TP@group_attributes.Disabled = 0
'; ';
...@@ -102,7 +102,7 @@ sub init ...@@ -102,7 +102,7 @@ sub init
FROM FROM
@TP@user_attributes @TP@user_attributes
WHERE WHERE
UserID = %{userdb.ID} UserID = %{user.ID}
AND Disabled = 0 AND Disabled = 0
'; ';
...@@ -111,7 +111,7 @@ sub init ...@@ -111,7 +111,7 @@ sub init
@TP@users_data (UserID, LastUpdated, Name, Value) @TP@users_data (UserID, LastUpdated, Name, Value)
VALUES VALUES
( (
%{userdb.ID}, %{user.ID},
%{query.LastUpdated}, %{query.LastUpdated},
%{query.Name}, %{query.Name},
%{query.Value} %{query.Value}
...@@ -125,7 +125,7 @@ sub init ...@@ -125,7 +125,7 @@ sub init
LastUpdated = %{query.LastUpdated}, LastUpdated = %{query.LastUpdated},
Value = %{query.Value} Value = %{query.Value}
WHERE WHERE
UserID = %{userdb.ID} UserID = %{user.ID}
AND Name = %{query.Name} AND Name = %{query.Name}
'; ';
...@@ -135,7 +135,7 @@ sub init ...@@ -135,7 +135,7 @@ sub init
FROM FROM
@TP@users_data @TP@users_data
WHERE WHERE
UserID = %{userdb.ID} UserID = %{user.ID}
AND Name = %{query.Name} AND Name = %{query.Name}
'; ';
...@@ -143,7 +143,7 @@ sub init ...@@ -143,7 +143,7 @@ sub init
DELETE FROM DELETE FROM
@TP@users_data @TP@users_data
WHERE WHERE
UserID = %{userdb.ID} UserID = %{user.ID}
AND Name = %{query.Name} AND Name = %{query.Name}
'; ';
...@@ -264,7 +264,9 @@ sub find ...@@ -264,7 +264,9 @@ sub find
foreach my $attr ($packet->attributes) { foreach my $attr ($packet->attributes) {
$template->{'request'}->{$attr} = $packet->rawattr($attr) $template->{'request'}->{$attr} = $packet->rawattr($attr)
} }
$template->{'user'} = $user;
# Add user details, not user ID is available here as thats what we are retrieving
$template->{'user'}->{'Username'} = $user->{'Username'};
# Replace template entries # Replace template entries
my @dbDoParams = templateReplace($config->{'userdb_find_query'},$template); my @dbDoParams = templateReplace($config->{'userdb_find_query'},$template);
...@@ -320,6 +322,11 @@ sub get ...@@ -320,6 +322,11 @@ sub get
foreach my $attr ($packet->attributes) { foreach my $attr ($packet->attributes) {
$template->{'request'}->{$attr} = $packet->rawattr($attr) $template->{'request'}->{$attr} = $packet->rawattr($attr)
} }
# Add user details
$template->{'user'}->{'ID'} = $user->{'ID'};
$template->{'user'}->{'Username'} = $user->{'Username'};
# Add in userdb data # Add in userdb data
foreach my $item (keys %{$user->{'_UserDB_Data'}}) { foreach my $item (keys %{$user->{'_UserDB_Data'}}) {
$template->{'userdb'}->{$item} = $user->{'_UserDB_Data'}->{$item}; $template->{'userdb'}->{$item} = $user->{'_UserDB_Data'}->{$item};
...@@ -381,16 +388,21 @@ sub data_set ...@@ -381,16 +388,21 @@ sub data_set
# Build template # Build template
my $template; my $template;
# Last updated time would be now
$template->{'query'}->{'LastUpdated'} = $user->{'_Internal'}->{'Timestamp'}; # Add user details
$template->{'query'}->{'Name'} = sprintf('%s/%s',$module,$name); $template->{'user'}->{'ID'} = $user->{'ID'};
$template->{'query'}->{'Value'} = $value; $template->{'user'}->{'Username'} = $user->{'Username'};
# Add in userdb data # Add in userdb data
foreach my $item (keys %{$user->{'_UserDB_Data'}}) { foreach my $item (keys %{$user->{'_UserDB_Data'}}) {
$template->{'userdb'}->{$item} = $user->{'_UserDB_Data'}->{$item}; $template->{'userdb'}->{$item} = $user->{'_UserDB_Data'}->{$item};
} }
# Last updated time would be now
$template->{'query'}->{'LastUpdated'} = $user->{'_Internal'}->{'Timestamp'};
$template->{'query'}->{'Name'} = sprintf('%s/%s',$module,$name);
$template->{'query'}->{'Value'} = $value;
# Replace template entries # Replace template entries
my @dbDoParams = templateReplace($config->{'users_data_update_query'},$template); my @dbDoParams = templateReplace($config->{'users_data_update_query'},$template);
...@@ -453,13 +465,18 @@ sub data_get ...@@ -453,13 +465,18 @@ sub data_get
# Build template # Build template
my $template; my $template;
$template->{'query'}->{'Name'} = sprintf('%s/%s',$module,$name);
# Add user details
$template->{'user'}->{'ID'} = $user->{'ID'};
$template->{'user'}->{'Username'} = $user->{'Username'};
# Add in userdb data # Add in userdb data
foreach my $item (keys %{$user->{'_UserDB_Data'}}) { foreach my $item (keys %{$user->{'_UserDB_Data'}}) {
$template->{'userdb'}->{$item} = $user->{'_UserDB_Data'}->{$item}; $template->{'userdb'}->{$item} = $user->{'_UserDB_Data'}->{$item};
} }
$template->{'query'}->{'Name'} = sprintf('%s/%s',$module,$name);
# If we using caching, check how old the result is # If we using caching, check how old the result is
if (defined($config->{'userdb_data_cache_time'})) { if (defined($config->{'userdb_data_cache_time'})) {
my ($res,$val) = cacheGetComplexKeyPair('mod_userdb_sql(data_get)', my ($res,$val) = cacheGetComplexKeyPair('mod_userdb_sql(data_get)',
......
...@@ -436,7 +436,7 @@ userdb_get_group_attributes_query=<<EOT ...@@ -436,7 +436,7 @@ userdb_get_group_attributes_query=<<EOT
FROM FROM
@TP@group_attributes, @TP@users_to_groups @TP@group_attributes, @TP@users_to_groups
WHERE WHERE
users_to_groups.UserID = %{userdb.ID} users_to_groups.UserID = %{user.ID}
AND group_attributes.GroupID = users_to_groups.GroupID AND group_attributes.GroupID = users_to_groups.GroupID
AND group_attributes.Disabled = 0 AND group_attributes.Disabled = 0
EOT EOT
...@@ -447,7 +447,7 @@ userdb_get_user_attributes_query=<<EOT ...@@ -447,7 +447,7 @@ userdb_get_user_attributes_query=<<EOT
FROM FROM
@TP@user_attributes @TP@user_attributes
WHERE WHERE
UserID = %{userdb.ID} UserID = %{user.ID}
AND Disabled = 0 AND Disabled = 0
EOT EOT
...@@ -456,7 +456,7 @@ users_data_set_query=<<EOT ...@@ -456,7 +456,7 @@ users_data_set_query=<<EOT
@TP@users_data (UserID, LastUpdated, Name, Value) @TP@users_data (UserID, LastUpdated, Name, Value)
VALUES VALUES
( (
%{userdb.ID}, %{user.ID},
%{query.LastUpdated}, %{query.LastUpdated},
%{query.Name}, %{query.Name},
%{query.Value} %{query.Value}
...@@ -470,7 +470,7 @@ users_data_update_query=<<EOT ...@@ -470,7 +470,7 @@ users_data_update_query=<<EOT
LastUpdated = %{query.LastUpdated}, LastUpdated = %{query.LastUpdated},
Value = %{query.Value} Value = %{query.Value}
WHERE WHERE
UserID = %{userdb.ID} UserID = %{user.ID}
AND Name = %{query.Name} AND Name = %{query.Name}
EOT EOT
...@@ -480,7 +480,7 @@ users_data_get_query=<<EOT ...@@ -480,7 +480,7 @@ users_data_get_query=<<EOT
FROM FROM
@TP@users_data @TP@users_data
WHERE WHERE
UserID = %{userdb.ID} UserID = %{user.ID}
AND Name = %{query.Name} AND Name = %{query.Name}
EOT EOT
...@@ -488,7 +488,7 @@ users_data_delete_query=<<EOT ...@@ -488,7 +488,7 @@ users_data_delete_query=<<EOT
DELETE FROM DELETE FROM
@TP@users_data @TP@users_data
WHERE WHERE
UserID = %{userdb.ID} UserID = %{user.ID}
AND Name = %{query.Name} AND Name = %{query.Name}
EOT EOT
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment