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
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 );

View file

@ -14,7 +14,7 @@
<form action="<%= url_for %>" method="post">
<div class="form-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">
<button class="btn btn-primary" type="submit">Add</button>
</span>
@ -23,9 +23,9 @@
</form>
<div class="list-group">
% 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>
%= $token->{accounttokenname}
%= $token->{name}
</div>
<div class="ml-auto">
<%= $token->{used} == 1 ? 'Used' : 'Available' %>

View file

@ -14,11 +14,11 @@
<form action="<%= url_for %>" method="post">
<div class="form-group">
<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 class="form-group">
<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="1"<%= $token->used == 1 ? ' selected' : '' %>>Used</option>
</select>