Bug 8753 - Smartmatch substitute, Math::Random::Secure, Perltidy, Passwordrecovery.t
[koha.git] / C4 / Passwordrecovery.pm
1 package C4::Passwordrecovery;
2
3 # Copyright 2014 Solutions InLibro inc.
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use C4::Context;
22 use Math::Random::Secure;
23
24 use vars qw($VERSION @ISA @EXPORT);
25
26 BEGIN {
27     # set the version for version checking
28     $VERSION = 3.07.00.049;
29     require Exporter;
30     @ISA = qw(Exporter);
31     push @EXPORT, qw(
32       &ValidateBorrowernumber
33       &SendPasswordRecoveryEmail
34       &GetValidLinkInfo
35       &CompletePasswordRecovery
36     );
37 }
38
39 =head1 NAME
40
41 C4::Passwordrecovery - Koha password recovery module
42
43 =head1 SYNOPSIS
44
45 use C4::Passwordrecovery;
46
47 =head1 FUNCTIONS
48
49 =head2 ValidateBorrowernumber
50
51 $alread = ValidateBorrowernumber( $borrower_number );
52
53 Check if the system already start recovery
54
55 Returns true false
56
57 =cut
58
59 sub ValidateBorrowernumber {
60     my ($borrower_number) = @_;
61     my $schema = Koha::Database->new->schema;
62
63     my $rs = $schema->resultset('BorrowerPasswordRecovery')->search(
64         {
65             borrowernumber => $borrower_number,
66             valid_until    => \'> NOW()'
67         },
68         { columns => 'borrowernumber' }
69     );
70
71     if ( $rs->next ) {
72         return 1;
73     }
74
75     return 0;
76 }
77
78 =head2 GetValidLinkInfo
79
80     Check if the link is still valid and return some info.
81
82 =cut
83
84 sub GetValidLinkInfo {
85     my ($uniqueKey) = @_;
86     my $dbh         = C4::Context->dbh;
87     my $query       = '
88     SELECT borrower_password_recovery.borrowernumber, userid
89     FROM borrower_password_recovery, borrowers
90     WHERE borrowers.borrowernumber = borrower_password_recovery.borrowernumber
91     AND NOW() < valid_until
92     AND uuid = ?
93     ';
94     my $sth = $dbh->prepare($query);
95     $sth->execute($uniqueKey);
96     return $sth->fetchrow;
97 }
98
99 =head2 SendPasswordRecoveryEmail
100
101  It creates an email using the templates and sends it to the user, using the specified email
102
103 =cut
104
105 sub SendPasswordRecoveryEmail {
106     my $borrower  = shift;    # Koha::Borrower
107     my $userEmail = shift;    #to_address (the one specified in the request)
108     my $update    = shift;
109
110     my $schema = Koha::Database->new->schema;
111
112     # generate UUID
113     my @chars = ( "A" .. "Z", "a" .. "z", "0" .. "9" );
114     my $uuid_str;
115     $uuid_str .= $chars[ rand @chars ] for 1 .. 32;
116
117     # insert into database
118     my $expirydate =
119       DateTime->now( time_zone => C4::Context->tz() )->add( days => 2 );
120     if ($update) {
121         my $rs =
122           $schema->resultset('BorrowerPasswordRecovery')
123           ->search( { borrowernumber => $borrower->borrowernumber, } );
124         $rs->update(
125             { uuid => $uuid_str, valid_until => $expirydate->datetime() } );
126     }
127     else {
128         my $rs = $schema->resultset('BorrowerPasswordRecovery')->create(
129             {
130                 borrowernumber => $borrower->borrowernumber,
131                 uuid           => $uuid_str,
132                 valid_until    => $expirydate->datetime()
133             }
134         );
135     }
136
137     # create link
138     my $uuidLink = C4::Context->preference('OPACBaseURL')
139       . "/cgi-bin/koha/opac-password-recovery.pl?uniqueKey=$uuid_str";
140
141     # prepare the email
142     my $letter = C4::Letters::GetPreparedLetter(
143         module      => 'members',
144         letter_code => 'PASSWORD_RESET',
145         branchcode  => $borrower->branchcode,
146         substitute =>
147           { passwordreseturl => $uuidLink, user => $borrower->userid },
148     );
149
150     # define to/from emails
151     my $kohaEmail = C4::Context->preference('KohaAdminEmailAddress');    # from
152
153     C4::Letters::EnqueueLetter(
154         {
155             letter                 => $letter,
156             borrowernumber         => $borrower->borrowernumber,
157             to_address             => $userEmail,
158             from_address           => $kohaEmail,
159             message_transport_type => 'email',
160         }
161     );
162
163     return 1;
164 }
165
166 =head2 CompletePasswordRecovery
167
168     $bool = CompletePasswordRevovery($uuid);
169
170     Deletes a password recovery entry.
171
172 =cut
173
174 sub CompletePasswordRecovery {
175     my $uniqueKey = shift;
176     my $model =
177       Koha::Database->new->schema->resultset('BorrowerPasswordRecovery');
178     my $entry = $model->search(
179         { -or => [ uuid => $uniqueKey, valid_until => \'< NOW()' ] } );
180     return $entry->delete();
181 }
182
183 END { }    # module clean-up code here (global destructor)
184
185 1;