add function for retrieving treatment info by observation unit ids
[sgn.git] / lib / SGN / Controller / User.pm
blob588228f7b3cf51fe1d0268e1222858ec8f56b93c
2 package SGN::Controller::User;
4 use Moose;
6 BEGIN { extends 'Catalyst::Controller' };
8 sub login :Path('/user/login') Args(0) {
9 my $self = shift;
10 my $c = shift;
12 $c->stash->{goto_url} = $c->req->param("goto_url");
14 print STDERR "GOTOURL=".$c->stash->{goto_url}."\n";
15 $c->stash->{template} = '/user/login.mas';
18 sub new_user :Path('/user/new') Args(0) {
19 my $self = shift;
20 my $c = shift;
22 # Redirect to the login page and display the new user form
23 $c->res->redirect('/user/login?goto_url=/&new_user=1');
24 $c->detach();
27 sub update_account :Path('/user/update') Args(0) {
28 my $self = shift;
29 my $c = shift;
31 if (! $c->user()) {
32 $c->res->redirect('/user/login');
33 return;
36 $c->stash->{logged_in_username} = $c->user()->get_username();
37 $c->stash->{private_email} = $c->user()->get_private_email();
39 $c->stash->{template} = '/user/change_account.mas';
42 sub confirm_user :Path('/user/confirm') Args(0) {
43 my $self = shift;
44 my $c = shift;
46 my $confirm_code = $c->req->param('confirm_code');
47 my $username = $c->req->param('username');
49 if ($c->config->{disable_account_confirm}) {
50 $c->stash->{template} = '/generic_message.mas';
51 $c->stash->{message} = 'Account confirmation is disabled on this site. Please contact nm529@cornell.edu to confirm your account.';
52 return;
55 my $sp = CXGN::People::Login->get_login( $c->dbc()->dbh(), $username );
57 if ( !$sp ) {
58 confirm_failure($c, "Username \"$username\" was not found.");
59 return;
62 if ( !$sp->get_confirm_code() ) {
63 confirm_failure($c, "No confirmation is required for user <b>$username</b>. This account has already been confirmed. <p><a href='/user/login'>[Login Page]</a></p>");
64 return;
67 if ( $sp->get_confirm_code() ne $confirm_code ) {
68 confirm_failure($c, "Confirmation code is not valid!\n");
69 return;
72 $sp->set_disabled(undef);
73 $sp->set_confirm_code(undef);
74 $sp->set_private_email( $sp->get_pending_email() );
76 $sp->store();
78 $c->stash->{template} = '/generic_message.mas';
79 $c->stash->{message} = "Confirmation successful for username <b>$username</b>";
82 sub confirm_failure {
83 my $c = shift;
84 my $reason = shift;
86 $c->stash->{template} = '/generic_message.mas';
87 $c->stash->{message} = "Sorry, this confirmation code is invalid. Please check that your complete confirmation URL has been pasted correctly into your browser. ($reason)";
91 sub reset_password_form :Path('/user/reset_password_form') Args(0) {
92 my $self = shift;
93 my $c = shift;
95 my $token = $c->req->param('reset_password_token');
97 my $person_id;
98 if ($token) {
99 my $person_id = CXGN::People::Login->get_login_by_token($c->dbc->dbh(), $token);
100 if (!$person_id) {
101 $c->stash->{message} = "The provided password reset link is invalid. Please try again with another link.";
102 $c->stash->{template} = '/generic_message.mas';
103 return;
106 my $person = CXGN::People::Person->new($c->dbc->dbh(), $person_id);
107 $c->stash->{token} = $token;
108 $c->stash->{person_id} = $person_id;
109 $c->stash->{username} = $person->get_username();
110 $c->stash->{template} = '/user/reset_password_form.mas';
112 else {
113 $c->stash->{message} = "No token provided. Please try again.";
114 $c->stash->{template} = '/generic_message.mas';
119 sub quick_create_account :Path('/user/admin/quick_create_account') {
120 my $self = shift;
121 my $c = shift;
123 if (!$c->user()) {
124 $c->forward('/user/login');
125 return;
128 $c->stash->{template} = '/user/quick_create_account.mas';