mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-06 16:36:59 +01:00
refactor: [WIP] Migrate user pass reset controller.
Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/*
|
||||
* sysPass
|
||||
*
|
||||
* @author nuxsmin
|
||||
* @link https://syspass.org
|
||||
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
|
||||
*
|
||||
* This file is part of sysPass.
|
||||
*
|
||||
* sysPass is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* sysPass is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace SP\Modules\Web\Controllers\UserPassReset;
|
||||
|
||||
|
||||
use Exception;
|
||||
use SP\Core\Events\Event;
|
||||
use SP\Core\Events\EventMessage;
|
||||
use SP\Core\Exceptions\SPException;
|
||||
use SP\Domain\User\Services\UserPassRecoverService;
|
||||
use SP\Http\JsonResponse;
|
||||
use SP\Modules\Web\Controllers\Traits\JsonTrait;
|
||||
|
||||
/**
|
||||
* Class SaveRequestController
|
||||
*/
|
||||
final class SaveRequestController extends UserPassResetSaveBase
|
||||
{
|
||||
use JsonTrait;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function saveRequestAction(): bool
|
||||
{
|
||||
try {
|
||||
$this->checkTracking();
|
||||
|
||||
$login = $this->request->analyzeString('login');
|
||||
$email = $this->request->analyzeEmail('email');
|
||||
|
||||
$userData = $this->userService->getByLogin($login);
|
||||
|
||||
if ($userData->getEmail() !== $email) {
|
||||
throw new SPException(__u('Wrong data'), SPException::WARNING);
|
||||
}
|
||||
|
||||
if ($userData->isDisabled() || $userData->isLdap()) {
|
||||
throw new SPException(
|
||||
__u('Unable to reset the password'),
|
||||
SPException::WARNING,
|
||||
__u('Please contact to the administrator')
|
||||
);
|
||||
}
|
||||
|
||||
$hash = $this->userPassRecoverService->requestForUserId($userData->getId());
|
||||
|
||||
$this->eventDispatcher->notifyEvent(
|
||||
'request.user.passReset',
|
||||
new Event(
|
||||
$this,
|
||||
EventMessage::factory()
|
||||
->addDescription(__u('Password Recovery'))
|
||||
->addDetail(__u('Requested for'), sprintf('%s (%s)', $login, $email))
|
||||
)
|
||||
);
|
||||
|
||||
$this->mailService->send(
|
||||
__('Password Change'),
|
||||
$email,
|
||||
UserPassRecoverService::getMailMessage($hash)
|
||||
);
|
||||
|
||||
return $this->returnJsonResponse(
|
||||
JsonResponse::JSON_SUCCESS,
|
||||
__u('Request sent'),
|
||||
[__u('You will receive an email to complete the request shortly.')]
|
||||
);
|
||||
} catch (Exception $e) {
|
||||
processException($e);
|
||||
|
||||
$this->addTracking();
|
||||
|
||||
$this->eventDispatcher->notifyEvent('exception', new Event($e));
|
||||
|
||||
return $this->returnJsonResponseException($e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user