refactor login logout calls
[sgn.git] / lib / CXGN / BrAPI / v1 / Authentication.pm
blob8e737b284a64beb941cb2230317cf49b0decf58e
1 package CXGN::BrAPI::v1::Authentication;
3 use Moose;
4 use Data::Dumper;
5 use CXGN::BrAPI::Pagination;
7 has 'bcs_schema' => ( isa => 'Bio::Chado::Schema',
8 is => 'rw',
9 required => 1,
12 has 'status' => ( isa => 'ArrayRef[Maybe[HashRef]]',
13 is => 'rw',
14 required => 1,
17 sub login {
18 my $self = shift;
19 my $grant_type = shift;
20 my $password = shift;
21 my $username = shift;
22 my $client_id = shift;
23 my $status = $self->status;
25 if ($client_id){
26 push @$status, { 'warning' => 'Parameter client_id not supported. Please use a username and password.' };
29 my $login_controller = CXGN::Login->new($self->bcs_schema->storage->dbh);
31 my $message = '';
32 my $cookie = '';
33 my $first_name = '';
34 my $last_name = '';
36 if ( $login_controller->login_allowed() ) {
37 if ($grant_type eq 'password' || !$grant_type) {
38 my $login_info = $login_controller->login_user( $username, $password );
39 if ($login_info->{account_disabled}) {
40 push @$status, { 'error' => 'Account Disabled' };
42 if ($login_info->{incorrect_password}) {
43 push @$status, { 'error' => 'Incorrect Password' };
45 if ($login_info->{duplicate_cookie_string}) {
46 push @$status, { 'error' => 'Duplicate Cookie String' };
48 if ($login_info->{logins_disabled}) {
49 push @$status, { 'error' => 'Logins Disabled' };
51 if ($login_info->{person_id}) {
52 push @$status, { 'success' => 'Login Successfull' };
53 $cookie = $login_info->{cookie_string};
54 $first_name = $login_info->{first_name};
55 $last_name = $login_info->{last_name};
57 } else {
58 push @$status, { 'error' => 'Grant Type Not Supported. Valid grant type: password' };
60 } else {
61 push @$status, { 'error' => 'Login Not Allowed At This Time.' };
63 my $pagination = CXGN::BrAPI::Pagination->pagination_response(0,1,0);
64 my $response = {
65 'status' => $status,
66 'pagination' => $pagination,
67 'result' => { 'first_name' => $first_name, 'last_name' => $last_name, 'cookie' =>$cookie },
68 'datafiles' => []
70 return $response;
73 sub logout {
74 my $self = shift;
75 my $login_controller = CXGN::Login->new($self->bcs_schema->storage->dbh);
76 my $status = $self->status;
77 $login_controller->logout_user();
78 push @$status, { 'success' => 'User Logged Out'};
79 my $pagination = CXGN::BrAPI::Pagination->pagination_response(0,1,0);
80 my $response = {
81 'status' => $status,
82 'pagination' => $pagination,
83 'result' => {},
84 'datafiles' => []
86 return $response;