Merge pull request #35 from Pear-Trading/finn/AdminAccountEdit

Account editing added
This commit is contained in:
Finn 2017-08-24 13:20:43 +01:00 committed by GitHub
commit 22c4a1198b
7 changed files with 378 additions and 109 deletions

View file

@ -169,6 +169,7 @@ sub startup {
$admin_routes->get('/users/:id')->to('admin-users#read'); $admin_routes->get('/users/:id')->to('admin-users#read');
$admin_routes->post('/users/:id')->to('admin-users#update'); $admin_routes->post('/users/:id')->to('admin-users#update');
$admin_routes->post('/users/:id/delete')->to('admin-users#delete'); $admin_routes->post('/users/:id/delete')->to('admin-users#delete');
$admin_routes->post('/users/:id/edit')->to('admin-users#edit');
$admin_routes->get('/organisations')->to('admin-organisations#list'); $admin_routes->get('/organisations')->to('admin-organisations#list');
$admin_routes->get('/organisations/add')->to('admin-organisations#add_org'); $admin_routes->get('/organisations/add')->to('admin-organisations#add_org');

View file

@ -1,15 +1,28 @@
package Pear::LocalLoop::Controller::Admin::Users; package Pear::LocalLoop::Controller::Admin::Users;
use Mojo::Base 'Mojolicious::Controller'; use Mojo::Base 'Mojolicious::Controller';
has result_set => sub { use Try::Tiny;
use Data::Dumper;
has user_result_set => sub {
my $c = shift; my $c = shift;
return $c->schema->resultset('User'); return $c->schema->resultset('User');
}; };
has customer_result_set => sub {
my $c = shift;
return $c->schema->resultset('Customer');
};
has organisation_result_set => sub {
my $c = shift;
return $c->schema->resultset('Organisation');
};
sub index { sub index {
my $c = shift; my $c = shift;
my $user_rs = $c->result_set; my $user_rs = $c->user_result_set;
$user_rs->result_class('DBIx::Class::ResultClass::HashRefInflator'); $user_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
$c->stash( users => [ $user_rs->all ] ); $c->stash( users => [ $user_rs->all ] );
} }
@ -19,7 +32,7 @@ sub read {
my $id = $c->param('id'); my $id = $c->param('id');
if ( my $user = $c->result_set->find($id) ) { if ( my $user = $c->user_result_set->find($id) ) {
$c->stash( user => $user ); $c->stash( user => $user );
} else { } else {
$c->flash( error => 'No User found' ); $c->flash( error => 'No User found' );
@ -27,6 +40,93 @@ sub read {
} }
} }
sub edit {
my $c = shift;
my $id = $c->param('id');
my $user;
unless ( $user = $c->user_result_set->find($id) ) {
$c->flash( error => 'No User found' );
return $c->redirect_to( '/admin/users/' . $id );
}
my $validation = $c->validation;
my $not_myself_user_rs = $c->user_result_set->search({
id => { "!=" => $user->id },
});
$validation->required('email')->email->not_in_resultset( 'email', $not_myself_user_rs );
$validation->required('postcode')->postcode;
$validation->optional('new_password');
if ( defined $user->customer_id ) {
$validation->required('display_name');
$validation->required('full_name');
} elsif ( defined $user->organisation_id ) {
$validation->required('name');
$validation->required('street_name');
$validation->required('town');
}
if ( $validation->has_error ) {
$c->flash( error => 'The validation has failed' );
$c->app->log->warn(Dumper $validation);
return $c->redirect_to( '/admin/users/' . $id );
}
if ( defined $user->customer_id ){
try {
$c->schema->txn_do( sub {
$user->customer->update({
full_name => $validation->param('full_name'),
display_name => $validation->param('display_name'),
postcode => $validation->param('postcode'),
});
$user->update({
email => $validation->param('email'),
( defined $validation->param('new_password') ? ( password => $validation->param('new_password') ) : () ),
});
});
} finally {
if ( @_ ) {
$c->flash( error => 'Something went wrong Updating the User' );
$c->app->log->warn(Dumper @_);
} else {
$c->flash( success => 'Updated User' );
};
}
}
elsif ( defined $user->organisation_id ) {
try {
$c->schema->txn_do( sub {
$user->organisation->update({
name => $validation->param('name'),
street_name => $validation->param('street_name'),
town => $validation->param('town'),
postcode => $validation->param('postcode'),
});
$user->update({
email => $validation->param('email'),
( defined $validation->param('new_password') ? ( password => $validation->param('new_password') ) : () ),
});
});
} finally {
if ( @_ ) {
$c->flash( error => 'Something went wrong Updating the User' );
$c->app->log->warn(Dumper @_);
} else {
$c->flash( success => 'Updated User' );
}
}
};
$c->redirect_to( '/admin/users/' . $id );
}
sub update { sub update {
my $c = shift; my $c = shift;
$c->redirect_to( '/admin/users' ); $c->redirect_to( '/admin/users' );

View file

@ -102,7 +102,6 @@ sub post_register{
} }
elsif ($usertype eq 'organisation') { elsif ($usertype eq 'organisation') {
my $fullAddress = $validation->param('fulladdress');
$c->schema->txn_do( sub { $c->schema->txn_do( sub {
$c->schema->resultset('AccountToken')->find({ $c->schema->resultset('AccountToken')->find({

View file

@ -148,7 +148,6 @@ sub post_account_update {
} }
elsif ( defined $user->organisation_id ) { elsif ( defined $user->organisation_id ) {
my $fullAddress = $validation->param('fulladdress');
$c->schema->txn_do( sub { $c->schema->txn_do( sub {
$user->organisation->update({ $user->organisation->update({

117
t/admin/user.t Normal file
View file

@ -0,0 +1,117 @@
use Mojo::Base -strict;
use Test::More;
use Mojo::JSON;
use Test::Pear::LocalLoop;
my $framework = Test::Pear::LocalLoop->new;
my $t = $framework->framework;
my $schema = $t->app->schema;
my $user = $schema->resultset('User')->create({
email => 'admin@example.com',
password => 'abc123',
administrator => {},
});
is $schema->resultset('Administrator')->count, 1, 'Admin Created';
my $user1 = {
token => 'a',
full_name => 'Test User1',
display_name => 'Test User1',
email => 'test1@example.com',
postcode => 'LA1 1AA',
password => 'abc123',
year_of_birth => 2006,
};
my $org = {
token => 'e',
email => 'test50@example.com',
name => '7th Heaven',
street_name => 'Slums, Sector 7',
town => 'Midgar',
postcode => 'WC1E 6AD',
password => 'abc123',
};
$schema->resultset('AccountToken')->create({ name => $_->{token} })
for ( $user1, $org );
$framework->register_customer($user1);
$framework->register_organisation($org);
#login to admin
$t->ua->max_redirects(10);
$t->post_ok('/admin', form => {
email => 'admin@example.com',
password => 'abc123',
})->status_is(200);
#Read customer user
$t->get_ok('/admin/users/2/')
->status_is(200);
#Read organisation user
$t->get_ok('/admin/users/3/')
->status_is(200);
#Valid customer user update
$t->post_ok('/admin/users/2/edit', form => {
email => 'test12@example.com',
new_password => 'abc123',
full_name => 'Test User1',
display_name => 'Test User1',
town => 'Midgar',
postcode => 'WC1E 6AD',
})->status_is(200)->content_like(qr/Updated User/);
#Failed validation on customer user from no postcode
$t->post_ok('/admin/users/2/edit', form => {
email => 'test12@example.com',
new_password => 'abc123',
full_name => 'Test User1',
display_name => 'Test User1',
town => 'Midgar',
})->content_like(qr/The validation has failed/);
#Failed validation on customer user from no display name
$t->post_ok('/admin/users/2/edit', form => {
email => 'test12@example.com',
new_password => 'abc123',
full_name => 'Test User1',
town => 'Midgar',
postcode => 'WC1E 6AD',
})->content_like(qr/The validation has failed/);
#Valid organisation user update
$t->post_ok('/admin/users/3/edit', form => {
email => 'test51@example.com',
new_password => 'abc123',
name => '7th Heaven',
street_name => 'Slums, Sector 7',
town => 'Midgar',
postcode => 'WC1E 6AD',
})->status_is(200)->content_like(qr/Updated User/);
#Failed validation on organisation user from no postcode
$t->post_ok('/admin/users/3/edit', form => {
email => 'test50@example.com',
new_password => 'abc123',
name => '7th Heaven',
street_name => 'Slums, Sector 7',
town => 'Midgar',
})->content_like(qr/The validation has failed/);
#Failed validation on organisation user from no street name
$t->post_ok('/admin/users/3/edit', form => {
email => 'test50@example.com',
new_password => 'abc123',
name => '7th Heaven',
town => 'Midgar',
postcode => 'WC1E 6AD',
})->content_like(qr/The validation has failed/);
done_testing();

View file

@ -92,12 +92,12 @@ $t->post_ok('/api/register' => json => $testJson)
#Blank name #Blank name
$testJson = { $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => 'a', 'token' => 'a',
'display_name' => 'test name', 'display_name' => 'test name',
'full_name' => '', 'full_name' => '',
'email' => 'a@b.com', 'email' => 'a@b.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'year_of_birth' => 2005 'year_of_birth' => 2005
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
@ -108,12 +108,12 @@ $t->post_ok('/api/register' => json => $testJson)
#Blank name #Blank name
$testJson = { $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => 'a', 'token' => 'a',
'display_name' => '', 'display_name' => '',
'full_name' => 'test name', 'full_name' => 'test name',
'email' => 'a@b.com', 'email' => 'a@b.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'year_of_birth' => 2005 'year_of_birth' => 2005
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
@ -125,13 +125,13 @@ $t->post_ok('/api/register' => json => $testJson)
#Valid customer #Valid customer
$testJson = { $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => 'a', 'token' => 'a',
'full_name' => 'test name', 'full_name' => 'test name',
'display_name' => 'test name', 'display_name' => 'test name',
'email' => 'a@b.com', 'email' => 'a@b.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'year_of_birth' => 2005 'year_of_birth' => 2005
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
@ -140,35 +140,35 @@ $t->post_ok('/api/register' => json => $testJson)
#Valid customer2 #Valid customer2
$testJson = { $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => 'b', 'token' => 'b',
'full_name' => 'test name', 'full_name' => 'test name',
'display_name' => 'test name', 'display_name' => 'test name',
'email' => 'b@c.com', 'email' => 'b@c.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'year_of_birth' => 2005 'year_of_birth' => 2005
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->or($dump_error) ->or($dump_error)
->status_is(200) ->status_is(200)
->or($dump_error) ->or($dump_error)
->json_is('/success', Mojo::JSON->true) ->json_is('/success', Mojo::JSON->true)
->or($dump_error); ->or($dump_error);
#Valid customer3 #Valid customer3
$testJson = { $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => 'c', 'token' => 'c',
'full_name' => 'test name', 'full_name' => 'test name',
'display_name' => 'test name', 'display_name' => 'test name',
'email' => 'c@d.com', 'email' => 'c@d.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'year_of_birth' => 2005 'year_of_birth' => 2005
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(200) ->status_is(200)
->json_is('/success', Mojo::JSON->true); ->json_is('/success', Mojo::JSON->true);
#email missing JSON #email missing JSON
@ -186,53 +186,53 @@ $t->post_ok('/api/register' => json => $testJson)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/no email sent/i); ->content_like(qr/no email sent/i);
#invalid email 1 #invalid email 1
$testJson = { $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => 'd', 'token' => 'd',
'full_name' => 'test name', 'full_name' => 'test name',
'display_name' => 'test name', 'display_name' => 'test name',
'email' => 'dfsd@.com', 'email' => 'dfsd@.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'year_of_birth' => 2006 'year_of_birth' => 2006
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/email/i) ->content_like(qr/email/i)
->content_like(qr/invalid/i); ->content_like(qr/invalid/i);
#invalid email 2 #invalid email 2
$testJson = { $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => 'd', 'token' => 'd',
'full_name' => 'test name', 'full_name' => 'test name',
'display_name' => 'test name', 'display_name' => 'test name',
'email' => 'dfsd@com', 'email' => 'dfsd@com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'year_of_birth' => 2006 'year_of_birth' => 2006
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/email/i) ->content_like(qr/email/i)
->content_like(qr/invalid/i); ->content_like(qr/invalid/i);
#Email exists #Email exists
$testJson = { $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => 'd', 'token' => 'd',
'full_name' => 'test name', 'full_name' => 'test name',
'display_name' => 'test name', 'display_name' => 'test name',
'email' => 'a@b.com', 'email' => 'a@b.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'year_of_birth' => 2006 'year_of_birth' => 2006
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(403) ->status_is(403)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/email/i) ->content_like(qr/email/i)
->content_like(qr/exists/i); ->content_like(qr/exists/i);
@ -288,16 +288,15 @@ $t->post_ok('/api/register' => json => $testJson)
#Invalid user type #Invalid user type
$testJson = { $testJson = {
'usertype' => 'organisation1', 'usertype' => 'organisation1',
'token' => 'f', 'token' => 'f',
'name' => 'test name', 'name' => 'test name',
'email' => 'org@org.com', 'email' => 'org@org.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'fulladdress' => 'mary lane testing....'
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/usertype/i) ->content_like(qr/usertype/i)
->content_like(qr/invalid/i); ->content_like(qr/invalid/i);
@ -320,29 +319,29 @@ $t->post_ok('/api/register' => json => $testJson)
#Age is invalid #Age is invalid
$testJson = { $testJson = {
'usertype' => 'customer', 'usertype' => 'customer',
'token' => 'f', 'token' => 'f',
'full_name' => 'test name', 'full_name' => 'test name',
'display_name' => 'test name', 'display_name' => 'test name',
'email' => 'test@example.com', 'email' => 'test@example.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'year_of_birth' => 'invalid' 'year_of_birth' => 'invalid'
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
->json_is('/success', Mojo::JSON->false) ->json_is('/success', Mojo::JSON->false)
->content_like(qr/year_of_birth/i) ->content_like(qr/year_of_birth/i)
->content_like(qr/invalid/i); ->content_like(qr/invalid/i);
#full address missing JSON #full address missing JSON
$testJson = { $testJson = {
'usertype' => 'organisation', 'usertype' => 'organisation',
'token' => 'f', 'token' => 'f',
'name' => 'test org', 'name' => 'test org',
'email' => 'org@org.com', 'email' => 'org@org.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(400) ->status_is(400)
@ -353,17 +352,17 @@ $t->post_ok('/api/register' => json => $testJson)
#Organisation valid #Organisation valid
$testJson = { $testJson = {
'usertype' => 'organisation', 'usertype' => 'organisation',
'token' => 'f', 'token' => 'f',
'name' => 'org name', 'name' => 'org name',
'email' => 'org@org.com', 'email' => 'org@org.com',
'postcode' => 'LA1 1AA', 'postcode' => 'LA1 1AA',
'password' => 'Meh', 'password' => 'Meh',
'street_name' => 'mary lane testing....', 'street_name' => 'mary lane testing....',
'town' => 'Lancaster', 'town' => 'Lancaster',
}; };
$t->post_ok('/api/register' => json => $testJson) $t->post_ok('/api/register' => json => $testJson)
->status_is(200) ->status_is(200)
->json_is('/success', Mojo::JSON->true); ->json_is('/success', Mojo::JSON->true);
is $t->app->schema->resultset('User')->count, 4, 'Correct user count'; is $t->app->schema->resultset('User')->count, 4, 'Correct user count';

View file

@ -11,20 +11,74 @@
<strong>Success!</strong> <%= $success %> <strong>Success!</strong> <%= $success %>
</div> </div>
% } % }
<form action="<%= url_for %>" method="post"> <form action="<%= url_for . '/edit' %>" method="post" autocomplete="off">
<div class="form-group"> <h3 class="card-header">
<label for="email">Email Address</label> User Details
<input id="email" type="text" class="form-control" placeholder="Email" name="email" value="<%= $user->email %>" disabled> </h3>
</div> <div class="form-group">
<div class="form-group"> <label for="email">Email Address</label>
<label for="joindate">Join Date</label> <input id="email" type="text" autocomplete="off" class="form-control" placeholder="Email" name="email" value="<%= $user->email %>">
<input id="joindate" type="datetime" class="form-control" placeholder="Date" name="joindate" value="<%= $user->join_date %>" disabled> </div>
</div> <div class="form-group">
<div class="form-group"> <label for="joindate">Join Date</label>
<label for="type">Account Type</label> <input id="joindate" type="datetime" class="form-control" placeholder="Date" name="joindate" value="<%= $user->join_date %>" disabled>
<input id="type" type="text" class="form-control" value="<%= defined $user->customer_id ? 'Customer' : 'Organisation' %>" disabled> </div>
</div> <div class="form-group">
<div class="form-group"> <label for="type">Account Type</label>
<button class="btn btn-primary form-control" type="submit">Update</button> <input id="type" type="text" class="form-control" value="<%= defined $user->customer_id ? 'Customer' : 'Organisation' %>" disabled>
</div> </div>
<div class="form-group">
<label for="new_password">New Password</label>
<input id="new_password" type="password" autocomplete="off" class="form-control" placeholder="New Password" name="new_password">
<p class="help-block">Leave blank unless you want to change their password</p>
</div>
% if ( my $customer_rs = $user->customer ) {
<h3 class="card-header">
Customer Details
</h3>
<div class="form-group">
<label for="postcode">Customer Postcode</label>
<input id="postcode" type="text" class="form-control" placeholder="Postcode" name="postcode" value="<%= $customer_rs->postcode %>">
</div>
<div class="form-group">
<label for="full_name">Full Name</label>
<input id="full_name" type="text" class="form-control" placeholder="Full Name" name="full_name" value="<%= $customer_rs->full_name %>">
</div>
<div class="form-group">
<label for="display_name">Display Name</label>
<input id="display_name" type="text" class="form-control" placeholder="Display Name" name="display_name" value="<%= $customer_rs->display_name %>">
</div>
<div class="form-group">
<label for="year_of_birth">Year of Birth</label>
<input id="year_of_birth" type="number" class="form-control" placeholder="Year of Birth" name="year_of_birth" value="<%= $customer_rs->year_of_birth %>" disabled>
</div>
% } elsif ( my $org_rs = $user->organisation ) {
<h3 class="card-header">
Organisation Details
</h3>
<div class="form-group">
<label for="postcode">Organisation Postcode</label>
<input id="postcode" type="text" class="form-control" placeholder="Postcode" name="postcode" value="<%= $org_rs->postcode %>">
</div>
<div class="form-group">
<label for="name">Organisation Name</label>
<input id="name" type="text" class="form-control" placeholder="Organisation Name" name="name" value="<%= $org_rs->name %>">
</div>
<div class="form-group">
<label for="street_name">Street Name</label>
<input id="street_name" type="text" class="form-control" placeholder="Street Name" name="street_name" value="<%= $org_rs->street_name %>">
</div>
<div class="form-group">
<label for="town">Town</label>
<input id="town" type="text" class="form-control" placeholder="Town" name="town" value="<%= $org_rs->town %>">
</div>
% } else {
<h3 class="card-header">
User is not a customer or an organisation
</h3>
% }
<div class="form-group">
<button class="btn btn-primary form-control" type="submit">Edit Account</button>
</div>
</form> </form>