See the README for current tweaks

This commit is contained in:
2023-05-31 11:51:12 +01:00
parent 97407e9532
commit a67214d024
18 changed files with 1260 additions and 2 deletions

View File

@@ -0,0 +1,107 @@
<?php
/*
* AVSDev UF Tweaks (https://avsdev.uk)
*
* @link https://git.avsdev.uk/avsdev/sprinkle-uf-tweaks
* @license https://git.avsdev.uk/avsdev/sprinkle-uf-tweaks/blob/master/LICENSE.md (LGPL-3.0 License)
*/
namespace UserFrosting\Sprinkle\UFTweaks\ServicesProvider;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
/**
* Registers services for the UFTweaks sprinkle.
*
* @author Craig Williams (https://avsdev.uk)
*/
class ServicesProvider
{
/**
* Register UserFrosting's services.
*
* @param ContainerInterface $container A DI container implementing ArrayAccess and psr-container.
*/
public function register(ContainerInterface $container)
{
/*
* Returns a callback that handles setting the `UF-Redirect` header after a successful login.
*
* Overrides the service definition in the account Sprinkle.
*
* @return callable
*/
$container['redirect.onLogin'] = function ($c) {
/*
* This method is invoked when a user completes the login process.
*
* Returns a callback that handles setting the `UF-Redirect` header after a successful login.
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Psr\Http\Message\ResponseInterface $response
* @param array $args
* @return \Psr\Http\Message\ResponseInterface
*/
return function (Request $request, Response $response, array $args) use ($c) {
/** @var \UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager */
$authorizer = $c->authorizer;
$currentUser = $c->authenticator->user();
if ($authorizer->checkAccess($currentUser, 'uri_dashboard')) {
return $response->withHeader('UF-Redirect', $c->router->pathFor('dashboard'));
} else {
return $response->withHeader('UF-Redirect', $c->router->pathFor('index'));
}
};
};
/*
* Extend the 'authorizer' service to fix some callbacks
*
* @return \UserFrosting\Sprinkle\Core\Util\ClassMapper
*/
$container->extend('authorizer', function ($authorizer, $c) {
/*
* Check if all values in the array $needle are present in the values of $haystack.
*
* @param array[mixed] $needle the array whose values we should look for in $haystack
* @param array[mixed] $haystack the array of values to search.
* @return bool true if every value in $needle is present in the values of $haystack, false otherwise.
*/
$authorizer->addCallback(
'subset',
function ($needle, $haystack) {
if (!is_countable($needle)) {
$needle = [ $needle ];
}
return count($needle) == count(array_intersect($needle, $haystack));
}
);
/*
* Check if all keys of the array $needle are present in the values of $haystack.
*
* This function is useful for whitelisting an array of key-value parameters.
* @param array[mixed] $needle the array whose keys we should look for in $haystack
* @param array[mixed] $haystack the array of values to search.
* @return bool true if every key in $needle is present in the values of $haystack, false otherwise.
*/
$authorizer->addCallback(
'subset_keys',
function ($needle, $haystack) {
if (!is_countable($needle)) {
$needle = [ $needle ];
}
return count($needle) == count(array_intersect(array_keys($needle), $haystack));
}
);
return $authorizer;
});
}
}