Implement getStatusText() for the `backup` multifactor method
authorTim Düsterhus <duesterhus@woltlab.com>
Thu, 5 Nov 2020 10:03:31 +0000 (11:03 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 16 Nov 2020 16:25:04 +0000 (17:25 +0100)
com.woltlab.wcf/templates/accountSecurity.tpl
wcfsetup/install/files/lib/system/user/multifactor/BackupMultifactorMethod.class.php
wcfsetup/install/files/lib/system/user/multifactor/IMultifactorMethod.class.php
wcfsetup/install/files/lib/system/user/multifactor/TotpMultifactorMethod.class.php

index 9684b731ae5a105b3003adcfbaa955279b35122e..68eee0901e781e592b9314469eff4150383b3042 100644 (file)
@@ -24,7 +24,9 @@
                                                        {/if}
                                                </h3>
                                                
-                                               {$method->getProcessor()->getStatusText($__wcf->user)}
+                                               {if $enabledMultifactorMethods[$method->objectTypeID]|isset}
+                                                       {$method->getProcessor()->getStatusText($enabledMultifactorMethods[$method->objectTypeID])}
+                                               {/if}
                                        </div>
                                        
                                        <div class="accountSecurityButtons">
index ac3fd59627d47e255cfc308b872158ab497783d0..a783a9b9b1e182179594cc5c21123a7cc3f7e040 100644 (file)
@@ -1,6 +1,5 @@
 <?php
 namespace wcf\system\user\multifactor;
-use wcf\data\user\User;
 use wcf\system\form\builder\container\FormContainer;
 use wcf\system\form\builder\field\ButtonFormField;
 use wcf\system\form\builder\field\validation\FormFieldValidationError;
@@ -37,9 +36,16 @@ class BackupMultifactorMethod implements IMultifactorMethod {
        /**
         * Returns the number of remaining codes.
         */
-       public function getStatusText(User $user): string {
-               // TODO: Return a proper text.
-               return random_int(10000, 99999)." codes remaining";
+       public function getStatusText(int $setupId): string {
+               $sql = "SELECT  COUNT(*)
+                       FROM    wcf".WCF_N."_user_multifactor_backup
+                       WHERE   setupID = ?
+                               AND useTime IS NULL";
+               $statement = WCF::getDB()->prepareStatement($sql);
+               $statement->execute([$setupId]);
+               
+               // TODO: Language item
+               return $statement->fetchSingleColumn()." codes remaining";
        }
        
        /**
index 22abe11556e08750ff345118e185edae4ee1b29e..b68bebb573757d047dc2933b2a89c1c147e7c5c0 100644 (file)
@@ -1,6 +1,5 @@
 <?php
 namespace wcf\system\user\multifactor;
-use wcf\data\user\User;
 use wcf\system\form\builder\IFormDocument;
 
 /**
@@ -14,11 +13,11 @@ use wcf\system\form\builder\IFormDocument;
  */
 interface IMultifactorMethod {
        /**
-        * Returns a human readable status text regarding the set-up status for the given user.
+        * Returns a human readable status text regarding the set-up status for the given setup.
         * 
         * An example text could be: "5 backup codes remaining".
         */
-       public function getStatusText(User $user): string;
+       public function getStatusText(int $setupId): string;
        
        /**
         * Populates the form to set-up and manage this method.
index 7cdafd988608a32a3084a31f55ad41cf5d3d5d28..0d5f5bb3f85af61e8cb6085de730d7c8353e4c8a 100644 (file)
@@ -1,6 +1,5 @@
 <?php
 namespace wcf\system\user\multifactor;
-use wcf\data\user\User;
 use wcf\system\exception\NotImplementedException;
 use wcf\system\form\builder\IFormDocument;
 
@@ -17,7 +16,7 @@ class TotpMultifactorMethod implements IMultifactorMethod {
        /**
         * Returns the number of devices the user set up.
         */
-       public function getStatusText(User $user): string {
+       public function getStatusText(int $setupId): string {
                // TODO: Return a proper text.
                return random_int(10000, 99999)." devices configured";
        }