Allow members to leave organisations
This commit is contained in:
@@ -836,6 +836,7 @@ class OrganisationController extends SimpleController
|
||||
'fields' => $fields,
|
||||
'tools' => $editButtons,
|
||||
'delete_redirect' => $this->ci->router->pathFor('uri_organisations'),
|
||||
'leave_redirect' => $this->ci->router->pathFor('dashboard'),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,75 @@ use UserFrosting\Support\Exception\NotFoundException;
|
||||
*/
|
||||
class OrganisationMembersController extends SimpleController
|
||||
{
|
||||
|
||||
/**
|
||||
* Processes the request to leave an organisation.
|
||||
*
|
||||
* Removes a member from the specified organisation.
|
||||
* Before doing so, checks that:
|
||||
* 1. The user has permission to leave this organisation;
|
||||
* 2. The submitted data is valid.
|
||||
* This route requires authentication.
|
||||
*
|
||||
* Request type: DELETE
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param array $args
|
||||
*
|
||||
* @throws NotFoundException If organisation is not found
|
||||
* @throws ForbiddenException If user is not authorized to access page
|
||||
* @throws BadRequestException
|
||||
*/
|
||||
public function leave(Request $request, Response $response, $args)
|
||||
{
|
||||
$organisation = $this->getOrganisationFromParams($args);
|
||||
|
||||
// If the organisation doesn't exist, return 404
|
||||
if (!$organisation) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
/** @var \UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager $authorizer */
|
||||
$authorizer = $this->ci->authorizer;
|
||||
|
||||
/** @var \UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface $currentUser */
|
||||
$currentUser = $this->ci->currentUser;
|
||||
|
||||
// Access-controlled page
|
||||
if (!$authorizer->checkAccess($currentUser, 'leave_organisation', [
|
||||
'organisation' => $organisation,
|
||||
])) {
|
||||
throw new ForbiddenException();
|
||||
}
|
||||
|
||||
/** @var \UserFrosting\Support\Repository\Repository $config */
|
||||
$config = $this->ci->config;
|
||||
|
||||
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
|
||||
$classMapper = $this->ci->classMapper;
|
||||
|
||||
// Begin transaction - DB will be rolled back if an exception occurs
|
||||
Capsule::transaction(function () use ($organisation, $currentUser) {
|
||||
$currentUser->organisations()->detach($organisation->id);
|
||||
|
||||
// Create activity record
|
||||
$this->ci->userActivityLogger->info("User {$currentUser->user_name} left organisation {$organisation->name}.", [
|
||||
'type' => 'leave_organisation',
|
||||
'user_id' => $currentUser->id,
|
||||
]);
|
||||
});
|
||||
|
||||
/** @var \UserFrosting\Sprinkle\Core\Alert\AlertStream $ms */
|
||||
$ms = $this->ci->alerts;
|
||||
|
||||
$ms->addMessageTranslated('success', 'ORGANISATION.LEAVE_SUCCESSFUL', [
|
||||
'name' => $organisationName,
|
||||
]);
|
||||
|
||||
return $response->withJson([], 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of organisation members.
|
||||
*
|
||||
@@ -82,6 +151,50 @@ class OrganisationMembersController extends SimpleController
|
||||
return $sprunje->toResponse($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get leave confirmation modal.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param array $args
|
||||
*
|
||||
* @throws NotFoundException If organisation is not found
|
||||
* @throws ForbiddenException If user is not authorized to access page
|
||||
* @throws BadRequestException
|
||||
*/
|
||||
public function getModalConfirmLeave(Request $request, Response $response, $args)
|
||||
{
|
||||
$organisation = $this->getOrganisationFromParams($args);
|
||||
|
||||
// If the organisation no longer exists, forward to main organisation listing page
|
||||
if (!$organisation) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
/** @var \UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager $authorizer */
|
||||
$authorizer = $this->ci->authorizer;
|
||||
|
||||
/** @var \UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface $currentUser */
|
||||
$currentUser = $this->ci->currentUser;
|
||||
|
||||
// Access-controlled page
|
||||
if (!$authorizer->checkAccess($currentUser, 'leave_organisation', [
|
||||
'organisation' => $organisation,
|
||||
])) {
|
||||
throw new ForbiddenException();
|
||||
}
|
||||
|
||||
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
|
||||
$classMapper = $this->ci->classMapper;
|
||||
|
||||
return $this->ci->view->render($response, 'modals/confirm-leave-organisation.html.twig', [
|
||||
'organisation' => $organisation,
|
||||
'form' => [
|
||||
'action' => "api/organisations/o/{$organisation->slug}/members",
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get organisation from params.
|
||||
|
||||
@@ -75,6 +75,12 @@ class OrganisationPermissions extends BaseSeed
|
||||
'conditions' => 'always()',
|
||||
'description' => 'Merge two organisations together, including all the members.',
|
||||
]),
|
||||
'leave_organisation' => new Permission([
|
||||
'slug' => 'leave_organisation',
|
||||
'name' => 'Leave organisation',
|
||||
'conditions' => 'always()',
|
||||
'description' => 'Allows members to leave organisations.',
|
||||
]),
|
||||
'delete_organisation' => new Permission([
|
||||
'slug' => 'delete_organisation',
|
||||
'name' => 'Delete organisation',
|
||||
@@ -163,6 +169,7 @@ class OrganisationPermissions extends BaseSeed
|
||||
$roleUser->permissions()->syncWithoutDetaching([
|
||||
$permissions['uri_organisation_own']->id,
|
||||
$permissions['view_organisation_field_own']->id,
|
||||
$permissions['leave_organisation']->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user