aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/DB/Result/User.pm
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet/DB/Result/User.pm')
-rw-r--r--perllib/FixMyStreet/DB/Result/User.pm59
1 files changed, 48 insertions, 11 deletions
diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm
index 19adf5d49..296cf997d 100644
--- a/perllib/FixMyStreet/DB/Result/User.pm
+++ b/perllib/FixMyStreet/DB/Result/User.pm
@@ -19,7 +19,7 @@ __PACKAGE__->add_columns(
sequence => "users_id_seq",
},
"email",
- { data_type => "text", is_nullable => 0 },
+ { data_type => "text", is_nullable => 1 },
"name",
{ data_type => "text", is_nullable => 1 },
"phone",
@@ -30,21 +30,24 @@ __PACKAGE__->add_columns(
{ data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
"flagged",
{ data_type => "boolean", default_value => \"false", is_nullable => 0 },
+ "is_superuser",
+ { data_type => "boolean", default_value => \"false", is_nullable => 0 },
"title",
{ data_type => "text", is_nullable => 1 },
"twitter_id",
{ data_type => "bigint", is_nullable => 1 },
"facebook_id",
{ data_type => "bigint", is_nullable => 1 },
- "is_superuser",
- { data_type => "boolean", default_value => \"false", is_nullable => 0 },
"area_id",
{ data_type => "integer", is_nullable => 1 },
"extra",
{ data_type => "text", is_nullable => 1 },
+ "email_verified",
+ { data_type => "boolean", default_value => \"false", is_nullable => 0 },
+ "phone_verified",
+ { data_type => "boolean", default_value => \"false", is_nullable => 0 },
);
__PACKAGE__->set_primary_key("id");
-__PACKAGE__->add_unique_constraint("users_email_key", ["email"]);
__PACKAGE__->add_unique_constraint("users_facebook_id_key", ["facebook_id"]);
__PACKAGE__->add_unique_constraint("users_twitter_id_key", ["twitter_id"]);
__PACKAGE__->has_many(
@@ -102,13 +105,19 @@ __PACKAGE__->has_many(
);
-# Created by DBIx::Class::Schema::Loader v0.07035 @ 2016-09-16 14:22:10
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:7wfF1VnZax2QTXCIPXr+vg
+# Created by DBIx::Class::Schema::Loader v0.07035 @ 2017-09-19 18:02:17
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:OKHKCSahWD3Ov6ulj+2f/w
+
+# These are not fully unique constraints (they only are when the *_verified
+# is true), but this is managed in ResultSet::User's find() wrapper.
+__PACKAGE__->add_unique_constraint("users_email_verified_key", ["email", "email_verified"]);
+__PACKAGE__->add_unique_constraint("users_phone_verified_key", ["phone", "phone_verified"]);
__PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn");
__PACKAGE__->rabx_column('extra');
use Moo;
+use FixMyStreet::SMS;
use mySociety::EmailUtil;
use namespace::clean -except => [ 'meta' ];
@@ -125,6 +134,26 @@ __PACKAGE__->add_columns(
},
);
+=head2 username
+
+Returns a verified email or phone for this user, preferring email,
+or undef if neither verified (shouldn't happen).
+
+=cut
+
+sub username {
+ my $self = shift;
+ return $self->email if $self->email_verified;
+ return $self->phone_display if $self->phone_verified;
+}
+
+sub phone_display {
+ my $self = shift;
+ return $self->phone unless $self->phone;
+ my $parsed = FixMyStreet::SMS->parse_username($self->phone);
+ return $parsed->{phone} ? $parsed->{phone}->format : $self->phone;
+}
+
sub latest_anonymity {
my $self = shift;
my $p = $self->problems->search(undef, { order_by => { -desc => 'id' } } )->first;
@@ -157,11 +186,19 @@ sub check_for_errors {
$errors{name} = _('Please enter your name');
}
- if ( $self->email !~ /\S/ ) {
- $errors{email} = _('Please enter your email');
- }
- elsif ( !mySociety::EmailUtil::is_valid_email( $self->email ) ) {
- $errors{email} = _('Please enter a valid email');
+ if ($self->email_verified) {
+ if ($self->email !~ /\S/) {
+ $errors{username} = _('Please enter your email');
+ } elsif (!mySociety::EmailUtil::is_valid_email($self->email)) {
+ $errors{username} = _('Please enter a valid email');
+ }
+ } elsif ($self->phone_verified) {
+ my $parsed = FixMyStreet::SMS->parse_username($self->phone);
+ if (!$parsed->{phone}) {
+ $errors{username} = _('Please check your phone number is correct');
+ } elsif (!$parsed->{phone}->is_mobile) {
+ $errors{username} = _('Please enter a mobile number');
+ }
}
return \%errors;