Skip to content

Commit

Permalink
feat(middleware): added page owner can edit middleware
Browse files Browse the repository at this point in the history
Allow routes to validate page owner canEdit rights in route definition
  • Loading branch information
jeabakker committed Oct 8, 2019
1 parent 7e86f57 commit b81fc72
Show file tree
Hide file tree
Showing 6 changed files with 1,289 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Elgg\Router\Middleware;

/**
* Check if the current route page owner can be edited (by the current logged in user) and is an user
*
* @since 3.2
*/
class GroupPageOwnerCanEditGatekeeper extends PageOwnerCanEditGatekeeper {

/**
* {@inheritDoc}
* @see \Elgg\Router\Middleware\PageOwnerCanEditGatekeeper::__invoke()
*/
public function __invoke(\Elgg\Request $request) {
$this->assertAccess($request, 'group');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Elgg\Router\Middleware;

use Elgg\Router\Route;
use Elgg\EntityPermissionsException;

/**
* Check if the current route page owner can be edited (by the current logged in user)
*
* @since 3.2
*/
class PageOwnerCanEditGatekeeper {

/**
* Validate the current request
*
* @param \Elgg\Request $request the current request
*
* @return void
* @throws EntityPermissionsException
*/
public function __invoke(\Elgg\Request $request) {
$this->assertAccess($request);
}

/**
* Validate the current request
*
* @param \Elgg\Request $request the current request
* @param string $type (optional) the required type of the page owner
* @param string $subtype (optional) the required subtype of the page owner
*
* @return void
* @throws EntityPermissionsException
*/
protected function assertAccess(\Elgg\Request $request, string $type = '', string $subtype = '') {

$route = $request->getHttpRequest()->getRoute();
if (!$route instanceof Route) {
return;
}

$page_owner = $route->resolvePageOwner();
if (!$page_owner instanceof \ElggEntity) {
return;
}

if (!$page_owner->canEdit()) {
throw new EntityPermissionsException();
}

if (!empty($type) && $page_owner->getType() !== $type) {
throw new EntityPermissionsException();
}

if (!empty($subtype) && $page_owner->getSubtype() !== $subtype) {
throw new EntityPermissionsException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Elgg\Router\Middleware;

/**
* Check if the current route page owner can be edited (by the current logged in user) and is an user
*
* @since 3.2
*/
class UserPageOwnerCanEditGatekeeper extends PageOwnerCanEditGatekeeper {

/**
* {@inheritDoc}
* @see \Elgg\Router\Middleware\PageOwnerCanEditGatekeeper::__invoke()
*/
public function __invoke(\Elgg\Request $request) {
$this->assertAccess($request, 'user');
}
}
Loading

0 comments on commit b81fc72

Please sign in to comment.