diff options
Diffstat (limited to 'perllib/FixMyStreet/App/Controller')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Auth.pm | 49 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/New.pm | 63 |
2 files changed, 112 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Auth.pm b/perllib/FixMyStreet/App/Controller/Auth.pm index c67de692a..3dc25dedf 100644 --- a/perllib/FixMyStreet/App/Controller/Auth.pm +++ b/perllib/FixMyStreet/App/Controller/Auth.pm @@ -7,6 +7,7 @@ BEGIN { extends 'Catalyst::Controller'; } use Email::Valid; use Net::Domain::TLD; use mySociety::AuthToken; +use JSON; =head1 NAME @@ -250,6 +251,54 @@ sub sign_out : Local { $c->logout(); } +sub ajax_sign_in : Path('ajax/sign_in') { + my ( $self, $c ) = @_; + + my $return = {}; + if ( $c->forward( 'sign_in' ) ) { + $return->{name} = $c->user->name; + } else { + $return->{error} = 1; + } + + my $body = JSON->new->utf8(1)->encode( $return ); + $c->res->content_type('application/json; charset=utf-8'); + $c->res->body($body); + + return 1; +} + +sub ajax_sign_out : Path('ajax/sign_out') { + my ( $self, $c ) = @_; + + $c->logout(); + + my $body = JSON->new->utf8(1)->encode( { signed_out => 1 } ); + $c->res->content_type('application/json; charset=utf-8'); + $c->res->body($body); + + return 1; +} + +sub ajax_check_auth : Path('ajax/check_auth') { + my ( $self, $c ) = @_; + + my $code = 401; + my $data = { not_authorized => 1 }; + + if ( $c->user ) { + $data = { name => $c->user->name }; + $code = 200; + } + + my $body = JSON->new->utf8(1)->encode( $data ); + $c->res->content_type('application/json; charset=utf-8'); + $c->res->code($code); + $c->res->body($body); + + return 1; +} + =head2 check_auth Utility page - returns a simple message 'OK' and a 200 response if the user is diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index daaaca499..35173816a 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -104,6 +104,68 @@ sub report_new : Path : Args(0) { $c->forward('redirect_or_confirm_creation'); } +# This is for the new phonegap versions of the app. It looks a lot like +# report_new but there's a few workflow differences as we only ever want +# to sent JSON back here + +sub report_new_ajax : Path('mobile') : Args(0) { + my ( $self, $c ) = @_; + + # create the report - loading a partial if available + $c->forward('initialize_report'); + + unless ( $c->forward('determine_location') ) { + $c->stash->{ json_response } = { errors => 'Unable to determine location' }; + $c->forward('send_json_response'); + return 1; + } + + $c->forward('setup_categories_and_councils'); + $c->forward('process_user'); + $c->forward('process_report'); + $c->forward('/photo/process_photo'); + + unless ($c->forward('check_for_errors')) { + $c->stash->{ json_response } = { errors => $c->stash->{field_errors} }; + $c->stash->{ json_response }->{check_name} = $c->user->name if $c->stash->{check_name}; + $c->forward('send_json_response'); + return 1; + } + + $c->forward('save_user_and_report'); + + my $report = $c->stash->{report}; + my $data = $c->stash->{token_data} || {}; + my $token = $c->model("DB::Token")->create( { + scope => 'problem', + data => { + %$data, + id => $report->id + } + } ); + if ( $report->confirmed ) { + $c->stash->{ json_response } = { success => 1, report => $report->id }; + } else { + $c->stash->{token_url} = $c->uri_for_email( '/P', $token->token ); + $c->send_email( 'problem-confirm.txt', { + to => [ [ $report->user->email, $report->name ] ], + } ); + $c->stash->{ json_response } = { success => 1 }; + } + + $c->forward('send_json_response'); +} + +sub send_json_response : Private { + my ( $self, $c ) = @_; + + my $body = JSON->new->utf8(1)->encode( + $c->stash->{json_response}, + ); + $c->res->content_type('application/json; charset=utf-8'); + $c->res->body($body); +} + sub report_form_ajax : Path('ajax') : Args(0) { my ( $self, $c ) = @_; @@ -680,6 +742,7 @@ sub process_user : Private { my $user = $c->user->obj; $report->user( $user ); $report->name( $user->name ); + $c->stash->{check_name} = 1; $c->stash->{field_errors}->{name} = _('You have successfully signed in; please check and confirm your details are accurate:'); $c->log->info($user->id . ' logged in during problem creation'); return 1; |