Admin   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B admin() 0 83 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\consentSimpleAdmin\Controller;
6
7
use Exception;
8
use SimpleSAML\Auth;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\Logger;
11
use SimpleSAML\Metadata\MetaDataStorageHandler;
12
use SimpleSAML\Module\consent\Auth\Process\Consent;
13
use SimpleSAML\Module\consent\Store;
14
use SimpleSAML\Session;
15
use SimpleSAML\XHTML\Template;
16
use Symfony\Component\HttpFoundation\Request;
17
18
use function array_key_exists;
19
use function count;
20
use function sprintf;
21
use function strval;
22
23
/**
24
 * Controller class for the consentsimpleadmin module.
25
 *
26
 * This class serves the different views available in the module.
27
 *
28
 * @package simplesamlphp/simplesamlphp-module-consentsimpleadmin
29
 */
30
class Admin
31
{
32
    /**
33
     * Controller constructor.
34
     *
35
     * It initializes the global configuration and session for the controllers implemented here.
36
     *
37
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
38
     * @param \SimpleSAML\Session $session The session to use by the controllers.
39
     *
40
     * @throws \Exception
41
     */
42
    public function __construct(
43
        protected Configuration $config,
44
        protected Session $session,
45
    ) {
46
    }
47
48
49
50
    /**
51
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
52
     *
53
     * @return \SimpleSAML\XHTML\Template
54
     */
55
    public function admin(Request $request): Template
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

55
    public function admin(/** @scrutinizer ignore-unused */ Request $request): Template

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

Loading history...
56
    {
57
        $consentconfig = Configuration::getConfig('module_consentSimpleAdmin.php');
58
59
        $as = $consentconfig->getValue('auth');
60
        $as = new Auth\Simple($as);
61
        $as->requireAuth();
62
63
        // Get all attributes
64
        $attributes = $as->getAttributes();
65
66
        // Get user ID
67
        $userid_attributename = $consentconfig->getOptionalValue('userid', 'eduPersonPrincipalName');
68
69
        if (empty($attributes[$userid_attributename])) {
70
            throw new Exception(sprintf(
71
                'Could not generate useridentifier for storing consent. Attribute [%s] was not available.',
72
                $userid_attributename,
73
            ));
74
        }
75
76
        $userid = $attributes[$userid_attributename][0];
77
78
        // Get metadata storage handler
79
        $metadata = MetaDataStorageHandler::getMetadataHandler();
80
81
        // Get IdP id and metadata
82
        $idp_entityid = $as->getAuthData('saml:sp:IdP');
83
        if ($idp_entityid !== null) {
84
            // From a remote idp (as bridge)
85
            $idp_metadata = $metadata->getMetaData($idp_entityid, 'saml20-idp-remote');
86
        } else {
87
            // from the local idp
88
            $idp_entityid = $metadata->getMetaDataCurrentEntityID('saml20-idp-hosted');
89
            $idp_metadata = $metadata->getMetaData($idp_entityid, 'saml20-idp-hosted');
90
        }
91
92
        Logger::debug('consentAdmin: IdP is [' . $idp_entityid . ']');
93
94
        $source = $idp_metadata['metadata-set'] . '|' . $idp_entityid;
95
96
        // Parse consent config
97
        $consent_storage = Store::parseStoreConfig($consentconfig->getValue('store'));
98
99
        // Calc correct user ID hash
100
        $hashed_user_id = Consent::getHashedUserID($userid, $source);
101
102
        // Check if button with withdraw all consent was clicked
103
        if (array_key_exists('withdraw', $_REQUEST)) {
104
            Logger::info(sprintf(
105
                'consentAdmin: UserID [%s] has requested to withdraw all consents given...',
106
                $hashed_user_id,
107
            ));
108
109
            $consent_storage->deleteAllConsents($hashed_user_id);
110
        }
111
112
        // Get all consents for user
113
        $user_consent_list = $consent_storage->getConsents($hashed_user_id);
114
115
        $consentServices = [];
116
        foreach ($user_consent_list as $c) {
117
            $consentServices[$c[1]] = 1;
118
        }
119
120
        Logger::debug(sprintf(
121
            'consentAdmin: no of consents [%d] no of services [%d]',
122
            count($user_consent_list),
123
            count($consentServices),
124
        ));
125
126
        // Init template
127
        $t = new Template($this->config, 'consentSimpleAdmin:consentadmin.twig');
128
        $translator = $t->getTranslator();
129
130
        $t->data['consentServices'] = count($consentServices);
131
        $t->data['consents'] = count($user_consent_list);
132
        $t->data['granted'] = $translator->t('{consentSimpleAdmin:consentsimpleadmin:granted}', [
0 ignored issues
show
Bug introduced by
The method t() does not exist on SimpleSAML\Locale\Translate. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
        /** @scrutinizer ignore-call */ 
133
        $t->data['granted'] = $translator->t('{consentSimpleAdmin:consentsimpleadmin:granted}', [

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
            '%NO%' => strval($this->data['consents']),
134
            '%OF%' => strval($this->data['consentServices']),
135
        ]);
136
137
        return $t;
138
    }
139
}
140