2020-07-10 12:20:25 +00:00
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-07-10 12:20:25 +00:00
|
|
|
/**
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2020 Ad Aures
|
2020-07-10 12:20:25 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
2022-10-15 11:22:08 +00:00
|
|
|
namespace Modules\Auth\Controllers;
|
2020-07-10 12:20:25 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
2024-04-28 16:39:01 +00:00
|
|
|
use CodeIgniter\Shield\Entities\User;
|
2022-10-15 11:22:08 +00:00
|
|
|
use Modules\Admin\Controllers\BaseController;
|
2020-07-10 12:20:25 +00:00
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
class MyAccountController extends BaseController
|
2020-07-10 12:20:25 +00:00
|
|
|
{
|
2021-05-14 17:59:35 +00:00
|
|
|
public function index(): string
|
2020-07-10 12:20:25 +00:00
|
|
|
{
|
2024-12-17 15:06:08 +00:00
|
|
|
$this->setHtmlHead(lang('MyAccount.info'));
|
2021-09-03 16:34:06 +00:00
|
|
|
return view('my_account/view');
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function changePassword(): string
|
2020-07-10 12:20:25 +00:00
|
|
|
{
|
2020-08-14 18:27:57 +00:00
|
|
|
helper('form');
|
|
|
|
|
2024-12-17 15:06:08 +00:00
|
|
|
$this->setHtmlHead(lang('MyAccount.changePassword'));
|
2021-09-03 16:34:06 +00:00
|
|
|
return view('my_account/change_password');
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
2024-12-18 16:05:25 +00:00
|
|
|
public function changeAction(): RedirectResponse
|
2020-07-10 12:20:25 +00:00
|
|
|
{
|
|
|
|
$rules = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'password' => 'required',
|
2020-08-14 18:27:57 +00:00
|
|
|
'new_password' => 'required|strong_password|differs[password]',
|
2020-07-10 12:20:25 +00:00
|
|
|
];
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! $this->validate($rules)) {
|
2020-07-10 12:20:25 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
2023-10-03 16:21:08 +00:00
|
|
|
->with('errors', $this->validator->getErrors());
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 15:42:52 +00:00
|
|
|
$validData = $this->validator->getValidated();
|
|
|
|
|
2022-10-15 11:22:08 +00:00
|
|
|
// check credentials with the old password if logged in without magic link
|
2020-07-10 12:20:25 +00:00
|
|
|
$credentials = [
|
2022-10-15 11:22:08 +00:00
|
|
|
'email' => auth()
|
|
|
|
->user()
|
2021-05-19 16:35:13 +00:00
|
|
|
->email,
|
2023-08-29 15:42:52 +00:00
|
|
|
'password' => $validData['password'],
|
2020-07-10 12:20:25 +00:00
|
|
|
];
|
|
|
|
|
2022-10-15 11:22:08 +00:00
|
|
|
$validCreds = auth()
|
|
|
|
->check($credentials);
|
|
|
|
|
|
|
|
if (! $validCreds->isOK()) {
|
|
|
|
return redirect()->back()
|
2020-08-14 18:27:57 +00:00
|
|
|
->with('error', lang('MyAccount.messages.wrongPasswordError'));
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
2024-04-28 16:39:01 +00:00
|
|
|
$user = auth()
|
|
|
|
->user();
|
2020-07-10 12:20:25 +00:00
|
|
|
|
2024-04-28 16:39:01 +00:00
|
|
|
if ($user instanceof User) {
|
|
|
|
// set new password to user
|
|
|
|
$user->password = $validData['new_password'];
|
|
|
|
|
|
|
|
$userModel = auth()
|
|
|
|
->getProvider();
|
|
|
|
$userModel->save($user);
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Success!
|
|
|
|
return redirect()
|
2020-08-14 18:27:57 +00:00
|
|
|
->back()
|
2020-08-04 11:25:22 +00:00
|
|
|
->with('message', lang('MyAccount.messages.passwordChangeSuccess'));
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
}
|