engelsystem/src/Controllers/SettingsController.php

110 lines
2.8 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-23 20:41:02 +01:00
$this->auth = $auth;
$this->config = $config;
$this->log = $log;
$this->redirect = $redirector;
$this->response = $response;
}
/**
* @return Response
*/
public function password(): Response
{
return $this->response->withView(
'pages/settings/password.twig',
$this->getNotifications()
);
}
/**
* @return Response
*/
public function savePassword(Request $request): Response
{
$user = $this->auth->user();
if (
!$request->has('password')
|| !$this->auth->verifyPassword($user, $request->postData('password'))
) {
$this->addNotification('-> not OK. Please try again.', 'errors');
} elseif (strlen($request->postData('new_password')) < config('min_password_length')) {
$this->addNotification('Your password is to short (please use at least 6 characters).', 'errors');
} elseif ($request->postData('new_password') != $request->postData('new_password2')) {
$this->addNotification('Your passwords don\'t match.', 'errors');
} else {
$this->auth->setPassword($user, $request->postData('new_password'));
$this->addNotification('Password saved.');
$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(
'pages/settings/oauth.twig',
[
'providers' => $providers,
] + $this->getNotifications(),
);
}
}