tourze / user-attribute-bundle
用户属性管理 Bundle,提供用户自定义属性的存储和管理功能
Installs: 30
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/tourze/user-attribute-bundle
Requires
- doctrine/collections: ^2.3
- doctrine/data-fixtures: ^2.0
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/doctrine-fixtures-bundle: ^4.0
- doctrine/orm: ^3.0
- doctrine/persistence: ^4.1
- easycorp/easyadmin-bundle: ^4
- knplabs/knp-menu: ^3.7
- nesbot/carbon: ^2.72 || ^3
- symfony/config: ^7.3
- symfony/dependency-injection: ^7.3
- symfony/doctrine-bridge: ^7.3
- symfony/framework-bundle: ^7.3
- symfony/http-kernel: ^7.3
- symfony/property-access: ^7.3
- symfony/routing: ^7.3
- symfony/security-bundle: ^7.3
- symfony/security-core: ^7.3
- symfony/serializer: ^7.3
- symfony/yaml: ^7.3
- tourze/arrayable: 1.*
- tourze/bundle-dependency: 1.*
- tourze/doctrine-ip-bundle: 1.0.*
- tourze/doctrine-snowflake-bundle: 1.0.*
- tourze/doctrine-timestamp-bundle: 1.0.*
- tourze/doctrine-user-bundle: 1.0.*
- tourze/easy-admin-menu-bundle: 1.0.*
- tourze/symfony-dependency-service-loader: 1.0.*
- tourze/user-service-contracts: 1.*
Requires (Dev)
This package is auto-updated.
Last update: 2025-11-13 18:14:19 UTC
README
A Symfony bundle for managing user attributes with key-value storage, IP tracking, and admin interface integration.
Features
- Key-value storage for user attributes
- User-specific attribute management
- IP tracking for operations
- Snowflake ID generation
- Admin menu integration
- REST API support
- Doctrine ORM integration
Installation
composer require tourze/user-attribute-bundle
Quick Start
- Add the bundle to your
config/bundles.php:
return [ // ... Tourze\UserAttributeBundle\UserAttributeBundle::class => ['all' => true], ];
- Run migrations to create the database table:
php bin/console doctrine:migrations:migrate
- The bundle will automatically register services and admin menu items.
Configuration
The bundle uses default configuration with the following features:
- Entity:
UserAttributewith IP tracking, timestamps, and blame tracking - Repository:
UserAttributeRepositoryfor data operations - Admin Menu: Automatic integration with EasyAdmin menu system
- Database Table:
biz_user_attributewith unique constraint onuser_idandname
Usage
Working with User Attributes
use Tourze\UserAttributeBundle\Entity\UserAttribute; use Tourze\UserAttributeBundle\Repository\UserAttributeRepository; // Create a new user attribute $attribute = new UserAttribute(); $attribute->setUser($user); $attribute->setName('preferred_language'); $attribute->setValue('en'); $attribute->setRemark('User preference for language'); $entityManager->persist($attribute); $entityManager->flush(); // Retrieve user attributes $repository = $entityManager->getRepository(UserAttribute::class); $attributes = $repository->findBy(['user' => $user]);
REST API Integration
The entity supports REST API serialization with groups:
// API response format $apiData = $attribute->retrieveApiArray(); // Returns: ['id' => 123, 'name' => 'preferred_language', 'value' => 'en'] // Admin interface format $adminData = $attribute->retrieveAdminArray(); // Returns: ['id' => 123, 'name' => 'preferred_language', 'value' => 'en', 'remark' => 'User preference']
Admin Menu Integration
The bundle automatically adds a "User Attribute Management" menu item under "User Module" in the admin interface.
Advanced Usage
Custom Repository Methods
You can extend the repository to add custom query methods:
// In your application class CustomUserAttributeRepository extends UserAttributeRepository { public function findAttributesByPrefix(UserInterface $user, string $prefix): array { return $this->createQueryBuilder('ua') ->where('ua.user = :user') ->andWhere('ua.name LIKE :prefix') ->setParameter('user', $user) ->setParameter('prefix', $prefix . '%') ->getQuery() ->getResult(); } }
Bulk Operations
For performance-critical operations, consider bulk updates:
// Bulk update user attributes $qb = $entityManager->createQueryBuilder() ->update(UserAttribute::class, 'ua') ->set('ua.value', ':newValue') ->where('ua.user = :user') ->andWhere('ua.name = :name') ->setParameter('newValue', $newValue) ->setParameter('user', $user) ->setParameter('name', $attributeName); $qb->getQuery()->execute();
Event Handling
You can listen to Doctrine events for custom logic:
use Doctrine\ORM\Events; use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener; #[AsDoctrineListener(event: Events::prePersist)] class UserAttributeListener { public function prePersist(UserAttribute $attribute): void { // Custom logic before saving if ($attribute->getName() === 'sensitive_data') { $attribute->setValue(hash('sha256', $attribute->getValue())); } } }
Dependencies
- PHP 8.4+
- Symfony 7.3+
- Doctrine ORM 3.0+
- KnpMenu 3.7+
- EasyAdmin 4.0+
- Various tourze/* packages for extended functionality
Contributing
Please see CONTRIBUTING.md for details on how to contribute to this project.
License
This bundle is released under the MIT License. See the LICENSE file for details.