From d0cbb215d86264f6c243537f2b9c505c1cd7d85d Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Fri, 21 Apr 2017 20:52:14 +0100 Subject: [PATCH] refactor user table and setup timestamp auto setting --- cpanfile | 1 + lib/Pear/LocalLoop.pm | 55 +---- lib/Pear/LocalLoop/Controller/Api/Auth.pm | 2 +- lib/Pear/LocalLoop/Controller/Api/Register.pm | 20 +- lib/Pear/LocalLoop/Controller/Api/User.pm | 2 +- lib/Pear/LocalLoop/Controller/Register.pm | 3 +- .../LocalLoop/Schema/Result/Administrator.pm | 78 +----- lib/Pear/LocalLoop/Schema/Result/Customer.pm | 2 +- .../LocalLoop/Schema/Result/Organisation.pm | 20 +- .../Schema/Result/PendingOrganisation.pm | 27 +- .../Schema/Result/PendingTransaction.pm | 2 +- .../LocalLoop/Schema/Result/SessionToken.pm | 2 +- .../LocalLoop/Schema/Result/Transaction.pm | 2 +- lib/Pear/LocalLoop/Schema/Result/User.pm | 232 ++++-------------- t/admin/login.t | 6 +- 15 files changed, 74 insertions(+), 380 deletions(-) diff --git a/cpanfile b/cpanfile index f84010f..8444dd6 100644 --- a/cpanfile +++ b/cpanfile @@ -10,6 +10,7 @@ requires 'Time::Fake'; requires 'Scalar::Util'; requires 'DBIx::Class'; requires 'DBIx::Class::PassphraseColumn'; +requires 'DBIx::Class::TimeStamp'; requires 'DBIx::Class::Schema::Loader'; requires 'SQL::Translator'; requires 'DateTime'; diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index faea104..966df36 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -42,7 +42,7 @@ sub startup { my $user = $c->schema->resultset('User')->find({email => $email}); if ( defined $user ) { if ( $user->check_password( $password ) ) { - return $user->userid; + return $user->id; } } return undef; @@ -150,64 +150,19 @@ sub startup { $self->res->headers->header('Access-Control-Allow-Origin' => '*') if $self->app->mode eq 'development'; }); - $self->helper(get_active_user_id => sub { - my $self = shift; - - my $token = $self->get_session_token(); - if (! defined $token){ - return undef; - } - - my @out = $self->db->selectrow_array("SELECT UserIdAssignedTo_FK FROM SessionTokens WHERE SessionTokenName = ?",undef,($token)); - if (! @out){ - return undef; - } - else{ - return $out[0]; - } - }); - - $self->helper(get_session_token => sub { - my $self = shift; - - #See if logged in. - my $sessionToken = undef; - - my $json = $self->req->json; - if (defined $json) { - $sessionToken = $json->{$self->app->config->{sessionTokenJsonName}}; - } - - if ( ! defined $sessionToken || $sessionToken eq "" ) { - $sessionToken = $self->session->{$self->app->config->{sessionTokenJsonName}}; - } - - if (defined $sessionToken && $sessionToken eq "" ) { - $sessionToken = undef; - } - - return $sessionToken; - }); - - #This assumes the user has no current session on that device. $self->helper(generate_session => sub { - my ($self, $userId) = @_; + my ($self, $user) = @_; - my $sessionToken = $self->generate_session_token(); + my $sessionToken = Data::UUID->new->create_str(); my $insertStatement = $self->db->prepare('INSERT INTO SessionTokens (SessionTokenName, UserIdAssignedTo_FK, ExpireDateTime) VALUES (?, ?, ?)'); - my $rowsAdded = $insertStatement->execute($sessionToken, $userId, DateTime->now()->add( years => 1 )); + my $rowsAdded = $insertStatement->execute($sessionToken, $user, DateTime->now()->add( years => 1 )); return $sessionToken; }); - $self->helper(generate_session_token => sub { - my $self = shift; - return Data::UUID->new->create_str(); - }); - - $self->helper(get_age_foreign_key => sub { + $self->helper(get_age_foreign_key => sub { my ( $c, $age_string ) = @_; my $age_range = $c->schema->resultset('AgeRange')->find({ agerangestring => $age_string }); return defined $age_range ? $age_range->agerangeid : undef; diff --git a/lib/Pear/LocalLoop/Controller/Api/Auth.pm b/lib/Pear/LocalLoop/Controller/Api/Auth.pm index 43ecfae..f628087 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Auth.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Auth.pm @@ -78,7 +78,7 @@ sub post_login { if ( defined $user_result ) { if ( $user_result->check_password($password) ) { - my $session_key = $c->generate_session( $user_result->userid ); + my $session_key = $c->generate_session( $user_result->id ); return $c->render( json => { success => Mojo::JSON->true, diff --git a/lib/Pear/LocalLoop/Controller/Api/Register.pm b/lib/Pear/LocalLoop/Controller/Api/Register.pm index 0708623..9f38b3a 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Register.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Register.pm @@ -113,13 +113,12 @@ sub post_register{ })->update({ used => 1 }); $c->schema->resultset('User')->create({ customer => { - username => $validation->param('username'), + username => $validation->param('username'), agerange_fk => $ageForeignKey, - postcode => $validation->param('postcode'), + postcode => $validation->param('postcode'), }, - email => $validation->param('email'), - hashedpassword => $validation->param('password'), - joindate => DateTime->now, + email => $validation->param('email'), + password => $validation->param('password'), }); }); @@ -134,14 +133,13 @@ sub post_register{ })->update({ used => 1 }); $c->schema->resultset('User')->create({ organisation => { - name => $validation->param('username'), + name => $validation->param('username'), street_name => $validation->param('street_name'), - town => $validation->param('town'), - postcode => $validation->param('postcode'), + town => $validation->param('town'), + postcode => $validation->param('postcode'), }, - email => $validation->param('email'), - hashedpassword => $validation->param('password'), - joindate => DateTime->now, + email => $validation->param('email'), + password => $validation->param('password'), }); }); } diff --git a/lib/Pear/LocalLoop/Controller/Api/User.pm b/lib/Pear/LocalLoop/Controller/Api/User.pm index 3f7b673..757ff2a 100644 --- a/lib/Pear/LocalLoop/Controller/Api/User.pm +++ b/lib/Pear/LocalLoop/Controller/Api/User.pm @@ -9,7 +9,7 @@ use TryCatch; sub post_user_history { my $self = shift; - my $userId = $self->get_active_user_id(); + my $userId = $self->stash->{api_user}->id; my $json = $self->req->json; if ( ! defined $json ) { $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); diff --git a/lib/Pear/LocalLoop/Controller/Register.pm b/lib/Pear/LocalLoop/Controller/Register.pm index 0a34896..0c90f54 100644 --- a/lib/Pear/LocalLoop/Controller/Register.pm +++ b/lib/Pear/LocalLoop/Controller/Register.pm @@ -47,8 +47,7 @@ sub register { } else { my $new_user = $c->schema->resultset('User')->find_or_new({ email => $validation->param('email'), - hashedpassword => $validation->param('password'), - joindate => DateTime->now(), + password => $validation->param('password'), customer => { username => $validation->param('name'), postcode => $validation->param('postcode'), diff --git a/lib/Pear/LocalLoop/Schema/Result/Administrator.pm b/lib/Pear/LocalLoop/Schema/Result/Administrator.pm index fcc190c..5797fc0 100644 --- a/lib/Pear/LocalLoop/Schema/Result/Administrator.pm +++ b/lib/Pear/LocalLoop/Schema/Result/Administrator.pm @@ -1,92 +1,28 @@ -use utf8; package Pear::LocalLoop::Schema::Result::Administrator; -# Created by DBIx::Class::Schema::Loader -# DO NOT MODIFY THE FIRST PART OF THIS FILE - -=head1 NAME - -Pear::LocalLoop::Schema::Result::Administrator - -=cut - use strict; use warnings; use base 'DBIx::Class::Core'; -=head1 COMPONENTS LOADED - -=over 4 - -=item * L - -=back - -=cut - -__PACKAGE__->load_components("InflateColumn::DateTime"); - -=head1 TABLE: C - -=cut - __PACKAGE__->table("Administrators"); -=head1 ACCESSORS - -=head2 userid - - data_type: 'integer' - is_auto_increment: 1 - is_foreign_key: 1 - is_nullable: 0 - -=cut - __PACKAGE__->add_columns( - "userid", + "user_id", { - data_type => "integer", - is_auto_increment => 1, - is_foreign_key => 1, - is_nullable => 0, + data_type => "integer", + is_foreign_key => 1, + is_nullable => 0, }, ); -=head1 PRIMARY KEY - -=over 4 - -=item * L - -=back - -=cut - -__PACKAGE__->set_primary_key("userid"); - -=head1 RELATIONS - -=head2 userid - -Type: belongs_to - -Related object: L - -=cut +__PACKAGE__->set_primary_key("user_id"); __PACKAGE__->belongs_to( - "userid", + "user", "Pear::LocalLoop::Schema::Result::User", - { userid => "userid" }, + { id => "user_id" }, { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, ); - -# Created by DBIx::Class::Schema::Loader v0.07046 @ 2017-02-24 17:32:21 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:YLzlp1ru+1id/O4bTJGqbw - - -# You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/lib/Pear/LocalLoop/Schema/Result/Customer.pm b/lib/Pear/LocalLoop/Schema/Result/Customer.pm index c59f178..466a029 100644 --- a/lib/Pear/LocalLoop/Schema/Result/Customer.pm +++ b/lib/Pear/LocalLoop/Schema/Result/Customer.pm @@ -124,7 +124,7 @@ Related object: L __PACKAGE__->might_have( "user", "Pear::LocalLoop::Schema::Result::User", - { "foreign.customerid_fk" => "self.customerid" }, + { "foreign.customer_id" => "self.customerid" }, { cascade_copy => 0, cascade_delete => 0 }, ); diff --git a/lib/Pear/LocalLoop/Schema/Result/Organisation.pm b/lib/Pear/LocalLoop/Schema/Result/Organisation.pm index 75efc69..d4a1b2d 100644 --- a/lib/Pear/LocalLoop/Schema/Result/Organisation.pm +++ b/lib/Pear/LocalLoop/Schema/Result/Organisation.pm @@ -38,16 +38,6 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key('id'); -=head1 RELATIONS - -=head2 transactions - -Type: has_many - -Related object: L - -=cut - __PACKAGE__->has_many( "transactions", "Pear::LocalLoop::Schema::Result::Transaction", @@ -55,18 +45,10 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 0 }, ); -=head2 user - -Type: might_have - -Related object: L - -=cut - __PACKAGE__->might_have( "user", "Pear::LocalLoop::Schema::Result::User", - { "foreign.organisationalid_fk" => 'self.id' }, + { "foreign.organisation_id" => 'self.id' }, { cascade_copy => 0, cascade_delete => 0 }, ); diff --git a/lib/Pear/LocalLoop/Schema/Result/PendingOrganisation.pm b/lib/Pear/LocalLoop/Schema/Result/PendingOrganisation.pm index 0ae5a63..fd6ca5b 100644 --- a/lib/Pear/LocalLoop/Schema/Result/PendingOrganisation.pm +++ b/lib/Pear/LocalLoop/Schema/Result/PendingOrganisation.pm @@ -1,4 +1,3 @@ -use utf8; package Pear::LocalLoop::Schema::Result::PendingOrganisation; use strict; @@ -48,16 +47,6 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key('id'); -=head1 RELATIONS - -=head2 pending_transactions - -Type: has_many - -Related object: L - -=cut - __PACKAGE__->has_many( "pending_transactions", "Pear::LocalLoop::Schema::Result::PendingTransaction", @@ -67,25 +56,11 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 1 }, ); -=head2 usersubmitted_fk - -Type: belongs_to - -Related object: L - -=cut - __PACKAGE__->belongs_to( "submitted_by", "Pear::LocalLoop::Schema::Result::User", - { userid => "submitted_by_id" }, + { id => "submitted_by_id" }, { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, ); - -# Created by DBIx::Class::Schema::Loader v0.07046 @ 2017-02-24 17:32:21 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ekEOt+ESCwQxrqqlMurehA - - -# You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/lib/Pear/LocalLoop/Schema/Result/PendingTransaction.pm b/lib/Pear/LocalLoop/Schema/Result/PendingTransaction.pm index 460a5fc..9854929 100644 --- a/lib/Pear/LocalLoop/Schema/Result/PendingTransaction.pm +++ b/lib/Pear/LocalLoop/Schema/Result/PendingTransaction.pm @@ -37,7 +37,7 @@ __PACKAGE__->set_primary_key("pendingtransactionid"); __PACKAGE__->belongs_to( "buyeruserid_fk", "Pear::LocalLoop::Schema::Result::User", - { userid => "buyeruserid_fk" }, + { id => "buyeruserid_fk" }, { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, ); diff --git a/lib/Pear/LocalLoop/Schema/Result/SessionToken.pm b/lib/Pear/LocalLoop/Schema/Result/SessionToken.pm index fddf569..d30aca5 100644 --- a/lib/Pear/LocalLoop/Schema/Result/SessionToken.pm +++ b/lib/Pear/LocalLoop/Schema/Result/SessionToken.pm @@ -109,7 +109,7 @@ Related object: L __PACKAGE__->belongs_to( "user", "Pear::LocalLoop::Schema::Result::User", - { userid => "useridassignedto_fk" }, + { id => "useridassignedto_fk" }, { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, ); diff --git a/lib/Pear/LocalLoop/Schema/Result/Transaction.pm b/lib/Pear/LocalLoop/Schema/Result/Transaction.pm index 5c3ea53..7381bef 100644 --- a/lib/Pear/LocalLoop/Schema/Result/Transaction.pm +++ b/lib/Pear/LocalLoop/Schema/Result/Transaction.pm @@ -116,7 +116,7 @@ Related object: L __PACKAGE__->belongs_to( "buyeruserid_fk", "Pear::LocalLoop::Schema::Result::User", - { userid => "buyeruserid_fk" }, + { id => "buyeruserid_fk" }, { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, ); diff --git a/lib/Pear/LocalLoop/Schema/Result/User.pm b/lib/Pear/LocalLoop/Schema/Result/User.pm index cf3883a..72ec624 100644 --- a/lib/Pear/LocalLoop/Schema/Result/User.pm +++ b/lib/Pear/LocalLoop/Schema/Result/User.pm @@ -1,88 +1,44 @@ use utf8; package Pear::LocalLoop::Schema::Result::User; -# Created by DBIx::Class::Schema::Loader -# DO NOT MODIFY THE FIRST PART OF THIS FILE - -=head1 NAME - -Pear::LocalLoop::Schema::Result::User - -=cut - use strict; use warnings; use base 'DBIx::Class::Core'; -=head1 COMPONENTS LOADED +__PACKAGE__->load_components( qw/ + InflateColumn::DateTime + PassphraseColumn + TimeStamp +/); -=over 4 - -=item * L - -=back - -=cut - -__PACKAGE__->load_components("InflateColumn::DateTime", "PassphraseColumn"); - -=head1 TABLE: C - -=cut - -__PACKAGE__->table("Users"); - -=head1 ACCESSORS - -=head2 userid - - data_type: 'integer' - is_auto_increment: 1 - is_nullable: 0 - -=head2 customerid_fk - - data_type: 'integer' - is_foreign_key: 1 - is_nullable: 1 - -=head2 organisationalid_fk - - data_type: 'integer' - is_foreign_key: 1 - is_nullable: 1 - -=head2 email - - data_type: 'text' - is_nullable: 0 - -=head2 joindate - - data_type: 'integer' - is_nullable: 0 - -=head2 hashedpassword - - data_type: 'text' - is_nullable: 0 - -=cut +__PACKAGE__->table("users"); __PACKAGE__->add_columns( - "userid", - { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, - "customerid_fk", - { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, - "organisationalid_fk", - { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, - "email", - { data_type => "text", is_nullable => 0 }, - "joindate", - { data_type => "datetime", is_nullable => 0 }, - "hashedpassword", - { + "id" => { + data_type => "integer", + is_auto_increment => 1, + is_nullable => 0, + }, + "customer_id" => { + data_type => "integer", + is_foreign_key => 1, + is_nullable => 1, + }, + "organisation_id" => { + data_type => "integer", + is_foreign_key => 1, + is_nullable => 1, + }, + "email" => { + data_type => "text", + is_nullable => 0, + }, + "join_date" => { + data_type => "datetime", + set_on_create => 1, + }, + "password" => { data_type => "varchar", is_nullable => 0, size => 100, @@ -96,85 +52,25 @@ __PACKAGE__->add_columns( }, ); -=head1 PRIMARY KEY +__PACKAGE__->set_primary_key("id"); -=over 4 +__PACKAGE__->add_unique_constraint(["customer_id"]); -=item * L +__PACKAGE__->add_unique_constraint(["email"]); -=back - -=cut - -__PACKAGE__->set_primary_key("userid"); - -=head1 UNIQUE CONSTRAINTS - -=head2 C - -=over 4 - -=item * L - -=back - -=cut - -__PACKAGE__->add_unique_constraint("customerid_fk_unique", ["customerid_fk"]); - -=head2 C - -=over 4 - -=item * L - -=back - -=cut - -__PACKAGE__->add_unique_constraint("email_unique", ["email"]); - -=head2 C - -=over 4 - -=item * L - -=back - -=cut - -__PACKAGE__->add_unique_constraint("organisationalid_fk_unique", ["organisationalid_fk"]); - -=head1 RELATIONS - -=head2 administrator - -Type: might_have - -Related object: L - -=cut +__PACKAGE__->add_unique_constraint(["organisation_id"]); __PACKAGE__->might_have( "administrator", "Pear::LocalLoop::Schema::Result::Administrator", - { "foreign.userid" => "self.userid" }, + { "foreign.user_id" => "self.id" }, { cascade_copy => 0, cascade_delete => 0 }, ); -=head2 customerid_fk - -Type: belongs_to - -Related object: L - -=cut - __PACKAGE__->belongs_to( "customer", "Pear::LocalLoop::Schema::Result::Customer", - { customerid => "customerid_fk" }, + { customerid => "customer_id" }, { is_deferrable => 0, join_type => "LEFT", @@ -183,18 +79,10 @@ __PACKAGE__->belongs_to( }, ); -=head2 organisationalid_fk - -Type: belongs_to - -Related object: L - -=cut - __PACKAGE__->belongs_to( "organisation", "Pear::LocalLoop::Schema::Result::Organisation", - { id => "organisationalid_fk" }, + { id => "organisation_id" }, { is_deferrable => 0, join_type => "LEFT", @@ -203,70 +91,32 @@ __PACKAGE__->belongs_to( }, ); -=head2 pending_organisations - -Type: has_many - -Related object: L - -=cut - __PACKAGE__->has_many( "pending_organisations", "Pear::LocalLoop::Schema::Result::PendingOrganisation", - { "foreign.submitted_by_id" => "self.userid" }, + { "foreign.submitted_by_id" => "self.id" }, { cascade_copy => 0, cascade_delete => 0 }, ); -=head2 pending_transactions - -Type: has_many - -Related object: L - -=cut - __PACKAGE__->has_many( "pending_transactions", "Pear::LocalLoop::Schema::Result::PendingTransaction", - { "foreign.buyeruserid_fk" => "self.userid" }, + { "foreign.buyeruserid_fk" => "self.id" }, { cascade_copy => 0, cascade_delete => 0 }, ); -=head2 session_tokens - -Type: has_many - -Related object: L - -=cut - __PACKAGE__->has_many( "session_tokens", "Pear::LocalLoop::Schema::Result::SessionToken", - { "foreign.useridassignedto_fk" => "self.userid" }, + { "foreign.useridassignedto_fk" => "self.id" }, { cascade_copy => 0, cascade_delete => 0 }, ); -=head2 transactions - -Type: has_many - -Related object: L - -=cut - __PACKAGE__->has_many( "transactions", "Pear::LocalLoop::Schema::Result::Transaction", - { "foreign.buyeruserid_fk" => "self.userid" }, + { "foreign.buyeruserid_fk" => "self.id" }, { cascade_copy => 0, cascade_delete => 0 }, ); - -# Created by DBIx::Class::Schema::Loader v0.07046 @ 2017-02-24 17:32:21 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:qjAgtJR1zaUr00HsiR1aPw - - -# You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/t/admin/login.t b/t/admin/login.t index 6110771..1f0eeff 100644 --- a/t/admin/login.t +++ b/t/admin/login.t @@ -27,15 +27,13 @@ $schema->deploy; $schema->resultset('User')->create({ email => 'admin@example.com', - hashedpassword => 'abc123', + password => 'abc123', administrator => {}, - joindate => DateTime->now, }); $schema->resultset('User')->create({ email => 'user@example.com', - hashedpassword => 'abc123', - joindate => DateTime->now, + password => 'abc123', }); is $schema->resultset('User')->count, 2, 'Users Created';