From 1d84bf53fde880886b6aee02111c1c8958f5a15b Mon Sep 17 00:00:00 2001 From: Victor Wagner Date: Thu, 10 Oct 2013 15:19:59 +0400 Subject: [PATCH] bans --- MANIFEST | 8 ++++ bin/userinfo | 33 ++++++++++++++ lib/VJournal/Session.pm | 99 ++++++++++++++++++++++++++++++++++++----- templates/post.html | 7 +++ templates/taglist.html | 7 +++ templates/tape.html | 7 +++ 6 files changed, 149 insertions(+), 12 deletions(-) create mode 100644 templates/post.html create mode 100644 templates/taglist.html create mode 100644 templates/tape.html diff --git a/MANIFEST b/MANIFEST index 7a17737..e169c19 100644 --- a/MANIFEST +++ b/MANIFEST @@ -2,4 +2,12 @@ Makefile.PL MANIFEST lib/VJournal/Format.pm lib/VJournal/Session.pm +bin/comment +bin/login +bin/post +bin/provider +bin/userinfo +templates/post.html +templates/tape.html +templates/taglist.html t/user_link.t diff --git a/bin/userinfo b/bin/userinfo index 0d92d69..a0dbbde 100644 --- a/bin/userinfo +++ b/bin/userinfo @@ -32,3 +32,36 @@ If user is not logged in, returns following structure: } =cut + +use VJournal::Session; +use JSON; +use CGI; + +my $cgi=new CGI; +my $session = VJourna::Session->new($cgi); +my $out={}; +if (!defined $session) { +# User is not authenticated. Return list of providers; + $out->{state}="notlogged"; + $session=$cgi; +} else { + if ($session->isowner()) { + $out->{state}="owner"; + } elsif ($session->banned()) { + $out->{state}="banned"; + } else { + $out->{state}="guest"; + } + + $out->{url}=$session->identity(); + $out->{displayname}=$session->name(); + %avatar=$session->avatar(); + if(exists $avatar{-src}) { + $out->{avatar}={src=>$avatar{-src},-width=>$avatar{-width}, + -height=>$avatar{-height}}; + } + +} +$session->header(-content_type=>"text/json",-charset=>utf-8); +print $encode_json($out); + diff --git a/lib/VJournal/Session.pm b/lib/VJournal/Session.pm index 99f18e1..e819786 100644 --- a/lib/VJournal/Session.pm +++ b/lib/VJournal/Session.pm @@ -68,12 +68,12 @@ sub new { dbmopen %userbase,$config{-sessionbase},0644; return undef if (!exists($userbase{$sess_id})); - my ($name,$avatar,$email,$avwidth,$avheight,$ip,$expire) = + my ($name,$identity,$avatar,$email,$avwidth,$avheight,$ip,$expire) = split(/:/,$userbase{$sess_id}); if ($ip && $ip ne $attrs{-cgi}->remote_addr()) { return undef; } - my $me={-id=>$sess_id,-name=>$name,-cgi=>$cgi}; + my $me={-id=>$sess_id,-name=>$name,-identity=>$identity,-cgi=>$cgi}; if ($avatar) { $me->{-avatar}=$avatar; $me->{-avwidth}=$avwidth; @@ -89,7 +89,7 @@ sub new { return undef; } elsif ($expire < $now - $config{-gracetime}) { $expire+=$config{-sessiontime}; - $userbase{$sess_id}=join(":",$name,$avatar,$email,$avwidth,$avheight,$ip,$expire); + $userbase{$sess_id}=join(":",$name,$identity,$avatar,$email,$avwidth,$avheight,$ip,$expire); $me->{-cookie}= [$cgi->cookie(-name=>COOKIE_NAME,-value=>$sess_id,-expires=> $expire)]; @@ -103,7 +103,8 @@ sub new { Creates new session for given user. It is assumet that user have been properly authenticated by caller. (i.e. using OpenID). - $session=VJournal::Session->create(-user=>'user',-cgi=>$cgi,-email=>$mailaddress, + $session=VJournal::Session->create(-identity=>'identity-url', + -name=>"user display name",-cgi=>$cgi,-email=>$mailaddress, -avatar=>$uri, -bind_to_ip=>1 ); @@ -124,15 +125,16 @@ sub create { croak("Invalid call to ".$pkg."->create"); } my %params = @_; - croack("User name is required by ".$pkg."->create") unless $params{-user}; + croack("User identity is required by ".$pkg."->create") unless + $params{-identity}; $params{-cgi}=CGI->new() if(!$params{-cgi}); load_config($params{-cgi}); - my $user=$params{-user}; + my $identity=$params{-identity}; my %users; my %sessions; dbmopen %users,$config{-userbase},0644; - my $session={-cgi=>$params{-cgi},-name=>$params{-user}}; - if (!exists($users{$user})) { + my $session={-cgi=>$params{-cgi},-identity=>$params{-identity}}; + if (!exists($users{$identity})) { # New user come. require VJournal::Avatar; my @avatar; @@ -145,9 +147,11 @@ sub create { @avatar = VJournal::Avatar::by_email($params{-email}); } my %a = @avatar; - $users{$user}=join(":",$params{-email},$a{-src},$a{-width},$a{-height}); + $users{$identity}=join(":",$params{-name}||$identity + ,$params{-email},$a{-src},$a{-width},$a{-height}); } - my ($email,$avatarsrc,$avatarwidth,$avatarheight)=split(":",$users{$user}); + my ($name,$email,$avatarsrc,$avatarwidth,$avatarheight)=split(":",$users{$user}); + $session->{-name} = $name; $session->{-email} = $email if $email; if ($avatarsrc) { $session->{-avatar} = $avatarsrc; @@ -157,7 +161,7 @@ sub create { my $expire = time()+$config{-sessiontime}; require Digest::MD5; my - $sessioninfo=join(":",$user,$avatarsrc,$email,$avatarwidth,$avatarheight, + $sessioninfo=join(":",$name,$identity,$avatarsrc,$email,$avatarwidth,$avatarheight, ($params{-bind_to_ip}?$session->{-cgi}->remote_addr():""),$expire); $session->{-id} = Digest::MD5::md5_base64($sessioninfo); $sessions{$session->{-id}} = $sessioninfo; @@ -183,6 +187,30 @@ sub set_cookie { push @{$self->{-cookie}},@_; } +=head2 identity + + $s->identity() + +Returns OpenID identity URL for current user + +=cut + +sub identity { + return shift->{-identity}; +} + +=head2 name + + $s->name() + +Returns display name for current user + +=cut + +sub name { + return shift->{-name}; +} + =head2 avatar print $s->avatar() @@ -251,9 +279,56 @@ returns true, if current user is owner of the blog sub isowner { my $self=shift; - return $self->{-name} eq $config->{-owner}; + return $self->{-identity} eq $config->{-owner}; } +=head2 banned + + $s->banned() + +Return true if current user is banned from leaving comments in the blog. + +=cut + +sub banned { + return exists shift->{-ban} +} + +=head2 ban + + $s->ban($identity_url); + +Marks user as banned in the current blog + +=cut + +sub ban { + my ($self,$foe) = @_; + if (!$self->isowner()) return undef; + my %bans; + dbmopen %bans,$config{-topdir}."/bans",0644; + $bans{$foe}=time(); +} + +=head2 _readban + + $session=>{-identity=>$identity,...,_readban($identity)} + +Returns aray (-ban => 1) if $identity is recorded in tbe bans dbm file +in the blog top url + +=cut + +sub _readban { + my $identity = shift; + dbmopen %bans,$config{-topdir}."/bans",0644; + if (exists $bans{-identity}) { + return (-ban=>1); + } else { + return (); + } +} + =head2 _update_user $s->_update_user diff --git a/templates/post.html b/templates/post.html new file mode 100644 index 0000000..9bc8b7d --- /dev/null +++ b/templates/post.html @@ -0,0 +1,7 @@ + + + + + + diff --git a/templates/taglist.html b/templates/taglist.html new file mode 100644 index 0000000..9bc8b7d --- /dev/null +++ b/templates/taglist.html @@ -0,0 +1,7 @@ + + + + + + diff --git a/templates/tape.html b/templates/tape.html new file mode 100644 index 0000000..9bc8b7d --- /dev/null +++ b/templates/tape.html @@ -0,0 +1,7 @@ + + + + + + -- 2.39.2