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


README

PHP Version License Build Status Code Coverage

English | 中文

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

  1. Add the bundle to your config/bundles.php:
return [
    // ...
    Tourze\UserAttributeBundle\UserAttributeBundle::class => ['all' => true],
];
  1. Run migrations to create the database table:
php bin/console doctrine:migrations:migrate
  1. The bundle will automatically register services and admin menu items.

Configuration

The bundle uses default configuration with the following features:

  • Entity: UserAttribute with IP tracking, timestamps, and blame tracking
  • Repository: UserAttributeRepository for data operations
  • Admin Menu: Automatic integration with EasyAdmin menu system
  • Database Table: biz_user_attribute with unique constraint on user_id and name

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.