adachsoft / principal-group
Optional user groups for adachsoft/principal-contracts: group model with roles, repository contract, principal expander, membership provider and group service.
Requires
- php: ^8.3
- adachsoft/principal-contracts: ^0.2
- adachsoft/reference-contracts: ^0.1
- psr/clock: ^1.0
Requires (Dev)
- adachsoft/php-code-style: ^0.7.3
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
- rector/rector: ^2.8
- symplify/phpstan-rules: ^14.19
Suggests
- phpunit/phpunit: Required to run the reusable group repository contract tests.
Provides
None
Conflicts
None
Replaces
None
README
adachsoft/principal-group adds optional groups to applications using adachsoft/principal-contracts. Groups act as principals and can contain leaf principals or nested groups.
Features
- Immutable group and membership models with
ADMINandMEMBERroles. - Nested membership lookup and group-to-principal expansion.
- Access-controlled group management with a last-administrator safeguard.
- Repository contract with optimistic version checks, plus an in-memory implementation.
- Injectable clock, identifier generator, and group-change listener.
Installation
composer require adachsoft/principal-group
Requirements
The package requires PHP 8.3 or later and uses adachsoft/reference-contracts, adachsoft/principal-contracts, and PSR-20 clocks.
Usage
use AdachSoft\PrincipalGroup\InMemory\InMemoryGroupRepository;
use AdachSoft\PrincipalGroup\Service\GroupServiceFactory;
use AdachSoft\ReferenceContracts\PrincipalReference;
$repository = new InMemoryGroupRepository();
$groups = GroupServiceFactory::create($repository);
$owner = new PrincipalReference('account', 'alice');
$group = $groups->createGroup($owner, 'Household');
$membershipResolver = GroupServiceFactory::createMembershipResolver($repository);
$principalResolver = GroupServiceFactory::createPrincipalResolver($repository, ['account']);
$memberships = $membershipResolver->membershipsOf($owner);
$accounts = $principalResolver->resolve($group->reference());
Use the in-memory repository for examples and ephemeral storage. For persistent storage, implement GroupRepositoryInterface and pass your adapter to GroupServiceFactory::create(). The same membership resolver can be integrated with other AdachSoft services that support composite membership resolution.
See the usage example for a complete runnable example. Applications can also extend AdachSoft\PrincipalGroup\Testing\AbstractGroupRepositoryContractTest to verify their repository adapter; PHPUnit is required to run those contract tests.