Skip to content

Commit

Permalink
add privacy option to automatically delete submissions older than x d…
Browse files Browse the repository at this point in the history
…ays (#104)

* switch to asdf

* remove old unused test

* add auto delete submissions command

* add mariadb to github action

* fix ci command

* update phpunit config

* fix nginx image

* add scheduler to prod dockerfile

* migrate phpunit config

* update base nginx docker image

* add settings for auto delete

* center delete button in submissions view
  • Loading branch information
PhilReinking committed Oct 26, 2023
1 parent d8e3b6e commit df9c4c5
Show file tree
Hide file tree
Showing 18 changed files with 383 additions and 161 deletions.
24 changes: 19 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,30 @@ jobs:
tests:
runs-on: ubuntu-latest

services:
mariadb.test:
image: "mariadb:10"
env:
MYSQL_ROOT_PASSWORD: "password"
MYSQL_ROOT_HOST: "%"
MYSQL_DATABASE: "input"
MYSQL_USER: "sail"
MYSQL_PASSWORD: "password"
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
ports:
- 3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: "8.1"

- uses: actions/checkout@v3

- uses: actions/setup-node@v3
with:
node-version: '18'
node-version: "18"

- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.dev.example', '.env');"
Expand All @@ -42,8 +56,8 @@ jobs:
- name: Execute tests (Unit and Feature tests) via PestPHP
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
DB_PORT: ${{ job.services['mariadb.test'].ports[3306] }}
DB_HOST: 127.0.0.1
run: vendor/bin/pest

vitest:
Expand All @@ -55,7 +69,7 @@ jobs:
- name: "Setup Node"
uses: actions/setup-node@v3
with:
node-version: '18'
node-version: "18"

- name: Install NPM Dependencies
run: npm ci
Expand Down
1 change: 0 additions & 1 deletion .nvmrc

This file was deleted.

1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 18.17.1
12 changes: 9 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM trafex/php-nginx as php_base
FROM trafex/php-nginx:3.3.0 as php_base

LABEL Maintainer="Philipp Reinking <philipp@deck9.co>" Description="Input is a no-code application to create simple & clean forms."
LABEL org.opencontainers.image.licenses="GNU Affero General Public License v3.0"
Expand Down Expand Up @@ -29,7 +29,7 @@ RUN apk add --no-cache \

COPY nginx.conf /etc/nginx/nginx.conf
COPY nginx.default.conf /etc/nginx/conf.d/default.conf
COPY php.conf.ini /etc/php81/conf.d/99-input.ini
COPY php.conf.ini /etc/php82/conf.d/99-input.ini

USER nobody
WORKDIR /var/www/html
Expand All @@ -39,9 +39,15 @@ COPY --chown=nobody . .

RUN composer install --optimize-autoloader --no-interaction --no-progress

# Remove Composer Cache & Script since we do not need it any more
USER root

# Remove Composer Cache & Script since we do not need it any more
RUN rm -rf /root/.composer /usr/bin/composer

# Copy Supervisor Config for Scheduler
COPY scheduler.conf /tmp/scheduler.conf
RUN cat /tmp/scheduler.conf >> /etc/supervisor/conf.d/supervisord.conf && rm -f /tmp/scheduler.conf

USER nobody

# ---
Expand Down
53 changes: 53 additions & 0 deletions app/Console/Commands/AutoDeleteSubmissions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Console\Commands;

use App\Models\Form;
use Illuminate\Console\Command;

class AutoDeleteSubmissions extends Command
{
/**
* The number of submissions cleaned.
* @var int
*/
protected $cleaned = 0;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'input:auto-delete-submissions';

/**
* The console command description.
*
* @var string
*/
protected $description = 'This command will delete old submissions if auto delete is enabled for the form using the retention days specified on the form.';

/**
* Execute the console command.
*/
public function handle(): void
{
Form::has('formSessions')->lazyById()->each(function (Form $form) {
if ($form->is_auto_delete_enabled === false) {
return;
}

$form->formSessions()->lazyById()->each(function ($session) use ($form) {
$retentionDays = $form->data_retention_days;

if ($session->updated_at->diffInDays(now()) > $retentionDays) {
$session->delete();
$this->cleaned++;
}

});
});

$this->info("Cleaned {$this->cleaned} submissions.");
}
}
4 changes: 3 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use App\Console\Commands\AutoDeleteSubmissions;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
Expand All @@ -13,7 +14,7 @@ class Kernel extends ConsoleKernel
* @var array
*/
protected $commands = [
//
AutoDeleteSubmissions::class,
];

/**
Expand All @@ -25,6 +26,7 @@ class Kernel extends ConsoleKernel
protected function schedule(Schedule $schedule)
{
$schedule->command('cache:prune-stale-tags')->hourly();
$schedule->command('input:auto-delete-submissions')->twiceDaily();
}

/**
Expand Down
6 changes: 6 additions & 0 deletions app/Http/Controllers/Api/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public function update(Request $request, Form $form)
{
$this->authorize('update', $form);

$request->validate([
'is_auto_delete_enabled' => 'boolean',
'data_retention_days' => 'required_if:is_auto_delete_enabled,true|integer|min:1',
]);

$form->update(
$request->only(
'name',
Expand Down Expand Up @@ -75,6 +80,7 @@ public function update(Request $request, Form $form)
'privacy_link',
'legal_notice_link',
'data_retention_days',
'is_auto_delete_enabled',
'show_cta_link',
'show_social_links',
)
Expand Down
16 changes: 3 additions & 13 deletions app/Models/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Form extends BaseModel
'show_social_links' => 'boolean',
'show_privacy_link' => 'boolean',
'has_data_privacy' => 'boolean',
'is_auto_delete_enabled' => 'boolean',
'user_id' => 'integer',
'published_at' => 'datetime',
'deleted_at' => 'datetime'
Expand Down Expand Up @@ -82,6 +83,7 @@ class Form extends BaseModel
'eoc_text',
'eoc_headline',
'data_retention_days',
'is_auto_delete_enabled',
'legal_notice_link',
'privacy_link',
'cta_label',
Expand Down Expand Up @@ -172,18 +174,6 @@ public function actionBlocksCount()
})->count();
}

public function responsesCount()
{
$result = $this->formSessionResponses()
->select(DB::raw('count(*) as response_count'))
->groupBy('form_block_id')
->orderBy('response_count', 'DESC')
->limit(1)
->first();

return $result ? $result->response_count : 0;
}

public function hasImage($type)
{
$fieldname = $type . '_path';
Expand Down Expand Up @@ -265,7 +255,7 @@ public function getInitialsAttribute()
' ',
collect($strings)
->take(2)
->map(fn ($item) => substr($item, 0, 2))
->map(fn($item) => substr($item, 0, 2))
->toArray()
);
}
Expand Down
22 changes: 22 additions & 0 deletions app/Models/FormBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,26 @@ public function submit(FormSession $session, array $data)
]);
}
}

public function getSubmitPayload(string|array $payload)
{
if (is_array($payload)) {
$action = collect($payload)->map(function ($index) {
$action = $this->formBlockInteractions[$index];
return [
'actionId' => $action->uuid,
'payload' => $action->label
];
})->toArray();
} elseif (is_string($payload)) {
$action = [
'actionId' => $this->formBlockInteractions[0]->uuid,
'payload' => $payload
];
}

return [
$this->uuid => $action
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('forms', function (Blueprint $table) {
$table->boolean('is_auto_delete_enabled')->default(false)->after('data_retention_days');
});
}
};
Loading

0 comments on commit df9c4c5

Please sign in to comment.