locky42 / leopard-user
A library for handling user authentication and management.
1.0.0
2026-03-17 20:24 UTC
Requires
- php: >=8.3
- doctrine/orm: ^3
Requires (Dev)
- phpunit/phpunit: ^12.3
README
locky42/leopard-user provides base user/auth/RBAC building blocks for Leopard projects.
Features
- Base Doctrine models:
Leopard\User\Models\BaseUserLeopard\User\Models\BaseRoleLeopard\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 -> BaseUserRoleInterface -> BaseRolePermissionInterface -> 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): ?UserInterfacelogin(UserInterface $user): boollogout(): boolgetCurrentUser(): ?UserInterfaceisAuthenticated(): boolattempt(string $identifier, string $password): ?UserInterfacedestroySession(): void
AuthorizationService
hasRole(UserInterface $user, string $roleName): boolhasAnyRole(UserInterface $user, array $roleNames): boolhasAllRoles(UserInterface $user, array $roleNames): boolhasPermission(UserInterface $user, string $permissionName): boolhasAnyPermission(UserInterface $user, array $permissionNames): boolhasAllPermissions(UserInterface $user, array $permissionNames): boolgetUserRoles(UserInterface $user): arraygetUserPermissions(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