Giter Site home page Giter Site logo

laravel-gdpr-compliance's Introduction

StyleCI

GDPR compliant data handling with ease

This package helps you get compliant with GDPR;

Article 7: Conditions for consent
Article 17: Right to be forgotten
Article 20: Right to data portability

Table of contents

Dependencies

  • PHP >= 7.0.0
  • Laravel >= 5.5

Installation

First, install the package via the Composer package manager:

$ composer require dialect/laravel-gdpr-compliance

After installing the package, you should publish the configuration file:

$ php artisan vendor:publish --provider="Dialect\Gdpr\GdprServiceProvider" --tag=gdpr-config

Configuration

GDPR Consent

The package includes a way for users to sign a GDPR-agreement. This will redirect the user to the agreement on the specified routes until the user has agreed to the new terms.

To add the agreement functionality:

  1. Publish the middleware:
    php artisan vendor:publish --provider="Dialect\Gdpr\GdprServiceProvider"
  2. Add 'gdpr.terms' => \App\Http\Middleware\RedirectIfUnansweredTerms::class
    to the $routeMiddleware middlewaregroup in app/Http/Kernel like so:
        protected $routeMiddleware = [
            'gdpr.terms' => \App\Http\Middleware\RedirectIfUnansweredTerms::class,
        ];
  3. Add the middleware to the routes that you want to check (normally the routes where auth is used):
        Route::group(['middleware' => ['auth', 'gdpr.terms']], function () {
           Route::get('/', 'HomeController@index');
        });
  4. Add the fields to $fillable in the User model:
        protected $fillable = [
            'last_activity',
            'accepted_gdpr',
            'isAnonymized'
        ];
  5. Change the Agreement text to your particular needs in resources/views/gdpr/message.blade.php

Portability

Add the Portable trait to the model model you want to be able to port:

namespace App;

use Dialect\Gdpr\Portable;

class User extends Model
{
    use Portable;
}

Anonymizability

Add the Anonymizable trait to the model you want to be able to anonymize:

namespace App;

use Dialect\Gdpr\Anonymizable;

class User extends Model
{
    use Anonymizable;
}

Automatic Anonymization of inactive users

The package adds a scheduled job intended to anonymize the User model automatically when the user has been inactive for a specific time. To specify the time, edit the ttl setting in the published config.
To activate this feature:

  1. Add the command to the schedule function in app/Console/Kernel.php like so:

        protected function schedule(Schedule $schedule)
        {
            $schedule->command('gdpr:anonymizeInactiveUsers')->daily();
        }
  2. Add the class to the $commands array in the same file like so:

```php
    protected $commands = [
        \Dialect\Gdpr\Commands\AnonymizeInactiveUsers::class,
    ];
```

Configuring Anonymizable Data

On the model, set gdprAnonymizableFields by adding the fields you want to anonymize on the model, you can also set up attribute-like functions on your model to supply replacement data.
If you have a unique-constraint on your model, you should use this. If no value is supplied, a default string from settings will be used.

/**
 * Using the default string from config.
 */
protected $gdprAnonymizableFields = [
    'name', 
    'email'
];
/**
 * Using replacement strings.
 */
protected $gdprAnonymizableFields = [
    'name' => 'Anonymized User', 
    'email' => '[email protected]'
];
namespace App;

use Dialect\Gdpr\Anonymizable;

class User extends Model
{
    use Anonymizable;

    protected $gdprAnonymizableFields = [
        'email'
    ];
    
    /**
    * Using getAnonymized{column} to return anonymizable data
    */
    public function getAnonymizedEmail()
    {
        return random_bytes(10);
    }
}

Recursive Anonymization

If the model has related models with fields that needs to be anonymized at the same time, add the related models to $gdprWith. On the related models. add the Anonymizable trait and specify the fields with $gdprAnonymizableFields like so:

class Order extends Model
{
    use Anonymizable;

	protected $guarded = [];
	protected $table = 'orders';
	protected $gdprWith = ['product'];
    protected $gdprAnonymizableFields = ['buyer' => 'Anonymized Buyer'];
    
	public function product()
	{
		return $this->belongsTo(Product::class);
	}
	public function customer()
	{
		return $this->belongsTo(Customer::class);
	}
}
class Customer extends Model
{
    use Anonymizable;
	protected $guarded = [];
	protected $table = 'customers';
	protected $gdprWith = ['orders'];

	protected $gdprAnonymizableFields = ['name' => 'Anonymized User'];

	public function orders()
	{
		return $this->hasMany(Order::class);
	}
}

Calling $customer->anonymize(); will also change the buyer-field on the related orders.

Configuring Portable Data

By default, the entire toArray form of the App\User model will be made available for download. If you would like to customize the downloadable data, you may override the toPortableArray() method on the model:

use Dialect\Gdpr\Portable;

class User extends Model
{
    use Portable;

    /**
     * Get the GDPR compliant data portability array for the model.
     *
     * @return array
     */
    public function toPortableArray()
    {
        $array = $this->toArray();

        // Customize array...

        return $array;
    }
}

Lazy Eager Loading Relationships

You may need to include a relationship in the data that will be made available for download. To do so, add a $gdprWith property to your App\User model:

use Dialect\Gdpr\Portable;

class User extends Model
{
    use Portable;

    /**
     * The relations to include in the downloadable data.
     *
     * @var array
     */
    protected $gdprWith = ['posts'];
}

Hiding Attributes

You may wish to limit the attributes, such as passwords, that are included in the downloadable data. To do so, add a $gdprHidden property to your App\User model:

use Dialect\Gdpr\Portable;

class User extends Model
{
    use Portable;

    /**
     * The attributes that should be hidden for the downloadable data.
     *
     * @var array
     */
    protected $gdprHidden = ['password'];
}

Alternatively, you may use the $gdprVisible property to define a white-list of attributes that should be included in the data that will be made available for download. All other attributes will be hidden when the model is converted:

use Dialect\Gdpr\Portable;

class User extends Moeld
{
    use Portable;

    /**
     * The attributes that should be visible in the downloadable data.
     *
     * @var array
     */
    protected $gdprVisible = ['name', 'email'];
}

Usage

This package exposes an endpoint at /gdpr/download. Only authenticated users should be able to access the routes. Your application should make a POST call, containing the currently authenticated user's password, to this endpoint. The re-authentication is needed to prevent information leakage.

Encryption

Before using encryption, you must set a key option in your config/app.php configuration file. If this value is not properly set, all encrypted values will be insecure.

You may encrypt/decrypt attributes on the fly using the EncryptsAttributes trait on any model. The trait expects the $encrypted property to be filled with attribute keys:

use Dialect\Gdpr\EncryptsAttributes;

class User extends Model
{
    use EncryptsAttributes;

    /**
     * The attributes that should be encrypted and decrypted on the fly.
     *
     * @var array
     */
    protected $encrypted = ['ssnumber'];
}

If all fields are encrypted, the model can be returned in decrypted state as an array or collection:

$decryptedArray = $this->decryptToArray();

$decryptedCollection = $this->customer->decryptToCollection();

Anonymization

To anonymize a model you call anonymize() on it:

class SomeController extends Controller
{
    public function anonymizeAGroupOfUsers() {
    	$users = User::where('last_activity', '<=', carbon::now()->submonths(config('gdpr.settings.ttl')))->get();
    	foreach ($users as $user) {
            $user->anonymize();
        }
    }
}

Tests

After installation you can run the package tests from your laravel-root folder with phpunit vendor/Dialect/gdpr

Security Vulnerabilities

If you discover a security vulnerability within this project, please send an e-mail to Dialect via [email protected]. All security vulnerabilities will be promptly addressed.

Credit

sander3: Author of the original package used as a startingpoint

License

This package is open-source software licensed under the MIT license.

laravel-gdpr-compliance's People

Contributors

dsbilling avatar karlhalonen avatar kristoffertennivaara avatar lewawan avatar pjrola avatar sander3 avatar stromgren avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

laravel-gdpr-compliance's Issues

Following relations upstream

It looks like in the orignal repo we can follow relationships by having protected $gdprWith defined on the relationship model. This doesn't seem to work here.

On the user model i have
protected $gdprWith = ['cars']

On the cars model i have
protected $gdprWith = ['insurance']

When exporting the user model I just get cars and not the insruance details. it would be ideal if we could export in this way rather than defining, the user model like,

protected $gdprWith = ['cars', 'cars.insurance']

artisan route:list gives an error

When I run php artisan route:list , I'm getting an error:
ReflectionException : Class Dialect\Gdpr\Http\Controllers\GdprController does not exist

Package not installing with Laravel 5.7

It seems like this package does not support Laravel 5.7, any input?

Application ready! Build something amazing.

post@DanielRTRD MINGW32 /e/Users/RTRD/Bitbucket/laravel57test
$ composer require dialect/laravel-gdpr-compliance
Using version ^1.4 for dialect/laravel-gdpr-compliance
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for dialect/laravel-gdpr-compliance ^1.4 -> satisfiable by dialect/laravel-gdpr-compliance[v1.4.0].
    - Conclusion: remove laravel/framework v5.7.12
    - Conclusion: don't install laravel/framework v5.7.12
    - dialect/laravel-gdpr-compliance v1.4.0 requires illuminate/support 5.5.x|5.6.x -> satisfiable by illuminate/support[5.5.x-dev, 5.6.x-dev, v5.5.0, v5.5.16, v5.5.17, v5.5.2, v5.5.28, v5.5.33, v5.5.34, v5.5.35, v5.5.36, v5.5.37, v5.5.39, v5.5.40, v5.5.41, v5.5.43, v5.5.44, v5.6.0, v5.6.1, v5.6.10, v5.6.11, v5.6.12, v5.6.13, v5.6.14, v5.6.15, v5.6.16, v5.6.17, v5.6.19, v5.6.2, v5.6.20, v5.6.21, v5.6.22, v5.6.23, v5.6.24, v5.6.25, v5.6.26, v5.6.27, v5.6.28, v5.6.29, v5.6.3, v5.6.30, v5.6.31, v5.6.32, v5.6.33, v5.6.34, v5.6.35, v5.6.36, v5.6.37, v5.6.38, v5.6.39, v5.6.4, v5.6.5, v5.6.6, v5.6.7, v5.6.8, v5.6.9].

Then if I require illuminate/support it outputs:

post@DanielRTRD MINGW32 /e/Users/RTRD/Bitbucket/laravel57test
$ composer require dialect/laravel-gdpr-compliance
Using version ^1.4 for dialect/laravel-gdpr-compliance
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for dialect/laravel-gdpr-compliance ^1.4 -> satisfiable by dialect/laravel-gdpr-compliance[v1.4.0].
    - Conclusion: remove laravel/framework v5.7.12
    - Conclusion: don't install laravel/framework v5.7.12
    - dialect/laravel-gdpr-compliance v1.4.0 requires illuminate/support 5.5.x|5.6.x -> satisfiable by illuminate/support[5.5.x-dev, 5.6.x-dev].
    - illuminate/support 5.5.x-dev conflicts with laravel/framework[v5.7.12].
    - don't install illuminate/support 5.6.x-dev|don't install laravel/framework v5.7.12
    - Installation request for laravel/framework (locked at v5.7.12, required as 5.7.*) -> satisfiable by laravel/framework[v5.7.12].


Installation failed, reverting ./composer.json to its original content.

Redirect to original page from termsAccepted

The termsAccepted function redirects automatically to / after the user accepts or denies the GDPR terms, I've tried setting the url in the session when showing the terms but it doesn't seem to work. Has anyone tried to get the accept/deny to return to the orginal url?

When anonymizing a user I dont want to anonymize relations

When anonymizing a user it is anonymizing relations, I dont want that. I prefer to delete it with another function. Is there a way to skip that when running anonymize on a user?

I have added $gdprWith relations for the download, but don't need them to anonymize.

README instructions contain a typo

In the README file, it is stated that

Using getAnonynomized{column} to return anonymizable data

public function getAnonynomizedEmail()
{
return 'your_return_value';
}

There is a typo in getAnonyNOmized{column}, it should be getAnonymized{column} for it to work

Just in case anyone ran into it not working properly and didn't know why

Download associated files

Is there a good way to extend this to download all user files (either along with associated models or as a zipped bundle)

Over API

Hi

Thanks for this package.

Any idea how to implement this over Rest API?

Laravel 8 Support

Hi.

Any plan to upgrade it to Laravel 8?

Waiting to see this to be compatible with Laravel 8.

  • Adi

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.