refactor user table and setup timestamp auto setting

This commit is contained in:
Tom Bloor 2017-04-21 20:52:14 +01:00
parent 6bfd516d76
commit d0cbb215d8
15 changed files with 74 additions and 380 deletions

View file

@ -10,6 +10,7 @@ requires 'Time::Fake';
requires 'Scalar::Util'; requires 'Scalar::Util';
requires 'DBIx::Class'; requires 'DBIx::Class';
requires 'DBIx::Class::PassphraseColumn'; requires 'DBIx::Class::PassphraseColumn';
requires 'DBIx::Class::TimeStamp';
requires 'DBIx::Class::Schema::Loader'; requires 'DBIx::Class::Schema::Loader';
requires 'SQL::Translator'; requires 'SQL::Translator';
requires 'DateTime'; requires 'DateTime';

View file

@ -42,7 +42,7 @@ sub startup {
my $user = $c->schema->resultset('User')->find({email => $email}); my $user = $c->schema->resultset('User')->find({email => $email});
if ( defined $user ) { if ( defined $user ) {
if ( $user->check_password( $password ) ) { if ( $user->check_password( $password ) ) {
return $user->userid; return $user->id;
} }
} }
return undef; return undef;
@ -150,63 +150,18 @@ sub startup {
$self->res->headers->header('Access-Control-Allow-Origin' => '*') if $self->app->mode eq 'development'; $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. #This assumes the user has no current session on that device.
$self->helper(generate_session => sub { $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 $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; 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 ( $c, $age_string ) = @_;
my $age_range = $c->schema->resultset('AgeRange')->find({ agerangestring => $age_string }); my $age_range = $c->schema->resultset('AgeRange')->find({ agerangestring => $age_string });

View file

@ -78,7 +78,7 @@ sub post_login {
if ( defined $user_result ) { if ( defined $user_result ) {
if ( $user_result->check_password($password) ) { 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 => { return $c->render( json => {
success => Mojo::JSON->true, success => Mojo::JSON->true,

View file

@ -118,8 +118,7 @@ sub post_register{
postcode => $validation->param('postcode'), postcode => $validation->param('postcode'),
}, },
email => $validation->param('email'), email => $validation->param('email'),
hashedpassword => $validation->param('password'), password => $validation->param('password'),
joindate => DateTime->now,
}); });
}); });
@ -140,8 +139,7 @@ sub post_register{
postcode => $validation->param('postcode'), postcode => $validation->param('postcode'),
}, },
email => $validation->param('email'), email => $validation->param('email'),
hashedpassword => $validation->param('password'), password => $validation->param('password'),
joindate => DateTime->now,
}); });
}); });
} }

View file

@ -9,7 +9,7 @@ use TryCatch;
sub post_user_history { sub post_user_history {
my $self = shift; my $self = shift;
my $userId = $self->get_active_user_id(); my $userId = $self->stash->{api_user}->id;
my $json = $self->req->json; my $json = $self->req->json;
if ( ! defined $json ) { if ( ! defined $json ) {
$self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__);

View file

@ -47,8 +47,7 @@ sub register {
} else { } else {
my $new_user = $c->schema->resultset('User')->find_or_new({ my $new_user = $c->schema->resultset('User')->find_or_new({
email => $validation->param('email'), email => $validation->param('email'),
hashedpassword => $validation->param('password'), password => $validation->param('password'),
joindate => DateTime->now(),
customer => { customer => {
username => $validation->param('name'), username => $validation->param('name'),
postcode => $validation->param('postcode'), postcode => $validation->param('postcode'),

View file

@ -1,92 +1,28 @@
use utf8;
package Pear::LocalLoop::Schema::Result::Administrator; 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 strict;
use warnings; use warnings;
use base 'DBIx::Class::Core'; use base 'DBIx::Class::Core';
=head1 COMPONENTS LOADED
=over 4
=item * L<DBIx::Class::InflateColumn::DateTime>
=back
=cut
__PACKAGE__->load_components("InflateColumn::DateTime");
=head1 TABLE: C<Administrators>
=cut
__PACKAGE__->table("Administrators"); __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( __PACKAGE__->add_columns(
"userid", "user_id",
{ {
data_type => "integer", data_type => "integer",
is_auto_increment => 1,
is_foreign_key => 1, is_foreign_key => 1,
is_nullable => 0, is_nullable => 0,
}, },
); );
=head1 PRIMARY KEY __PACKAGE__->set_primary_key("user_id");
=over 4
=item * L</userid>
=back
=cut
__PACKAGE__->set_primary_key("userid");
=head1 RELATIONS
=head2 userid
Type: belongs_to
Related object: L<Pear::LocalLoop::Schema::Result::User>
=cut
__PACKAGE__->belongs_to( __PACKAGE__->belongs_to(
"userid", "user",
"Pear::LocalLoop::Schema::Result::User", "Pear::LocalLoop::Schema::Result::User",
{ userid => "userid" }, { id => "user_id" },
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, { 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; 1;

View file

@ -124,7 +124,7 @@ Related object: L<Pear::LocalLoop::Schema::Result::User>
__PACKAGE__->might_have( __PACKAGE__->might_have(
"user", "user",
"Pear::LocalLoop::Schema::Result::User", "Pear::LocalLoop::Schema::Result::User",
{ "foreign.customerid_fk" => "self.customerid" }, { "foreign.customer_id" => "self.customerid" },
{ cascade_copy => 0, cascade_delete => 0 }, { cascade_copy => 0, cascade_delete => 0 },
); );

View file

@ -38,16 +38,6 @@ __PACKAGE__->add_columns(
__PACKAGE__->set_primary_key('id'); __PACKAGE__->set_primary_key('id');
=head1 RELATIONS
=head2 transactions
Type: has_many
Related object: L<Pear::LocalLoop::Schema::Result::Transaction>
=cut
__PACKAGE__->has_many( __PACKAGE__->has_many(
"transactions", "transactions",
"Pear::LocalLoop::Schema::Result::Transaction", "Pear::LocalLoop::Schema::Result::Transaction",
@ -55,18 +45,10 @@ __PACKAGE__->has_many(
{ cascade_copy => 0, cascade_delete => 0 }, { cascade_copy => 0, cascade_delete => 0 },
); );
=head2 user
Type: might_have
Related object: L<Pear::LocalLoop::Schema::Result::User>
=cut
__PACKAGE__->might_have( __PACKAGE__->might_have(
"user", "user",
"Pear::LocalLoop::Schema::Result::User", "Pear::LocalLoop::Schema::Result::User",
{ "foreign.organisationalid_fk" => 'self.id' }, { "foreign.organisation_id" => 'self.id' },
{ cascade_copy => 0, cascade_delete => 0 }, { cascade_copy => 0, cascade_delete => 0 },
); );

View file

@ -1,4 +1,3 @@
use utf8;
package Pear::LocalLoop::Schema::Result::PendingOrganisation; package Pear::LocalLoop::Schema::Result::PendingOrganisation;
use strict; use strict;
@ -48,16 +47,6 @@ __PACKAGE__->add_columns(
__PACKAGE__->set_primary_key('id'); __PACKAGE__->set_primary_key('id');
=head1 RELATIONS
=head2 pending_transactions
Type: has_many
Related object: L<Pear::LocalLoop::Schema::Result::PendingTransaction>
=cut
__PACKAGE__->has_many( __PACKAGE__->has_many(
"pending_transactions", "pending_transactions",
"Pear::LocalLoop::Schema::Result::PendingTransaction", "Pear::LocalLoop::Schema::Result::PendingTransaction",
@ -67,25 +56,11 @@ __PACKAGE__->has_many(
{ cascade_copy => 0, cascade_delete => 1 }, { cascade_copy => 0, cascade_delete => 1 },
); );
=head2 usersubmitted_fk
Type: belongs_to
Related object: L<Pear::LocalLoop::Schema::Result::User>
=cut
__PACKAGE__->belongs_to( __PACKAGE__->belongs_to(
"submitted_by", "submitted_by",
"Pear::LocalLoop::Schema::Result::User", "Pear::LocalLoop::Schema::Result::User",
{ userid => "submitted_by_id" }, { id => "submitted_by_id" },
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, { 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; 1;

View file

@ -37,7 +37,7 @@ __PACKAGE__->set_primary_key("pendingtransactionid");
__PACKAGE__->belongs_to( __PACKAGE__->belongs_to(
"buyeruserid_fk", "buyeruserid_fk",
"Pear::LocalLoop::Schema::Result::User", "Pear::LocalLoop::Schema::Result::User",
{ userid => "buyeruserid_fk" }, { id => "buyeruserid_fk" },
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
); );

View file

@ -109,7 +109,7 @@ Related object: L<Pear::LocalLoop::Schema::Result::User>
__PACKAGE__->belongs_to( __PACKAGE__->belongs_to(
"user", "user",
"Pear::LocalLoop::Schema::Result::User", "Pear::LocalLoop::Schema::Result::User",
{ userid => "useridassignedto_fk" }, { id => "useridassignedto_fk" },
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
); );

View file

@ -116,7 +116,7 @@ Related object: L<Pear::LocalLoop::Schema::Result::User>
__PACKAGE__->belongs_to( __PACKAGE__->belongs_to(
"buyeruserid_fk", "buyeruserid_fk",
"Pear::LocalLoop::Schema::Result::User", "Pear::LocalLoop::Schema::Result::User",
{ userid => "buyeruserid_fk" }, { id => "buyeruserid_fk" },
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" }, { is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
); );

View file

@ -1,88 +1,44 @@
use utf8; use utf8;
package Pear::LocalLoop::Schema::Result::User; 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 strict;
use warnings; use warnings;
use base 'DBIx::Class::Core'; use base 'DBIx::Class::Core';
=head1 COMPONENTS LOADED __PACKAGE__->load_components( qw/
InflateColumn::DateTime
PassphraseColumn
TimeStamp
/);
=over 4 __PACKAGE__->table("users");
=item * L<DBIx::Class::InflateColumn::DateTime>
=back
=cut
__PACKAGE__->load_components("InflateColumn::DateTime", "PassphraseColumn");
=head1 TABLE: C<Users>
=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__->add_columns( __PACKAGE__->add_columns(
"userid", "id" => {
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, data_type => "integer",
"customerid_fk", is_auto_increment => 1,
{ data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, is_nullable => 0,
"organisationalid_fk", },
{ data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, "customer_id" => {
"email", data_type => "integer",
{ data_type => "text", is_nullable => 0 }, is_foreign_key => 1,
"joindate", is_nullable => 1,
{ data_type => "datetime", is_nullable => 0 }, },
"hashedpassword", "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", data_type => "varchar",
is_nullable => 0, is_nullable => 0,
size => 100, 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</userid> __PACKAGE__->add_unique_constraint(["email"]);
=back __PACKAGE__->add_unique_constraint(["organisation_id"]);
=cut
__PACKAGE__->set_primary_key("userid");
=head1 UNIQUE CONSTRAINTS
=head2 C<customerid_fk_unique>
=over 4
=item * L</customerid_fk>
=back
=cut
__PACKAGE__->add_unique_constraint("customerid_fk_unique", ["customerid_fk"]);
=head2 C<email_unique>
=over 4
=item * L</email>
=back
=cut
__PACKAGE__->add_unique_constraint("email_unique", ["email"]);
=head2 C<organisationalid_fk_unique>
=over 4
=item * L</organisationalid_fk>
=back
=cut
__PACKAGE__->add_unique_constraint("organisationalid_fk_unique", ["organisationalid_fk"]);
=head1 RELATIONS
=head2 administrator
Type: might_have
Related object: L<Pear::LocalLoop::Schema::Result::Administrator>
=cut
__PACKAGE__->might_have( __PACKAGE__->might_have(
"administrator", "administrator",
"Pear::LocalLoop::Schema::Result::Administrator", "Pear::LocalLoop::Schema::Result::Administrator",
{ "foreign.userid" => "self.userid" }, { "foreign.user_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 }, { cascade_copy => 0, cascade_delete => 0 },
); );
=head2 customerid_fk
Type: belongs_to
Related object: L<Pear::LocalLoop::Schema::Result::Customer>
=cut
__PACKAGE__->belongs_to( __PACKAGE__->belongs_to(
"customer", "customer",
"Pear::LocalLoop::Schema::Result::Customer", "Pear::LocalLoop::Schema::Result::Customer",
{ customerid => "customerid_fk" }, { customerid => "customer_id" },
{ {
is_deferrable => 0, is_deferrable => 0,
join_type => "LEFT", join_type => "LEFT",
@ -183,18 +79,10 @@ __PACKAGE__->belongs_to(
}, },
); );
=head2 organisationalid_fk
Type: belongs_to
Related object: L<Pear::LocalLoop::Schema::Result::Organisation>
=cut
__PACKAGE__->belongs_to( __PACKAGE__->belongs_to(
"organisation", "organisation",
"Pear::LocalLoop::Schema::Result::Organisation", "Pear::LocalLoop::Schema::Result::Organisation",
{ id => "organisationalid_fk" }, { id => "organisation_id" },
{ {
is_deferrable => 0, is_deferrable => 0,
join_type => "LEFT", join_type => "LEFT",
@ -203,70 +91,32 @@ __PACKAGE__->belongs_to(
}, },
); );
=head2 pending_organisations
Type: has_many
Related object: L<Pear::LocalLoop::Schema::Result::PendingOrganisation>
=cut
__PACKAGE__->has_many( __PACKAGE__->has_many(
"pending_organisations", "pending_organisations",
"Pear::LocalLoop::Schema::Result::PendingOrganisation", "Pear::LocalLoop::Schema::Result::PendingOrganisation",
{ "foreign.submitted_by_id" => "self.userid" }, { "foreign.submitted_by_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 }, { cascade_copy => 0, cascade_delete => 0 },
); );
=head2 pending_transactions
Type: has_many
Related object: L<Pear::LocalLoop::Schema::Result::PendingTransaction>
=cut
__PACKAGE__->has_many( __PACKAGE__->has_many(
"pending_transactions", "pending_transactions",
"Pear::LocalLoop::Schema::Result::PendingTransaction", "Pear::LocalLoop::Schema::Result::PendingTransaction",
{ "foreign.buyeruserid_fk" => "self.userid" }, { "foreign.buyeruserid_fk" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 }, { cascade_copy => 0, cascade_delete => 0 },
); );
=head2 session_tokens
Type: has_many
Related object: L<Pear::LocalLoop::Schema::Result::SessionToken>
=cut
__PACKAGE__->has_many( __PACKAGE__->has_many(
"session_tokens", "session_tokens",
"Pear::LocalLoop::Schema::Result::SessionToken", "Pear::LocalLoop::Schema::Result::SessionToken",
{ "foreign.useridassignedto_fk" => "self.userid" }, { "foreign.useridassignedto_fk" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 }, { cascade_copy => 0, cascade_delete => 0 },
); );
=head2 transactions
Type: has_many
Related object: L<Pear::LocalLoop::Schema::Result::Transaction>
=cut
__PACKAGE__->has_many( __PACKAGE__->has_many(
"transactions", "transactions",
"Pear::LocalLoop::Schema::Result::Transaction", "Pear::LocalLoop::Schema::Result::Transaction",
{ "foreign.buyeruserid_fk" => "self.userid" }, { "foreign.buyeruserid_fk" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 }, { 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; 1;

View file

@ -27,15 +27,13 @@ $schema->deploy;
$schema->resultset('User')->create({ $schema->resultset('User')->create({
email => 'admin@example.com', email => 'admin@example.com',
hashedpassword => 'abc123', password => 'abc123',
administrator => {}, administrator => {},
joindate => DateTime->now,
}); });
$schema->resultset('User')->create({ $schema->resultset('User')->create({
email => 'user@example.com', email => 'user@example.com',
hashedpassword => 'abc123', password => 'abc123',
joindate => DateTime->now,
}); });
is $schema->resultset('User')->count, 2, 'Users Created'; is $schema->resultset('User')->count, 2, 'Users Created';