-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathOnlyLoggedInMiddleware.php
More file actions
55 lines (48 loc) · 1.46 KB
/
OnlyLoggedInMiddleware.php
File metadata and controls
55 lines (48 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\User_SAML\Middleware;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Middleware;
use OCP\AppFramework\Utility\IControllerMethodReflector;
use OCP\IURLGenerator;
use OCP\IUserSession;
/**
* Class OnlyLoggedInMiddleware prevents access to a controller method if the user
* is already logged-in.
*
* @package OCA\User_SAML\MiddleWare
*/
class OnlyLoggedInMiddleware extends Middleware {
public function __construct(
private IControllerMethodReflector $reflector,
private IUserSession $userSession,
private IURLGenerator $urlGenerator,
) {
}
/**
* @param \OCP\AppFramework\Controller $controller
* @param string $methodName
* @throws \Exception
*/
public function beforeController($controller, $methodName) {
if ($this->reflector->hasAnnotation('OnlyUnauthenticatedUsers') && $this->userSession->isLoggedIn()) {
throw new \Exception('User is already logged-in');
}
}
/**
* @param \OCP\AppFramework\Controller $controller
* @param string $methodName
* @param \Exception $exception
* @return RedirectResponse
* @throws \Exception
*/
public function afterException($controller, $methodName, \Exception $exception) {
if ($exception->getMessage() === 'User is already logged-in') {
return new RedirectResponse($this->urlGenerator->getAbsoluteURL('/'));
}
throw $exception;
}
}