summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2009-11-15 10:46:54 +0100
committerYorhel <git@yorhel.nl>2009-11-15 10:50:19 +0100
commit7f1b892cb46c8f713a3bb7c5ea94de2f7aa42508 (patch)
treee4cf9fff72f8aefd541e52cbe47bf79284fe228f /lib
parentcd566ad416c19c81e7574491c78b81a7c609c5d2 (diff)
Auth: Combined dbSessionCheck into dbUserGet
This one query is a bit faster than the two queries executed seperately, and with a query that is executed on each pageview it does matter. Ideally, the dbUserMessageCount() is cached and fetched with the same query, this should save another 1-2ms. But this is probably not worth the extra code it would require.
Diffstat (limited to 'lib')
-rw-r--r--lib/VNDB/DB/Users.pm25
-rw-r--r--lib/VNDB/Util/Auth.pm4
2 files changed, 13 insertions, 16 deletions
diff --git a/lib/VNDB/DB/Users.pm b/lib/VNDB/DB/Users.pm
index 593c6415..39429a02 100644
--- a/lib/VNDB/DB/Users.pm
+++ b/lib/VNDB/DB/Users.pm
@@ -5,10 +5,10 @@ use strict;
use warnings;
use Exporter 'import';
-our @EXPORT = qw|dbUserGet dbUserEdit dbUserAdd dbUserDel dbUserMessageCount dbSessionAdd dbSessionDel dbSessionCheck|;
+our @EXPORT = qw|dbUserGet dbUserEdit dbUserAdd dbUserDel dbUserMessageCount dbSessionAdd dbSessionDel|;
-# %options->{ username passwd mail order uid ip registered search results page what }
+# %options->{ username passwd mail session order uid ip registered search results page what }
# what: stats extended
sub dbUserGet {
my $s = shift;
@@ -40,6 +40,8 @@ sub dbUserGet {
'registered > to_timestamp(?)' => $o{registered} ) : (),
$o{search} ? (
'username ILIKE ?' => "%$o{search}%") : (),
+ $o{session} ? (
+ q|s.token = decode(?, 'hex')| => $o{session} ) : (),
);
my @select = (
@@ -59,12 +61,17 @@ sub dbUserGet {
) : (),
);
+ my @join = (
+ $o{session} ? 'JOIN sessions s ON s.uid = u.id' : (),
+ );
+
my($r, $np) = $s->dbPage(\%o, q|
SELECT !s
FROM users u
+ !s
!W
ORDER BY !s|,
- join(', ', @select), \%where, $o{order}
+ join(', ', @select), join(' ', @join), \%where, $o{order}
);
return wantarray ? ($r, $np) : $r;
}
@@ -147,15 +154,5 @@ sub dbSessionDel {
}
-# Queries the database for the validity of a session
-# Returns 1 if corresponding session found, 0 if not
-# uid, token
-sub dbSessionCheck {
- my($s, @o) = @_;
- return $s->dbRow(
- q|SELECT count(uid) AS count FROM sessions WHERE uid = ? AND token = decode(?, 'hex') LIMIT 1|, @o
- )->{count}||0;
-}
-
-
1;
+
diff --git a/lib/VNDB/Util/Auth.pm b/lib/VNDB/Util/Auth.pm
index a3bf7c29..cef9b905 100644
--- a/lib/VNDB/Util/Auth.pm
+++ b/lib/VNDB/Util/Auth.pm
@@ -25,8 +25,8 @@ sub authInit {
return _rmcookie($self) if length($cookie) < 41;
my $token = substr($cookie, 0, 40);
my $uid = substr($cookie, 40);
- return _rmcookie($self) if $uid !~ /^\d+$/ || !$self->dbSessionCheck($uid, $token);
- $self->{_auth} = $self->dbUserGet(uid => $uid, what => 'extended')->[0];
+ $self->{_auth} = $uid =~ /^\d+$/ && $self->dbUserGet(uid => $uid, session => $token, what => 'extended')->[0];
+ return _rmcookie($self) if !$self->{_auth};
}