Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

User Bundle Laravel Package

demontpx/user-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require demontpx/user-bundle
    

    Ensure your project uses Symfony 5.x/6.x (FOSUserBundle dependency).

  2. Basic Configuration Add demontpx_user.yaml to config/packages/:

    demontpx_user:
        roles:
            ROLE_ADMIN: Administrator
            ROLE_USER: Regular User
    
  3. First Use Case

    • Create a user via fixtures (see below) or manually:
      use Demontpx\UserBundle\Entity\User;
      
      $user = new User();
      $user->setUsername('testuser');
      $user->setPlainPassword('testuser'); // Auto-set by bundle
      $user->setRoles(['ROLE_USER']);
      $entityManager->persist($user);
      
  4. Testing Use UserWebTestCase for functional tests:

    use Demontpx\UserBundle\Tests\UserWebTestCase;
    
    class MyTest extends UserWebTestCase {
        public function testAdminAccess() {
            $this->loadUserFromFixture('admin'); // Requires test/security.yml config
            $this->assertTrue($this->client->getResponse()->isSuccessful());
        }
    }
    

Implementation Patterns

Core Workflows

  1. Role Management

    • Define roles in demontpx_user.yaml (e.g., ROLE_ADMIN, ROLE_GROUP_MANAGER).
    • Assign roles dynamically:
      $user->setRoles(['ROLE_ADMIN', 'ROLE_GROUP_MANAGER']);
      
  2. Fixtures for Testing

    • Configure test fixtures in config/packages/test/demontpx_user.yml:
      demontpx_user:
          fixtures:
              admin: { roles: [ROLE_ADMIN] }
              editor: { roles: [ROLE_EDITOR], email: 'editor@example.com' }
      
    • Load fixtures in tests:
      $this->loadUserFromFixture('admin');
      
  3. Authentication

    • Leverage Symfony’s security system with FOSUserBundle’s built-in features:
      # config/packages/security.yaml
      security:
          providers:
              fos_userbundle:
                  id: fos_user.user_provider.username_email
          firewalls:
              main:
                  form_login:
                      provider: fos_userbundle
      
  4. Frontend Integration

    • Import SCSS:
      // assets/app.scss
      @import 'user-bundle';
      
    • Enhance select2 elements (e.g., role selectors) via JavaScript:
      $('.select2').select2();
      
  5. Custom User Fields Extend the User entity (inherits from FOSUserBundle’s User):

    // src/Entity/User.php
    namespace App\Entity;
    
    use Demontpx\UserBundle\Entity\User as BaseUser;
    
    class User extends BaseUser {
        private $customField;
    
        // Add getters/setters
    }
    

Gotchas and Tips

Pitfalls

  1. Password Handling

    • The bundle auto-sets passwords to match usernames in fixtures/tests. Override in production to avoid security risks:
      # config/packages/test/demontpx_user.yml
      demontpx_user:
          fixtures:
              admin: { roles: [ROLE_ADMIN], password: 'SecurePass123!' }
      
  2. Firewall Configuration

    • UserWebTestCase requires http_basic in test/security.yml. Skip this in production or use form_login instead.
  3. Entity Inheritance

    • Extending User may break bundle features if not done carefully. Prefer composition for complex extensions:
      class User extends BaseUser {
          private $profile;
      
          public function __construct() {
              parent::__construct();
              $this->profile = new UserProfile();
          }
      }
      
  4. Select2 Dependencies

    • The select2 class hint assumes jQuery and Select2 are loaded. Verify assets are enqueued:
      {{ encore_entry_link_tags('app') }}
      <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
      

Debugging Tips

  1. Fixture Loading

    • Debug missing fixtures with:
      $this->assertTrue($this->loadUserFromFixture('admin')->isLoaded());
      
  2. Role Assignment

    • Check role assignments in the database or via:
      dump($user->getRoles()); // Returns array of roles
      
  3. SCSS Compilation

    • Ensure assets/user-bundle.scss is compiled by Encore:
      // webpack.config.js
      Encore
          .addEntry('app', './assets/app.js')
          .addEntry('user-bundle', './assets/user-bundle.scss')
          .enableSassLoader();
      

Extension Points

  1. Custom User Provider Override the default provider in security.yaml:

    security:
        providers:
            app_user_provider:
                id: App\Security\UserProvider
    
  2. Event Listeners Subscribe to FOSUserBundle events (e.g., UserRegistered):

    // src/EventListener/UserListener.php
    namespace App\EventListener;
    
    use Demontpx\UserBundle\Event\UserEvent;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    
    class UserListener implements EventSubscriberInterface {
        public static function getSubscribedEvents() {
            return [
                UserEvent::REGISTERED => 'onUserRegistered',
            ];
        }
    
        public function onUserRegistered(UserEvent $event) {
            $user = $event->getUser();
            // Custom logic (e.g., send welcome email)
        }
    }
    
  3. API Integration Use the User entity with API Platform or similar:

    # config/api/platform.yaml
    api_platform:
        formats:
            jsonld:
                mime_types: ['application/ld+json']
        collection:
            operations:
                - { method: 'GET', path: '/users', controller: 'api.user_action' }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui