From 9446d0280af15d108dc2e57962dc4a1c7d8b2616 Mon Sep 17 00:00:00 2001 From: Varakh Date: Thu, 5 Apr 2018 12:30:06 +0200 Subject: [PATCH] Use session for selecting a server --- composer.json | 3 +- config/ACL.php | 3 +- config/routes.php | 7 +++- data/locale/en.yml | 19 +++++++---- public/index.php | 22 ++++++++++-- src/Control/Actions/AbstractAction.php | 7 ++++ src/Control/Actions/AuthLogoutAction.php | 3 +- src/Control/Actions/LogsAction.php | 10 ++++-- src/Control/Actions/ProfileAction.php | 5 +++ src/Control/Actions/ServerSelectAction.php | 20 +++++++++++ src/View/material/layout/menu.twig | 37 ++++++++++++++++++-- src/View/material/logs.twig | 6 ++-- src/View/material/servers.twig | 39 ++++------------------ 13 files changed, 127 insertions(+), 54 deletions(-) create mode 100644 src/Control/Actions/ServerSelectAction.php diff --git a/composer.json b/composer.json index c03a29f..18c3a0c 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "jeremykendall/slim-auth": "dev-slim-3.x", "par0noid/ts3admin": "^1.0", "planetteamspeak/ts3-php-framework": "^1.1", - "nesbot/carbon": "^1.25" + "nesbot/carbon": "^1.25", + "bryanjhv/slim-session": "^3.5" }, "config": { "bin-dir": "bin/" diff --git a/config/ACL.php b/config/ACL.php index ed22807..122821e 100644 --- a/config/ACL.php +++ b/config/ACL.php @@ -28,11 +28,12 @@ class ACL extends \Zend\Permissions\Acl\Acl '/instance', '/instance/edit', - '/logs', + '/logs[/{sid}]', '/servers', '/servers/create', '/servers/{sid}', + '/servers/select/{sid}', '/servers/delete/{sid}', '/servers/start/{sid}', '/servers/stop/{sid}', diff --git a/config/routes.php b/config/routes.php index 6caa253..5062c5d 100644 --- a/config/routes.php +++ b/config/routes.php @@ -56,7 +56,7 @@ $app->get('/profile/credentials', ProfileCredentialsChangeAction::class); $container[LogsAction::class] = function ($container) { return new LogsAction($container); }; -$app->get('/logs', LogsAction::class); +$app->get('/logs[/{sid}]', LogsAction::class); // instance $container[InstanceAction::class] = function ($container) { @@ -80,6 +80,11 @@ $container[ServerCreateAction::class] = function ($container) { }; $app->post('/servers/create', ServerCreateAction::class); +$container[ServerSelectAction::class] = function ($container) { + return new ServerSelectAction($container); +}; +$app->get('/servers/select/{sid}', ServerSelectAction::class); + $container[ServerDeleteAction::class] = function ($container) { return new ServerDeleteAction($container); }; diff --git a/data/locale/en.yml b/data/locale/en.yml index 50e2ba0..f3d3d6d 100644 --- a/data/locale/en.yml +++ b/data/locale/en.yml @@ -53,13 +53,22 @@ validate_valid_url: "The %field% field is required to be a valid URL." # menu menu.instance: "Instance" menu.servers: "Servers" -menu.logs: "Logs" +menu.logs: "Instance Log" menu.profile: "Profile" +menu.servers.info: "Info" +menu.servers.online: "Online" +menu.servers.channels: "Channels" +menu.servers.clients: "Clients" +menu.servers.groups: "Groups" +menu.servers.bans: "Bans" +menu.servers.complains: "Complains" +menu.servers.logs: "Log" + # titles instance.title: "Instance" servers.title: "Servers" -logs.title: "Latest 100 Logs" +logs.title: "Latest 100 Log Entries" server_info.title: "Server Info" online.title: "Online Clients" online_info.title: "Online Info" @@ -94,11 +103,7 @@ instance_edit.edited.success: "Edited instance." servers.noneselected: "Cannot find server." servers.start: "Start" servers.stop: "Stop" -servers.channels: "Channels" -servers.clients: "Clients" -servers.groups: "Groups" -servers.bans: "Bans" -servers.complains: "Complains" +servers.select: "Select" servers.delete: "Delete" servers.started.success: "Started virtual server %sid%." servers.stopped.success: "Stopped virtual server %sid%." diff --git a/public/index.php b/public/index.php index f792629..48e0883 100644 --- a/public/index.php +++ b/public/index.php @@ -75,14 +75,22 @@ $container['authAdapter'] = function ($container) { $adapter = new TSAuthAdapter(getenv('teamspeak_default_host'), getenv('teamspeak_default_query_port'), $container['logger'], $container['ts']); return $adapter; }; - $container['acl'] = function () { return new ACL(); }; - $container->register(new \JeremyKendall\Slim\Auth\ServiceProvider\SlimAuthProvider()); $app->add($app->getContainer()->get('slimAuthRedirectMiddleware')); +// session +$app->add(new \Slim\Middleware\Session([ + 'name' => 'dummy_session', + 'autorefresh' => true, + 'lifetime' => '1 hour' +])); +$container['session'] = function () { + return new \SlimSession\Helper; +}; + // view $container['flash'] = function () { return new Slim\Flash\Messages; @@ -137,6 +145,16 @@ $container['view'] = function ($container) use ($app) { return $res; })); + // session exist + $view->getEnvironment()->addFunction(new Twig_SimpleFunction('session_exists', function($key) use ($container) { + return $container['session']->exists($key); + })); + + // session get + $view->getEnvironment()->addFunction(new Twig_SimpleFunction('session_get', function($key) use ($container) { + return $container['session']->get($key); + })); + // flash $view['flash'] = $container['flash']; diff --git a/src/Control/Actions/AbstractAction.php b/src/Control/Actions/AbstractAction.php index a67d153..b423100 100644 --- a/src/Control/Actions/AbstractAction.php +++ b/src/Control/Actions/AbstractAction.php @@ -41,6 +41,11 @@ abstract class AbstractAction */ protected $translator; + /** + * @var \SlimSession\Helper + */ + protected $session; + /** * @var TSInstance */ @@ -58,6 +63,8 @@ abstract class AbstractAction $this->auth = $container->get('authenticator'); $this->acl = $container->get('acl'); $this->translator = $container->get('translator'); + + $this->session = $container->get('session'); $this->ts = $container->get('ts'); } diff --git a/src/Control/Actions/AuthLogoutAction.php b/src/Control/Actions/AuthLogoutAction.php index 89be6af..203cf06 100644 --- a/src/Control/Actions/AuthLogoutAction.php +++ b/src/Control/Actions/AuthLogoutAction.php @@ -11,8 +11,9 @@ final class AuthLogoutAction extends AbstractAction $this->ts->login($this->auth->getIdentity()['user'], $this->auth->getIdentity()['password']); $this->ts->getInstance()->logout(); - $this->auth->logout(); + $this->session->clear(); + return $response->withRedirect('/login'); } } \ No newline at end of file diff --git a/src/Control/Actions/LogsAction.php b/src/Control/Actions/LogsAction.php index c76650b..fbe6257 100644 --- a/src/Control/Actions/LogsAction.php +++ b/src/Control/Actions/LogsAction.php @@ -7,9 +7,15 @@ final class LogsAction extends AbstractAction { public function __invoke(Request $request, Response $response, $args) { - $this->ts->login($this->auth->getIdentity()['user'], $this->auth->getIdentity()['password']); + $sid = $args['sid']; - $dataResult = $this->ts->getInstance()->logView(100, 1, 1); + $this->ts->login($this->auth->getIdentity()['user'], $this->auth->getIdentity()['password']); + if (empty($sid)) { + $dataResult = $this->ts->getInstance()->logView(100, 1, 1); + } else { + $selectResult = $this->ts->getInstance()->selectServer($sid, 'serverId'); + $dataResult = $this->ts->getInstance()->logView(100, 1, 0); + } // render GET $this->view->render($response, 'logs.twig', [ diff --git a/src/Control/Actions/ProfileAction.php b/src/Control/Actions/ProfileAction.php index eac7497..4f57333 100644 --- a/src/Control/Actions/ProfileAction.php +++ b/src/Control/Actions/ProfileAction.php @@ -8,6 +8,11 @@ final class ProfileAction extends AbstractAction public function __invoke(Request $request, Response $response, $args) { $this->ts->login($this->auth->getIdentity()['user'], $this->auth->getIdentity()['password']); + + if ($this->session->exists('sid')) { + $selectResult = $this->ts->getInstance()->selectServer($this->session->get('sid'), 'serverId'); + } + $whoisResult = $this->ts->getInstance()->whoAmI(); // render GET diff --git a/src/Control/Actions/ServerSelectAction.php b/src/Control/Actions/ServerSelectAction.php new file mode 100644 index 0000000..93fde77 --- /dev/null +++ b/src/Control/Actions/ServerSelectAction.php @@ -0,0 +1,20 @@ +ts->login($this->auth->getIdentity()['user'], $this->auth->getIdentity()['password']); + $selectResult = $this->ts->getInstance()->selectServer($sid, 'serverId'); + + $this->session->set('sid', $sid); + + $this->flash->addMessage('success', $this->translator->trans('done')); + return $response->withRedirect('/servers'); + } +} \ No newline at end of file diff --git a/src/View/material/layout/menu.twig b/src/View/material/layout/menu.twig index 35c0d4a..4045d1b 100644 --- a/src/View/material/layout/menu.twig +++ b/src/View/material/layout/menu.twig @@ -4,15 +4,46 @@
  • {% trans %}menu.instance{% endtrans %}
  • - -
  • {% trans %}menu.servers{% endtrans %}
  • -
  • {% trans %}menu.logs{% endtrans %}
  • + +
  • {% trans %}menu.servers{% endtrans %}
  • +
    + + {% if session_exists('sid') %} +
    + +
  • {% trans %}menu.servers.info{% endtrans %}
  • +
    + +
  • {% trans %}menu.servers.online{% endtrans %}
  • +
    + +
  • {% trans %}menu.servers.clients{% endtrans %}
  • +
    + +
  • {% trans %}menu.servers.groups{% endtrans %}
  • +
    + +
  • {% trans %}menu.servers.channels{% endtrans %}
  • +
    + +
  • {% trans %}menu.servers.bans{% endtrans %}
  • +
    + +
  • {% trans %}menu.servers.complains{% endtrans %}
  • +
    + +
  • {% trans %}menu.servers.logs{% endtrans %}
  • +
    +
    + {% endif %} +
  • {% trans %}menu.profile{% endtrans %}
  • +
  • {% trans %}menu.logout{% endtrans %}
  • diff --git a/src/View/material/logs.twig b/src/View/material/logs.twig index 844a8a2..11082d3 100644 --- a/src/View/material/logs.twig +++ b/src/View/material/logs.twig @@ -2,9 +2,9 @@ {% block content %}

    {{ title }}

    -
    +    
    {% for log in data %} - {{ log.l }} +

    {{ log.l }}

    {% endfor %} -
    + {% endblock %} \ No newline at end of file diff --git a/src/View/material/servers.twig b/src/View/material/servers.twig index 6df387d..88737ab 100644 --- a/src/View/material/servers.twig +++ b/src/View/material/servers.twig @@ -5,7 +5,6 @@

    {% trans %}servers.h.details{% endtrans %}

    {% if data|length > 0 %} -
    {% include 'table.twig' with {'data': data, 'filters': [ {'key': 'virtualserver_uptime', 'apply': 'timeInSeconds'}, @@ -15,6 +14,12 @@ {'key': 'virtualserver_clientsonline', 'uri': '/online', 'uri_param': 'virtualserver_id'} ], 'additional_links': [ + { + 'header_label': 'servers.select'|trans, + 'label': 'check_circle', + 'uri': '/servers/select', + 'uri_param': 'virtualserver_id' + }, { 'header_label': 'servers.start'|trans, 'label': 'arrow_forward', @@ -27,36 +32,6 @@ 'uri': '/servers/stop', 'uri_param': 'virtualserver_id' }, - { - 'header_label': 'servers.clients'|trans, - 'label': 'account_circle', - 'uri': '/clients', - 'uri_param': 'virtualserver_id' - }, - { - 'header_label': 'servers.groups'|trans, - 'label': 'group', - 'uri': '/groups', - 'uri_param': 'virtualserver_id' - }, - { - 'header_label': 'servers.channels'|trans, - 'label': 'settings_voice', - 'uri': '/channels', - 'uri_param': 'virtualserver_id' - }, - { - 'header_label': 'servers.bans'|trans, - 'label': 'account_box', - 'uri': '/bans', - 'uri_param': 'virtualserver_id' - }, - { - 'header_label': 'servers.complains'|trans, - 'label': 'report_problem', - 'uri': '/complains', - 'uri_param': 'virtualserver_id' - }, { 'header_label': 'servers.delete'|trans, 'label': 'delete', @@ -69,8 +44,6 @@ {% include 'no_entities.twig' %} {% endif %} -
    -

    {% trans %}servers.h.create{% endtrans %}

    {% include 'form.twig' with { 'additional_forms': [