Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support gdpr #48

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ jobs:
with:
enable_backend_testing: false
enable_phpstan: true
php_versions: '["8.0", "8.1", "8.2"]'
backend_directory: .
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
}
],
"require": {
"flarum/core": "^1.2.0"
"flarum/core": "^1.8.3"
},
"replace": {
"flagrow/terms": "*"
Expand Down Expand Up @@ -63,7 +63,8 @@
}
},
"require-dev": {
"flarum/phpstan": "*"
"flarum/phpstan": "*",
"blomstra/gdpr": "@beta"
},
"scripts": {
"analyse:phpstan": "phpstan analyse",
Expand Down
7 changes: 7 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace FoF\Terms;

use Blomstra\Gdpr\Extend\UserData;
use Flarum\Api\Controller\ShowForumController;
use Flarum\Api\Serializer\BasicUserSerializer;
use Flarum\Api\Serializer\ForumSerializer;
Expand Down Expand Up @@ -95,4 +96,10 @@
$data['fofTermsPolicies'] = $policies->all();
})
->addInclude('fofTermsPolicies'),

(new Extend\Conditional())
->whenExtensionEnabled('blomstra-gdpr', fn () => [
(new UserData())
->addType(Data\UserPolicyData::class),
]),
];
7 changes: 7 additions & 0 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,10 @@ fof-terms:

user_controls:
state_button: Terms accept state

blomstra-gdpr:
lib:
data:
userpolicydata:
export_description: Details about the user's acceptance of the terms
delete_description: Removes the user's acceptance of the terms
74 changes: 74 additions & 0 deletions src/Data/UserPolicyData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/*
* This file is part of fof/terms.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\Terms\Data;

use Blomstra\Gdpr\Data\Type;
use Carbon\Carbon;
use FoF\Terms\Policy;
use FoF\Terms\Repositories\PolicyRepository;
use Illuminate\Database\Eloquent\Collection;

class UserPolicyData extends Type
{
public function export(): ?array
{
$exportData = [];

$policyRepository = resolve(PolicyRepository::class);

$policyRepository->all()->each(function (Policy $policy) use (&$exportData) {
$exportData[] = ["terms/policy-{$policy->id}.json" => $this->encodeForExport($this->constructExportData($policy))];
});

return $exportData;
}

protected function constructExportData(Policy $policy): array
{
/**
* @var Collection $userPolicies
*
* @phpstan-ignore-next-line
*/
$userPolicies = $this->user->fofTermsPolicies->keyBy('id');

$accepted_at = $userPolicies->has($policy->id) ? Carbon::parse($userPolicies->get($policy->id)->pivot->accepted_at) : null;
$has_update = !$accepted_at || (($policy->terms_updated_at !== null) && $policy->terms_updated_at->gt($accepted_at));

return [
'name' => $policy->name,
'url' => $policy->url,
'created_at' => $policy->created_at,
'update_message' => $policy->update_message,
'terms_updated_at' => $policy->terms_updated_at,
'accepted_at' => $accepted_at,
'has_update' => $has_update,
'must_accept' => $has_update && !$this->user->can('postponeAccept', $policy),
];
}

public static function anonymizeDescription(): string
{
return self::deleteDescription();
}

public function anonymize(): void
{
$this->delete();
}

public function delete(): void
{
$policyRepository = resolve(PolicyRepository::class);
$policyRepository->declineAll($this->user);
}
}
5 changes: 5 additions & 0 deletions src/Repositories/PolicyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ public function acceptAll(User $user)
$this->getUserPolicyRelationship($user)->attach($relationship);
}

public function declineAll(User $user)
{
$this->getUserPolicyRelationship($user)->detach();
}

public function sorting(array $sorting)
{
foreach ($sorting as $i => $fieldId) {
Expand Down
Loading