PHP code example of chubbyphp / chubbyphp-security
1. Go to this page and download the library: Download chubbyphp/chubbyphp-security 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/ */
chubbyphp / chubbyphp-security example snippets
use Chubbyphp\Security\Authentication\AuthenticationProvider;
use Chubbyphp\Security\Authentication\FormAuthentication;
use Pimple\Container;
$container->register(new AuthenticationProvider);
$container->extend('security.authentication.authentications', function (array $authentications) use ($container) {
$authentications[] = new FormAuthentication(...);
return $authentications;
});
$container['security.authentication']->isAuthenticated($request);
use Chubbyphp\Security\Authentication\AuthenticationErrorHandlerInterface;
use Chubbyphp\Security\Authentication\AuthenticationErrorResponseMiddleware;
use Chubbyphp\Security\Authentication\FormAuthentication;
$middleware = new AuthenticationErrorResponseMiddleware(
new FormAuthentication(...),
new class() implements AuthenticationErrorHandlerInterface {
public function errorResponse(
Request $request,
Response $response,
int $code
): Response {
return $response->withStatus($code);
}
}
);
$middleware($request, $response);
use Chubbyphp\Security\Authentication\AuthenticationMiddleware;
use Chubbyphp\Security\Authentication\FormAuthentication;
$middleware = new AuthenticationMiddleware(new FormAuthentication(...));
$middleware($request, $response);
use Chubbyphp\Security\Authentication\FormAuthentication;
use Chubbyphp\Security\Authentication\PasswordManager;
use Chubbyphp\Session\Session;
$authentication = new FormAuthentication(new PasswordManager, new Session, ...);
$authentication->login($request);
$authentication->logout($request);
$authentication->isAuthenticated($request);
$authentication->getAuthenticatedUser($request);
use Chubbyphp\Security\Authentication\PasswordManager;
$manager = new PasswordManager();
$hash = $manager->hash('password');
$manager->verify('password', $hash);
use Chubbyphp\Security\Authorization\AuthorizationProvider;
use Chubbyphp\Security\Authorization\RoleAuthorization;
use Pimple\Container;
$container->register(new AuthorizationProvider);
$container->extend('security.authorization.rolehierarchy', function (array $rolehierarchy) use ($container) {
$rolehierarchy['ADMIN'] = ['USER_MANAGEMENT'];
$rolehierarchy['USER_MANAGEMENT'] = ['USER_LIST', 'USER_CREATE', 'USER_EDIT', 'USER_VIEW', 'USER_DELETE'];
return $rolehierarchy;
});
$container['security.authorization.rolehierarchyresolver']->resolve($roles);
$container->extend('security.authorization.authorizations', function (array $authorizations) use ($container) {
$authorizations[] = new RoleAuthorization(...);
return $$authorizations;
});
$container['security.authorization']->isGranted($user, 'USER_EDIT');
use Chubbyphp\Security\Authorization\RoleAuthorization;
use Chubbyphp\Security\Authorization\RoleHierarchyResolver;
$user->setRoles(['ADMIN']);
$resolver = new RoleHierarchyResolver([
'ADMIN' => ['USER_MANAGEMENT'],
'USER_MANAGEMENT' => ['USER_CREATE', 'USER_EDIT', 'USER_VIEW', 'USER_DELETE']
]);
$authorization = new RoleAuthorization($resolver);
$authorization->isGranted($user, 'USER_EDIT'); // true
use Chubbyphp\Security\Authorization\RoleHierarchyResolver;
$user->setRoles(['ADMIN']);
$resolver = new RoleHierarchyResolver([
'ADMIN' => ['USER_MANAGEMENT'],
'USER_MANAGEMENT' => ['USER_CREATE', 'USER_EDIT', 'USER_VIEW', 'USER_DELETE']
]);
$resolver->resolve(['ADMIN']);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.