Download the PHP package ashallendesign/redactable-models without Composer

On this page you can find all versions of the php package ashallendesign/redactable-models. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package redactable-models

Latest Version on Packagist Total Downloads PHP from Packagist GitHub license

Table of Contents

Overview

[!IMPORTANT]
This package is still in development. There may be some bugs and API changes before the first stable release.

A Laravel package that you can use to redact, obfuscate, or mask fields from your models in a consistent and easy way.

When building web applications, you'll often need to keep hold of old data for auditing or reporting purposes. But for data privacy and security reasons, you may want to redact the sensitive information from the data that you store. This way, you can keep the rows in the database, but without the sensitive information.

This package allows you to define which models and fields should be redacted, and how they should be redacted.

Installation

Requirements

The package has been developed and tested to work with the following minimum requirements:

Install the Package

You can install the package via Composer:

Usage

Defining Redactable Models

In order to make a model redactable, you need to add the AshAllenDesign\RedactableModels\Interfaces\Redactable interface to the model. This will enforce two new methods (redactable and redactionStrategy) that you need to implement.

Your model may look something like so:

The redactable method allows you to return an instance of Illuminate\Contracts\Database\Eloquent\Builder which defines the models that are redactable.

The redactionStrategy method allows you to return an instance of AshAllenDesign\RedactableModels\Interfaces\RedactionStrategy which defines how the fields should be redacted. We'll cover the available strategies further down.

As an example, if we wanted to redact the email and name fields from all App\Models\User models older than 30 days, we could do the following:

The model:redact Command

In order to automatically redact the fields on the models, you can use the package's model:redact command like so:

This will find all the models within your app's app/Models directory that implement the AshAllenDesign\RedactableModels\Interfaces\Redactable interface and redact the fields based on the defined redaction strategy and query.

You may want to set this to run on a schedule (such as on a daily basis) in your Laravel app's scheduler.

Redaction Strategies

The package ships with several strategies that you can use redacting fields:

ReplaceContents

The ReplaceContents strategy allows you to replace the contents of the fields with a specified value.

For example, if we wanted to replace the name and email fields, we could do the following:

Running this against a model would replace the name field with REDACTED and the email field with [email protected].

The ReplaceContents strategy also allows you to use a closure to define the replacement value. This can be useful if you want a bit more control over the redaction process. The closure should accept the model as an argument and have a void return type.

For example, say you want to replace the name field with name_ followed by their ID. You could do the following:

Imagine we have a user with ID 123 and a name of John Doe. Running the above code would replace the name field with name_123.

HashContents

The HashContents strategy allows you to MD5 hash the contents of the field.

This can be useful when you still want to be able to compare the redacted fields, but don't want to expose the original data.

For example, imagine you have an invitations table that contains an email field. You may want to find out how many unique email addresses have been invited, but you don't want to expose the email addresses themselves. You could do the following:

MaskContents

The MaskContents strategy allows you to mask the contents of the field with a specified character.

You can define the character to use for the mask, and how many characters from the start and end of the field to leave unmasked like so:

In the above example, the name field would be masked with * and the first 4 characters would be left unmasked. The email field would be masked with - and the first 2 and last 3 characters would be left unmasked.

This means if a user's name was "Ash Allen" and their email was "[email protected]", after redaction their name would be "****Allen" and their email would be "as---xample.com".

Custom Redaction Strategies

Although the package ships with several redaction strategies out of the box, you can create your own custom redaction strategies.

You just need to create a class that implements the AshAllenDesign\RedactableModels\Interfaces\RedactionStrategy interface. This method enforces an apply method which accepts the model and defines the redaction logic.

An example of a custom redaction strategy might look like so:

Manually Redacting Models

There may be times when you want to manually redact a model rather than using the model:redact command. To do this you can use the redactFields method that is available via the AshAllenDesign\RedactableModels\Traits\HasRedactableFields trait.

You can apply the trait to your model like so:

You can now use the redactFields method to redact the fields on the model like so:

By default, this will redact the fields using the strategy defined in the model's redactionStrategy method.

You can override this by passing a custom redaction strategy to the redactFields method like so:

Events

ModelRedacted

When a model is redacted, an AshAllenDesign\RedactableModels\Events\ModelRedacted event is fired that can be listened on.

Testing

To run the package's unit tests, run the following command:

Security

If you find any security related issues, please contact me directly at [email protected] to report it.

Contribution

If you wish to make any changes or improvements to the package, feel free to make a pull request.

Note: A contribution guide will be added soon.

Credits

Changelog

Check the CHANGELOG to get more information about the latest changes.

License

The MIT License (MIT). Please see License File for more information.

Support Me

If you've found this package useful, please consider buying a copy of Battle Ready Laravel to support me and my work.

Every sale makes a huge difference to me and allows me to spend more time working on open-source projects and tutorials.

To say a huge thanks, you can use the code BATTLE20 to get a 20% discount on the book.

👉 Get Your Copy!

Battle Ready Laravel


All versions of redactable-models with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
laravel/framework Version ^10.0|^11.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package ashallendesign/redactable-models contains the following files

Loading the files please wait ....