diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2019-07-21 13:42:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-21 13:42:19 +0200 |
commit | 43fa21f6553bb9e46f00d0768eab56e74c842bb4 (patch) | |
tree | 00df255fcd7c3777855bee27967117440c4add8c /src/Controllers | |
parent | 4f60daa29568a43ab1da761d89124308dc7b37e7 (diff) | |
parent | 1e16f4c47ec15ffea41e1fa612d205b8db2a16d1 (diff) |
Merge branch 'master' into docker
Diffstat (limited to 'src/Controllers')
-rw-r--r-- | src/Controllers/AuthController.php | 86 | ||||
-rw-r--r-- | src/Controllers/BaseController.php | 4 | ||||
-rw-r--r-- | src/Controllers/CreditsController.php | 13 | ||||
-rw-r--r-- | src/Controllers/Metrics/Controller.php | 21 | ||||
-rw-r--r-- | src/Controllers/Metrics/MetricsEngine.php | 18 |
5 files changed, 131 insertions, 11 deletions
diff --git a/src/Controllers/AuthController.php b/src/Controllers/AuthController.php index cdaee167..55dd56b0 100644 --- a/src/Controllers/AuthController.php +++ b/src/Controllers/AuthController.php @@ -2,8 +2,14 @@ namespace Engelsystem\Controllers; +use Carbon\Carbon; +use Engelsystem\Helpers\Authenticator; +use Engelsystem\Http\Request; use Engelsystem\Http\Response; use Engelsystem\Http\UrlGeneratorInterface; +use Engelsystem\Models\User\User; +use Illuminate\Support\Arr; +use Illuminate\Support\Collection; use Symfony\Component\HttpFoundation\Session\SessionInterface; class AuthController extends BaseController @@ -17,17 +23,91 @@ class AuthController extends BaseController /** @var UrlGeneratorInterface */ protected $url; - public function __construct(Response $response, SessionInterface $session, UrlGeneratorInterface $url) - { + /** @var Authenticator */ + protected $auth; + + /** @var array */ + protected $permissions = [ + 'login' => 'login', + 'postLogin' => 'login', + ]; + + /** + * @param Response $response + * @param SessionInterface $session + * @param UrlGeneratorInterface $url + * @param Authenticator $auth + */ + public function __construct( + Response $response, + SessionInterface $session, + UrlGeneratorInterface $url, + Authenticator $auth + ) { $this->response = $response; $this->session = $session; $this->url = $url; + $this->auth = $auth; + } + + /** + * @return Response + */ + public function login(): Response + { + return $this->showLogin(); + } + + /** + * @param bool $showRecovery + * @return Response + */ + protected function showLogin($showRecovery = false): Response + { + $errors = Collection::make(Arr::flatten($this->session->get('errors', []))); + $this->session->remove('errors'); + + return $this->response->withView( + 'pages/login', + ['errors' => $errors, 'show_password_recovery' => $showRecovery] + ); + } + + /** + * Posted login form + * + * @param Request $request + * @return Response + */ + public function postLogin(Request $request): Response + { + $data = $this->validate($request, [ + 'login' => 'required', + 'password' => 'required', + ]); + + $user = $this->auth->authenticate($data['login'], $data['password']); + + if (!$user instanceof User) { + $this->session->set('errors', $this->session->get('errors', []) + ['auth.not-found']); + + return $this->showLogin(true); + } + + $this->session->invalidate(); + $this->session->set('user_id', $user->id); + $this->session->set('locale', $user->settings->language); + + $user->last_login_at = new Carbon(); + $user->save(['touch' => false]); + + return $this->response->redirectTo('news'); } /** * @return Response */ - public function logout() + public function logout(): Response { $this->session->invalidate(); diff --git a/src/Controllers/BaseController.php b/src/Controllers/BaseController.php index cbc00931..655ed759 100644 --- a/src/Controllers/BaseController.php +++ b/src/Controllers/BaseController.php @@ -2,8 +2,12 @@ namespace Engelsystem\Controllers; +use Engelsystem\Http\Validation\ValidatesRequest; + abstract class BaseController { + use ValidatesRequest; + /** @var string[]|string[][] A list of Permissions required to access the controller or certain pages */ protected $permissions = []; diff --git a/src/Controllers/CreditsController.php b/src/Controllers/CreditsController.php index b2805b84..ade97649 100644 --- a/src/Controllers/CreditsController.php +++ b/src/Controllers/CreditsController.php @@ -3,6 +3,7 @@ namespace Engelsystem\Controllers; use Engelsystem\Config\Config; +use Engelsystem\Helpers\Version; use Engelsystem\Http\Response; class CreditsController extends BaseController @@ -13,14 +14,19 @@ class CreditsController extends BaseController /** @var Response */ protected $response; + /** @var Version */ + protected $version; + /** * @param Response $response * @param Config $config + * @param Version $version */ - public function __construct(Response $response, Config $config) + public function __construct(Response $response, Config $config, Version $version) { $this->config = $config; $this->response = $response; + $this->version = $version; } /** @@ -30,7 +36,10 @@ class CreditsController extends BaseController { return $this->response->withView( 'pages/credits.twig', - ['credits' => $this->config->get('credits')] + [ + 'credits' => $this->config->get('credits'), + 'version' => $this->version->getVersion(), + ] ); } } diff --git a/src/Controllers/Metrics/Controller.php b/src/Controllers/Metrics/Controller.php index f6ea3967..ffb2a41b 100644 --- a/src/Controllers/Metrics/Controller.php +++ b/src/Controllers/Metrics/Controller.php @@ -4,6 +4,7 @@ namespace Engelsystem\Controllers\Metrics; use Engelsystem\Config\Config; use Engelsystem\Controllers\BaseController; +use Engelsystem\Helpers\Version; use Engelsystem\Http\Exceptions\HttpForbidden; use Engelsystem\Http\Request; use Engelsystem\Http\Response; @@ -26,25 +27,31 @@ class Controller extends BaseController /** @var Stats */ protected $stats; + /** @var Version */ + protected $version; + /** * @param Response $response * @param MetricsEngine $engine * @param Config $config * @param Request $request * @param Stats $stats + * @param Version $version */ public function __construct( Response $response, MetricsEngine $engine, Config $config, Request $request, - Stats $stats + Stats $stats, + Version $version ) { $this->config = $config; $this->engine = $engine; $this->request = $request; $this->response = $response; $this->stats = $stats; + $this->version = $version; } /** @@ -68,6 +75,18 @@ class Controller extends BaseController $data = [ $this->config->get('app_name') . ' stats', + 'info' => [ + 'type' => 'gauge', + 'help' => 'About the environment', + [ + 'labels' => [ + 'os' => PHP_OS_FAMILY, + 'php' => implode('.', [PHP_MAJOR_VERSION, PHP_MINOR_VERSION]), + 'version' => $this->version->getVersion(), + ], + 'value' => 1, + ], + ], 'users' => [ 'type' => 'gauge', ['labels' => ['state' => 'incoming'], 'value' => $this->stats->newUsers()], diff --git a/src/Controllers/Metrics/MetricsEngine.php b/src/Controllers/Metrics/MetricsEngine.php index 1e0f6957..21ae8fd0 100644 --- a/src/Controllers/Metrics/MetricsEngine.php +++ b/src/Controllers/Metrics/MetricsEngine.php @@ -9,13 +9,13 @@ class MetricsEngine implements EngineInterface /** * Render metrics * - * @example $data = ['foo' => [['labels' => ['foo'=>'bar'], 'value'=>42]], 'bar'=>123] - * * @param string $path * @param mixed[] $data * @return string + * + * @example $data = ['foo' => [['labels' => ['foo'=>'bar'], 'value'=>42]], 'bar'=>123] */ - public function get($path, $data = []): string + public function get(string $path, array $data = []): string { $return = []; foreach ($data as $name => $list) { @@ -52,7 +52,7 @@ class MetricsEngine implements EngineInterface * @param string $path * @return bool */ - public function canRender($path): bool + public function canRender(string $path): bool { return $path == '/metrics'; } @@ -60,8 +60,8 @@ class MetricsEngine implements EngineInterface /** * @param string $name * @param array|mixed $row - * @see https://prometheus.io/docs/instrumenting/exposition_formats/ * @return string + * @see https://prometheus.io/docs/instrumenting/exposition_formats/ */ protected function formatData($name, $row): string { @@ -135,4 +135,12 @@ class MetricsEngine implements EngineInterface $value ); } + + /** + * Does nothing as shared data will onyly result in unexpected behaviour + * + * @param string|mixed[] $key + * @param mixed $value + */ + public function share($key, $value = null) { } } |