Bracket::Controller::Root - Root Controller for Bracket


Bracket documentation Contained in the Bracket distribution.

Index


Code Index:

NAME

Top

Bracket::Controller::Root - Root Controller for Bracket

DESCRIPTION

Top

Handle Security.

METHODS

Top

auto

Make sure we're logged in when we should be.

index

  Handle root index by redirecting to home when logged in.

default

end

Attempt to render a view, if needed.

AUTHOR

Top

root

LICENSE

Top

This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself.


Bracket documentation Contained in the Bracket distribution.
package Bracket::Controller::Root;

use strict;
use warnings;
use parent 'Catalyst::Controller';
use Perl6::Junction qw/ any /;
use DateTime;

#
# Sets the actions in this controller to be registered with no prefix
# so they function identically to actions created in MyApp.pm
#
__PACKAGE__->config->{namespace} = '';

sub auto : Private {
    my ($self, $c) = @_;

    my @open_actions = (
        $c->controller('Auth')->action_for('register'),
        $c->controller('Auth')->action_for('login'),
        $c->controller('Auth')->action_for('email_reset_password_link'),
        $c->controller('Auth')->action_for('reset_password'),
    );

    # Allow unauthenticated users to reach the open actions like 'login'.
    if ($c->action eq any(@open_actions)) {
        return 1;
    }

    # If a user doesn't exist, force login
    if (!$c->user_exists) {

        # Redirect the user to the login page
        $c->response->redirect($c->uri_for($c->controller('Auth')->action_for('login')));

        # Return 0 to cancel 'post-auto' processing and prevent use of application
        return 0;
    }
    else {

        # Stash in home page link if we're not on home page.
        if (
            ($c->action ne $c->controller('Player')->action_for('home'))
            || (   ($c->action eq $c->controller('Player')->action_for('home'))
                && ($c->req->args->[0] && ($c->req->args->[0]) != $c->user->id))
          )
        {
            $c->stash->{show_home} = 1;
        }

        # See if player is an admin to get admin only links (e.g. Perfect Player access)
        my @user_roles = $c->user->roles;
        if ('admin' eq any(@user_roles)) {
            $c->stash->{is_admin} = 1;
        }
        
        # Set cutoff state
        $c->stash->{is_game_time} = (DateTime->now > edit_cutoff_time());
    }

    # User found, so return 1 to continue with processing after this 'auto'
    return 1;

}

sub index : Path : Args(0) {
    my ($self, $c) = @_;

    # Clear the user's state

    # Send the user to the starting point
    $c->go($c->controller('Player')->action_for('home'), [ $c->user->id ]);
}

sub default : Private {
    my ($self, $c) = @_;

    #	$c->response->body( $c->welcome_message );
    $c->go('/error_404');

}

sub error_404 : Path('error_404') : Args(0) {
    my ($self, $c) = @_;
    $c->res->status(404);

    $c->stash->{template} = 'page_not_found.tt';
}

sub end : ActionClass('RenderView') {
}

# This needs to be edited in future years to reflect the start date/time.
# TODO: Put into conf
sub edit_cutoff_time {

    return DateTime->new(
        year   => 2011,
        month  => 3,
        day    => 17,
        hour   => 16,
        minute => 15,
        second => 0,
    );

}

1;