Passed
Push — master ( 791909...fe60d3 )
by Timo
52s
created

AuthorizationService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 16.22%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 4
dl 0
loc 89
ccs 6
cts 37
cp 0.1622
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getUser() 0 8 1
A authUser() 0 12 2
B getGroups() 0 28 4
1
<?php
2
namespace ApacheSolrForTypo3\Solr\IndexQueue\FrontendHelper;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2011-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Access\Rootline;
28
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerRequestHandler;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Sv\AbstractAuthenticationService;
31
32
/**
33
 * Authentication service to authorize the Index Queue page indexer to access
34
 * protected pages.
35
 *
36
 * @author Ingo Renner <[email protected]>
37
 */
38
class AuthorizationService extends AbstractAuthenticationService
39
{
40
41
    /**
42
     * User used when authenticating the page indexer for protected pages,
43
     * to allow the indexer to access and protected content. May also allow to
44
     * identify requests by the page indexer.
45
     *
46
     * @var string
47
     */
48
    const SOLR_INDEXER_USERNAME = '__SolrIndexerUser__';
49
50
    /**
51
     * Gets a fake frontend user record to allow access to protected pages.
52
     *
53
     * @return array An array representing a frontend user.
54
     */
55
    public function getUser()
56
    {
57
        return [
58
            'uid' => 0,
59
            'username' => self::SOLR_INDEXER_USERNAME,
60
            'authenticated' => true
61
        ];
62
    }
63
64
    /**
65
     * Authenticates the page indexer frontend user to grant it access to
66
     * protected pages and page content.
67
     *
68
     * Returns 200 which automatically grants access for the current fake page
69
     * indexer user. A status of >= 200 also tells TYPO3 that it doesn't need to
70
     * conduct other services that might be registered for "their opinion"
71
     * whether a user is authenticated.
72
     *
73
     * @see \TYPO3\CMS\Core\Authentication\AbstractUserAuthentication::checkAuthentication()
74
     * @param array $user Array of user data
75
     * @return int Returns 200 to grant access for the page indexer.
76
     */
77
    public function authUser($user)
78
    {
79
        // shouldn't happen, but in case we get a regular user we just
80
        // pass it on to another (regular) auth service
81
        $authenticationLevel = 100;
82
83
        if ($user['username'] == self::SOLR_INDEXER_USERNAME) {
84
            $authenticationLevel = 200;
85
        }
86
87
        return $authenticationLevel;
88
    }
89
90
    /**
91
     * Creates user group records so that the page indexer is granted access to
92
     * protected pages.
93
     *
94
     * @param array $user Data of user.
95
     * @param array $knownGroups Group data array of already known groups. This is handy if you want select other related groups. Keys in this array are unique IDs of those groups.
96
     * @return mixed Groups array, keys = uid which must be unique
97
     */
98 1
    public function getGroups(
99
        $user,
100
        /** @noinspection PhpUnusedParameterInspection */
101
        $knownGroups
0 ignored issues
show
Unused Code introduced by
The parameter $knownGroups is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
    ) {
103 1
        $groupData = [];
104
105
            /** @var $requestHandler PageIndexerRequestHandler */
106 1
        $requestHandler = GeneralUtility::makeInstance(PageIndexerRequestHandler::class);
107 1
        $accessRootline = $requestHandler->getRequest()->getParameter('accessRootline');
108
109 1
        if ($user['username'] == self::SOLR_INDEXER_USERNAME && !empty($accessRootline)) {
110
            $accessRootline = GeneralUtility::makeInstance(Rootline::class, $accessRootline);
111
            $groups = $accessRootline->getGroups();
112
113
            foreach ($groups as $groupId) {
114
                // faking a user group record
115
                $groupData[] = [
116
                    'uid' => $groupId,
117
                    'pid' => 0,
118
                    'title' => '__SolrIndexerGroup__',
119
                    'TSconfig' => ''
120
                ];
121
            }
122
        }
123
124 1
        return $groupData;
125
    }
126
}
127