Files
sprinkle-organisations/src/ServicesProvider/ServicesProvider.php

128 lines
5.4 KiB
PHP

<?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\ServicesProvider;
use Illuminate\Database\Capsule\Manager as Capsule;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use UserFrosting\Sprinkle\Organisations\Database\Models\Interfaces\OrganisationInterface as OrganisationInterface;
use UserFrosting\Sprinkle\Organisations\Authorize\AuthorizationManager;
/**
* Registers services for the organisation sprinkle.
*
* @author Craig Williams (https://avsdev.uk)
*/
class ServicesProvider
{
/**
* Register UserFrosting's organisation services.
*
* @param ContainerInterface $container A DI container implementing ArrayAccess and psr-container.
*/
public function register(ContainerInterface $container)
{
/*
* Extend the 'classMapper' service to register sprunje and model classes.
*
* Mappings added: 'organisation', 'organisation_sprunje'
*
* @return \UserFrosting\Sprinkle\Core\Util\ClassMapper
*/
$container->extend('classMapper', function ($classMapper, $c) {
$classMapper->setClassMapping('organisation', 'UserFrosting\Sprinkle\Organisations\Database\Models\Organisation');
$classMapper->setClassMapping('organisation_sprunje', 'UserFrosting\Sprinkle\Organisations\Sprunje\OrganisationSprunje');
$classMapper->setClassMapping('user', 'UserFrosting\Sprinkle\Organisations\Database\Models\User');
$classMapper->setClassMapping('user_sprunje', 'UserFrosting\Sprinkle\Organisations\Sprunje\UserSprunje');
return $classMapper;
});
/*
* Extend the 'authorizer' service to add extra access condition callbacks.
*
* @return \UserFrosting\Sprinkle\Core\Util\ClassMapper
*/
$container->extend('authorizer', function ($authorizer, $c) {
$new_authorizer = new AuthorizationManager($c, $authorizer->getCallbacks());
/*
* Check if all $user is a member of $organisation.
*
* @param int $user_id the id of the requesting user (normally currentUser->id).
* @param int $organisation_id the id of the target organisation.
* @return bool true if $user is a member of $organisation.
*/
$new_authorizer->addCallback('is_organisation_member', function ($user_id, $organisation_id) {
return Capsule::table('organisation_members')
->where('user_id', $user_id)
->where('organisation_id', $organisation_id)
->count() > 0;
});
/*
* Check if all $user is an administrator of $organisation.
*
* @param int $user_id the id of the requesting user (normally currentUser->id).
* @param int $organisation_id the id of the target organisation.
* @return bool true if $user is an administrator of $organisation.
*/
$new_authorizer->addCallback('is_organisation_admin', function ($user_id, $organisation_id) {
return Capsule::table('organisation_members')
->where('user_id', $user_id)
->where('organisation_id', $organisation_id)
->where('flag_admin', true)
->count() > 0;
});
return $new_authorizer;
});
/*
* Returns a callback that handles merging any organisation objects.
*
* @return callable
*/
$container['organisation.beforeMerge'] = function ($c) {
/*
* This method is invoked when an organisation is about to be merged
*
* Returns a callback that handles re-owning any organisation objects.
* Throwing exceptions is allowed but not recommended. This method is triggered within a Capsule context.
* @param \UserFrosting\Sprinkle\Organisations\Database\Models\Interfaces\OrganisationInterfaces $source Organisation merging from
* @param \UserFrosting\Sprinkle\Organisations\Database\Models\Interfaces\OrganisationInterfaces $target Organisation merging towards
*/
return function (OrganisationInterface $source, OrganisationInterface $target) use ($c) {
};
};
/*
* Returns a callback that handles hard deleting an organisation (clean up any associated objects).
*
* @return callable
*/
$container['organisation.beforeDelete'] = function ($c) {
/*
* This method is invoked when an organisation is about to be merged
*
* Returns a callback that handles re-owning any organisation objects.
* Throwing exceptions is allowed but not recommended. This method is triggered within a Capsule context.
* @param \UserFrosting\Sprinkle\Organisations\Database\Models\Interfaces\OrganisationInterfaces $organisation Organisation about to be deleted
*/
return function (OrganisationInterface $organisation) use ($c) {
};
};
}
}