From 00b068713665c01b1d2067c4d2dfb06c7c6be88b Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Fri, 21 Apr 2017 22:48:47 +0100 Subject: [PATCH] Fixed token administration --- lib/Pear/LocalLoop/Controller/Admin/Tokens.pm | 29 ++++++++++--------- templates/admin/tokens/index.html.ep | 6 ++-- templates/admin/tokens/read.html.ep | 4 +-- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Admin/Tokens.pm b/lib/Pear/LocalLoop/Controller/Admin/Tokens.pm index 982aca8..5fec0cb 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/Tokens.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/Tokens.pm @@ -17,21 +17,24 @@ sub index { # POST sub create { my $c = shift; + my $validation = $c->validation; - $validation->required('token-name', 'trim')->like(qr/^[\w]*$/); + $validation->required('token', 'trim')->like(qr/^[\w]*$/)->not_in_resultset('name', $c->result_set); - my $token_name = $validation->param('token-name'); + my $token_name = $validation->param('token'); - my $token_rs = $c->result_set; - if ( $validation->has_error ) { - # Only one validator, fairly obvious whats broke - $c->flash( error => 'Token name not valid - Alphanumeric characters and Underscore only' ); - } elsif ( $token_rs->find({ accounttokenname => $token_name }) ) { - $c->flash( error => 'Token Already Exists' ); + 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' ); - $token_rs->create({ accounttokenname => $token_name }); + $c->result_set->create({ name => $token_name }); } $c->redirect_to( '/admin/tokens' ); } @@ -54,8 +57,8 @@ sub read { sub update { my $c = shift; my $validation = $c->validation; - $validation->required('token-name', 'trim')->like(qr/^[\w]*$/); - $validation->required('token-used')->in( qw/ 0 1 / ); + $validation->required('token', 'trim')->like(qr/^[\w]*$/); + $validation->required('used')->in( qw/ 0 1 / ); my $id = $c->param('id'); @@ -65,8 +68,8 @@ sub update { $c->redirect_to( '/admin/tokens/' . $id ); } elsif ( my $token = $c->result_set->find($id) ) { $token->update({ - accounttokenname => $validation->param('token-name'), - used => $validation->param('token-used'), + name => $validation->param('token'), + used => $validation->param('used'), }); $c->flash( success => 'Token Updated' ); $c->redirect_to( '/admin/tokens/' . $id ); diff --git a/templates/admin/tokens/index.html.ep b/templates/admin/tokens/index.html.ep index 6cece6e..772463a 100644 --- a/templates/admin/tokens/index.html.ep +++ b/templates/admin/tokens/index.html.ep @@ -14,7 +14,7 @@