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

98 lines
2.4 KiB
Perl
Raw 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 {
my $c = shift;
return $c->schema->resultset('AccountToken');
};
sub index {
my $c = shift;
my $token_rs = $c->result_set;
$token_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
$c->stash( tokens => [ $token_rs->all ] );
}
# POST
sub create {
my $c = shift;
2017-04-21 21:48:47 +00:00
my $validation = $c->validation;
2017-04-21 21:48:47 +00:00
$validation->required('token', 'trim')->like(qr/^[\w]*$/)->not_in_resultset('name', $c->result_set);
2017-04-08 01:32:13 +00:00
2017-04-21 21:48:47 +00:00
my $token_name = $validation->param('token');
2017-04-08 01:32:13 +00:00
if ( $validation->has_error ) {
2017-04-21 21:48:47 +00:00
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' );
}
2017-04-08 01:32:13 +00:00
} else {
$c->flash( success => 'Token Created' );
2017-04-21 21:48:47 +00:00
$c->result_set->create({ name => $token_name });
2017-04-08 01:32:13 +00:00
}
$c->redirect_to( '/admin/tokens' );
}
# GET
sub read {
my $c = shift;
my $id = $c->param('id');
if ( my $token = $c->result_set->find($id) ) {
$c->stash( token => $token );
} else {
$c->flash( error => 'No Token found' );
$c->redirect_to( '/admin/tokens' );
}
}
# POST
sub update {
my $c = shift;
my $validation = $c->validation;
2017-04-21 21:48:47 +00:00
$validation->required('token', 'trim')->like(qr/^[\w]*$/);
$validation->required('used')->in( qw/ 0 1 / );
2017-04-08 01:32:13 +00:00
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) ) {
2017-04-08 01:32:13 +00:00
$token->update({
2017-04-21 21:48:47 +00:00
name => $validation->param('token'),
used => $validation->param('used'),
2017-04-08 01:32:13 +00:00
});
$c->flash( success => 'Token Updated' );
$c->redirect_to( '/admin/tokens/' . $id );
} else {
$c->flash( error => 'No Token found' );
$c->redirect_to( '/admin/tokens' );
}
}
# DELETE
sub delete {
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' );
}
1;