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/Tokens.pm

121 lines
2.8 KiB
Perl
Raw Permalink Normal View History

2017-04-08 01:32:13 +00:00
package Pear::LocalLoop::Controller::Admin::Tokens;
use Mojo::Base 'Mojolicious::Controller';
has result_set => sub {
2021-03-20 12:09:50 +00:00
my $c = shift;
return $c->schema->resultset('AccountToken');
2017-04-08 01:32:13 +00:00
};
2021-03-20 19:03:59 +00:00
sub idx {
2021-03-20 12:09:50 +00:00
my $c = shift;
2017-04-08 01:32:13 +00:00
2021-03-20 12:09:50 +00:00
my $token_rs = $c->result_set;
$token_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
$c->stash( tokens => [ $token_rs->all ] );
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
2017-04-08 01:32:13 +00:00
}
# POST
sub create {
2021-03-20 12:09:50 +00:00
my $c = shift;
my $validation = $c->validation;
$validation->required( 'token', 'trim' )->like(qr/^[\w]*$/)
->not_in_resultset( 'name', $c->result_set );
my $token_name = $validation->param('token');
if ( $validation->has_error ) {
my $check = shift @{ $c->validation->error('token') };
if ( $check eq 'required' ) {
$c->flash( error => 'Token name is required' );
}
elsif ( $check eq 'like' ) {
$c->flash( error =>
'Token name not valid - Alphanumeric characters and Underscore only'
);
}
elsif ( $check eq 'not_in_resultset' ) {
$c->flash( error => 'Token Already Exists' );
}
}
else {
$c->flash( success => 'Token Created' );
$c->result_set->create( { name => $token_name } );
}
$c->redirect_to('/admin/tokens');
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
2017-04-08 01:32:13 +00:00
}
# GET
2021-03-20 19:03:59 +00:00
sub get {
2021-03-20 12:09:50 +00:00
my $c = shift;
2017-04-08 01:32:13 +00:00
2021-03-20 12:09:50 +00:00
my $id = $c->param('id');
2017-04-08 01:32:13 +00:00
2021-03-20 12:09:50 +00:00
if ( my $token = $c->result_set->find($id) ) {
$c->stash( token => $token );
}
else {
$c->flash( error => 'No Token found' );
$c->redirect_to('/admin/tokens');
}
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
2017-04-08 01:32:13 +00:00
}
# POST
sub update {
2021-03-20 12:09:50 +00:00
my $c = shift;
my $validation = $c->validation;
$validation->required( 'token', 'trim' )->like(qr/^[\w]*$/);
$validation->required('used')->in(qw/ 0 1 /);
my $id = $c->param('id');
if ( $validation->has_error ) {
my $names = $validation->failed;
$c->flash(
error => 'Error in submitted data: ' . join( ', ', @$names ) );
$c->redirect_to( '/admin/tokens/' . $id );
}
elsif ( my $token = $c->result_set->find($id) ) {
$token->update(
{
name => $validation->param('token'),
used => $validation->param('used'),
}
);
$c->flash( success => 'Token Updated' );
$c->redirect_to( '/admin/tokens/' . $id );
}
else {
$c->flash( error => 'No Token found' );
$c->redirect_to('/admin/tokens');
}
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
2017-04-08 01:32:13 +00:00
}
# DELETE
2021-03-20 19:03:59 +00:00
sub del {
2021-03-20 12:09:50 +00:00
my $c = shift;
my $id = $c->param('id');
if ( my $token = $c->result_set->find($id) ) {
$token->delete;
$c->flash( success => 'Token Deleted' );
}
else {
$c->flash( error => 'No Token found' );
}
$c->redirect_to('/admin/tokens');
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
2017-04-08 01:32:13 +00:00
}
1;