Admin   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 100
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 77 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
22
/**
23
 * Controller class for the consentsimpleadmin module.
24
 *
25
 * This class serves the different views available in the module.
26
 *
27
 * @package simplesamlphp/simplesamlphp-module-consentsimpleadmin
28
 */
29
class Admin
30
{
31
    /**
32
     * Controller constructor.
33
     *
34
     * It initializes the global configuration and session for the controllers implemented here.
35
     *
36
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
37
     * @param \SimpleSAML\Session $session The session to use by the controllers.
38
     */
39
    public function __construct(
40
        protected Configuration $config,
41
        protected Session $session,
42
    ) {
43
    }
44
45
46
47
    /**
48
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
49
     *
50
     * @return \SimpleSAML\XHTML\Template
51
     */
52
    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

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