]> git.donarmstrong.com Git - biopieces.git/commitdiff
added login to browser
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Tue, 8 Dec 2009 13:26:55 +0000 (13:26 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Tue, 8 Dec 2009 13:26:55 +0000 (13:26 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@796 74ccb610-7750-0410-82ae-013aeee3265d

code_perl/Maasha/BBrowser/Session.pm [new file with mode: 0644]
www/bgb.css
www/index.cgi

diff --git a/code_perl/Maasha/BBrowser/Session.pm b/code_perl/Maasha/BBrowser/Session.pm
new file mode 100644 (file)
index 0000000..132d5cc
--- /dev/null
@@ -0,0 +1,132 @@
+package Maasha::BBrowser::Session;
+
+# Copyright (C) 2009 Martin A. Hansen.
+
+# 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 the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+# http://www.gnu.org/copyleft/gpl.html
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+# Routines for session handling of the Biopieces Browser.
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+use warnings;
+use strict;
+use Data::Dumper;
+use Digest::MD5;
+use Maasha::Common;
+use Maasha::Filesys;
+
+use vars qw( @ISA @EXPORT );
+
+@ISA = qw( Exporter );
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+sub session_new
+{
+    # Martin A. Hansen, December 2009.
+  
+    # Create a new session id which is md5 hashed.
+
+    # Returns a string.
+
+    my ( $sid );
+
+    $sid = Digest::MD5::md5_hex( Maasha::Common::get_sessionid() );
+
+    return $sid;
+}
+
+
+sub session_restore
+{
+    # Martin A. Hansen, December 2009.
+    
+    # Parses a tab seperated session file and returns the data
+    # as a hash with user as key, and the rest of the columns as
+    # a hash.
+
+    my ( $file,   # session file
+       ) = @_;
+
+    # Returns a hashref.
+
+    my ( $fh, $line, $user, $password, $sid, $time, %session );
+
+    $fh = Maasha::Filesys::file_read_open( $file );
+
+    while ( $line = <$fh> )
+    {
+        chomp $line;
+
+        ( $user, $password, $sid, $time ) = split /\t/, $line;
+
+        $session{ $user } = {
+            PASSWORD   => $password,
+            SESSION_ID => $sid,
+            TIME       => $time,
+        };
+    }
+
+    close $fh;
+
+    return wantarray ? %session : \%session;
+}
+
+
+sub session_store
+{
+    # Martin A. Hansen, December 2009.
+
+    # Stores a session hash to file.
+
+    my ( $file,      # file to store in.
+         $session,   # session to store.
+       ) = @_;
+
+    # Returns nothing.
+
+    my ( $fh, $user );
+
+    $fh = Maasha::Filesys::file_write_open( $file );
+
+    foreach $user ( keys %{ $session } )
+    {
+        print $fh join(
+            "\t",
+            $user,
+            $session->{ $user }->{ 'PASSWORD' },
+            $session->{ $user }->{ 'SESSION_ID' },
+            $session->{ $user }->{ 'TIME' }
+        ), "\n";
+    }
+
+    close $fh;
+}
+
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+1;
index d0f5368522dcd15af1ab269d882a3a3bc09d7ed7..39b6863481712cf271e870215d2283a533ccb48a 100644 (file)
@@ -271,6 +271,11 @@ div.browse {
     font-family:       courier;
 }
 
+.error {
+    text-align:         center;
+    color:              red;
+}
+
 
 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> IMAGE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
 
index b4ccffc2a3ad9cd19478f3ef2fd39423e2af97f8..76036aa0d8ee824852971130f86ce2af958c9258 100755 (executable)
@@ -27,11 +27,13 @@ use lib $ENV{ 'BP_PERL' };
 
 use CGI;
 use Data::Dumper;
+use Digest::MD5;
 use Maasha::Common;
 use Maasha::Filesys;
 use Maasha::Calc;
 use Maasha::XHTML;
 use Maasha::KISS;
+use Maasha::BBrowser::Session;
 use Maasha::BBrowser::Track;
 use Maasha::BBrowser::Draw;
 
@@ -51,7 +53,7 @@ push @html, Maasha::XHTML::html_header(
 );
 
 push @html, Maasha::XHTML::h1( txt => "Biopieces Genome Browser", class => 'center' );
-push @html, Maasha::XHTML::form_beg( action => $cookie->{ 'SCRIPT' }, method => "get", enctype => "multipart/form-data" );
+push @html, Maasha::XHTML::form_beg( action => $cookie->{ 'SCRIPT' }, method => "post", enctype => "multipart/form-data" );
 
 push @html, page( $cookie );
 
@@ -60,7 +62,8 @@ push @html, Maasha::XHTML::body_end;
 push @html, Maasha::XHTML::html_end;
 
 # push @html, Maasha::XHTML::hdump( [ $cgi->Vars ] );  # DEBUG
-# push @html, Maasha::XHTML::hdump( $cookie );     # DEBUG
+# push @html, Maasha::XHTML::hdump( \%ENV );           # DEBUG
+# push @html, Maasha::XHTML::hdump( $cookie );         # DEBUG
 
 print "$_\n" foreach @html;
 
@@ -103,9 +106,17 @@ sub cookie_default
 
     $cookie->{ 'SCRIPT' }          = Maasha::Common::get_scriptname();
     $cookie->{ 'DATA_DIR' }        = "Data";
-    $cookie->{ 'LIST_PAGES' }      = [ qw( user clade genome assembly contig browse ) ];
-    $cookie->{ 'PAGE' }            = $cgi->param( 'page' )     || 'user';
-    $cookie->{ 'USER' }            = $cgi->param( 'user' )     || '';
+    $cookie->{ 'SESSION_DIR' }     = "Sessions";
+    $cookie->{ 'LIST_PAGES' }      = [ qw( clade genome assembly contig browse ) ];
+
+    $cookie->{ 'USER' }            = $cgi->param( 'user' );
+    $cookie->{ 'PASSWORD' }        = $cgi->param( 'password' );
+    $cookie->{ 'SESSION_ID' }      = $cgi->param( 'session_id' );
+
+    cookie_session( $cookie );
+    cookie_login( $cookie );
+
+    $cookie->{ 'PAGE' }          ||= $cgi->param( 'page' )     || 'login';
     $cookie->{ 'CLADE' }           = $cgi->param( 'clade' )    || '';
     $cookie->{ 'GENOME' }          = $cgi->param( 'genome' )   || '';
     $cookie->{ 'ASSEMBLY' }        = $cgi->param( 'assembly' ) || '';
@@ -173,6 +184,66 @@ sub cookie_default
 }
 
 
+sub cookie_session
+{
+    # Martin A. Hansen, December 2009.
+
+    # Check cookie information against session information.
+
+    my ( $cookie,   # cookie hash
+       ) = @_;
+
+    # Returns nothing.
+
+    my ( $session );
+
+    $session = Maasha::BBrowser::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/sessions.txt" );
+
+    $cookie->{ 'PAGE' } = 'login' if not $cookie->{ 'SESSION_ID' };
+    $cookie->{ 'PAGE' } = 'login' if not exists $session->{ $cookie->{ 'USER' } };
+    $cookie->{ 'PAGE' } = 'login' if $session->{ $cookie->{ 'USER' } }->{ 'SESSION_ID' } ne $cookie->{ 'SESSION_ID' };
+}
+
+
+sub cookie_login
+{
+    # Martin A. Hansen, December 2009.
+
+    # Check a user and password from CGI against the password file and
+    # set the session ID, if found, in the cookie.
+
+    my ( $cookie,   # cookie hash
+       ) = @_;
+
+    # Returns nothing.
+
+    my ( $session );
+
+    $session = Maasha::BBrowser::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/sessions.txt" );
+    
+    if ( $cookie->{ 'USER' } and $cookie->{ 'PASSWORD' } )
+    {
+        if ( exists $session->{ $cookie->{ 'USER' } } and
+             $session->{ $cookie->{ 'USER' } }->{ 'PASSWORD' } eq Digest::MD5::md5_hex( $cookie->{ 'PASSWORD' } ) )
+        {
+            $session->{ $cookie->{ 'USER' } }->{ 'SESSION_ID' } ||= Maasha::BBrowser::Session::session_new();
+            $session->{ $cookie->{ 'USER' } }->{ 'TIME' }         = Maasha::Common::time_stamp();
+
+            $cookie->{ 'SESSION_ID' } = $session->{ $cookie->{ 'USER' } }->{ 'SESSION_ID' };
+            $cookie->{ 'LOGIN' }      = "OK";
+            $cookie->{ 'USER' }       = $cookie->{ 'USER' };
+            $cookie->{ 'PAGE' }       = 'clade'; 
+
+            Maasha::BBrowser::Session::session_store( "$cookie->{ 'SESSION_DIR' }/sessions.txt", $session );
+        }
+        else
+        {
+            $cookie->{ 'LOGIN' } = "ERROR";
+        }
+    }
+}
+
+
 sub cookie_start
 {
     # Martin A. Hansen, November 2009.
@@ -330,10 +401,10 @@ sub cookie_href
 
     my ( @href_list, %href_hash, $href );
 
+    $href_hash{ "user" } = $cookie->{ 'USER' } if $cookie->{ 'USER' };
+
     while ( 1 )
     {
-        $href_hash{ "user" }  = $cookie->{ 'USER' }         if $cookie->{ 'USER' };
-        last if $page eq 'user';
         $href_hash{ "clade" } = $cookie->{ 'CLADE' }        if $cookie->{ 'CLADE' };
         last if $page eq 'clade';
         $href_hash{ "genome" } = $cookie->{ 'GENOME' }      if $cookie->{ 'GENOME' };
@@ -349,10 +420,11 @@ sub cookie_href
         $href_hash{ "nav_search" } = "$cookie->{ 'NAV_START' }-$cookie->{ 'NAV_END' }";
     }
 
-    $href_hash{ "s_beg" }  = $cookie->{ 'S_BEG' }  if defined $cookie->{ 'S_BEG' };
-    $href_hash{ "s_end" }  = $cookie->{ 'S_END' }  if defined $cookie->{ 'S_END' };
-    $href_hash{ "q_id" }   = $cookie->{ 'Q_ID' }   if defined $cookie->{ 'Q_ID' };
-    $href_hash{ "strand" } = $cookie->{ 'STRAND' } if defined $cookie->{ 'STRAND' };
+    $href_hash{ "s_beg" }      = $cookie->{ 'S_BEG' }      if defined $cookie->{ 'S_BEG' };
+    $href_hash{ "s_end" }      = $cookie->{ 'S_END' }      if defined $cookie->{ 'S_END' };
+    $href_hash{ "q_id" }       = $cookie->{ 'Q_ID' }       if defined $cookie->{ 'Q_ID' };
+    $href_hash{ "strand" }     = $cookie->{ 'STRAND' }     if defined $cookie->{ 'STRAND' };
+    $href_hash{ "session_id" } = $cookie->{ 'SESSION_ID' } if defined $cookie->{ 'SESSION_ID' };
 
     push @href_list, "$cookie->{ 'SCRIPT' }?page=$page";
 
@@ -402,27 +474,31 @@ sub page
 
     my ( @html );
 
-    if ( $cookie->{ 'PAGE' } eq 'export' ) {
-        push @html, page_export( $cookie );
+    if ( $cookie->{ 'PAGE' } eq 'login' ) {
+        push @html, page_login( $cookie );
     } elsif ( $cookie->{ 'PAGE' } eq 'search' ) {
         push @html, page_search( $cookie );
     } elsif ( $cookie->{ 'PAGE' } eq 'browse' ) {
         push @html, page_browse( $cookie );
     } elsif ( $cookie->{ 'PAGE' } eq 'dna' ) {
         push @html, page_dna( $cookie );
+    } elsif ( $cookie->{ 'PAGE' } eq 'export' ) {
+        push @html, page_export( $cookie );
     } else {
         push @html, page_taxonomy( $cookie );
     }
 
+    push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "session_id", value => "$cookie->{ 'SESSION_ID' }" ) );
+
     return wantarray ? @html : \@html;
 }
 
 
-sub page_export
+sub page_login
 {
-    # Martin A. Hansen, November 2009.
+    # Martin A. Hansen, December 2009.
 
-    # Renders the export page.
+    # Renders the login page.
 
     my ( $cookie,
        ) = @_;
@@ -431,8 +507,7 @@ sub page_export
 
     my ( @html );
 
-    push @html, section_taxonomy_table( $cookie );
-    push @html, section_export( $cookie );
+    push @html, section_login( $cookie );
 
     return wantarray ? @html : \@html;
 }
@@ -499,6 +574,26 @@ sub page_dna
 }
 
 
+sub page_export
+{
+    # Martin A. Hansen, November 2009.
+
+    # Renders the export page.
+
+    my ( $cookie,
+       ) = @_;
+
+    # Returns a list.
+
+    my ( @html );
+
+    push @html, section_taxonomy_table( $cookie );
+    push @html, section_export( $cookie );
+
+    return wantarray ? @html : \@html;
+}
+
+
 sub page_taxonomy
 {
     # Martin A. Hansen, November 2009.
@@ -522,6 +617,39 @@ sub page_taxonomy
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SECTIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
+sub section_login
+{
+    # Martin A. Hansen, December 2009.
+   
+    # Returns a HTML section with a login menu.
+
+    my ( $cookie,   # cookie hash
+       ) = @_;
+
+    # Returns a list.
+
+    my ( $user, $password, $login, @html );
+
+    $user     = Maasha::XHTML::text( name => "user", value => "", size => 20 );
+    $password = Maasha::XHTML::password( name => "password", value => "", size => 20 );
+    $login    = Maasha::XHTML::submit( name => "login_submit", value => "Login" );
+
+    push @html, Maasha::XHTML::h2( txt => "Login", class => 'center' );
+
+    push @html, Maasha::XHTML::table_beg( summary => "Login table", align => 'center' );
+    push @html, Maasha::XHTML::table_row_simple( tr => [ "User:", $user ] );
+    push @html, Maasha::XHTML::table_row_simple( tr => [ "Password:", $password ] );
+    push @html, Maasha::XHTML::table_row_simple( tr => [ "", $login ] );
+    push @html, Maasha::XHTML::table_end;
+
+    if ( $cookie->{ 'LOGIN' } and $cookie->{ 'LOGIN' } eq 'ERROR' ) {
+        push @html, Maasha::XHTML::h3( txt => "Bad user or password - please retry", class => 'error' );
+    }
+
+    return wantarray ? @html : \@html;
+}
+
+
 sub section_taxonomy_table
 {
     # Martin A. Hansen, November 2009.