From 1fe761f7640e9f42078c1ae8f42b72d2630d7470 Mon Sep 17 00:00:00 2001 From: Lukas Mueller Date: Tue, 24 Apr 2018 06:23:56 -0400 Subject: [PATCH] draw login buttons using javascript. --- lib/SGN/Controller/AJAX/User.pm | 129 +++++++++++++++++++++++++++++++++++-- lib/SGN/Controller/User.pm | 22 ++++++- mason/site/toolbar/login.mas | 81 +++++++---------------- mason/user/reset_password.mas | 36 +++++++++-- mason/user/reset_password_form.mas | 12 ++-- 5 files changed, 207 insertions(+), 73 deletions(-) rewrite mason/site/toolbar/login.mas (99%) diff --git a/lib/SGN/Controller/AJAX/User.pm b/lib/SGN/Controller/AJAX/User.pm index cde2a5b33..14ed40508 100644 --- a/lib/SGN/Controller/AJAX/User.pm +++ b/lib/SGN/Controller/AJAX/User.pm @@ -14,6 +14,32 @@ __PACKAGE__->config( ); +sub login : Path('/ajax/user/login') Args(0) { + my $self = shift; + my $c = shift; + + my $username = $c->req->param("username"); + my $password = $c->req->param("password"); + + my $login = CXGN::Login->new($c->dbc->dbh()); + my $login_info = $login->login_user($username, $password); + + if (exists($login_info->{incorrect_password}) && $login_info->{incorrect_password} == 1) { + $c->stash->{rest} = { error => "Login credentials are incorrect. Please try again." }; + } + + elsif (exists($login_info->{account_disabled}) && $login_info->{account_disabled}) { + $c->stash->{rest} = { error => "This account has been disabled due to $login_info->{account_disabled}. Please contact the database to fix this problem." }; + } + + else { + $c->stash->{rest} = { message => 'Something happened, but nodoby knows what.' }; + } + $c->stash->{rest} = { message => "Login successful" }; + + +} + sub new_account :Path('/ajax/user/new') Args(0) { my $self = shift; my $c = shift; @@ -75,8 +101,6 @@ sub new_account :Path('/ajax/user/new') Args(0) { } } - - my $confirm_code = $self->tempname(); my $new_user = CXGN::People::Login->new($c->dbc->dbh()); $new_user -> set_username($username); @@ -292,16 +316,26 @@ sub reset_password :Path('/ajax/user/reset_password') Args(0) { $c->stash->{rest} = { message => "Reset link sent. Please check your email and click on the link." }; } -sub process_reset_password_form :Path('/user/process_reset_password') Args(0) { +sub process_reset_password_form :Path('/ajax/user/process_reset_password') Args(0) { my $self = shift; my $c = shift; my $token = $c->req->param("token"); my $new_password = $c->req->param(""); - - my $sp_person_id = CXGN::People::Login->get_login_by_token($c->dbc->dbh, $token); - + eval { + my $sp_person_id = CXGN::People::Login->get_login_by_token($c->dbc->dbh, $token); + + my $login = CXGN::People::Login->new($c->dbc->dbh(), $sp_person_id); + $login->update_password($new_password); + $login->update_confirm_code(""); + }; + if ($@) { + $c->stash->{rest} = { error => $@ }; + } + else { + $c->stash->{rest} = { message => "The password was successfully updated." }; + } } @@ -357,6 +391,89 @@ sub tempname { return $rand_string; } +sub get_login_button_html :Path('/ajax/user/login_button_html') Args(0) { + my $self = shift; + my $c = shift; + eval { + my $production_site = $c->config->{main_production_site_url}; + print STDERR "Get login button... site: $production_site\n"; + if ($c->user()) { + print STDERR "Detected logged in users...\n"; + } + else { + print STDERR "No logged in user found!\n"; + } + my $html = ""; + # if the site is a mirror, gray out the login/logout links + if( $c->config->{'is_mirror'} ) { + print STDERR "generating login button for mirror site...\n"; + $html = <main site + } elsif ( $c->config->{disable_login} ) { + + +HTML + + } elsif ( $c->req->uri->path_query =~ "logout=yes") { + print STDERR "generating login button for logout...\n"; + $html = < + + +HTML + +} elsif ( $c->user_exists ) { + print STDERR "Generate login button for logged in user...\n"; + my $sp_person_id = $c->user->get_object->get_sp_person_id; + my $username = $c->user->get_username(); + $html = < +
+ + + + +
+ +HTML + + } else { + print STDERR "generating regular login button..\n"; + $html = < + + +HTML + +}; + if ($@) { + print STDERR "ERROR: $@\n"; + $c->stash->{rest} = { error => $@ }; + } + return $c->stash->{rest} = { html => $html }; + } +} + + + + + + 1; diff --git a/lib/SGN/Controller/User.pm b/lib/SGN/Controller/User.pm index e43cffb92..c5b028c20 100644 --- a/lib/SGN/Controller/User.pm +++ b/lib/SGN/Controller/User.pm @@ -5,6 +5,20 @@ use Moose; BEGIN { extends 'Catalyst::Controller' }; +sub login :Path('/user/login') Args(0) { + my $self = shift; + my $c = shift; + + $c->stash->{template} = '/user/login.mas'; +} + +sub logout :Path('/user/logout') Args(0) { + my $self = shift; + my $c = shift; + + +} + sub new_account :Path('/user/new') Args(0) { my $self = shift; my $c = shift; @@ -78,7 +92,7 @@ sub confirm_failure { } -sub check_password_reset_token :Path('/user/reset_password_form') Args(0) { +sub reset_password_form :Path('/user/reset_password_form') Args(0) { my $self = shift; my $c = shift; @@ -92,13 +106,19 @@ sub check_password_reset_token :Path('/user/reset_password_form') Args(0) { $c->stash->{template} = '/generic_message.mas'; return; } + my $person = CXGN::People::Person->new($c->dbc->dbh(), $person_id); $c->stash->{token} = $token; $c->stash->{person_id} = $person_id; $c->stash->{username} = $person->get_username(); $c->stash->{template} = '/user/reset_password_form.mas'; } + else { + $c->stash->{message} = "No token provided. Please try again."; + $c->stash->{template} = '/generic_message.mas'; + } } + 1; diff --git a/mason/site/toolbar/login.mas b/mason/site/toolbar/login.mas dissimilarity index 99% index e6792af8f..44fc16702 100644 --- a/mason/site/toolbar/login.mas +++ b/mason/site/toolbar/login.mas @@ -1,56 +1,25 @@ - -% # if the site is a mirror, gray out the login/logout links -% if( $c->config->{'is_mirror'} ) { - main site -% } elsif ( $c->config->{disable_login} ) { - - - -% } elsif ( $c->req->uri->path_query =~ "logout=yes") { - - - -% } elsif ( $c->user_exists ) { -% my $sp_person_id = $c->user->get_object->get_sp_person_id; -
  • - -
    - - - - - -
    - -
  • - -% } else { - - -% } + +[Login] + + + + diff --git a/mason/user/reset_password.mas b/mason/user/reset_password.mas index 092787767..02b8df19f 100644 --- a/mason/user/reset_password.mas +++ b/mason/user/reset_password.mas @@ -3,21 +3,45 @@ + + + <& /page/page_title.mas, title=>"Reset Password" &>
    To reset your password, please enter your email address. A link will be sent to that address with a link that will enable you to reset your password.
    - -
    +
    - - + +
    + +
    +

    - - + +
    -- 2.11.4.GIT