summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2010-12-23 11:48:09 +0100
committerYorhel <git@yorhel.nl>2010-12-23 11:48:09 +0100
commite0131b6ab501eaeda1003ac5131f803a36192d46 (patch)
treef18682fd8e087264e73631bcd12b662627e631ca /lib
parentaca465c568a4fba065d04fdb236309a9fcb1c37c (diff)
Added users_prefs table and removed users.(skin|customcss)
Will convert the other preferences later.
Diffstat (limited to 'lib')
-rw-r--r--lib/VNDB/DB/Users.pm31
-rw-r--r--lib/VNDB/Handler/Users.pm8
-rw-r--r--lib/VNDB/Util/Auth.pm12
-rw-r--r--lib/VNDB/Util/LayoutHTML.pm6
4 files changed, 44 insertions, 13 deletions
diff --git a/lib/VNDB/DB/Users.pm b/lib/VNDB/DB/Users.pm
index 7440f495..73479784 100644
--- a/lib/VNDB/DB/Users.pm
+++ b/lib/VNDB/DB/Users.pm
@@ -6,14 +6,14 @@ use warnings;
use Exporter 'import';
our @EXPORT = qw|
- dbUserGet dbUserEdit dbUserAdd dbUserDel
+ dbUserGet dbUserEdit dbUserAdd dbUserDel dbUserPrefSet
dbSessionAdd dbSessionDel dbSessionUpdateLastUsed
dbNotifyGet dbNotifyMarkRead dbNotifyRemove
|;
# %options->{ username passwd mail session uid ip registered search results page what sort reverse }
-# what: notifycount stats extended
+# what: notifycount stats extended prefs
# sort: username registered votes changes tags
sub dbUserGet {
my $s = shift;
@@ -54,7 +54,7 @@ sub dbUserGet {
qw|id username c_votes c_changes show_list c_tags|,
q|extract('epoch' from registered) as registered|,
$o{what} =~ /extended/ ? (
- qw|mail rank salt skin customcss show_nsfw ign_votes notify_dbedit notify_announce|,
+ qw|mail rank salt show_nsfw ign_votes notify_dbedit notify_announce|,
q|encode(passwd, 'hex') AS passwd|
) : (),
$o{what} =~ /notifycount/ ?
@@ -90,6 +90,20 @@ sub dbUserGet {
ORDER BY !s|,
join(', ', @select), join(' ', @join), \%where, $order
);
+
+ if($o{what} =~ /prefs/) {
+ my %r = map {
+ $r->[$_]{prefs} = {};
+ ($r->[$_]{id}, $r->[$_])
+ } 0..$#$r;
+
+ $r{$_->{uid}}{prefs}{$_->{key}} = $_->{value} for (@{$s->dbAll(q|
+ SELECT uid, key, value
+ FROM users_prefs
+ WHERE uid IN(!l)|,
+ [ keys %r ]
+ )});
+ }
return wantarray ? ($r, $np) : $r;
}
@@ -100,7 +114,7 @@ sub dbUserEdit {
my %h;
defined $o{$_} && ($h{$_.' = ?'} = $o{$_})
- for (qw| username mail rank show_nsfw show_list skin customcss salt ign_votes notify_dbedit notify_announce |);
+ for (qw| username mail rank show_nsfw show_list salt ign_votes notify_dbedit notify_announce |);
$h{'passwd = decode(?, \'hex\')'} = $o{passwd}
if defined $o{passwd};
@@ -127,6 +141,15 @@ sub dbUserDel {
}
+# uid, key, val
+sub dbUserPrefSet {
+ my($s, $uid, $key, $val) = @_;
+ !$val ? $s->dbExec('DELETE FROM users_prefs WHERE uid = ? AND key = ?', $uid, $key)
+ : $s->dbExec('UPDATE users_prefs SET value = ? WHERE uid = ? AND key = ?', $val, $uid, $key)
+ || $s->dbExec('INSERT INTO users_prefs (uid, key, value) VALUES (?, ?, ?)', $uid, $key, $val);
+}
+
+
# Adds a session to the database
# uid, 40 character session token
sub dbSessionAdd {
diff --git a/lib/VNDB/Handler/Users.pm b/lib/VNDB/Handler/Users.pm
index 3e2a1aef..31de6bd7 100644
--- a/lib/VNDB/Handler/Users.pm
+++ b/lib/VNDB/Handler/Users.pm
@@ -291,7 +291,7 @@ sub edit {
return $self->htmlDenied if !$self->authInfo->{id} || $self->authInfo->{id} != $uid && !$self->authCan('usermod');
# fetch user info (cached if uid == loggedin uid)
- my $u = $self->authInfo->{id} == $uid ? $self->authInfo : $self->dbUserGet(uid => $uid, what => 'extended')->[0];
+ my $u = $self->authInfo->{id} == $uid ? $self->authInfo : $self->dbUserGet(uid => $uid, what => 'extended prefs')->[0];
return 404 if !$u->{id};
# check POST data
@@ -315,12 +315,11 @@ sub edit {
push @{$frm->{_err}}, 'passmatch'
if ($frm->{usrpass} || $frm->{usrpass2}) && (!$frm->{usrpass} || !$frm->{usrpass2} || $frm->{usrpass} ne $frm->{usrpass2});
if(!$frm->{_err}) {
+ $self->dbUserPrefSet($uid, $_ => $frm->{$_}) for (qw|skin customcss|);
my %o;
$o{username} = $frm->{usrname} if $frm->{usrname};
$o{rank} = $frm->{rank} if $frm->{rank};
$o{mail} = $frm->{mail};
- $o{skin} = $frm->{skin};
- $o{customcss} = $frm->{customcss};
($o{passwd}, $o{salt}) = $self->authPreparePass($frm->{usrpass}) if $frm->{usrpass};
$o{show_list} = $frm->{flags_list} ? 1 : 0;
$o{show_nsfw} = $frm->{flags_nsfw} ? 1 : 0;
@@ -334,7 +333,8 @@ sub edit {
# fill out default values
$frm->{usrname} ||= $u->{username};
- $frm->{$_} ||= $u->{$_} for(qw|rank mail skin customcss|);
+ $frm->{$_} ||= $u->{$_} for(qw|rank mail|);
+ $frm->{$_} ||= $u->{prefs}{$_} for(qw|skin customcss|);
$frm->{flags_list} = $u->{show_list} if !defined $frm->{flags_list};
$frm->{flags_nsfw} = $u->{show_nsfw} if !defined $frm->{flags_nsfw};
$frm->{ign_votes} = $u->{ign_votes} if !defined $frm->{ign_votes};
diff --git a/lib/VNDB/Util/Auth.pm b/lib/VNDB/Util/Auth.pm
index 24e316ce..50b81eb2 100644
--- a/lib/VNDB/Util/Auth.pm
+++ b/lib/VNDB/Util/Auth.pm
@@ -14,7 +14,7 @@ use YAWF ':html';
use VNDB::Func;
-our @EXPORT = qw| authInit authLogin authLogout authInfo authCan authPreparePass authGetCode authCheckCode |;
+our @EXPORT = qw| authInit authLogin authLogout authInfo authCan authPreparePass authGetCode authCheckCode authPref |;
# initializes authentication information and checks the vndb_auth cookie
@@ -27,7 +27,7 @@ sub authInit {
return _rmcookie($self) if length($cookie) < 41;
my $token = substr($cookie, 0, 40);
my $uid = substr($cookie, 40);
- $self->{_auth} = $uid =~ /^\d+$/ && $self->dbUserGet(uid => $uid, session => $token, what => 'extended notifycount')->[0];
+ $self->{_auth} = $uid =~ /^\d+$/ && $self->dbUserGet(uid => $uid, session => $token, what => 'extended notifycount prefs')->[0];
# update the sessions.lastused column if lastused < now()'6 hours'
$self->dbSessionUpdateLastUsed($uid, $token) if $self->{_auth} && $self->{_auth}{session_lastused} < time()-6*3600;
return _rmcookie($self) if !$self->{_auth};
@@ -196,5 +196,13 @@ sub _incorrectcode {
}
+sub authPref {
+ my($self, $key, $val) = @_;
+ return '' if !$self->authInfo->{id};
+ return $self->{_auth}{prefs}{$key}||'' if @_ == 2;
+ $self->{_auth}{prefs}{$key} = $val;
+ $self->dbUserPrefSet($key, $val);
+}
+
1;
diff --git a/lib/VNDB/Util/LayoutHTML.pm b/lib/VNDB/Util/LayoutHTML.pm
index c241101f..a94289fa 100644
--- a/lib/VNDB/Util/LayoutHTML.pm
+++ b/lib/VNDB/Util/LayoutHTML.pm
@@ -12,7 +12,7 @@ our @EXPORT = qw|htmlHeader htmlFooter|;
sub htmlHeader { # %options->{ title, noindex, search, feeds }
my($self, %o) = @_;
- my $skin = $self->reqParam('skin') || $self->authInfo->{skin} || $self->{skin_default};
+ my $skin = $self->reqParam('skin') || $self->authPref('skin') || $self->{skin_default};
$skin = $self->{skin_default} if !$self->{skins}{$skin} || !-d "$VNDB::ROOT/static/s/$skin";
# heading
@@ -22,8 +22,8 @@ sub htmlHeader { # %options->{ title, noindex, search, feeds }
Link rel => 'shortcut icon', href => '/favicon.ico', type => 'image/x-icon';
Link rel => 'stylesheet', href => $self->{url_static}.'/s/'.$skin.'/style.css?'.$self->{version}, type => 'text/css', media => 'all';
Link rel => 'search', type => 'application/opensearchdescription+xml', title => 'VNDB VN Search', href => $self->{url}.'/opensearch.xml';
- if($self->authInfo->{customcss}) {
- (my $css = $self->authInfo->{customcss}) =~ s/\n/ /g;
+ if($self->authPref('customcss')) {
+ (my $css = $self->authPref('customcss')) =~ s/\n/ /g;
style type => 'text/css', $css;
}
Link rel => 'alternate', type => 'application/atom+xml', href => "/feeds/$_.atom", title => $self->{atom_feeds}{$_}[1]