Elgg  Version 7.1
Gatekeeper.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
17 use Elgg\Http\Request as HttpRequest;
20 
26 class Gatekeeper {
27 
39  public function __construct(
40  protected SessionManagerService $session_manager,
41  protected HttpRequest $request,
42  protected RedirectService $redirects,
43  protected EntityTable $entities,
44  protected AccessCollections $access,
45  protected Translator $translator,
47  ) {
48  }
49 
56  public function assertAuthenticatedUser(): void {
57  if ($this->session_manager->isLoggedIn()) {
58  return;
59  }
60 
61  $this->redirects->setLastForwardFrom();
62 
63  throw new LoggedInGatekeeperException();
64  }
65 
72  public function assertUnauthenticatedUser(): void {
73  if (!$this->session_manager->isLoggedIn()) {
74  return;
75  }
76 
78  $exception->setRedirectUrl(elgg_get_site_url());
79 
80  throw $exception;
81  }
82 
90  public function assertAuthenticatedAdmin(): void {
91  $this->assertAuthenticatedUser();
92 
93  $user = $this->session_manager->getLoggedInUser();
94  if ($user->isAdmin()) {
95  return;
96  }
97 
98  $this->redirects->setLastForwardFrom();
99 
100  throw new AdminGatekeeperException();
101  }
102 
116  public function assertExists(int $guid, ?string $type = null, ?string $subtype = null): \ElggEntity {
118  return $this->entities->get($guid, $type, $subtype);
119  });
120 
121  if (!$entity instanceof \ElggEntity) {
123  $exception->setParams([
124  'guid' => $guid,
125  'type' => $type,
126  'subtype' => $subtype,
127  'route' => $this->request->getParam('_route', null, false),
128  ]);
129  throw $exception;
130  }
131 
132  return $entity;
133  }
134 
145  public function assertAccessibleEntity(\ElggEntity $entity, ?\ElggUser $user = null, bool $validate_can_edit = false): void {
146 
147  $result = true;
148 
149  try {
150  $user_guid = $user ? $user->guid : 0;
151  if (!$this->session_manager->getIgnoreAccess() && !$entity->hasAccess($user_guid)) {
152  // user is logged in but still does not have access to it
153  $msg = $this->translator->translate('limited_access');
155  $exception->setParams([
156  'entity' => $entity,
157  'user' => $user,
158  'route' => $this->request->getParam('_route', null, false),
159  ]);
160  throw $exception;
161  }
162 
163  if ($validate_can_edit && !$entity->canEdit($user_guid)) {
164  // logged in user does not have edit or write access to it
165  $msg = $this->translator->translate('limited_access');
167  $exception->setParams([
168  'entity' => $entity,
169  'user' => $user,
170  'route' => $this->request->getParam('_route', null, false),
171  ]);
172  throw $exception;
173  }
174 
175  if (!$entity->isEnabled() && !$this->session_manager->getDisabledEntityVisibility()) {
176  // entity exists, but is disabled
178  $exception->setParams([
179  'entity' => $entity,
180  'user' => $user,
181  'route' => $this->request->getParam('_route', null, false),
182  ]);
183  throw $exception;
184  }
185 
186  if ($entity->isDeleted() && !$this->session_manager->getDeletedEntityVisibility()) {
187  // entity exists, but is deleted
189  $exception->setParams([
190  'entity' => $entity,
191  'user' => $user,
192  'route' => $this->request->getParam('_route', null, false),
193  ]);
194  throw $exception;
195  }
196 
197  if ($entity instanceof \ElggGroup) {
198  $this->assertAccessibleGroup($entity, $user);
199  }
200 
201  foreach (['owner_guid', 'container_guid'] as $prop) {
202  if (!$entity->$prop) {
203  continue;
204  }
205 
206  $parent = $this->assertExists($entity->$prop);
207  $this->assertAccessibleEntity($parent, $user);
208  }
209  } catch (HttpException $ex) {
210  $result = $ex;
211  }
212 
213  $params = [
214  'entity' => $entity,
215  'user' => $user,
216  'route' => $this->request->getParam('_route', null, false),
217  ];
218 
219  $result = _elgg_services()->events->triggerResults('gatekeeper', "{$entity->type}:{$entity->subtype}", $params, $result);
220 
221  if ($result instanceof HttpException) {
222  throw $result;
223  } else if ($result === false) {
224  throw new HttpException();
225  }
226  }
227 
237  public function assertAccessibleUser(\ElggUser $user, ?\ElggUser $viewer = null): void {
238  if (!$user->isBanned()) {
239  return;
240  }
241 
242  if (!isset($viewer)) {
243  $viewer = $this->session_manager->getLoggedInUser();
244  }
245 
246  if (!$viewer || !$viewer->isAdmin()) {
247  $exception = new EntityNotFoundException();
248  $exception->setParams([
249  'entity' => $user,
250  'user' => $viewer,
251  'route' => $this->request->getParam('_route', null, false),
252  ]);
253  throw $exception;
254  }
255  }
256 
267  public function assertAccessibleGroup(\ElggGroup $group, ?\ElggUser $user = null): void {
268  if ($group->canAccessContent($user)) {
269  return;
270  }
271 
272  $this->assertAuthenticatedUser();
273 
274  $this->redirects->setLastForwardFrom();
275 
277  $exception->setParams([
278  'entity' => $group,
279  'user' => $user,
280  'route' => $this->request->getParam('_route', null, false),
281  ]);
282  $exception->setRedirectUrl($group->getURL());
283  throw $exception;
284  }
285 
296  public function assertGroupToolEnabled(string $group_tool, ?\ElggGroup $group = null): void {
297  $group = $group ?? $this->page_owner->getPageOwnerEntity();
298  if (!$group instanceof \ElggGroup) {
299  return;
300  }
301 
302  if ($group->isToolEnabled($group_tool)) {
303  return;
304  }
305 
306  $ex = new GroupToolGatekeeperException();
307  $ex->setRedirectUrl($group->getURL());
308  $ex->setParams([
309  'entity' => $group,
310  'tool' => $group_tool,
311  ]);
312 
313  throw $ex;
314  }
315 
322  public function assertXmlHttpRequest(): void {
323  if ($this->request->isXmlHttpRequest()) {
324  return;
325  }
326 
327  throw new AjaxGatekeeperException();
328  }
329 }
$entity
Definition: reset.php:8
$guid
Reset an ElggUpgrade.
Definition: reset.php:6
$subtype
Definition: delete.php:22
$type
Definition: delete.php:21
$params
Saves global plugin settings.
Definition: save.php:13
$page_owner
Definition: add.php:16
$user
Definition: ban.php:7
Access collections database service.
Entity table database service.
Definition: EntityTable.php:24
Generic HTTP exception.
setParams(array $params=[])
Set params to provide context about the exception.
setRedirectUrl($url)
Set preferred redirect URL If set, a redirect response will be issued.
Thrown when entity can not be edited or container permissions do not allow it to be written.
Thrown when one of the gatekeepers prevents access.
Thrown when the logged in user is not an admin.
Thrown when the request is not a valid ajax request.
Thrown when one of the gatekeepers prevents access.
Thrown when the requested group tool isn't enabled for a group.
Gatekeeper.
Definition: Gatekeeper.php:26
assertAuthenticatedUser()
Require a user to be authenticated to with code execution.
Definition: Gatekeeper.php:56
assertAccessibleGroup(\ElggGroup $group, ?\ElggUser $user=null)
Validate group content visibility.
Definition: Gatekeeper.php:267
assertAuthenticatedAdmin()
Require an admin user to be authenticated to proceed with code execution.
Definition: Gatekeeper.php:90
assertXmlHttpRequest()
Require XmlHttpRequest.
Definition: Gatekeeper.php:322
assertUnauthenticatedUser()
Require a user to be not authenticated (logged out) to with code execution.
Definition: Gatekeeper.php:72
assertExists(int $guid, ?string $type=null, ?string $subtype=null)
Require an entity with a given guid, type and subtype to proceed with code execution.
Definition: Gatekeeper.php:116
assertAccessibleEntity(\ElggEntity $entity, ?\ElggUser $user=null, bool $validate_can_edit=false)
Require that authenticated user has access to entity.
Definition: Gatekeeper.php:145
__construct(protected SessionManagerService $session_manager, protected HttpRequest $request, protected RedirectService $redirects, protected EntityTable $entities, protected AccessCollections $access, protected Translator $translator, protected PageOwnerService $page_owner)
Constructor.
Definition: Gatekeeper.php:39
assertAccessibleUser(\ElggUser $user, ?\ElggUser $viewer=null)
Validate active user account.
Definition: Gatekeeper.php:237
assertGroupToolEnabled(string $group_tool, ?\ElggGroup $group=null)
Validate group tool enabled.
Definition: Gatekeeper.php:296
Elgg HTTP request.
Definition: Request.php:17
Holds page owner related functions.
Handles common tasks when redirecting a request.
elgg_get_site_url()
Get the URL for the current (or specified) site, ending with "/".
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:121
const ELGG_SHOW_DISABLED_ENTITIES
Definition: constants.php:123
const ELGG_SHOW_DELETED_ENTITIES
Definition: constants.php:127
_elgg_services()
Get the global service provider.
Definition: elgglib.php:347
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags.
Definition: elgglib.php:300
$request
Definition: livesearch.php:12
$user_guid
Definition: login_as.php:10
$exception
Definition: error.php:15
if(!elgg_get_config('trash_enabled')) $group
Definition: group.php:13
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
if(elgg_view_exists("widgets/{$widget->handler}/edit")) $access
Definition: save.php:19