AbstractAuthenticationService.php 2.62 KB
Newer Older
Fabian Galinski's avatar
Fabian Galinski committed
1
2
<?php

3
namespace SGalinski\SgRest\Service\Authentication;
Fabian Galinski's avatar
Fabian Galinski committed
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

/***************************************************************
 *  Copyright notice
 *
 *  (c) sgalinski Internet Services (http://www.sgalinski.de)
 *
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

29
30
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
31
use SGalinski\SgRest\Service\RegistrationService;
Fabian Galinski's avatar
Fabian Galinski committed
32
33
34
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
35
36
37
 * Class AbstractAuthenticationService
 *
 * @package SGalinski\SgRest\Service\Authentication
Fabian Galinski's avatar
Fabian Galinski committed
38
 */
39
abstract class AbstractAuthenticationService implements AuthenticationServiceInterface, LoggerAwareInterface {
40
	use LoggerAwareTrait;
41

Fabian Galinski's avatar
Fabian Galinski committed
42
	/**
43
	 * @var array
Fabian Galinski's avatar
Fabian Galinski committed
44
45
46
47
	 */
	protected $authenticatedUser;

	/**
48
49
	 *
	 * @var RegistrationService
Fabian Galinski's avatar
Fabian Galinski committed
50
51
52
	 */
	protected $registrationService;

53
54
55
	/**
	 * @param RegistrationService $registrationService
	 */
56
	public function injectRegistrationService(RegistrationService $registrationService): void {
57
58
59
		$this->registrationService = $registrationService;
	}

Fabian Galinski's avatar
Fabian Galinski committed
60
61
62
	/**
	 * Verify if the authenticated user has access to the given apikey.
	 *
63
	 * @param string $apiKey
Fabian Galinski's avatar
Fabian Galinski committed
64
65
	 * @return bool
	 */
66
	public function verifyUserAccess($apiKey): bool {
Fabian Galinski's avatar
Fabian Galinski committed
67
68
69
		$verifyAccess = FALSE;

		if ($this->authenticatedUser) {
70
			$accessGroups = GeneralUtility::trimExplode(',', $this->authenticatedUser['tx_sgrest_access_groups'], TRUE);
71
			$verifyAccess = in_array($apiKey, $accessGroups, TRUE);
Fabian Galinski's avatar
Fabian Galinski committed
72
73
74
75
76
77
78
79
		}

		return $verifyAccess;
	}

	/**
	 * Getter for the authenticated user.
	 *
80
	 * @return array|NULL
Fabian Galinski's avatar
Fabian Galinski committed
81
	 */
82
	public function getAuthenticatedUser(): ?array {
Fabian Galinski's avatar
Fabian Galinski committed
83
84
85
		return $this->authenticatedUser;
	}

86
87
88
89
90
	/**
	 * Method to check if the authenticated user is in test-mode.
	 *
	 * @return bool
	 */
91
	public function isAuthenticatedUserInTestMode(): bool {
92
93
94
95
96
97
98
		$authendicatedUser = $this->getAuthenticatedUser();
		if (!$authendicatedUser) {
			return FALSE;
		}

		return $authendicatedUser->isTestMode();
	}
99
}