Elgg  Version 3.0
Gatekeeper.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
9 use ElggEntity;
10 use ElggGroup;
11 use ElggSession;
12 use ElggUser;
13 use Exception;
18 
24 class Gatekeeper {
25 
29  protected $session;
30 
34  protected $request;
35 
39  protected $redirects;
40 
44  protected $entities;
45 
49  protected $access;
50 
54  protected $translator;
55 
66  public function __construct(
67  ElggSession $session,
69  RedirectService $redirects,
70  EntityTable $entities,
72  Translator $translator
73  ) {
74  $this->session = $session;
75  $this->request = $request;
76  $this->redirects = $redirects;
77  $this->entities = $entities;
78  $this->access = $access;
79  $this->translator = $translator;
80  }
81 
87  public function assertAuthenticatedUser() {
88  if ($this->session->isLoggedIn()) {
89  return;
90  }
91 
92  $this->redirects->setLastForwardFrom();
93 
94  throw new LoggedInGatekeeperException();
95  }
96 
102  public function assertUnauthenticatedUser() {
103  if (!$this->session->isLoggedIn()) {
104  return;
105  }
106 
108  $exception->setRedirectUrl(elgg_get_site_url());
109 
110  throw $exception;
111  }
112 
119  public function assertAuthenticatedAdmin() {
120  $this->assertAuthenticatedUser();
121 
122  $user = $this->session->getLoggedInUser();
123  if ($user->isAdmin()) {
124  return;
125  }
126 
127  $this->redirects->setLastForwardFrom();
128 
129  throw new AdminGatekeeperException();
130  }
131 
146  public function assertExists($guid, $type = null, $subtype = null) {
148  return $this->entities->get($guid, $type, $subtype);
149  });
150 
151  if (!$entity) {
153  $exception->setParams([
154  'guid' => $guid,
155  'type' => $type,
156  'subtype' => $subtype,
157  'route' => $this->request->get('_route'),
158  ]);
159  throw $exception;
160  }
161 
162  return $entity;
163  }
164 
175 
176  $result = true;
177 
178  try {
179  if (!$this->session->getIgnoreAccess() && !$this->access->hasAccessToEntity($entity, $user)) {
180  // user is logged in but still does not have access to it
181  $msg = $this->translator->translate('limited_access');
183  $exception->setParams([
184  'entity' => $entity,
185  'user' => $user,
186  'route' => $this->request->get('_route'),
187  ]);
188  throw $exception;
189  }
190 
191  if (!$entity->isEnabled() && !$this->session->getDisabledEntityVisibility()) {
192  // entity exists, but is disabled
194  $exception->setParams([
195  'entity' => $entity,
196  'user' => $user,
197  'route' => $this->request->get('_route'),
198  ]);
199  throw $exception;
200  }
201 
202  if ($entity instanceof ElggGroup) {
203  $this->assertAccessibleGroup($entity, $user);
204  }
205 
206  foreach (['owner_guid', 'container_guid'] as $prop) {
207  if (!$entity->$prop) {
208  continue;
209  }
210 
211  $parent = $this->assertExists($entity->$prop);
212  $this->assertAccessibleEntity($parent, $user);
213  }
214  } catch (HttpException $ex) {
215  $result = $ex;
216  }
217 
218  $hook_params = [
219  'entity' => $entity,
220  'user' => $user,
221  'route' => $this->request->get('_route'),
222  ];
223 
224  $result = _elgg_services()->hooks->trigger('gatekeeper', "{$entity->type}:{$entity->subtype}", $hook_params, $result);
225 
226  if ($result instanceof HttpException) {
227  throw $result;
228  } else if ($result === false) {
229  throw new HttpException();
230  }
231  }
232 
242  public function assertAccessibleUser(ElggUser $user, ElggUser $viewer = null) {
243  if (!$user->isBanned()) {
244  return;
245  }
246 
247  if (!isset($viewer)) {
248  $viewer = $this->session->getLoggedInUser();
249  }
250 
251  if (!$viewer || !$viewer->isAdmin()) {
253  $exception->setParams([
254  'entity' => $user,
255  'user' => $viewer,
256  'route' => $this->request->get('_route'),
257  ]);
258  throw $exception;
259  }
260  }
261 
272  public function assertAccessibleGroup(ElggGroup $group, ElggUser $user = null) {
273  if ($group->canAccessContent($user)) {
274  return;
275  }
276 
277  $this->assertAuthenticatedUser();
278 
279  $this->redirects->setLastForwardFrom();
280 
282  $exception->setParams([
283  'entity' => $group,
284  'user' => $user,
285  'route' => $this->request->get('_route'),
286  ]);
287  $exception->setRedirectUrl($group->getURL());
288  throw $exception;
289  }
290 
297  public function assertXmlHttpRequest() {
298  if ($this->request->isXmlHttpRequest()) {
299  return;
300  }
301 
302  throw new AjaxGatekeeperException();
303  }
304 
305 }
Thrown when the request is not a valid ajax request.
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:1176
Generic HTTP exception.
assertXmlHttpRequest()
Require XmlHttpRequest.
Definition: Gatekeeper.php:297
assertAccessibleGroup(ElggGroup $group, ElggUser $user=null)
Validate group content visibility.
Definition: Gatekeeper.php:272
$request
Page handler for autocomplete endpoint.
Definition: livesearch.php:9
$subtype
Definition: delete.php:22
$guid
Removes an admin notice.
$type
Definition: delete.php:21
assertAccessibleUser(ElggUser $user, ElggUser $viewer=null)
Validate active user account.
Definition: Gatekeeper.php:242
assertExists($guid, $type=null, $subtype=null)
Require an entity with a given guid, type and subtype to proceed with code execution.
Definition: Gatekeeper.php:146
Thrown when logged in but this isn&#39;t allowed.
Thrown when entity can not be found.
assertUnauthenticatedUser()
Require a user to be not authenticated (logged out) to with code execution.
Definition: Gatekeeper.php:102
Thrown when the logged in user is not an admin.
assertAccessibleEntity(ElggEntity $entity, ElggUser $user=null)
Require that authenticated user has access to entity.
Definition: Gatekeeper.php:174
Handles common tasks when redirecting a request.
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:156
Configuration exception.
$entity
Definition: reset.php:8
const ELGG_SHOW_DISABLED_ENTITIES
Definition: constants.php:158
assertAuthenticatedAdmin()
Require an admin user to be authenticated to proceed with code execution.
Definition: Gatekeeper.php:119
$exception
$user
Definition: ban.php:7
elgg ElggUser
Definition: ElggUser.js:12
elgg_get_site_url()
Get the URL for the current (or specified) site, ending with "/".
isEnabled()
Is this entity enabled?
Thrown when entity can not be edited or container permissions do not allow it to be written...
assertAuthenticatedUser()
Require a user to be authenticated to with code execution.
Definition: Gatekeeper.php:87
Thrown when one of the gatekeepers prevents access.
__construct(ElggSession $session, HttpRequest $request, RedirectService $redirects, EntityTable $entities, AccessCollections $access, Translator $translator)
Constructor.
Definition: Gatekeeper.php:66
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
isBanned()
Is this user banned or not?
Definition: ElggUser.php:171
canAccessContent(ElggUser $user=null)
Check if current user can access group content based on his/her membership status and group&#39;s content...
Definition: ElggGroup.php:350
Gatekeeper.
Definition: Gatekeeper.php:24
elgg ElggEntity
Definition: ElggEntity.js:15
getURL()
Gets the URL for this entity.
WARNING: API IN FLUX.
Definition: EntityTable.php:38
$access
Definition: save.php:18