| 1 | package Daje::Database::Model::Login; | ||
| 2 | 0 | ▶ | use Mojo::Base 'Daje::Database::Model::Super::Common::Base', -base, -async_await; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 3 | use v5.42; | ||
| 4 | |||
| 5 | # Autogenerated class Sun Feb 9 13:11:20 2025 | ||
| 6 | # This class is generated once. Any over rides or special methods should be put here | ||
| 7 | |||
| 8 | # NAME | ||
| 9 | # ==== | ||
| 10 | # | ||
| 11 | # Daje::Database::Model::Login - It's the login class for users | ||
| 12 | # | ||
| 13 | # | ||
| 14 | # REQUIRES | ||
| 15 | # ======== | ||
| 16 | # | ||
| 17 | # use Mojo::Base; | ||
| 18 | # use Daje::Database::Model::Super::Users; | ||
| 19 | # | ||
| 20 | # DESCRIPTION | ||
| 21 | # =========== | ||
| 22 | # | ||
| 23 | # Daje::Database::Model::Login is logging in users | ||
| 24 | # | ||
| 25 | # LICENSE | ||
| 26 | # ======= | ||
| 27 | # | ||
| 28 | # Copyright (C) janeskil1525. | ||
| 29 | # | ||
| 30 | # This library is free software; you can redistribute it and/or modify | ||
| 31 | # it under the same terms as Perl itself. | ||
| 32 | # | ||
| 33 | # AUTHOR | ||
| 34 | # ====== | ||
| 35 | # | ||
| 36 | # janeskil1525 E<lt>janeskil1525@gmail.comE<gt> | ||
| 37 | # | ||
| 38 | # | ||
| 39 | |||
| 40 | use Data::Dumper; | ||
| 41 | our $VERSION = "0.01"; | ||
| 42 | |||
| 43 | has 'db'; | ||
| 44 | |||
| 45 | sub login ($self, $mail, $password) { | ||
| 46 | my $outcome; | ||
| 47 | my $login_stmt = qq{ | ||
| 48 | SELECT users_users_pkey, mail, users_workflow_fkey, avatar, name | ||
| 49 | FROM users_users | ||
| 50 | WHERE mail = ? AND password = ? AND active = true | ||
| 51 | }; | ||
| 52 | |||
| 53 | my $result = $self->db->query($login_stmt,($mail, $password)); | ||
| 54 | |||
| 55 | my $hash; | ||
| 56 | $hash = $result->hash if $result->rows; | ||
| 57 | if(!defined $hash) { | ||
| 58 | $login_stmt = qq{ | ||
| 59 | SELECT count(*) as exists | ||
| 60 | FROM users_users | ||
| 61 | WHERE mail = ? | ||
| 62 | }; | ||
| 63 | $result = $self->db->query($login_stmt,($mail)); | ||
| 64 | $hash = $result->hash if $result->rows; | ||
| 65 | if($hash->{exists} > 0) { | ||
| 66 | $outcome->{status} = "exists"; | ||
| 67 | } else { | ||
| 68 | $outcome->{status} = "nonexistent"; | ||
| 69 | } | ||
| 70 | |||
| 71 | if($outcome->{status} eq "exists") { | ||
| 72 | $login_stmt = qq{ | ||
| 73 | SELECT count(*) as inactive | ||
| 74 | FROM users_users | ||
| 75 | WHERE mail = ? AND password = ? AND active = false | ||
| 76 | }; | ||
| 77 | |||
| 78 | $result = $self->db->query($login_stmt, ($mail, $password)); | ||
| 79 | $hash = $result->hash if $result->rows; | ||
| 80 | if ($hash->{inactive} > 0) { | ||
| 81 | $hash = {}; | ||
| 82 | $outcome = $self->is_user_registered($mail, $password); | ||
| 83 | } else { | ||
| 84 | $outcome->{status} = "wrong_password"; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } else { | ||
| 88 | $outcome = $hash; | ||
| 89 | $outcome->{status} = "success"; | ||
| 90 | } | ||
| 91 | return $outcome; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub is_user_registered ($self, $mail, $password) { | ||
| 95 | |||
| 96 | my $hash->{status} = "inactive"; | ||
| 97 | my $stmt = qq{ | ||
| 98 | SELECT count(*) as has_company | ||
| 99 | FROM companies_users | ||
| 100 | WHERE users_users_fkey = (SELECT users_users_pkey FROM users_users WHERE mail = ? AND password = ?) | ||
| 101 | }; | ||
| 102 | my $result = $self->db->query($stmt,($mail, $password)); | ||
| 103 | $hash->{company} = $result->hash->{has_company}; | ||
| 104 | $hash->{status} = "unregistered" if $hash->{company} == 0; | ||
| 105 | if($hash->{status} eq "unregistered") { | ||
| 106 | $stmt = qq{ | ||
| 107 | SELECT users_users_pkey, mail, users_workflow_fkey | ||
| 108 | FROM users_users | ||
| 109 | WHERE mail = ? AND password = ? | ||
| 110 | }; | ||
| 111 | $result = $self->db->query($stmt,($mail, $password))->hash; | ||
| 112 | $hash->{users_users_pkey} = $result->{users_users_pkey}; | ||
| 113 | $hash->{users_workflow_fkey} = $result->{users_workflow_fkey}; | ||
| 114 | $hash->{mail} = $result->{mail}; | ||
| 115 | } | ||
| 116 | |||
| 117 | return $hash; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub check_creds ($self, $mail, $password) { | ||
| 121 | |||
| 122 | my $check_stmt = qq{ | ||
| 123 | SELECT count(*) as exists | ||
| 124 | FROM users_users | ||
| 125 | WHERE mail = ? AND password = ? AND (active = true OR verified = false) | ||
| 126 | }; | ||
| 127 | |||
| 128 | my $result = $self->db->query( | ||
| 129 | $check_stmt,($mail, $password) | ||
| 130 | )->hash->{exists}; | ||
| 131 | |||
| 132 | return $result; | ||
| 133 | } | ||
| 134 | |||
| 135 | 1; | ||
| 136 | |||
| 137 | |||
| 138 | #################### pod generated by Pod::Autopod - keep this line to make pod updates possible #################### | ||
| 139 | |||
| 140 | =head1 NAME | ||
| 141 | |||
| 142 | Daje::Database::Model::Login | ||
| 143 | |||
| 144 | |||
| 145 | =head1 DESCRIPTION | ||
| 146 | |||
| 147 | Autogenerated class Sun Feb 9 13:11:20 2025 | ||
| 148 | This class is generated once. Any over rides or special methods should be put here | ||
| 149 | |||
| 150 | |||
| 151 | =head1 REQUIRES | ||
| 152 | |||
| 153 | L<Digest::SHA> | ||
| 154 | |||
| 155 | L<Data::Dumper> | ||
| 156 | |||
| 157 | L<Mojo::Base> | ||
| 158 | |||
| 159 | |||
| 160 | =head1 METHODS | ||
| 161 | |||
| 162 | =head2 check_creds | ||
| 163 | |||
| 164 | check_creds(); | ||
| 165 | |||
| 166 | =head2 login | ||
| 167 | |||
| 168 | login(); | ||
| 169 | |||
| 170 | |||
| 171 | =cut | ||
| 172 | |||