1. Go to this page and download the library: Download azaharizaman/nexus-identity library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
azaharizaman / nexus-identity example snippets
// For read-only operations
public function __construct(
private readonly UserQueryInterface $userQuery
) {}
// For write operations
public function __construct(
private readonly UserPersistInterface $userPersist
) {}
use Nexus\Identity\Services\UserManager;
use Nexus\Identity\ValueObjects\Credentials;
// Create a new user
$user = $userManager->createUser([
'email' => '[email protected]',
'password' => 'SecureP@ssw0rd!',
'name' => 'John Doe',
'tenant_id' => 'tenant_ulid',
]);
// Change password
$userManager->changePassword($user->getId(), 'NewSecureP@ssw0rd!');
// Activate user
$userManager->activateUser($user->getId());
// Lock user
$userManager->lockUser($user->getId(), 'Suspicious activity detected');
use Nexus\Identity\Services\PermissionChecker;
// Check permission
if ($permissionChecker->hasPermission($user, 'users.create')) {
// User can create users
}
// Check multiple permissions
if ($permissionChecker->hasAllPermissions($user, ['users.create', 'users.update'])) {
// User has all permissions
}
// Check role
if ($permissionChecker->hasRole($user, 'admin')) {
// User is an admin
}
// Wildcard permission matching
// If user has "users.*", they have "users.create", "users.update", etc.
use Nexus\Identity\Contracts\PolicyEvaluatorInterface;
use Nexus\Identity\ValueObjects\Policy;
// Register a custom policy
$leavePolicy = Policy::define('hrm.leave.apply_on_behalf')
->description('User can apply leave on behalf of employees in same department or as their manager')
->check(function(UserInterface $user, string $action, mixed $resource, array $context) use ($employeeQuery) {
// Extract target employee from context
$targetEmployeeId = $context['target_employee_id'] ?? null;
if (!$targetEmployeeId) {
return false;
}
$userEmployee = $employeeQuery->findByUserId($user->getId());
$targetEmployee = $employeeQuery->findById($targetEmployeeId);
// Check if same department OR user is manager
return $userEmployee?->getDepartmentId() === $targetEmployee?->getDepartmentId()
|| $userEmployee?->getId() === $targetEmployee?->getManagerId();
});
$policyEvaluator->registerPolicy(
$leavePolicy->getName(),
$leavePolicy->getEvaluator()
);
// Evaluate policy with context
$canApply = $policyEvaluator->evaluate(
user: $user,
action: 'hrm.leave.apply_on_behalf',
resource: null,
context: ['target_employee_id' => $employeeId]
);
if ($canApply) {
// User can apply leave on behalf of this employee
}
use Nexus\Identity\Services\RoleManager;
// Create a role
$role = $roleManager->createRole([
'name' => 'manager',
'description' => 'Department Manager',
'tenant_id' => 'tenant_ulid',
]);
// Assign permission to role
$roleManager->assignPermission($role->getId(), $permission->getId());
// Assign role to user
$userManager->assignRole($user->getId(), $role->getId());
use Nexus\Identity\ValueObjects\Credentials;
$credentials = new Credentials('[email protected]', 'password');
// Validates email format on construction
use Nexus\Identity\Services\UserManager;
use Nexus\Identity\Contracts\UserRepositoryInterface;
use PHPUnit\Framework\TestCase;
class UserManagerTest extends TestCase
{
public function test_create_user()
{
$mockRepo = $this->createMock(UserRepositoryInterface::class);
$mockRepo->expects($this->once())
->method('emailExists')
->willReturn(false);
$userManager = new UserManager($mockRepo, $hasher, $validator);
// ... test logic
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.