engelsystem/src/Controllers/SettingsController.php

134 lines
3.3 KiB
PHP
Raw Normal View History

2020-11-15 18:47:30 +01:00
<?php
namespace Engelsystem\Controllers;
use Engelsystem\Config\Config;
use Engelsystem\Http\Exceptions\HttpNotFound;
use Engelsystem\Http\Response;
2020-11-23 20:41:02 +01:00
use Engelsystem\Http\Redirector;
use Engelsystem\Http\Request;
use Engelsystem\Helpers\Authenticator;
use Psr\Log\LoggerInterface;
2020-11-15 18:47:30 +01:00
class SettingsController extends BaseController
{
use HasUserNotifications;
2020-11-23 20:41:02 +01:00
/** @var Authenticator */
protected $auth;
2020-11-15 18:47:30 +01:00
/** @var Config */
protected $config;
2020-11-23 20:41:02 +01:00
/** @var LoggerInterface */
protected $log;
/** @var Redirector */
protected $redirect;
2020-11-15 18:47:30 +01:00
/** @var Response */
protected $response;
2020-11-23 20:41:02 +01:00
/** @var string[] */
protected $permissions = [
'user_settings',
];
2020-11-15 18:47:30 +01:00
/**
* @param Config $config
* @param Response $response
*/
public function __construct(
2020-11-23 20:41:02 +01:00
Authenticator $auth,
2020-11-15 18:47:30 +01:00
Config $config,
2020-11-23 20:41:02 +01:00
LoggerInterface $log,
Redirector $redirector,
2020-11-15 18:47:30 +01:00
Response $response
) {
2020-11-24 17:27:21 +01:00
$this->auth = $auth;
$this->config = $config;
$this->log = $log;
$this->redirect = $redirector;
$this->response = $response;
2020-11-23 20:41:02 +01:00
}
/**
* @return Response
*/
public function password(): Response
{
return $this->response->withView(
2020-11-24 17:27:21 +01:00
'pages/settings/password',
[
'settings_menu' => $this->settingsMenu(),
'min_length' => config('min_password_length')
] + $this->getNotifications()
2020-11-23 20:41:02 +01:00
);
}
/**
2020-11-24 17:27:21 +01:00
* @param Request $request
2020-11-23 20:41:02 +01:00
* @return Response
*/
public function savePassword(Request $request): Response
{
$user = $this->auth->user();
2020-11-24 17:27:21 +01:00
$minLength = config('min_password_length');
$data = $this->validate($request, [
'password' => 'required',
'new_password' => 'required|min:' . $minLength,
'new_password2' => 'required'
]);
if (!$this->auth->verifyPassword($user, $data['password'])) {
$this->addNotification('auth.password.error', 'errors');
} elseif ($data['new_password'] != $data['new_password2']) {
$this->addNotification('validation.password.confirmed', 'errors');
2020-11-23 20:41:02 +01:00
} else {
2020-11-24 17:27:21 +01:00
$this->auth->setPassword($user, $data['new_password']);
2020-11-23 20:41:02 +01:00
2020-11-24 17:27:21 +01:00
$this->addNotification('settings.password.success');
2020-11-23 20:41:02 +01:00
$this->log->info('User set new password.');
}
return $this->redirect->to('/settings/password');
2020-11-15 18:47:30 +01:00
}
/**
* @return Response
*/
public function oauth(): Response
{
$providers = $this->config->get('oauth');
if (empty($providers)) {
throw new HttpNotFound();
}
return $this->response->withView(
2020-11-24 17:27:21 +01:00
'pages/settings/oauth',
2020-11-15 18:47:30 +01:00
[
2020-11-24 17:27:21 +01:00
'settings_menu' => $this->settingsMenu(),
2020-11-15 18:47:30 +01:00
'providers' => $providers,
] + $this->getNotifications(),
);
}
2020-11-24 17:27:21 +01:00
/**
* @return array
*/
public function settingsMenu(): array
{
$menu = [
url('/user-settings') => 'settings.profile',
url('/settings/password') => 'settings.password'
];
if (!empty(config('oauth'))) {
$menu[url('/settings/oauth')] = 'settings.oauth';
}
return $menu;
}
2020-11-15 18:47:30 +01:00
}