Skip to content

Commit

Permalink
feat: support gdpr (#48)
Browse files Browse the repository at this point in the history
* feat: support gdpr

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
imorland and StyleCIBot authored Nov 1, 2023
1 parent 38de513 commit e8893c4
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 2 deletions.
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

0 comments on commit e8893c4

Please sign in to comment.