diff options
author | Matthew Somerville <matthew@mysociety.org> | 2016-01-15 18:25:48 +0000 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2016-01-22 17:27:34 +0000 |
commit | b137a5910e72a2f3ffabdfe29c0a156d7197695a (patch) | |
tree | fca93f9f06938ef47381bfa82a5f30e2057c6710 | |
parent | a1da835f294dbb4014585c04716a88211d686e62 (diff) |
Add login by Facebook when updating.
Makes the flow more like new reporting.
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/Update.pm | 166 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Tokens.pm | 1 | ||||
-rw-r--r-- | templates/web/base/js/validation_rules.html | 3 | ||||
-rw-r--r-- | templates/web/base/report/display.html | 21 | ||||
-rw-r--r-- | templates/web/base/report/update-form.html | 23 | ||||
-rw-r--r-- | templates/web/base/report/update/form_user_loggedout.html | 20 | ||||
-rw-r--r-- | templates/web/base/report/update/form_user_loggedout_email.html | 4 | ||||
-rw-r--r-- | web/js/fixmystreet.js | 1 |
8 files changed, 189 insertions, 50 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Report/Update.pm b/perllib/FixMyStreet/App/Controller/Report/Update.pm index 445723fec..45d924335 100644 --- a/perllib/FixMyStreet/App/Controller/Report/Update.pm +++ b/perllib/FixMyStreet/App/Controller/Report/Update.pm @@ -20,12 +20,16 @@ Creates an update to a report sub report_update : Path : Args(0) { my ( $self, $c ) = @_; - $c->forward( '/report/load_problem_or_display_error', [ $c->get_param('id') ] ); + $c->forward('initialize_update'); + $c->forward('load_problem'); + $c->forward('check_form_submitted') + or $c->go( '/report/display', [ $c->stash->{problem}->id ] ); + $c->forward('process_update'); $c->forward('process_user'); $c->forward('/photo/process_photo'); $c->forward('check_for_errors') - or $c->go( '/report/display', [ $c->get_param('id') ] ); + or $c->go( '/report/display', [ $c->stash->{problem}->id ] ); $c->forward('save_update'); $c->forward('redirect_or_confirm_creation'); @@ -151,19 +155,46 @@ sub process_user : Private { return 1; } -=head2 process_update +=head2 oauth_callback -Take the submitted params and create a new update item. Does not save -anything to the database. +Called when we successfully login via OAuth. Stores the token so we can look up +what we have so far. -NB: relies on their being a problem and update_user in the stash. May -want to move adding these elsewhere +=cut + +sub oauth_callback : Private { + my ( $self, $c, $token_code ) = @_; + $c->stash->{oauth_update} = $token_code; + $c->detach('report_update'); +} + +=head2 initialize_update + +Create an initial update object, either empty or from stored OAuth data. =cut -sub process_update : Private { +sub initialize_update : Private { my ( $self, $c ) = @_; + my $update; + if ($c->stash->{oauth_update}) { + my $auth_token = $c->forward( '/tokens/load_auth_token', + [ $c->stash->{oauth_update}, 'update/social' ] ); + $update = $c->model("DB::Comment")->new($auth_token->data); + } + + if ($update) { + $c->stash->{upload_fileid} = $update->get_photoset->data; + } else { + $update = $c->model('DB::Comment')->new({ + state => 'unconfirmed', + cobrand => $c->cobrand->moniker, + cobrand_data => '', + lang => $c->stash->{lang_code}, + }); + } + if ( $c->get_param('first_name') && $c->get_param('last_name') ) { my $first_name = $c->get_param('first_name'); my $last_name = $c->get_param('last_name'); @@ -173,6 +204,48 @@ sub process_update : Private { $c->stash->{last_name} = $last_name; } + $c->stash->{update} = $update; +} + +=head2 load_problem + +Our update could be prefilled, or we could be submitting a form containing an +ID. Look up the relevant report either way. + +=cut + +sub load_problem : Private { + my ( $self, $c ) = @_; + + my $update = $c->stash->{update}; + # Problem ID could come from existing update in token, or from query parameter + my $problem_id = $update->problem_id || $c->get_param('id'); + $c->forward( '/report/load_problem_or_display_error', [ $problem_id ] ); + $update->problem($c->stash->{problem}); +} + +=head2 check_form_submitted + +This makes sure we only proceed to processing if we've had the form submitted +(we may have come here via an OAuth login, for example). + +=cut + +sub check_form_submitted : Private { + my ( $self, $c ) = @_; + return $c->get_param('submit_update') || ''; +} + +=head2 process_update + +Take the submitted params and updates our update item. Does not save +anything to the database. + +=cut + +sub process_update : Private { + my ( $self, $c ) = @_; + my %params = map { $_ => $c->get_param($_) } ( 'update', 'name', 'fixed', 'state', 'reopen' ); @@ -184,20 +257,12 @@ sub process_update : Private { $params{reopen} = 0 unless $c->user && $c->user->id == $c->stash->{problem}->user->id; - my $update = $c->model('DB::Comment')->new( - { - text => $params{update}, - name => $name, - problem => $c->stash->{problem}, - state => 'unconfirmed', - mark_fixed => $params{fixed} ? 1 : 0, - mark_open => $params{reopen} ? 1 : 0, - cobrand => $c->cobrand->moniker, - cobrand_data => '', - lang => $c->stash->{lang_code}, - anonymous => $anonymous, - } - ); + my $update = $c->stash->{update}; + $update->text($params{update}); + $update->name($name); + $update->mark_fixed($params{fixed} ? 1 : 0); + $update->mark_open($params{reopen} ? 1 : 0); + $update->anonymous($anonymous); if ( $params{state} ) { $params{state} = 'fixed - council' @@ -241,7 +306,6 @@ sub process_update : Private { $c->log->debug( 'name is ' . $c->get_param('name') ); - $c->stash->{update} = $update; $c->stash->{add_alert} = $c->get_param('add_alert'); return 1; @@ -283,6 +347,13 @@ sub check_for_errors : Private { %{ $c->stash->{update}->check_for_errors }, ); + # if using social login then we don't care about name and email errors + $c->stash->{is_social_user} = $c->get_param('facebook_sign_in') || $c->get_param('twitter_sign_in'); + if ( $c->stash->{is_social_user} ) { + delete $field_errors{name}; + delete $field_errors{email}; + } + if ( my $photo_error = delete $c->stash->{photo_error} ) { $field_errors{photo} = $photo_error; } @@ -302,6 +373,17 @@ sub check_for_errors : Private { return; } +# Store changes in token for when token is validated. +sub tokenize_user : Private { + my ($self, $c, $update) = @_; + $c->stash->{token_data} = { + name => $update->user->name, + password => $update->user->password, + }; + $c->stash->{token_data}{facebook_id} = $c->session->{oauth}{facebook_id} + if $c->get_param('oauth_need_email') && $c->session->{oauth}{facebook_id}; +} + =head2 save_update Save the update and the user as appropriate. @@ -313,6 +395,27 @@ sub save_update : Private { my $update = $c->stash->{update}; + # If there was a photo add that too + if ( my $fileid = $c->stash->{upload_fileid} ) { + $update->photo($fileid); + } + + if ( $c->stash->{is_social_user} ) { + my $token = $c->model("DB::Token")->create( { + scope => 'update/social', + data => { $update->get_inflated_columns }, + } ); + + $c->stash->{detach_to} = '/report/update/oauth_callback'; + $c->stash->{detach_args} = [$token->token]; + + if ( $c->get_param('facebook_sign_in') ) { + $c->detach('/auth/facebook_sign_in'); + } elsif ( $c->get_param('twitter_sign_in') ) { + $c->detach('/auth/twitter_sign_in'); + } + } + if ( $c->cobrand->never_confirm_updates ) { if ( $update->user->in_storage() ) { $update->user->update(); @@ -322,11 +425,7 @@ sub save_update : Private { $update->confirm(); } elsif ( !$update->user->in_storage ) { # User does not exist. - # Store changes in token for when token is validated. - $c->stash->{token_data} = { - name => $update->user->name, - password => $update->user->password, - }; + $c->forward('tokenize_user', [ $update ]); $update->user->name( undef ); $update->user->password( '', 1 ); $update->user->insert; @@ -338,19 +437,10 @@ sub save_update : Private { $update->confirm; } else { # User exists and we are not logged in as them. - # Store changes in token for when token is validated. - $c->stash->{token_data} = { - name => $update->user->name, - password => $update->user->password, - }; + $c->forward('tokenize_user', [ $update ]); $update->user->discard_changes(); } - # If there was a photo add that too - if ( my $fileid = $c->stash->{upload_fileid} ) { - $update->photo($fileid); - } - if ( $update->in_storage ) { $update->update; } diff --git a/perllib/FixMyStreet/App/Controller/Tokens.pm b/perllib/FixMyStreet/App/Controller/Tokens.pm index 9057f217e..0db7d5a11 100644 --- a/perllib/FixMyStreet/App/Controller/Tokens.pm +++ b/perllib/FixMyStreet/App/Controller/Tokens.pm @@ -231,6 +231,7 @@ sub confirm_update : Path('/C') { if ( $data->{name} || $data->{password} ) { $comment->user->name( $data->{name} ) if $data->{name}; $comment->user->password( $data->{password}, 1 ) if $data->{password}; + $comment->user->facebook_id( $data->{facebook_id} ) if $data->{facebook_id}; $comment->user->update; } diff --git a/templates/web/base/js/validation_rules.html b/templates/web/base/js/validation_rules.html index 5d55baff1..5295a53ca 100644 --- a/templates/web/base/js/validation_rules.html +++ b/templates/web/base/js/validation_rules.html @@ -1,6 +1,5 @@ validation_rules = { title: { required: true }, detail: { required: true }, - update: { required: true }, - rznvy: { required: true } + update: { required: true } }; diff --git a/templates/web/base/report/display.html b/templates/web/base/report/display.html index 8d4cc931e..05e07d501 100644 --- a/templates/web/base/report/display.html +++ b/templates/web/base/report/display.html @@ -15,15 +15,30 @@ [% IF login_success %] <p class='form-success'>[% loc('You have successfully signed in; please check and confirm your details are accurate:') %]</p> + [% INCLUDE 'report/update-form.html' %] + [% SET shown_form = 1 %] +[% ELSIF oauth_failure %] + <p class="form-error">[% loc('Sorry, we could not log you in. Please fill in the form below.') %]</p> + [% INCLUDE 'report/update-form.html' %] + [% SET shown_form = 1 %] +[% ELSIF oauth_need_email %] + <p class="form-error"> + [% loc('Please note your update has <strong>not yet been posted</strong>.') %] + [% loc('We need your email address, please give it below.') %] + </p> + [% INCLUDE 'report/update-form.html' %] + [% SET shown_form = 1 %] [% END %] [% INCLUDE 'report/banner.html' %] - [% INCLUDE 'report/_main.html' %] [% TRY %][% INCLUDE 'report/_message_manager.html' %][% CATCH file %][% END %] [% INCLUDE 'report/display_tools.html' %] - [% TRY %][% INCLUDE 'report/sharing.html' %][% CATCH file %][% END %] [% INCLUDE 'report/updates.html' %] -[% INCLUDE 'report/update-form.html' %] + +[% IF NOT shown_form %] + [% INCLUDE 'report/update-form.html' %] +[% END %] + [% INCLUDE 'footer.html' %] diff --git a/templates/web/base/report/update-form.html b/templates/web/base/report/update-form.html index a92930f62..3e0ac890b 100644 --- a/templates/web/base/report/update-form.html +++ b/templates/web/base/report/update-form.html @@ -1,6 +1,8 @@ [% allow_creation = !c.cobrand.only_authed_can_create || (c.user && c.user.from_body) %] -[% IF allow_creation %] +[% RETURN IF NOT allow_creation %] + <div id="update_form"> + [% IF NOT login_success AND NOT oauth_need_email %] <h2>[% loc( 'Provide an update') %]</h2> [% IF c.cobrand.moniker != 'emptyhomes' AND c.cobrand.moniker != 'stevenage' %] @@ -8,20 +10,33 @@ [% INCLUDE 'report/updates-sidebar-notes.html' %] </div> [% END %] + [% END %] [% INCLUDE 'errors.html' %] <form method="post" action="[% c.uri_for( '/report/update' ) %]" id="form_update_form" name="updateForm" class="validate"[% IF c.cobrand.allow_photo_upload %] enctype="multipart/form-data"[% END %]> <fieldset> + [% IF NOT login_success AND NOT oauth_need_email %] [% INCLUDE 'report/update/form_update.html' %] - + [% END %] [% IF c.user_exists %] [% INCLUDE 'report/update/form_name.html' %] - <input class="final-submit green-btn" type="submit" id="update_post" value="[% loc('Post') %]"> + <div class="cf"><input class="final-submit green-btn" type="submit" id="update_post" value="[% loc('Post') %]"></div> + [% ELSIF oauth_need_email %] + [% INCLUDE 'report/update/form_user_loggedout_email.html' required = 1 %] + <div id="form_sign_in"> + <h3>[% loc("Now to submit your update…") %]</h3> + <h2>[% tprintf(loc("Do you have a %s password?", "%s is the site name"), site_name) %]</h2> + [% INCLUDE 'report/update/form_user_loggedout_by_email.html' %] + [% INCLUDE 'report/update/form_user_loggedout_password.html' %] + <input type="hidden" name="oauth_need_email" value="1"> + </div> [% ELSE %] [% INCLUDE 'report/update/form_user_loggedout.html' %] [% END %] + [% IF login_success OR oauth_need_email %] + [% INCLUDE 'report/update/form_update.html' %] + [% END %] </fieldset> </form> </div> -[% END %] diff --git a/templates/web/base/report/update/form_user_loggedout.html b/templates/web/base/report/update/form_user_loggedout.html index 76ac88d2f..4176633f1 100644 --- a/templates/web/base/report/update/form_user_loggedout.html +++ b/templates/web/base/report/update/form_user_loggedout.html @@ -1,8 +1,24 @@ -[% INCLUDE 'report/update/form_user_loggedout_email.html' %] -<h3>[% loc("Now to submit your update…") %]</h3> +[% IF c.config.FACEBOOK_APP_ID %] + <h3>[% loc("Now to submit your update…") %]</h3> + <div class="form-box"> + <button name="facebook_sign_in" id="facebook_sign_in" value="facebook_sign_in" class="btn btn--block btn--social btn--facebook"> + <img alt="" src="/i/facebook-icon-32.png" width="17" height="32"> + Log in with Facebook + </button> + </div> + <div id="js-social-email-hide"> + [% INCLUDE 'report/update/form_user_loggedout_email.html' required=0 %] +[% ELSE %] + [% INCLUDE 'report/update/form_user_loggedout_email.html' required=1 %] + <h3>[% loc("Now to submit your update…") %]</h3> +[% END %] <div id="form_sign_in"> <h2>[% tprintf(loc("Do you have a %s password?", "%s is the site name"), site_name) %]</h2> [% INCLUDE 'report/update/form_user_loggedout_password.html' %] [% INCLUDE 'report/update/form_user_loggedout_by_email.html' %] </div> + +[% IF c.config.FACEBOOK_APP_ID %] + </div> +[% END %] diff --git a/templates/web/base/report/update/form_user_loggedout_email.html b/templates/web/base/report/update/form_user_loggedout_email.html index e03e81a4c..95a3b5578 100644 --- a/templates/web/base/report/update/form_user_loggedout_email.html +++ b/templates/web/base/report/update/form_user_loggedout_email.html @@ -4,4 +4,6 @@ [% IF field_errors.email %] <p class='form-error'>[% field_errors.email %]</p> [% END %] -<input type="email" name="rznvy" id="form_rznvy" value="[% update.user.email | html %]" placeholder="[% loc('Your email address' ) %]" required> +<input type="email" name="rznvy" id="form_rznvy" value="[% update.user.email | html %]" placeholder="[% loc('Your email address' ) %]" + [% IF required %]required[% END %] + class="required"> diff --git a/web/js/fixmystreet.js b/web/js/fixmystreet.js index ad3a8c570..a1c6e6f73 100644 --- a/web/js/fixmystreet.js +++ b/web/js/fixmystreet.js @@ -130,6 +130,7 @@ $(function(){ $('#facebook_sign_in').click(function(e){ $('#form_email').removeClass(); + $('#form_rznvy').removeClass(); }); // Geolocation |