From de2df5677858a1c82ab3a39fccf4896b4da254d4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Sun, 25 Dec 2022 22:47:10 +0100
Subject: [PATCH] Logovani kliku

---
 Dockerfile                       | 2 ++
 TODO                             | 1 -
 VERSION                          | 2 +-
 lib/PZ.pm                        | 1 +
 lib/PZ/Controller/Shortcut.pm    | 5 +++++
 lib/PZ/Schema/Result/Shortcut.pm | 4 ++++
 sql/migrations.sql               | 3 +++
 7 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index d6f1084..af287d8 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -28,6 +28,8 @@ RUN cpanm \
     Mojo::Redis \
     Mojo::JWT \
     Mojolicious::Plugin::Authentication \
+    Mojolicious::Plugin::BrowserDetect \
+    Mojolicious::Plugin::ForwardedFor \
     Mojolicious::Plugin::OpenAPI
 
 ADD . /opt/PZ
diff --git a/TODO b/TODO
index 5966e7f..30ecaf9 100644
--- a/TODO
+++ b/TODO
@@ -1,4 +1,3 @@
 - statistika
-- editace/smazani
 - loop detect
 - 302 nebo informacni stranka
diff --git a/VERSION b/VERSION
index 7dea76e..9084fa2 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.0.1
+1.1.0
diff --git a/lib/PZ.pm b/lib/PZ.pm
index 81221fb..96d5e13 100644
--- a/lib/PZ.pm
+++ b/lib/PZ.pm
@@ -20,6 +20,7 @@ sub startup {
     # Delka session
     $self->sessions->default_expiration($cfg->{session}{lifetime});
 
+    $self->plugin("ForwardedFor");
     $self->plugin('PZ::Helpers::Core');
     $self->plugin('PZ::Helpers::OIDC');
 
diff --git a/lib/PZ/Controller/Shortcut.pm b/lib/PZ/Controller/Shortcut.pm
index e46d6c2..be4db46 100644
--- a/lib/PZ/Controller/Shortcut.pm
+++ b/lib/PZ/Controller/Shortcut.pm
@@ -16,6 +16,11 @@ sub redirect ($c) {
         return;
     }
 
+    $shortcut->add_to_log_items({
+        ip => ($c->forwarded_for || $c->tx->remote_address),
+        ua => $c->req->headers->user_agent,
+    });
+
     $c->res->code($shortcut->code);
     $c->redirect_to($shortcut->url);
 }
diff --git a/lib/PZ/Schema/Result/Shortcut.pm b/lib/PZ/Schema/Result/Shortcut.pm
index d79d5fa..11d3022 100644
--- a/lib/PZ/Schema/Result/Shortcut.pm
+++ b/lib/PZ/Schema/Result/Shortcut.pm
@@ -37,5 +37,9 @@ __PACKAGE__->add_unique_constraint(
     'shortcut' => [qw(shortcut)]
 );
 
+__PACKAGE__->has_many(
+    log_items => 'PZ::Schema::Result::Log',
+    { 'foreign.shortcut_id' => 'self.id', },
+);
 1;
 
diff --git a/sql/migrations.sql b/sql/migrations.sql
index eda2436..9797780 100644
--- a/sql/migrations.sql
+++ b/sql/migrations.sql
@@ -44,3 +44,6 @@ create table "log" (
 alter table "shortcuts" alter column "code" set default 302;
 alter table "shortcuts" drop constraint "shortcuts_shortcut_key";
 create index "shortcuts_shortcut_idx" on "shortcuts" ("shortcut");
+
+-- 3 up
+alter table "log" add column "ua" text;
-- 
GitLab