Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mikamatto/reservedvaluesbundle
A Symfony bundle to restrict reserved values in form fields such as usernames or slugs, with support for exact matches and regular expressions.
https://github.com/mikamatto/reservedvaluesbundle
reservedslug reservedusernames reservedwords restrictedwords symfony symfony-bundle
Last synced: about 1 month ago
JSON representation
A Symfony bundle to restrict reserved values in form fields such as usernames or slugs, with support for exact matches and regular expressions.
- Host: GitHub
- URL: https://github.com/mikamatto/reservedvaluesbundle
- Owner: mikamatto
- License: mit
- Created: 2024-10-22T15:40:43.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2024-10-22T15:41:16.000Z (2 months ago)
- Last Synced: 2024-11-22T18:11:52.352Z (about 1 month ago)
- Topics: reservedslug, reservedusernames, reservedwords, restrictedwords, symfony, symfony-bundle
- Language: PHP
- Homepage:
- Size: 4.88 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# ReservedValuesBundle
**ReservedValuesBundle** is a Symfony bundle that allows you to restrict the use of reserved values across different fields by adding custom validation rules. You can specify exact restrictions or use patterns to block specific values, applicable to any field, such as usernames, slugs, or any other user-defined attributes that require validation.
## Features
- Block specific exact usernames (e.g., `admin`, `support`).
- Block usernames based on patterns (e.g., usernames starting with `admin` or `support`).
- Support for multiple keys to apply a different set of restrictions to different fields.## Installation
### 1. Require the Bundle
You can install the bundle using Composer:
```bash
composer require mikamatto/reserved-values-bundle
```### 2. Enable the Bundle
If you're using Symfony Flex, the bundle will be enabled automatically. Otherwise, you may need to manually enable it in your `config/bundles.php`:
```php
return [
// Other bundles...
Mikamatto\ReservedValuesBundle\ReservedValuesBundle::class => ['all' => true],
];
```### 3. Configuration
You need to create a configuration file for the bundle at `config/packages/reserved_values.yaml`.
The format of the configuration file is as follows:
```yaml
reserved_values:
username:
exact:
- _error
- _wdt
- _profiler
- account
- configuration
- contact
- posts
- dmca
- login
- logout
- page
- password
- register
- scheduler
- settings
- slugger
- sfw
- userpatterns:
- '^admin.*' # Bans 'admin', 'administrator', 'admin123', etc.
- '^support.*' # Bans 'support', 'support1', 'support-team', etc.slug:
exact:
- example-slug
- another-slugpatterns:
- '^draft-.*' # Bans 'draft-post', 'draft-article', etc.
```#### Configuration Options
- **key**: This is a custom identifier for the field (e.g., username, slug) to which the restrictions apply.
- **exact**: This section contains a list of values that are strictly forbidden for the specified key. Users attempting to use any of these values will receive a validation error.
- **patterns**: This section allows you to specify regular expressions for matching values that should be restricted. Any value that matches one of the defined patterns will also trigger a validation error.Make sure to define at least one of the options under each key in your configuration file. If both sections are left empty for a key, no values will be restricted for that field.
### 4. Applying the Validation to an Entity
To apply the `ReservedValues` validation to your entity (for example, User), you need to include the constraint in your entity class. You can do this either with annotations or attributes. Here’s how to do it with both methods:
#### Using Annotations
```php
namespace App\Entity;use Mikamatto\ReservedValuesBundle\Validator\Constraints\ReservedValues;
use Symfony\Component\Validator\Constraints as Assert;class User
{
/**
* @Assert\NotBlank
* @ReservedValues(key="username")
*/
private $username;// Other properties and methods...
}
```
#### Using Attributes (Symfony 6.0+)If you prefer using PHP attributes instead of annotations, you can do so like this:
```php
namespace App\Entity;use Mikamatto\ReservedValuesBundle\Validator\Constraints\ReservedValues;
use Symfony\Component\Validator\Constraints as Assert;class User
{
#[Assert\NotBlank]
#[ReservedValues(key: "username")]
private string $username;// Other properties and methods...
}
```In the examples above, both methods trigger validation whenever a User instance is validated, ensuring that the specified usernames are restricted according to your configuration.