From 03d540f3cbac12cf2aea627befe2f85b0ae5c976 Mon Sep 17 00:00:00 2001 From: Kaitlyn Parkhurst Date: Thu, 1 Sep 2022 14:27:03 +0000 Subject: [PATCH] Add Web --- Web/cpanfile | 5 +++ Web/lib/WeightGrapher/Web.pm | 85 ++++++++++++++++++++++++++++++++++++ Web/script/weightgrapher | 11 +++++ 3 files changed, 101 insertions(+) create mode 100644 Web/cpanfile create mode 100644 Web/lib/WeightGrapher/Web.pm create mode 100755 Web/script/weightgrapher diff --git a/Web/cpanfile b/Web/cpanfile new file mode 100644 index 0000000..d97a6d7 --- /dev/null +++ b/Web/cpanfile @@ -0,0 +1,5 @@ +requires 'Mojolicious'; +requires 'Mojo::Pg'; +requires 'Mojo::File'; +requires 'DateTime::Format::Pg'; +requires 'Text::CSV'; diff --git a/Web/lib/WeightGrapher/Web.pm b/Web/lib/WeightGrapher/Web.pm new file mode 100644 index 0000000..c321686 --- /dev/null +++ b/Web/lib/WeightGrapher/Web.pm @@ -0,0 +1,85 @@ +package WeightGrapher::Web; +use Mojo::Base 'Mojolicious', -signatures; +use WeightGrapher::DB; + +sub startup ($self) { + my $config = $self->plugin('NotYAMLConfig', { file => -e 'weightgrapher.yml' + ? 'weightgrapher.yml' + : '/etc/weightgrapher.yml' + }); + + # Configure the application + $self->secrets($config->{secrets}); + + # Set the cookie expires to 30 days. + $self->sessions->default_expiration(2592000); + + # Load our custom commands. + push @{$self->commands->namespaces}, 'WeightGrapher::Web::Command'; + + $self->helper( db => sub { + return state $db = WeightGrapher::DB->connect($config->{database}->{weightgrapher}); + }); + + # Standard router. + my $r = $self->routes->under( '/' => sub ($c) { + + # If the user has a uid session cookie, then load their user account. + if ( $c->session('uid') ) { + my $person = $c->db->resultset('Person')->find( $c->session('uid') ); + if ( $person && $person->is_enabled ) { + $c->stash->{person} = $person; + } + } + + return 1; + }); + + # Create a router chain that ensures the request is from an authenticated user. + my $auth = $r->under( '/' => sub ($c) { + + # Logged in user exists. + if ( $c->stash->{person} ) { + return 1; + } + + # No user account for this seession. + $c->redirect_to( $c->url_for( 'show_login' ) ); + return undef; + }); + + # Create a router chain that ensures the request is from an admin user. + my $admin = $auth->under( '/' => sub ($c) { + + # Logged in user exists. + if ( $c->stash->{person}->is_admin ) { + return 1; + } + + # No user account for this seession. + $c->redirect_to( $c->url_for( 'show_dashboard' ) ); + return undef; + }); + + # User registration, login, and logout. + $r->get ( '/register' )->to( 'Auth#register' )->name('show_register' ); + $r->post ( '/register' )->to( 'Auth#do_register' )->name('do_register' ); + $r->get ( '/login' )->to( 'Auth#login' )->name('show_login' ); + $r->post ( '/login' )->to( 'Auth#do_login' )->name('do_login' ); + $auth->get( '/logout' )->to( 'Auth#do_logout' )->name('do_logout' ); + + # User Forgot Password Workflow. + $r->get ( '/forgot' )->to('Auth#forgot' )->name('show_forgot' ); + $r->post( '/forgot' )->to('Auth#do_forgot' )->name('do_forgot' ); + $r->get ( '/reset/:token' )->to('Auth#reset' )->name('show_reset' ); + $r->post( '/reset/:token' )->to('Auth#do_reset' )->name('do_reset' ); + + # User setting changes when logged in + $auth->get ( '/profile' )->to('UserSettings#profile' )->name('show_profile' ); + $auth->post( '/profile' )->to('UserSettings#do_profile' )->name('do_profile' ); + $auth->get ( '/password' )->to('UserSettings#change_password' )->name('show_change_password' ); + $auth->post( '/password' )->to('UserSettings#do_change_password' )->name('do_change_password' ); + +} + +1; diff --git a/Web/script/weightgrapher b/Web/script/weightgrapher new file mode 100755 index 0000000..93c5da3 --- /dev/null +++ b/Web/script/weightgrapher @@ -0,0 +1,11 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use Mojo::File qw(curfile); +use lib curfile->dirname->sibling('lib')->to_string; +use Mojolicious::Commands; + +# Start command line interface for application +Mojolicious::Commands->start_app('WeightGrapher::Web'); +