Fixed token administration

This commit is contained in:
Tom Bloor 2017-04-21 22:48:47 +01:00
parent 17dacd83c3
commit 00b0687136
3 changed files with 21 additions and 18 deletions

View file

@ -17,21 +17,24 @@ sub index {
# POST # POST
sub create { sub create {
my $c = shift; my $c = shift;
my $validation = $c->validation; 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 ) { if ( $validation->has_error ) {
# Only one validator, fairly obvious whats broke my $check = shift @{ $c->validation->error('token') };
$c->flash( error => 'Token name not valid - Alphanumeric characters and Underscore only' ); if ( $check eq 'required' ) {
} elsif ( $token_rs->find({ accounttokenname => $token_name }) ) { $c->flash( error => 'Token name is required' );
$c->flash( error => 'Token Already Exists' ); } 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 { } else {
$c->flash( success => 'Token Created' ); $c->flash( success => 'Token Created' );
$token_rs->create({ accounttokenname => $token_name }); $c->result_set->create({ name => $token_name });
} }
$c->redirect_to( '/admin/tokens' ); $c->redirect_to( '/admin/tokens' );
} }
@ -54,8 +57,8 @@ sub read {
sub update { sub update {
my $c = shift; my $c = shift;
my $validation = $c->validation; my $validation = $c->validation;
$validation->required('token-name', 'trim')->like(qr/^[\w]*$/); $validation->required('token', 'trim')->like(qr/^[\w]*$/);
$validation->required('token-used')->in( qw/ 0 1 / ); $validation->required('used')->in( qw/ 0 1 / );
my $id = $c->param('id'); my $id = $c->param('id');
@ -65,8 +68,8 @@ sub update {
$c->redirect_to( '/admin/tokens/' . $id ); $c->redirect_to( '/admin/tokens/' . $id );
} elsif ( my $token = $c->result_set->find($id) ) { } elsif ( my $token = $c->result_set->find($id) ) {
$token->update({ $token->update({
accounttokenname => $validation->param('token-name'), name => $validation->param('token'),
used => $validation->param('token-used'), used => $validation->param('used'),
}); });
$c->flash( success => 'Token Updated' ); $c->flash( success => 'Token Updated' );
$c->redirect_to( '/admin/tokens/' . $id ); $c->redirect_to( '/admin/tokens/' . $id );

View file

@ -14,7 +14,7 @@
<form action="<%= url_for %>" method="post"> <form action="<%= url_for %>" method="post">
<div class="form-group"> <div class="form-group">
<div class="input-group"> <div class="input-group">
<input type="text" class="form-control" placeholder="Token Text" name="token-name"> <input type="text" class="form-control" placeholder="Token Text" name="token">
<span class="input-group-btn"> <span class="input-group-btn">
<button class="btn btn-primary" type="submit">Add</button> <button class="btn btn-primary" type="submit">Add</button>
</span> </span>
@ -23,9 +23,9 @@
</form> </form>
<div class="list-group"> <div class="list-group">
% for my $token (@$tokens) { % for my $token (@$tokens) {
<a href="<%= url_for . '/' . $token->{accounttokenid} %>" class="list-group-item list-group-item-action"> <a href="<%= url_for . '/' . $token->{id} %>" class="list-group-item list-group-item-action">
<div> <div>
%= $token->{accounttokenname} %= $token->{name}
</div> </div>
<div class="ml-auto"> <div class="ml-auto">
<%= $token->{used} == 1 ? 'Used' : 'Available' %> <%= $token->{used} == 1 ? 'Used' : 'Available' %>

View file

@ -14,11 +14,11 @@
<form action="<%= url_for %>" method="post"> <form action="<%= url_for %>" method="post">
<div class="form-group"> <div class="form-group">
<label for="token-name">Token Name</label> <label for="token-name">Token Name</label>
<input id="token-name" type="text" class="form-control" placeholder="Token Text" name="token-name" value="<%= $token->accounttokenname %>"> <input id="token-name" type="text" class="form-control" placeholder="Token Text" name="token" value="<%= $token->name %>">
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="token-used">Token Used?</label> <label for="token-used">Token Used?</label>
<select id="token-used" class="form-control" name="token-used"> <select id="token-used" class="form-control" name="used">
<option value="0"<%= $token->used == 0 ? ' selected' : '' %>>Available</option> <option value="0"<%= $token->used == 0 ? ' selected' : '' %>>Available</option>
<option value="1"<%= $token->used == 1 ? ' selected' : '' %>>Used</option> <option value="1"<%= $token->used == 1 ? ' selected' : '' %>>Used</option>
</select> </select>