Created some twig extensions for checking membership using the new authorization manager callbacks
This commit is contained in:
@@ -13,9 +13,11 @@ use Illuminate\Database\Capsule\Manager as Capsule;
|
|||||||
use Psr\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
use Psr\Http\Message\ResponseInterface as Response;
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use UserFrosting\Sprinkle\Organisations\Database\Models\Interfaces\OrganisationInterface as OrganisationInterface;
|
use UserFrosting\Sprinkle\Organisations\Database\Models\Interfaces\OrganisationInterface;
|
||||||
|
use UserFrosting\Sprinkle\Organisations\Twig\OrganisationsExtension;
|
||||||
use UserFrosting\Sprinkle\Organisations\Authorize\AuthorizationManager;
|
use UserFrosting\Sprinkle\Organisations\Authorize\AuthorizationManager;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers services for the organisation sprinkle.
|
* Registers services for the organisation sprinkle.
|
||||||
*
|
*
|
||||||
@@ -87,6 +89,19 @@ class ServicesProvider
|
|||||||
return $new_authorizer;
|
return $new_authorizer;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Extends the 'view' service with the OrganisationsExtension for Twig.
|
||||||
|
*
|
||||||
|
* @return \Slim\Views\Twig
|
||||||
|
*/
|
||||||
|
$container->extend('view', function ($view, $c) {
|
||||||
|
$twig = $view->getEnvironment();
|
||||||
|
$extension = new OrganisationsExtension($c);
|
||||||
|
$twig->addExtension($extension);
|
||||||
|
|
||||||
|
return $view;
|
||||||
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Returns a callback that handles merging any organisation objects.
|
* Returns a callback that handles merging any organisation objects.
|
||||||
*
|
*
|
||||||
|
|||||||
72
src/Twig/OrganisationsExtension.php
Normal file
72
src/Twig/OrganisationsExtension.php
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* AVSDev UF Organisations (https://avsdev.uk)
|
||||||
|
*
|
||||||
|
* @link https://git.avsdev.uk/avsdev/sprinkle-organisations
|
||||||
|
* @license https://git.avsdev.uk/avsdev/sprinkle-organisations/blob/master/LICENSE.md (LGPL-3.0 License)
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace UserFrosting\Sprinkle\Organisations\Twig;
|
||||||
|
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
use Twig\Extension\AbstractExtension;
|
||||||
|
use Twig\Extension\GlobalsInterface;
|
||||||
|
use Twig\TwigFunction;
|
||||||
|
use UserFrosting\Support\Repository\Repository as Config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extends Twig functionality for the Organisation sprinkle.
|
||||||
|
*
|
||||||
|
* @author Craig Williams (https://avsdev.uk)
|
||||||
|
*/
|
||||||
|
class OrganisationsExtension extends AbstractExtension implements GlobalsInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var ContainerInterface
|
||||||
|
*/
|
||||||
|
protected $services;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Config
|
||||||
|
*/
|
||||||
|
protected $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ContainerInterface $services
|
||||||
|
*/
|
||||||
|
public function __construct(ContainerInterface $services)
|
||||||
|
{
|
||||||
|
$this->services = $services;
|
||||||
|
$this->config = $services->config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return 'avsdev/organisations';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFunctions()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
// Add Twig function for checking permissions during dynamic menu rendering
|
||||||
|
new TwigFunction('isOrganisationMember', function ($organisation) {
|
||||||
|
$authorizer = $this->services->authorizer;
|
||||||
|
$currentUser = $this->services->currentUser;
|
||||||
|
|
||||||
|
return $authorizer->runCallback($currentUser, 'is_organisation_member', $currentUser->id, $organisation->id);
|
||||||
|
}),
|
||||||
|
new TwigFunction('isOrganisationAdmin', function ($organisation) {
|
||||||
|
$authorizer = $this->services->authorizer;
|
||||||
|
$currentUser = $this->services->currentUser;
|
||||||
|
|
||||||
|
return $authorizer->runCallback($currentUser, 'is_organisation_admin', $currentUser->id, $organisation->id);
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getGlobals()
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user