#1042 Send new password if user passoword is empty (for migration)

This commit is contained in:
Diatrex 2020-03-06 17:26:42 +03:00
parent aa511fff07
commit fded095c60
3 changed files with 64 additions and 1 deletions

View File

@ -26,4 +26,5 @@ return [
'error_valid_phone' => 'Phone number format is not correct.',
'registered_phone' => 'This phone number has already been registered.',
'ajax_address_error' => 'No address or not authorized to view.',
'empty_password_sms_message' => 'Due to security issues, we changed your password! Your new password is:',
];

View File

@ -0,0 +1,27 @@
<?php namespace Visiosoft\ProfileModule\Profile\Events;
use Anomaly\UsersModule\User\UserModel;
class SendEmptyPassword
{
public $password;
public $userId;
public function __construct($userId, $password)
{
$this->password = $password;
$this->userId = $userId;
}
public function user()
{
$user_model = new UserModel();
$user = $user_model->find($this->userId);
return $user;
}
public function password()
{
return $this->password;
}
}

View File

@ -4,20 +4,28 @@ use Anomaly\Streams\Platform\Addon\Extension\ExtensionCollection;
use Anomaly\UsersModule\User\Authenticator\Contract\AuthenticatorExtensionInterface;
use Anomaly\UsersModule\User\Contract\UserInterface;
use Anomaly\UsersModule\User\Contract\UserRepositoryInterface;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Http\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Visiosoft\ProfileModule\Profile\Events\SendEmptyPassword;
use Visiosoft\ProfileModule\Profile\SignIn\SignInFormBuilder;
class ValidateCredentials
{
private $extensions;
private $repository;
private $dispatcher;
public function __construct(
ExtensionCollection $extensions,
UserRepositoryInterface $userRepository
UserRepositoryInterface $userRepository,
Dispatcher $dispatcher
)
{
$this->extensions = $extensions;
$this->repository = $userRepository;
$this->dispatcher = $dispatcher;
}
public function authenticate(array $credentials)
@ -41,6 +49,23 @@ class ValidateCredentials
}
$response = $this->repository->findByCredentials($credentials);
// Send new password if users password is empty
if (is_null($response)) {
if (isset($credentials['email'])) {
$probableUser = $this->repository->findByEmail($credentials['email']);
} elseif (isset($credentials['username'])) {
$probableUser = $this->repository->findByUsername($credentials['username']);
}
if ($probableUser) {
if (is_null($probableUser->password) || empty($probableUser->password)) {
$password = $this->randomPassword();
$probableUser->setAttribute('password', $password);
$probableUser->update();
$this->dispatcher->dispatch(new SendEmptyPassword($probableUser->id, $password));
}
}
}
} else {
$response = $authenticator->authenticate($credentials);
}
@ -57,6 +82,16 @@ class ValidateCredentials
return false;
}
public function randomPassword() {
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 8; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass); //turn the array into a string
}
public function handle(SignInFormBuilder $builder)
{