locky42/leopard-user

A library for handling user authentication and management.

Maintainers

Package info

github.com/locky42/leopard-user

pkg:composer/locky42/leopard-user

Statistics

Installs: 3

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-03-17 20:24 UTC

This package is auto-updated.

Last update: 2026-03-17 20:25:24 UTC


README

locky42/leopard-user provides base user/auth/RBAC building blocks for Leopard projects.

Features

  • Base Doctrine models:
    • Leopard\User\Models\BaseUser
    • Leopard\User\Models\BaseRole
    • Leopard\User\Models\BasePermission
  • Authentication service with session restore
  • Authorization service for role/permission checks
  • Contract-first model mapping (UserInterface, RoleInterface, PermissionInterface)

Requirements

  • PHP ^8.3
  • locky42/leopard-doctrine

Doctrine Integration

Package bootstrap registers resolve-target mappings and model paths automatically.

Registered default mappings:

  • UserInterface -> BaseUser
  • RoleInterface -> BaseRole
  • PermissionInterface -> BasePermission

Quick Start

Authentication

use Leopard\User\Contracts\Models\UserInterface;
use Leopard\User\Models\BaseUser;
use Leopard\User\Services\AuthenticationService;

$userFinder = function (string $identifier) use ($entityManager): ?UserInterface {
    return $entityManager->getRepository(BaseUser::class)
        ->findOneBy(['username' => $identifier]);
};

$userFinderById = function (int $id) use ($entityManager): ?UserInterface {
    return $entityManager->getRepository(BaseUser::class)->find($id);
};

$authService = new AuthenticationService($userFinder, $userFinderById);

$user = $authService->attempt('john', 'password123');
if ($user !== null) {
    echo 'Login successful';
}

Authorization

use Leopard\User\Services\AuthorizationService;

$authzService = new AuthorizationService();

if ($authzService->hasRole($user, 'admin')) {
    echo 'User is admin';
}

if ($authzService->hasPermission($user, 'posts.delete')) {
    echo 'User can delete posts';
}

Services API

AuthenticationService

  • authenticate(string $identifier, string $password): ?UserInterface
  • login(UserInterface $user): bool
  • logout(): bool
  • getCurrentUser(): ?UserInterface
  • isAuthenticated(): bool
  • attempt(string $identifier, string $password): ?UserInterface
  • destroySession(): void

AuthorizationService

  • hasRole(UserInterface $user, string $roleName): bool
  • hasAnyRole(UserInterface $user, array $roleNames): bool
  • hasAllRoles(UserInterface $user, array $roleNames): bool
  • hasPermission(UserInterface $user, string $permissionName): bool
  • hasAnyPermission(UserInterface $user, array $permissionNames): bool
  • hasAllPermissions(UserInterface $user, array $permissionNames): bool
  • getUserRoles(UserInterface $user): array
  • getUserPermissions(UserInterface $user): array

ContractFactory Integration

In application code, use ContractFactory to map contracts to your own entities:

use Leopard\Core\Factory\ContractFactory;
use Leopard\User\Contracts\Models\UserInterface;
use Leopard\User\Contracts\Models\RoleInterface;
use Leopard\User\Contracts\Models\PermissionInterface;
use App\Models\User;
use App\Models\Role;
use App\Models\Permission;

ContractFactory::register(UserInterface::class, User::class);
ContractFactory::register(RoleInterface::class, Role::class);
ContractFactory::register(PermissionInterface::class, Permission::class);

Testing

From repository root:

vendor/bin/phpunit --testsuite=leopard-user

or

composer test:user

License

MIT