227 lines
9.7 KiB
PHP
227 lines
9.7 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;
|
|
use UserFrosting\Sprinkle\Organisations\Database\Models\User;
|
|
use UserFrosting\Sprinkle\Organisations\Twig\OrganisationsExtension;
|
|
use UserFrosting\Sprinkle\Organisations\Repository\OrganisationApprovalRepository;
|
|
use UserFrosting\Sprinkle\Organisations\Repository\OrganisationMembershipApprovalRepository;
|
|
|
|
|
|
/**
|
|
* 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_approval', 'UserFrosting\Sprinkle\Organisations\Database\Models\OrganisationApproval');
|
|
$classMapper->setClassMapping('organisation_sprunje', 'UserFrosting\Sprinkle\Organisations\Sprunje\OrganisationSprunje');
|
|
$classMapper->setClassMapping('organisation_member', 'UserFrosting\Sprinkle\Organisations\Database\Models\OrganisationMember');
|
|
$classMapper->setClassMapping('organisation_membership_approval', 'UserFrosting\Sprinkle\Organisations\Database\Models\OrganisationMembershipApproval');
|
|
$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) {
|
|
/*
|
|
* Check if all $user has an $organisation.
|
|
*
|
|
* @param int $user_id the id of the requesting user (normally currentUser->id).
|
|
* @return bool true if $user is a verified member of any organisation.
|
|
*/
|
|
$authorizer->addCallback('has_organisation', function ($user_id) {
|
|
$query = Capsule::table('organisation_members')
|
|
->join('organisations', 'organisation_members.organisation_id', 'organisations.id')
|
|
->where('user_id', $user_id)
|
|
->where('organisation_members.flag_approved', true)
|
|
->where('organisations.flag_approved', true);
|
|
|
|
return $query->count() > 0;
|
|
});
|
|
|
|
/*
|
|
* 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.
|
|
*/
|
|
$authorizer->addCallback('is_organisation_member', function ($user_id, $organisation_id, $explicit = false) {
|
|
$query = Capsule::table('organisation_members')
|
|
->where('user_id', $user_id)
|
|
->where('organisation_id', $organisation_id)
|
|
->where('flag_approved', true);
|
|
|
|
if ($explicit) {
|
|
$query = $query->where('flag_admin', false);
|
|
}
|
|
|
|
return $query->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.
|
|
*/
|
|
$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);
|
|
|
|
return $query->count() > 0;
|
|
});
|
|
|
|
/*
|
|
* Check if $user_A_id is in an organisation that $user_B_id is also in
|
|
*
|
|
* @param int $user_A_id the id of the first user (normally currentUser->id).
|
|
* @param int $user_B_id the id of the second user.
|
|
* @param bool $check_is_admin also check if A can administrate B.
|
|
* @return bool true if $user_A_id in an organisation with $user_B_id in.
|
|
*/
|
|
$authorizer->addCallback('has_matching_organisation', function ($user_A_id, $user_B_id, $check_is_admin = false) {
|
|
$user_A = User::findInt($user_A_id);
|
|
$user_B = User::findInt($user_B_id);
|
|
|
|
if ($check_is_admin) {
|
|
foreach($user_A->adminForOrganisations()->get() as $org) {
|
|
if ($org->members(true)->where('user_id', $user_B_id)->count() > 0) {
|
|
return true;
|
|
}
|
|
}
|
|
} else {
|
|
foreach($user_A->organisations()->get() as $org) {
|
|
if ($org->members(true)->where('user_id', $user_B_id)->count() > 0) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
});
|
|
|
|
return $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.
|
|
*
|
|
* @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 deleted
|
|
*
|
|
* 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) {
|
|
|
|
};
|
|
};
|
|
|
|
/*
|
|
* Repository for organisation registration approval requests.
|
|
*
|
|
* @return \UserFrosting\Sprinkle\Organisations\Repository\OrganisationApprovalRepository
|
|
*/
|
|
$container['repoOrganisationApproval'] = function ($c) {
|
|
$classMapper = $c->classMapper;
|
|
$config = $c->config;
|
|
|
|
$repo = new OrganisationApprovalRepository($classMapper, $config['verification.algorithm'], $c['tokenLogger'], $config['debug.tokens']);
|
|
|
|
return $repo;
|
|
};
|
|
|
|
/*
|
|
* Repository for membership approval requests.
|
|
*
|
|
* @return \UserFrosting\Sprinkle\Organisations\Repository\OrganisationMembershipApprovalRepository
|
|
*/
|
|
$container['repoOrganisationMembershipApproval'] = function ($c) {
|
|
$classMapper = $c->classMapper;
|
|
$config = $c->config;
|
|
|
|
$repo = new OrganisationMembershipApprovalRepository($classMapper, $config['verification.algorithm'], $c['tokenLogger'], $config['debug.tokens']);
|
|
|
|
return $repo;
|
|
};
|
|
}
|
|
}
|