1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-21 22:47:43 +00:00
Files
matomo/plugins/TwoFactorAuth/Dao/RecoveryCodeDao.php
Michal Kleiner 9a3ef94df6 [Coding Style] Enable rule PSR12.Files.FileHeader + unify file headers (#22132)
* [Coding Style] Enable rule PSR12.Files.FileHeader

* Apply CS

* Replace Piwik with Matomo in file headers

* Unify file headers (position, no. of lines, https links)

* Rebuild dist files

* Apply CS

* Fix system test that relies on line numbers in a file that had the file header updated

---------

Co-authored-by: Stefan Giehl <stefan@matomo.org>
2024-04-20 20:50:47 +02:00

88 خطوط
2.4 KiB
PHP

<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\TwoFactorAuth\Dao;
use Piwik\Common;
use Piwik\Db;
class RecoveryCodeDao
{
protected $table = 'twofactor_recovery_code';
protected $tablePrefixed = '';
/**
* @var RecoveryCodeRandomGenerator $generator
*/
private $generator;
public function __construct(RecoveryCodeRandomGenerator $generator)
{
$this->tablePrefixed = Common::prefixTable($this->table);
$this->generator = $generator;
}
public function getPrefixedTableName()
{
return $this->tablePrefixed;
}
public function createRecoveryCodesForLogin($login)
{
$codes = array();
$this->deleteAllRecoveryCodesForLogin($login);
for ($i = 0; $i < 10; $i++) {
$code = $this->generator->generateCode();
$code = mb_strtoupper($code);
$this->insertRecoveryCode($login, $code);
$codes[] = $code;
}
return $codes;
}
public function insertRecoveryCode($login, $recoveryCode)
{
// we do not really care about duplicates as it is very unlikely to happen, that's why we don't even use a
// unique login,recovery_code index
$sql = sprintf('INSERT INTO %s (`login`, `recovery_code`) VALUES(?,?)', $this->tablePrefixed);
Db::query($sql, array($login, $recoveryCode));
}
public function useRecoveryCode($login, $recoveryCode)
{
if ($this->deleteRecoveryCode($login, $recoveryCode)) {
return true;
}
return false;
}
public function getAllRecoveryCodesForLogin($login)
{
$sql = sprintf('SELECT recovery_code FROM %s WHERE login = ?', $this->tablePrefixed);
$rows = Db::fetchAll($sql, array($login));
$codes = array_column($rows, 'recovery_code');
return $codes;
}
public function deleteRecoveryCode($login, $recoveryCode)
{
$sql = sprintf('DELETE FROM %s WHERE login = ? and recovery_code = ?', $this->tablePrefixed);
$query = Db::query($sql, array($login, $recoveryCode));
return $query->rowCount();
}
public function deleteAllRecoveryCodesForLogin($login)
{
$query = sprintf('DELETE FROM %s WHERE login = ?', $this->tablePrefixed);
Db::query($query, array($login));
}
}