This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
Foodloop-Server/lib/Pear/LocalLoop/Controller/Admin.pm

71 lines
1.7 KiB
Perl
Raw Permalink Normal View History

package Pear::LocalLoop::Controller::Admin;
use Mojo::Base 'Mojolicious::Controller';
sub under {
2021-03-20 12:09:50 +00:00
my $c = shift;
2021-03-20 12:09:50 +00:00
if ( $c->is_user_authenticated ) {
return 1 if $c->current_user->is_admin;
}
$c->redirect_to('/admin');
return 0;
}
sub home {
2021-03-20 12:09:50 +00:00
my $c = shift;
my $user_rs = $c->schema->resultset('User');
my $token_rs = $c->schema->resultset('AccountToken');
my $pending_orgs_rs =
$c->schema->resultset('Organisation')->search( { pending => 1 } );
my $pending_transaction_rs = $pending_orgs_rs->entity->sales;
my $feedback_rs = $c->schema->resultset('Feedback');
my $pending_feedback_rs = $feedback_rs->search( { actioned => 0 } );
$c->stash(
user_count => $user_rs->count,
tokens => {
total => $token_rs->count,
unused => $token_rs->search( { used => 0 } )->count,
},
pending_orgs => $pending_orgs_rs->count,
pending_trans => $pending_transaction_rs->count,
feedback => {
total => $feedback_rs->count,
pending => $pending_feedback_rs->count,
},
);
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
}
sub auth_login {
2021-03-20 12:09:50 +00:00
my $c = shift;
2021-03-20 12:09:50 +00:00
$c->app->log->debug(
__PACKAGE__ . " admin login attempt for [" . $c->param('email') . "]" );
2017-11-21 10:42:23 +00:00
2021-03-20 12:09:50 +00:00
if ( $c->authenticate( $c->param('email'), $c->param('password') ) ) {
$c->redirect_to('/admin/home');
}
else {
$c->app->log->info( __PACKAGE__
. " failed admin login for ["
. $c->param('email')
. "]" );
$c->redirect_to('/admin');
}
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
}
sub auth_logout {
2021-03-20 12:09:50 +00:00
my $c = shift;
2021-03-20 12:09:50 +00:00
$c->logout;
$c->redirect_to('/admin');
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
}
1;