Merge pull request #35 from Pear-Trading/finn/AdminAccountEdit
Account editing added
This commit is contained in:
commit
22c4a1198b
7 changed files with 378 additions and 109 deletions
|
@ -169,6 +169,7 @@ sub startup {
|
|||
$admin_routes->get('/users/:id')->to('admin-users#read');
|
||||
$admin_routes->post('/users/:id')->to('admin-users#update');
|
||||
$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/add')->to('admin-organisations#add_org');
|
||||
|
|
|
@ -1,15 +1,28 @@
|
|||
package Pear::LocalLoop::Controller::Admin::Users;
|
||||
use Mojo::Base 'Mojolicious::Controller';
|
||||
|
||||
has result_set => sub {
|
||||
use Try::Tiny;
|
||||
use Data::Dumper;
|
||||
|
||||
has user_result_set => sub {
|
||||
my $c = shift;
|
||||
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 {
|
||||
my $c = shift;
|
||||
|
||||
my $user_rs = $c->result_set;
|
||||
my $user_rs = $c->user_result_set;
|
||||
$user_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
|
||||
$c->stash( users => [ $user_rs->all ] );
|
||||
}
|
||||
|
@ -19,7 +32,7 @@ sub read {
|
|||
|
||||
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 );
|
||||
} else {
|
||||
$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 {
|
||||
my $c = shift;
|
||||
$c->redirect_to( '/admin/users' );
|
||||
|
|
|
@ -102,7 +102,6 @@ sub post_register{
|
|||
|
||||
}
|
||||
elsif ($usertype eq 'organisation') {
|
||||
my $fullAddress = $validation->param('fulladdress');
|
||||
|
||||
$c->schema->txn_do( sub {
|
||||
$c->schema->resultset('AccountToken')->find({
|
||||
|
|
|
@ -148,7 +148,6 @@ sub post_account_update {
|
|||
|
||||
}
|
||||
elsif ( defined $user->organisation_id ) {
|
||||
my $fullAddress = $validation->param('fulladdress');
|
||||
|
||||
$c->schema->txn_do( sub {
|
||||
$user->organisation->update({
|
||||
|
|
117
t/admin/user.t
Normal file
117
t/admin/user.t
Normal 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();
|
|
@ -294,7 +294,6 @@ $testJson = {
|
|||
'email' => 'org@org.com',
|
||||
'postcode' => 'LA1 1AA',
|
||||
'password' => 'Meh',
|
||||
'fulladdress' => 'mary lane testing....'
|
||||
};
|
||||
$t->post_ok('/api/register' => json => $testJson)
|
||||
->status_is(400)
|
||||
|
|
|
@ -11,20 +11,74 @@
|
|||
<strong>Success!</strong> <%= $success %>
|
||||
</div>
|
||||
% }
|
||||
<form action="<%= url_for %>" method="post">
|
||||
<div class="form-group">
|
||||
<label for="email">Email Address</label>
|
||||
<input id="email" type="text" class="form-control" placeholder="Email" name="email" value="<%= $user->email %>" disabled>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="joindate">Join Date</label>
|
||||
<input id="joindate" type="datetime" class="form-control" placeholder="Date" name="joindate" value="<%= $user->join_date %>" disabled>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="type">Account Type</label>
|
||||
<input id="type" type="text" class="form-control" value="<%= defined $user->customer_id ? 'Customer' : 'Organisation' %>" disabled>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<button class="btn btn-primary form-control" type="submit">Update</button>
|
||||
</div>
|
||||
<form action="<%= url_for . '/edit' %>" method="post" autocomplete="off">
|
||||
<h3 class="card-header">
|
||||
User Details
|
||||
</h3>
|
||||
<div class="form-group">
|
||||
<label for="email">Email Address</label>
|
||||
<input id="email" type="text" autocomplete="off" class="form-control" placeholder="Email" name="email" value="<%= $user->email %>">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="joindate">Join Date</label>
|
||||
<input id="joindate" type="datetime" class="form-control" placeholder="Date" name="joindate" value="<%= $user->join_date %>" disabled>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="type">Account Type</label>
|
||||
<input id="type" type="text" class="form-control" value="<%= defined $user->customer_id ? 'Customer' : 'Organisation' %>" disabled>
|
||||
</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>
|
||||
|
|
Reference in a new issue