Bug 18298: minPaswordLength should not be < 3
[koha_ffzg] / C4 / Auth.pm
index c817ffb..11f57b2 100644 (file)
@@ -182,9 +182,9 @@ sub get_template_and_user {
     }
 
 
-    # If the user logged in is the SCO user and he or she tries to go out the SCO module, log the user out removing the CGISESSID cookie
+    # If the user logged in is the SCO user and they try to go out of the SCO module, log the user out removing the CGISESSID cookie
     if ( $in->{type} eq 'opac' and $in->{template_name} !~ m|sco/| ) {
-        if (  C4::Context->preference('AutoSelfCheckID') && $user eq C4::Context->preference('AutoSelfCheckID') ) {
+        if ( $user && C4::Context->preference('AutoSelfCheckID') && $user eq C4::Context->preference('AutoSelfCheckID') ) {
             $template = C4::Templates::gettemplate( 'opac-auth.tt', 'opac', $in->{query} );
             my $cookie = $in->{query}->cookie(
                 -name     => 'CGISESSID',
@@ -211,7 +211,6 @@ sub get_template_and_user {
 
     my $borrowernumber;
     if ($user) {
-        require C4::Members;
 
         # It's possible for $user to be the borrowernumber if they don't have a
         # userid defined (and are logging in through some other method, such
@@ -219,8 +218,9 @@ sub get_template_and_user {
         my $borrower;
         $borrowernumber = getborrowernumber($user) if defined($user);
         if ( !defined($borrowernumber) && defined($user) ) {
-            $borrower = C4::Members::GetMember( borrowernumber => $user );
+            $borrower = Koha::Patrons->find( $user );
             if ($borrower) {
+                $borrower = $borrower->unblessed;
                 $borrowernumber = $user;
 
                 # A bit of a hack, but I don't know there's a nicer way
@@ -228,7 +228,8 @@ sub get_template_and_user {
                 $user = $borrower->{firstname} . ' ' . $borrower->{surname};
             }
         } else {
-            $borrower = C4::Members::GetMember( borrowernumber => $borrowernumber );
+            $borrower = Koha::Patrons->find( $borrowernumber );
+            $borrower->unblessed if $borrower; # FIXME Otherwise, what to do?
         }
 
         # user info
@@ -260,7 +261,7 @@ sub get_template_and_user {
         my $all_perms = get_all_subpermissions();
 
         my @flagroots = qw(circulate catalogue parameters borrowers permissions reserveforothers borrow
-          editcatalogue updatecharges management tools editauthorities serials reports acquisition);
+          editcatalogue updatecharges management tools editauthorities serials reports acquisition clubs);
 
         # We are going to use the $flags returned by checkauth
         # to create the template's parameters that will indicate
@@ -283,8 +284,9 @@ sub get_template_and_user {
             $template->param( CAN_user_staffaccess      => 1 );
             $template->param( CAN_user_plugins          => 1 );
             $template->param( CAN_user_coursereserves   => 1 );
-            foreach my $module ( keys %$all_perms ) {
+            $template->param( CAN_user_clubs            => 1 );
 
+            foreach my $module ( keys %$all_perms ) {
                 foreach my $subperm ( keys %{ $all_perms->{$module} } ) {
                     $template->param( "CAN_user_${module}_${subperm}" => 1 );
                 }
@@ -415,6 +417,8 @@ sub get_template_and_user {
     my $https = $in->{query}->https();
     my $using_https = ( defined $https and $https ne 'OFF' ) ? 1 : 0;
 
+    my $minPasswordLength = C4::Context->preference('minPasswordLength');
+    $minPasswordLength = 3 if not $minPasswordLength or $minPasswordLength < 3;
     $template->param(
         "BiblioDefaultView" . C4::Context->preference("BiblioDefaultView") => 1,
         EnhancedMessagingPreferences                                       => C4::Context->preference('EnhancedMessagingPreferences'),
@@ -436,6 +440,7 @@ sub get_template_and_user {
         noItemTypeImages   => C4::Context->preference("noItemTypeImages"),
         marcflavour        => C4::Context->preference("marcflavour"),
         OPACBaseURL        => C4::Context->preference('OPACBaseURL'),
+        minPasswordLength  => $minPasswordLength,
     );
     if ( $in->{'type'} eq "intranet" ) {
         $template->param(
@@ -964,12 +969,12 @@ sub checkauth {
                     # doesn't have a userid. So if there is none, we pass along the
                     # borrower number, and the bits of code that need to know the user
                     # ID will have to be smart enough to handle that.
-                    require C4::Members;
-                    my @users_info = C4::Members::GetBorrowersWithEmail($value);
-                    if (@users_info) {
+                    my $patrons = Koha::Patrons->search({ email => $value });
+                    if ($patrons->count) {
 
                         # First the userid, then the borrowernum
-                        $value = $users_info[0][1] || $users_info[0][0];
+                        my $patron = $patrons->next;
+                        $value = $patron->userid || $patron->borrowernumber;
                     } else {
                         undef $value;
                     }
@@ -994,12 +999,12 @@ sub checkauth {
                         # doesn't have a userid. So if there is none, we pass along the
                         # borrower number, and the bits of code that need to know the user
                         # ID will have to be smart enough to handle that.
-                        require C4::Members;
-                        my @users_info = C4::Members::GetBorrowersWithEmail($value);
-                        if (@users_info) {
+                        my $patrons = Koha::Patrons->search({ email => $value });
+                        if ($patrons->count) {
 
                             # First the userid, then the borrowernum
-                            $value = $users_info[0][1] || $users_info[0][0];
+                            my $patron = $patrons->next;
+                            $value = $patron->userid || $patron->borrowernumber;
                         } else {
                             undef $value;
                         }
@@ -1020,6 +1025,8 @@ sub checkauth {
 
             # $return: 1 = valid user, 2 = superlibrarian
             if ($return) {
+                # If DB user is logged in
+                $userid ||= $q_userid if $return == 2;
 
                 #_session_log(sprintf "%20s from %16s logged in  at %30s.\n", $userid,$ENV{'REMOTE_ADDR'},(strftime '%c', localtime));
                 if ( $flags = haspermission( $userid, $flagsrequired ) ) {
@@ -1206,6 +1213,8 @@ sub checkauth {
         push @inputs, { name => $name, value => $value };
     }
 
+    my $patron = Koha::Patrons->find({ userid => $q_userid }); # Not necessary logged in!
+
     my $LibraryNameTitle = C4::Context->preference("LibraryName");
     $LibraryNameTitle =~ s/<(?:\/?)(?:br|p)\s*(?:\/?)>/ /sgi;
     $LibraryNameTitle =~ s/<(?:[^<>'"]|'(?:[^']*)'|"(?:[^"]*)")*>//sg;
@@ -1255,6 +1264,7 @@ sub checkauth {
         PatronSelfRegistration                => C4::Context->preference("PatronSelfRegistration"),
         PatronSelfRegistrationDefaultCategory => C4::Context->preference("PatronSelfRegistrationDefaultCategory"),
         opac_css_override                     => $ENV{'OPAC_CSS_OVERRIDE'},
+        too_many_login_attempts               => ( $patron and $patron->account_locked ),
     );
 
     $template->param( SCO_login => 1 ) if ( $query->param('sco_user_login') );
@@ -1753,28 +1763,45 @@ sub get_session {
 sub checkpw {
     my ( $dbh, $userid, $password, $query, $type, $no_set_userenv ) = @_;
     $type = 'opac' unless $type;
-    if ($ldap) {
+
+    my @return;
+    my $patron = Koha::Patrons->find({ userid => $userid });
+    my $check_internal_as_fallback = 0;
+    my $passwd_ok = 0;
+    # Note: checkpw_* routines returns:
+    # 1 if auth is ok
+    # 0 if auth is nok
+    # -1 if user bind failed (LDAP only)
+    # 2 if DB user is used (internal only)
+
+    if ( $patron and $patron->account_locked ) {
+        # Nothing to check, account is locked
+    } elsif ($ldap) {
         $debug and print STDERR "## checkpw - checking LDAP\n";
         my ( $retval, $retcard, $retuserid ) = checkpw_ldap(@_);    # EXTERNAL AUTH
-        return 0 if $retval == -1;                                  # Incorrect password for LDAP login attempt
-        ($retval) and return ( $retval, $retcard, $retuserid );
-    }
+        if ( $retval == 1 ) {
+            @return = ( $retval, $retcard, $retuserid );
+            $passwd_ok = 1;
+        }
+        $check_internal_as_fallback = 1 if $retval == 0;
 
-    if ( $cas && $query && $query->param('ticket') ) {
+    } elsif ( $cas && $query && $query->param('ticket') ) {
         $debug and print STDERR "## checkpw - checking CAS\n";
 
         # In case of a CAS authentication, we use the ticket instead of the password
         my $ticket = $query->param('ticket');
         $query->delete('ticket');                                   # remove ticket to come back to original URL
         my ( $retval, $retcard, $retuserid ) = checkpw_cas( $dbh, $ticket, $query, $type );    # EXTERNAL AUTH
-        ($retval) and return ( $retval, $retcard, $retuserid );
-        return 0;
+        if ( $retval ) {
+            @return = ( $retval, $retcard, $retuserid );
+        }
+        $passwd_ok = $retval;
     }
 
     # If we are in a shibboleth session (shibboleth is enabled, and a shibboleth match attribute is present)
     # Check for password to asertain whether we want to be testing against shibboleth or another method this
     # time around.
-    if ( $shib && $shib_login && !$password ) {
+    elsif ( $shib && $shib_login && !$password ) {
 
         $debug and print STDERR "## checkpw - checking Shibboleth\n";
 
@@ -1785,13 +1812,29 @@ sub checkpw {
         # Then, we check if it matches a valid koha user
         if ($shib_login) {
             my ( $retval, $retcard, $retuserid ) = C4::Auth_with_shibboleth::checkpw_shib($shib_login);    # EXTERNAL AUTH
-            ($retval) and return ( $retval, $retcard, $retuserid );
-            return 0;
+            if ( $retval ) {
+                @return = ( $retval, $retcard, $retuserid );
+            }
+            $passwd_ok = $retval;
         }
+    } else {
+        $check_internal_as_fallback = 1;
     }
 
     # INTERNAL AUTH
-    return checkpw_internal( $dbh, $userid, $password, $no_set_userenv);
+    if ( $check_internal_as_fallback ) {
+        @return = checkpw_internal( $dbh, $userid, $password, $no_set_userenv);
+        $passwd_ok = 1 if $return[0] > 0; # 1 or 2
+    }
+
+    if( $patron ) {
+        if ( $passwd_ok ) {
+            $patron->update({ login_attempts => 0 });
+        } else {
+            $patron->update({ login_attempts => $patron->login_attempts + 1 });
+        }
+    }
+    return @return;
 }
 
 sub checkpw_internal {