ClearPress::authdecor - ClearPress::authdecor documentation


ClearPress documentation Contained in the ClearPress distribution.

Index


Code Index:

NAME

Top

ClearPress::authdecor

VERSION

Top

$Revision: 349 $

SYNOPSIS

Top

DESCRIPTION

Top

SUBROUTINES/METHODS

Top

new - constructor, overridden from superclass but calls up. Present here to set default page attributes like title, stylesheet etc.

  my $oDecor = ClearPress::decor->new();

username - overridden from superclass - fetch username of authenticated user from LDAP or session

  my $sUsername = $oDecor->username();

linkbucket - data structure containing default links. TODO: examine user session

  my $arLinkHash = $oDecor->linkbucket();

  Structure is:
  [
    { Name => href },
    { Name => href },
    { Name => href },
    { Name => href },
  ]

site_header - overridden from superclass. Tacks the linkbucket on to the default header

  my $sSiteHeaderHTML = $oDecor->site_header();

site_login_form - authentication form used for popup and cgi-bin/login

  my $sSiteLoginHTML = $oDecor->site_login_form();

DIAGNOSTICS

Top

CONFIGURATION AND ENVIRONMENT

Top

DEPENDENCIES

Top

strict
warnings
Readonly
ClearPress::authenticator::session
ClearPress::decorator
Exporter

INCOMPATIBILITIES

Top

BUGS AND LIMITATIONS

Top

AUTHOR

Top

$Author: Roger Pettett$

LICENSE AND COPYRIGHT

Top


ClearPress documentation Contained in the ClearPress distribution.

#########
# Author:        rmp
# Last Modified: $Date: 2010-01-04 14:37:33 +0000 (Mon, 04 Jan 2010) $
# Id:            $Id: authdecor.pm 349 2010-01-04 14:37:33Z zerojinx $
# Source:        $Source$
# $HeadURL: https://clearpress.svn.sourceforge.net/svnroot/clearpress/trunk/lib/ClearPress/authdecor.pm $
#
package ClearPress::authdecor;
use strict;
use warnings;
use base qw(ClearPress::decorator Exporter);
use ClearPress::authenticator::session;
use Readonly;

our $VERSION = do { my ($r) = q$Revision: 349 $ =~ /(\d+)/smx; $r; };
Readonly::Scalar our $DOMAIN      => 'mysite.com';
Readonly::Scalar our $AUTH_COOKIE => 'mysite_sso';
Readonly::Array  our @EXPORT_OK   => qw($AUTH_COOKIE);

sub new {
  my ($class, @args) = @_;
  my $self = $class->SUPER::new(@args);

  $self->{title}       = 'My Site';
  $self->{stylesheet}  = [qw(/css/mysite.css)];
  $self->{meta_author} = q$Author: zerojinx $;

  if(!ref $self->{jsfile}) {
    $self->{jsfile} = [];
  }

  unshift @{$self->{jsfile}}, qw(/js/jquery-1.3.2.min.js);

  return $self;
}

sub username {
  my ($self, $username) = @_;

  if(defined $username) {
    $self->{username} = $username;
  }

  if(defined $self->{username}) {
    return $self->{username};
  }

  my $auth   = ClearPress::authenticator::session->new();
  my $cgi    = $self->cgi();
  my $cookie = $cgi->cookie($AUTH_COOKIE);

  if(!$cookie) {
    #########
    # no auth cookie. don't bother trying to decrypt
    #
    return;
  }

  my $ref = $auth->authen_token($cookie);
  if(!$ref) {
    #########
    # Failed to authenticate session token
    #
    return;
  }

  return $ref->{username};
}

sub linkbucket {
  my $self = shift;

  my $authenticated = $self->username()?1:0;

  #########
  # un/authenticated stuff
  #
  my $defaults = [
		  $authenticated ? {'Logout' => '/logout'} : {'Login' => '/login'},
		 ];

  #########
  # standard links
  #
  my $standard = [
		  {'Home' => q[/]},
		 ];
  #########
  # session-based (user-defined) links
  #
  my $session = [];

  return [@{$defaults},
	  @{$standard},
	  @{$session}];
}

sub site_header {
  my ($self, @args) = @_;

  my $header = $self->SUPER::site_header(@args);
  $header   .= q[<div><a id="homelink" href="/"><img src="/gfx/blank.gif" alt="" title="Home"/></a></div><div><ul id="siteactions">];
  my $links  = $self->linkbucket();

  for my $link (@{$links}) {
    my ($k, $v) = each %{$link};
    my $onclick = q[];

    if($v =~ /[ ]/smx) {
      #########
      # $v includes ajax onclick
      #
      ($v, $onclick) = split /\s+/smx, $v, 2;
      $onclick = qq[ onclick="$onclick"];
    }

    my $id   = 'act_'.lc $k;
    $id      =~ s/[^[:lower:]]/_/smxg;
    $header .= qq[<li><a id="$id" href="$v"$onclick>$k</a></li>];
  }

  $header .= qq[</ul><br style="clear:both"/></div><!--end siteactions-->
<div id="d_login_cntr">@{[$self->site_login_form]}</div><!--end d_login_cntr-->\n];

  return $header.q[<div id="main">];
}

sub footer {
  return q[</div><!--end main--></body></html>];
}

sub site_login_form {
  my $host = $ENV{HTTP_X_FORWARDED_HOST} || $ENV{HTTP_HOST} || q[];
  if($host) {
    $host = "https://$host";
  }
  return qq[<form class="login_form" method="post" action="$host/login">
  <dl class="tbl">
    <dt><label for="cred_0">Username</label></dt>
    <dd><input type="text" size="14" name="cred_0" id="cred_0"/></dd>
    <dt><label for="cred_1">Password</label></dt>
    <dd><input type="password" size="14" name="cred_1" id="cred_1"/></dd>
  </dl>
  <p class="buttons"><input type="submit" value="Log in"/></p>
</form>];
}

1;
__END__