diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 4813064..c41371d 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -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'); diff --git a/lib/Pear/LocalLoop/Controller/Admin/Users.pm b/lib/Pear/LocalLoop/Controller/Admin/Users.pm index 2fbdd17..2fbc110 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/Users.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/Users.pm @@ -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' ); diff --git a/lib/Pear/LocalLoop/Controller/Api/Register.pm b/lib/Pear/LocalLoop/Controller/Api/Register.pm index ee760ee..da6805a 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Register.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Register.pm @@ -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({ diff --git a/lib/Pear/LocalLoop/Controller/Api/User.pm b/lib/Pear/LocalLoop/Controller/Api/User.pm index ef9ec89..cb2396f 100644 --- a/lib/Pear/LocalLoop/Controller/Api/User.pm +++ b/lib/Pear/LocalLoop/Controller/Api/User.pm @@ -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({ diff --git a/t/admin/user.t b/t/admin/user.t new file mode 100644 index 0000000..a531600 --- /dev/null +++ b/t/admin/user.t @@ -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(); diff --git a/t/api/register.t b/t/api/register.t index 40020ac..a1a20ec 100644 --- a/t/api/register.t +++ b/t/api/register.t @@ -92,12 +92,12 @@ $t->post_ok('/api/register' => json => $testJson) #Blank name $testJson = { 'usertype' => 'customer', - 'token' => 'a', - 'display_name' => 'test name', - 'full_name' => '', - 'email' => 'a@b.com', - 'postcode' => 'LA1 1AA', - 'password' => 'Meh', + 'token' => 'a', + 'display_name' => 'test name', + 'full_name' => '', + 'email' => 'a@b.com', + 'postcode' => 'LA1 1AA', + 'password' => 'Meh', 'year_of_birth' => 2005 }; $t->post_ok('/api/register' => json => $testJson) @@ -108,12 +108,12 @@ $t->post_ok('/api/register' => json => $testJson) #Blank name $testJson = { 'usertype' => 'customer', - 'token' => 'a', - 'display_name' => '', - 'full_name' => 'test name', - 'email' => 'a@b.com', - 'postcode' => 'LA1 1AA', - 'password' => 'Meh', + 'token' => 'a', + 'display_name' => '', + 'full_name' => 'test name', + 'email' => 'a@b.com', + 'postcode' => 'LA1 1AA', + 'password' => 'Meh', 'year_of_birth' => 2005 }; $t->post_ok('/api/register' => json => $testJson) @@ -125,13 +125,13 @@ $t->post_ok('/api/register' => json => $testJson) #Valid customer $testJson = { - 'usertype' => 'customer', - 'token' => 'a', - 'full_name' => 'test name', - 'display_name' => 'test name', - 'email' => 'a@b.com', - 'postcode' => 'LA1 1AA', - 'password' => 'Meh', + 'usertype' => 'customer', + 'token' => 'a', + 'full_name' => 'test name', + 'display_name' => 'test name', + 'email' => 'a@b.com', + 'postcode' => 'LA1 1AA', + 'password' => 'Meh', 'year_of_birth' => 2005 }; $t->post_ok('/api/register' => json => $testJson) @@ -140,35 +140,35 @@ $t->post_ok('/api/register' => json => $testJson) #Valid customer2 $testJson = { - 'usertype' => 'customer', - 'token' => 'b', - 'full_name' => 'test name', - 'display_name' => 'test name', - 'email' => 'b@c.com', - 'postcode' => 'LA1 1AA', - 'password' => 'Meh', + 'usertype' => 'customer', + 'token' => 'b', + 'full_name' => 'test name', + 'display_name' => 'test name', + 'email' => 'b@c.com', + 'postcode' => 'LA1 1AA', + 'password' => 'Meh', 'year_of_birth' => 2005 }; $t->post_ok('/api/register' => json => $testJson) ->or($dump_error) - ->status_is(200) + ->status_is(200) ->or($dump_error) ->json_is('/success', Mojo::JSON->true) ->or($dump_error); #Valid customer3 $testJson = { - 'usertype' => 'customer', - 'token' => 'c', - 'full_name' => 'test name', - 'display_name' => 'test name', - 'email' => 'c@d.com', - 'postcode' => 'LA1 1AA', - 'password' => 'Meh', + 'usertype' => 'customer', + 'token' => 'c', + 'full_name' => 'test name', + 'display_name' => 'test name', + 'email' => 'c@d.com', + 'postcode' => 'LA1 1AA', + 'password' => 'Meh', 'year_of_birth' => 2005 }; $t->post_ok('/api/register' => json => $testJson) - ->status_is(200) + ->status_is(200) ->json_is('/success', Mojo::JSON->true); #email missing JSON @@ -186,53 +186,53 @@ $t->post_ok('/api/register' => json => $testJson) ->json_is('/success', Mojo::JSON->false) ->content_like(qr/no email sent/i); -#invalid email 1 +#invalid email 1 $testJson = { 'usertype' => 'customer', - 'token' => 'd', - 'full_name' => 'test name', - 'display_name' => 'test name', - 'email' => 'dfsd@.com', - 'postcode' => 'LA1 1AA', - 'password' => 'Meh', + 'token' => 'd', + 'full_name' => 'test name', + 'display_name' => 'test name', + 'email' => 'dfsd@.com', + 'postcode' => 'LA1 1AA', + 'password' => 'Meh', 'year_of_birth' => 2006 }; $t->post_ok('/api/register' => json => $testJson) - ->status_is(400) + ->status_is(400) ->json_is('/success', Mojo::JSON->false) ->content_like(qr/email/i) ->content_like(qr/invalid/i); #invalid email 2 $testJson = { - 'usertype' => 'customer', - 'token' => 'd', - 'full_name' => 'test name', - 'display_name' => 'test name', - 'email' => 'dfsd@com', - 'postcode' => 'LA1 1AA', - 'password' => 'Meh', + 'usertype' => 'customer', + 'token' => 'd', + 'full_name' => 'test name', + 'display_name' => 'test name', + 'email' => 'dfsd@com', + 'postcode' => 'LA1 1AA', + 'password' => 'Meh', 'year_of_birth' => 2006 }; $t->post_ok('/api/register' => json => $testJson) - ->status_is(400) + ->status_is(400) ->json_is('/success', Mojo::JSON->false) ->content_like(qr/email/i) ->content_like(qr/invalid/i); #Email exists $testJson = { - 'usertype' => 'customer', - 'token' => 'd', - 'full_name' => 'test name', - 'display_name' => 'test name', - 'email' => 'a@b.com', - 'postcode' => 'LA1 1AA', - 'password' => 'Meh', + 'usertype' => 'customer', + 'token' => 'd', + 'full_name' => 'test name', + 'display_name' => 'test name', + 'email' => 'a@b.com', + 'postcode' => 'LA1 1AA', + 'password' => 'Meh', 'year_of_birth' => 2006 }; $t->post_ok('/api/register' => json => $testJson) - ->status_is(403) + ->status_is(403) ->json_is('/success', Mojo::JSON->false) ->content_like(qr/email/i) ->content_like(qr/exists/i); @@ -288,16 +288,15 @@ $t->post_ok('/api/register' => json => $testJson) #Invalid user type $testJson = { - 'usertype' => 'organisation1', - 'token' => 'f', - 'name' => 'test name', - 'email' => 'org@org.com', - 'postcode' => 'LA1 1AA', - 'password' => 'Meh', - 'fulladdress' => 'mary lane testing....' + 'usertype' => 'organisation1', + 'token' => 'f', + 'name' => 'test name', + 'email' => 'org@org.com', + 'postcode' => 'LA1 1AA', + 'password' => 'Meh', }; $t->post_ok('/api/register' => json => $testJson) - ->status_is(400) + ->status_is(400) ->json_is('/success', Mojo::JSON->false) ->content_like(qr/usertype/i) ->content_like(qr/invalid/i); @@ -320,29 +319,29 @@ $t->post_ok('/api/register' => json => $testJson) #Age is invalid $testJson = { - 'usertype' => 'customer', - 'token' => 'f', - 'full_name' => 'test name', - 'display_name' => 'test name', - 'email' => 'test@example.com', - 'postcode' => 'LA1 1AA', - 'password' => 'Meh', + 'usertype' => 'customer', + 'token' => 'f', + 'full_name' => 'test name', + 'display_name' => 'test name', + 'email' => 'test@example.com', + 'postcode' => 'LA1 1AA', + 'password' => 'Meh', 'year_of_birth' => 'invalid' }; $t->post_ok('/api/register' => json => $testJson) - ->status_is(400) + ->status_is(400) ->json_is('/success', Mojo::JSON->false) ->content_like(qr/year_of_birth/i) ->content_like(qr/invalid/i); #full address missing JSON $testJson = { - 'usertype' => 'organisation', - 'token' => 'f', - 'name' => 'test org', - 'email' => 'org@org.com', - 'postcode' => 'LA1 1AA', - 'password' => 'Meh', + 'usertype' => 'organisation', + 'token' => 'f', + 'name' => 'test org', + 'email' => 'org@org.com', + 'postcode' => 'LA1 1AA', + 'password' => 'Meh', }; $t->post_ok('/api/register' => json => $testJson) ->status_is(400) @@ -353,17 +352,17 @@ $t->post_ok('/api/register' => json => $testJson) #Organisation valid $testJson = { - 'usertype' => 'organisation', - 'token' => 'f', - 'name' => 'org name', - 'email' => 'org@org.com', - 'postcode' => 'LA1 1AA', - 'password' => 'Meh', + 'usertype' => 'organisation', + 'token' => 'f', + 'name' => 'org name', + 'email' => 'org@org.com', + 'postcode' => 'LA1 1AA', + 'password' => 'Meh', 'street_name' => 'mary lane testing....', 'town' => 'Lancaster', }; $t->post_ok('/api/register' => json => $testJson) - ->status_is(200) + ->status_is(200) ->json_is('/success', Mojo::JSON->true); is $t->app->schema->resultset('User')->count, 4, 'Correct user count'; diff --git a/templates/admin/users/read.html.ep b/templates/admin/users/read.html.ep index eaca969..202efed 100644 --- a/templates/admin/users/read.html.ep +++ b/templates/admin/users/read.html.ep @@ -11,20 +11,74 @@ Success! <%= $success %> % } -
-
- - -
-
- - -
-
- - -
-
- -
+ +

+ User Details +

+
+ + +
+
+ + +
+
+ + +
+
+ + +

Leave blank unless you want to change their password

+
+ % if ( my $customer_rs = $user->customer ) { +

+ Customer Details +

+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ % } elsif ( my $org_rs = $user->organisation ) { +

+ Organisation Details +

+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ % } else { +

+ User is not a customer or an organisation +

+ % } + +
+ +