Skip to content

Commit

Permalink
Fix error for user without email address
Browse files Browse the repository at this point in the history
yurabakhtin committed Dec 13, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 8b58f81 commit 1d9607f
Showing 5 changed files with 36 additions and 7 deletions.
6 changes: 4 additions & 2 deletions Module.php
Original file line number Diff line number Diff line change
@@ -97,9 +97,10 @@ public function renderDriverCheckboxItem($index, $label, $name, $checked, $value
/**
* Get enabled drivers
*
* @param bool $checkActive
* @return array
*/
public function getEnabledDrivers()
public function getEnabledDrivers(bool $checkActive = true): array
{
$enabledDrivers = $this->settings->get('enabledDrivers', implode(',', $this->drivers));

@@ -110,7 +111,8 @@ public function getEnabledDrivers()
// Check if each enabled Driver is properly installed:
$enabledDrivers = explode(',', $enabledDrivers);
foreach ($enabledDrivers as $d => $enabledDriverClassName) {
if (!TwofaHelper::getDriverByClassName($enabledDriverClassName)->isInstalled()) {
$enabledDriver = TwofaHelper::getDriverByClassName($enabledDriverClassName);
if (!$enabledDriver->isInstalled() || ($checkActive && !$enabledDriver->isActive())) {
unset($enabledDrivers[$d]);
}
}
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ Changelog
- Enh: Added French translations
- Enh #33: Added trusted network functionality
- Enh #16: Added remember browser for X days
- Fix #41: Fix error for user without email address

1.0.5 (August 10 , 2021)
-----------------------
18 changes: 15 additions & 3 deletions drivers/BaseDriver.php
Original file line number Diff line number Diff line change
@@ -58,16 +58,28 @@ public function isInstalled()
return true;
}

/**
* Check if this Driver is active for current User
*
* @return bool
*/
public function isActive(): bool
{
if (Yii::$app->user->isGuest) {
return false;
}

return Yii::$app->user->getIdentity() instanceof User;
}

/**
* Action before send/generate code
*
* @return bool
*/
protected function beforeSend()
{
/** @var User $user */
$user = Yii::$app->user->getIdentity();
if (!$user) {
if (!$this->isActive()) {
return false;
}

16 changes: 15 additions & 1 deletion drivers/EmailDriver.php
Original file line number Diff line number Diff line change
@@ -9,8 +9,9 @@
namespace humhub\modules\twofa\drivers;

use humhub\modules\user\models\User;
use yii\mail\BaseMessage;
use Yii;
use yii\mail\BaseMessage;
use yii\validators\EmailValidator;

class EmailDriver extends BaseDriver
{
@@ -27,6 +28,11 @@ public function init()
/**
* @inheritdoc
*/
public function isActive(): bool
{
return parent::isActive() && $this->isValidUserEmail();
}

public function send()
{
if (!$this->beforeSend()) {
@@ -49,4 +55,12 @@ public function send()

return $mail->send();
}

protected function isValidUserEmail(): bool
{
/* @var User $user */
$user = Yii::$app->user->getIdentity();

return !empty($user->email) && (new EmailValidator)->validate($user->email);
}
}
2 changes: 1 addition & 1 deletion models/Config.php
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ public function init()
parent::init();

$this->module = Yii::$app->getModule('twofa');
$this->enabledDrivers = $this->module->getEnabledDrivers();
$this->enabledDrivers = $this->module->getEnabledDrivers(false);
$this->codeLength = $this->module->getCodeLength();
$this->rememberMeDays = $this->module->getRememberMeDays();
$this->enforcedGroups = $this->module->getEnforcedGroups();

0 comments on commit 1d9607f

Please sign in to comment.