diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2022-05-26 21:18:29 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2022-05-27 01:04:47 +0300 |
commit | cf0b9f036b3e3eb218610e7eeececda1320d9f50 (patch) | |
tree | 39e6d1853aecb3fb77036a941a4c6df12a0ce793 /localwebsite/handlers | |
parent | c3ed2483ea508141431be74f29f7c209271897cd (diff) |
auth
Diffstat (limited to 'localwebsite/handlers')
-rw-r--r-- | localwebsite/handlers/AuthHandler.php | 36 | ||||
-rw-r--r-- | localwebsite/handlers/MiscHandler.php | 15 | ||||
-rw-r--r-- | localwebsite/handlers/RequestHandler.php | 11 |
3 files changed, 54 insertions, 8 deletions
diff --git a/localwebsite/handlers/AuthHandler.php b/localwebsite/handlers/AuthHandler.php new file mode 100644 index 0000000..971f850 --- /dev/null +++ b/localwebsite/handlers/AuthHandler.php @@ -0,0 +1,36 @@ +<?php + +class AuthHandler extends RequestHandler { + + protected function before_dispatch(string $method, string $act) { + return null; + } + + public function GET_auth() { + list($error) = $this->input('error'); + $this->tpl->set(['error' => $error]); + $this->tpl->set_title('Авторизация'); + $this->tpl->render_page('auth.twig'); + } + + public function POST_auth() { + list($username, $password) = $this->input('username, password'); + + $result = users::validatePassword($username, $password); + if (!$result) { + debugError('invalid login attempt: '.$_SERVER['REMOTE_ADDR'].', '.$_SERVER['HTTP_USER_AGENT'].", username=$username, password=$password"); + redirect('/auth/?error='.urlencode('неверный логин или пароль')); + } + + auth::setToken(pwhash($password)); + redirect('/'); + } + + public function GET_deauth() { + if (auth::id()) + auth::logout(); + + redirect('/'); + } + +} diff --git a/localwebsite/handlers/MiscHandler.php b/localwebsite/handlers/MiscHandler.php index ef4d8ef..b7c312a 100644 --- a/localwebsite/handlers/MiscHandler.php +++ b/localwebsite/handlers/MiscHandler.php @@ -8,11 +8,6 @@ class MiscHandler extends RequestHandler $this->tpl->render_page('index.twig'); } - public function GET_phpinfo() { - phpinfo(); - exit; - } - public function GET_sensors_page() { global $config; @@ -68,9 +63,9 @@ class MiscHandler extends RequestHandler $hls_opts['debug'] = true; $this->tpl->add_external_static('js', 'https://cdn.jsdelivr.net/npm/hls.js@latest'); - - $hls_host = is_callable($config['cam_hls_host']) ? $config['cam_hls_host']() : $config['cam_hls_host']; - $hls_proto = is_callable($config['cam_hls_proto']) ? $config['cam_hls_proto']() : $config['cam_hls_proto']; + + $hls_host = config::get('cam_hls_host'); + $hls_proto = config::get('cam_hls_proto'); $this->tpl->set([ 'hls_host' => $hls_host, @@ -89,4 +84,8 @@ class MiscHandler extends RequestHandler print_r($_SERVER); } + public function GET_phpinfo() { + phpinfo(); + } + }
\ No newline at end of file diff --git a/localwebsite/handlers/RequestHandler.php b/localwebsite/handlers/RequestHandler.php index 2fffdc0..136a23e 100644 --- a/localwebsite/handlers/RequestHandler.php +++ b/localwebsite/handlers/RequestHandler.php @@ -15,6 +15,12 @@ class RequestHandler extends request_handler { $this->tpl->add_static('polyfills.js'); $this->tpl->add_static('app.js'); $this->tpl->add_static('app.css'); + + if (auth::id()) { + $this->tpl->set_global([ + 'auth_user' => auth::$authorizedUser + ]); + } } public function dispatch(string $act) { @@ -38,4 +44,9 @@ class RequestHandler extends request_handler { ajax_error('unknown act "'.$act.'"', 404); } + + protected function before_dispatch(string $method, string $act) { + if (config::get('auth_need') && !auth::id()) + redirect('/auth/'); + } }
\ No newline at end of file |