aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/DB
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet/DB')
-rw-r--r--perllib/FixMyStreet/DB/Result/User.pm33
-rw-r--r--perllib/FixMyStreet/DB/ResultSet/User.pm45
2 files changed, 75 insertions, 3 deletions
diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm
index 1ba2f094a..099ec2349 100644
--- a/perllib/FixMyStreet/DB/Result/User.pm
+++ b/perllib/FixMyStreet/DB/Result/User.pm
@@ -23,12 +23,39 @@ __PACKAGE__->add_columns(
{ data_type => "text", is_nullable => 1 },
"password",
{ data_type => "text", is_nullable => 1 },
+ "is_confirmed",
+ { data_type => "boolean", default_value => \"false", is_nullable => 0 },
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->add_unique_constraint( "users_email_key", ["email"] );
-# Created by DBIx::Class::Schema::Loader v0.07009 @ 2011-03-02 12:20:05
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:M8SDJhpzhBZJmhar+MGQhQ
+# Created by DBIx::Class::Schema::Loader v0.07009 @ 2011-03-03 10:05:03
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:4dpBN1I88nB1BYtHT/AfKA
+
+=head2 create_confirm_token
+
+ $token = $user->create_confirm_token();
+
+Create a token that can be emailed to the user. When it is returned it can be
+used to confirm that the email address works.
+
+See also the ::ResultSet::User method 'confirm_user_from_token'.
+
+=cut
+
+sub create_confirm_token {
+ my $self = shift;
+
+ my $token_rs = $self->result_source->schema->resultset('Token');
+
+ my $token_obj = $token_rs->create(
+ {
+ scope => 'user_confirm', #
+ data => { email => $self->email }
+ }
+ );
+
+ return $token_obj->token;
+}
-# You can replace this text with custom code or comments, and it will be preserved on regeneration
1;
diff --git a/perllib/FixMyStreet/DB/ResultSet/User.pm b/perllib/FixMyStreet/DB/ResultSet/User.pm
new file mode 100644
index 000000000..a84286a78
--- /dev/null
+++ b/perllib/FixMyStreet/DB/ResultSet/User.pm
@@ -0,0 +1,45 @@
+package FixMyStreet::DB::ResultSet::User;
+use base 'DBIx::Class::ResultSet';
+
+use strict;
+use warnings;
+
+=head2 confirm_user_from_token
+
+ $user = $rs->confirm_user_from_token( $token );
+
+Given a token retrieve it from the database, find the user it relates to and
+confirm them. Return the user an the end. If anything goes wrong return undef.
+
+Delete the token afterwards.
+
+See also the ::Result::User method 'create_confirm_token'
+
+=cut
+
+sub confirm_user_from_token {
+ my $self = shift;
+ my $token_string = shift || return;
+
+ # retrieve the token or return
+ my $token_rs = $self->result_source->schema->resultset('Token');
+ my $token_obj =
+ $token_rs->find( { scope => 'user_confirm', token => $token_string, } )
+ || return;
+
+ # find the user related to the token
+ my $user = $self->find( { email => $token_obj->data->{email} } );
+
+ # If we found a user confirm them and delete the token - in transaction
+ $self->result_source->schema->txn_do(
+ sub {
+ $user->update( { is_confirmed => 1 } ) if $user;
+ $token_obj->delete;
+ }
+ );
+
+ # return the user (possibly undef if none found)
+ return $user;
+}
+
+1;