Gantry-Plugins-Session

 view release on metacpan or  search on metacpan

lib/Gantry/Plugins/Session.pm  view on Meta::CPAN

# $class->get_callbacks( $namespace )
#-----------------------------------------------------------
sub get_callbacks {
    my ( $class, $namespace ) = @_;

    return if ( $registered_callbacks{ $namespace }++ );

    warn "Your app needs a 'namespace' method which doesn't return 'Gantry'"
            if ( $namespace eq 'Gantry' );
    return (
        { phase => 'init', callback => \&initialize }
    );
}

#-----------------------------------------------------------
# initialize
#-----------------------------------------------------------
sub initialize {
    my ($gobj) = @_;

	$gobj->session_init();

}

sub session_init {
    my ($gobj) = @_;

    my $cache;
    my $cookie;
    my $session;
    my $app_rootp = $gobj->app_rootp || "";
    my $cookiecheck = $app_rootp . '/cookiecheck';
    my $regex = qr/^${app_rootp}\/(cookiecheck).*/;
    my $secret = $gobj->fish_config('session_secret') || 'w3s3cR7';
    my $crypt = Gantry::Utils::Crypt->new({'secret' => $secret});

    return if ($gobj->fish_uri =~ /^$regex/);

    # check to see if a previous session is active

    if ($session = $gobj->get_cookies('_session_id_')) {

        # OK, store the session id

        $gobj->session_id($session);

    } else {

        # set a cookie and see if it works

        $cookie = $crypt->encrypt(time, {}, rand(), $$);

        $gobj->set_cookie(
            {
                name => '_session_id_',
                value => $cookie,
                path => '/'
            }
        );

          $gobj->relocate($cookiecheck);

    }

}

sub do_cookiecheck {
    my $gobj = shift;

    my $session;
    my $app_rootp = $gobj->app_rootp || "/";

    # if cookies are enabled they should be returned on the redirect

    if ($session = $gobj->get_cookies('_session_id_')) {

        # Ok, redirect them back to the applicaion

        $gobj->session_inited(1);
        $gobj->session_id($session);
        $gobj->session_store($lock, '0');
        $gobj->relocate($app_rootp);

    } else {

        # Hmmm, OK, lets give them a nudge

        my $session_title = $gobj->fish_config('session_title') || 'Missing Cookies';
        my $session_wrapper = $gobj->fish_config('session_wrapper') || 'default.tt';
        my $session_template = $gobj->fish_config('session_template') || 'session.tt';

        $gobj->template_wrapper($session_wrapper);
        $gobj->stash->view->title($session_title);
        $gobj->stash->view->template($session_template);

    }

}

#-----------------------------------------------------------
# session_store
#-----------------------------------------------------------
sub session_store {
    my ($gobj, $key, $value) = (shift, shift, shift);

    my $session = $gobj->session_id();

    $gobj->cache_namespace($session);
    $gobj->cache_set($key, $value);

}

#-----------------------------------------------------------
# session_retrieve
#-----------------------------------------------------------
sub session_retrieve {
    my ($gobj, $key) = (shift, shift);

    my $data;
    my $session = $gobj->session_id();

    $gobj->cache_namespace($session);
    $data = $gobj->cache_get($key);

    return $data;

}

#-----------------------------------------------------------
# session_remove
#-----------------------------------------------------------
sub session_remove {
    my ($gobj, $key) = (shift, shift);

    my $session = $gobj->session_id();

    $gobj->cache_namespace($session);
    $gobj->cache_del($key);

}

#-----------------------------------------------------------



( run in 0.853 second using v1.01-cache-2.11-cpan-5511b514fd6 )