Newer
Older

Yassine Doghri
committed
<?php

Yassine Doghri
committed

Yassine Doghri
committed
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Controllers\Admin;
use App\Models\UserModel;
use CodeIgniter\HTTP\RedirectResponse;

Yassine Doghri
committed
use Config\Services;

Yassine Doghri
committed
class MyAccountController extends BaseController

Yassine Doghri
committed
{
public function index(): string

Yassine Doghri
committed
{
return view('admin/my_account/view');
}
public function changePassword(): string

Yassine Doghri
committed
{

Yassine Doghri
committed
return view('admin/my_account/change_password');
}
public function attemptChange(): RedirectResponse

Yassine Doghri
committed
{
$auth = Services::authentication();

Yassine Doghri
committed
$userModel = new UserModel();

Yassine Doghri
committed
// Validate here first, since some things,
// like the password, can only be validated properly here.
$rules = [
'password' => 'required',
'new_password' => 'required|strong_password|differs[password]',

Yassine Doghri
committed
];
if (! $this->validate($rules)) {

Yassine Doghri
committed
return redirect()
->back()
->withInput()

Yassine Doghri
committed
->with('errors', $userModel->errors());

Yassine Doghri
committed
}
$credentials = [
'email' => user()
->email,

Yassine Doghri
committed
'password' => $this->request->getPost('password'),
];
if (! $auth->validate($credentials)) {

Yassine Doghri
committed
return redirect()
->back()
->withInput()
->with('error', lang('MyAccount.messages.wrongPasswordError'));

Yassine Doghri
committed
}
user()
->password = $this->request->getPost('new_password');

Yassine Doghri
committed
if (! $userModel->update(user_id(), user())) {

Yassine Doghri
committed
return redirect()
->back()
->withInput()

Yassine Doghri
committed
->with('errors', $userModel->errors());

Yassine Doghri
committed
}
// Success!
return redirect()

Yassine Doghri
committed
->with('message', lang('MyAccount.messages.passwordChangeSuccess'));