承接 zepekegno/obfuscate-id-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zepekegno/obfuscate-id-bundle

最新稳定版本:v1.1.0

Composer 安装命令:

composer require zepekegno/obfuscate-id-bundle

包简介

Symfony bundle to securely obfuscate and deobfuscate entity IDs in URLs. Provides attributes and Twig filters to handle obfuscation seamlessly within Symfony applications.

README 文档

README

Latest Stable Version Total Downloads License PHP Version Require

Introduction

ObfuscateIdBundle is a Symfony extension that enables obfuscation of identifiers in URLs and API responses. This new version introduces several improvements, including:

  • Automatic obfuscation of IDs when loading fixtures with Doctrine
  • Support for dynamic properties via the #[ObfuscateId] annotation
  • Improved Twig filters
  • Persistent default key generation when no key is provided
  • Preserved compatibility with both entity-based and raw ID controller actions

Installation

Install the bundle via Composer:

composer require zepekegno/obfuscate-id-bundle

Then, activate it in bundles.php if not automatically enabled:

return [
    Zepekegno\ObfuscateIdBundle\ObfuscateIdBundle::class => ['all' => true],
];

Configuration

Add the following configuration in config/packages/obfuscate_id.yaml:

obfuscate_id:
    secret_key: '%env(OBFUSCATE_ID_SECRET)%'

Define the environment variables in .env:

OBFUSCATE_ID_SECRET="your_secret_key"

If OBFUSCATE_ID_SECRET is not defined, the bundle will automatically generate a persistent default secret key at installation time. This ensures a consistent encryption key across application restarts and deployments.

The persistent IV will also be generated.

Usage

1. Obfuscation in Controllers

Obfuscation is now automatic, meaning you no longer need to manually annotate route parameters. The bundle will automatically deobfuscate IDs in controller actions:

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;

#[Route('/user/{id}', name: 'user_show')]
public function show(int $id): Response
{
    return new Response("Deobfuscated ID: " . $id);
}

For entity-based routes, it automatically resolves the entity:

#[Route('/user/{id}', name: 'user_show')]
public function show(User $user): Response
{
    return new Response("User: " . $user->getId());
}

You may also use the annotation explicitly to control deobfuscation:

use Zepekegno\ObfuscateIdBundle\ValueResolver\Attribute\ObfuscateId;

#[Route('/user/{id}', name: 'user_show')]
public function show(#[ObfuscateId(entity: User::class)] User $user): Response
{
    return new Response("User: " . $user->getId());
}

2. Automatic Obfuscation of IDs in Doctrine

When an entity is loaded, its ID is automatically obfuscated. Add the #[Obfuscate] annotation to the relevant property:

use Zepekegno\ObfuscateIdBundle\Attribute\Obfuscate;

#[ORM\Entity]
class User
{
    #[ORM\Id, ORM\GeneratedValue, ORM\Column(type: 'integer')]
    #[Obfuscate] // This property will be automatically obfuscated
    private ?int $id = null;
}

3. Using Obfuscation in Twig

Use the obfuscate filter to mask an identifier in a template:

<a href="{{ path('user_show', { id: user.id|obfuscate }) }}">View User</a>

Doctrine Events

The bundle listens for the following events:

  • postLoad → Automatically applies obfuscation to loaded entities.

Development

Install dependencies:

## Contributing

1. Fork the repository
2. Clone your fork
3. Create your feature branch: `git checkout -b feature/YourFeature`
4. Commit your changes: `git commit -am 'Add new feature'`
5. Push to the branch: `git push origin feature/YourFeature`
6. Create a new Pull Request

### Guidelines

- Ensure test coverage for your changes.
- Follow PSR coding standards.
- Prefer small, focused pull requests.

Support

If you encounter any issues, open an issue on GitHub. For feature requests, please provide a detailed description of the use case and expected behavior.

统计信息

  • 总下载量: 49
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-28