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
|
|
|
/**
|
|
|
|
* @copyright 2020 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
use App\Models\UserModel;
|
2021-05-14 17:59:35 +00:00
|
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
2020-07-31 16:05:10 +00:00
|
|
|
use Config\Services;
|
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
|
|
|
{
|
|
|
|
return view('admin/my_account/view');
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
|
2020-07-10 12:20:25 +00:00
|
|
|
return view('admin/my_account/change_password');
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function attemptChange(): RedirectResponse
|
2020-07-10 12:20:25 +00:00
|
|
|
{
|
|
|
|
$auth = Services::authentication();
|
2020-08-04 11:25:22 +00:00
|
|
|
$userModel = new UserModel();
|
2020-07-10 12:20:25 +00:00
|
|
|
|
|
|
|
// Validate here first, since some things,
|
|
|
|
// like the password, can only be validated properly here.
|
|
|
|
$rules = [
|
|
|
|
'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()
|
2020-08-04 11:25:22 +00:00
|
|
|
->with('errors', $userModel->errors());
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$credentials = [
|
2021-05-19 16:35:13 +00:00
|
|
|
'email' => user()
|
|
|
|
->email,
|
2020-07-10 12:20:25 +00:00
|
|
|
'password' => $this->request->getPost('password'),
|
|
|
|
];
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! $auth->validate($credentials)) {
|
2020-07-10 12:20:25 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
2020-08-14 18:27:57 +00:00
|
|
|
->with('error', lang('MyAccount.messages.wrongPasswordError'));
|
2020-07-10 12:20:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
user()
|
|
|
|
->password = $this->request->getPost('new_password');
|
2020-07-10 12:20:25 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! $userModel->update(user_id(), user())) {
|
2020-07-10 12:20:25 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
2020-08-04 11:25:22 +00:00
|
|
|
->with('errors', $userModel->errors());
|
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
|
|
|
}
|
|
|
|
}
|