Issues (1892)

public/main/auth/tc.php (1 issue)

Severity
1
<?php
2
require_once __DIR__.'/../inc/global.inc.php';
3
4
use Chamilo\CoreBundle\Framework\Container;
5
use Chamilo\CoreBundle\Helpers\ChamiloHelper;
6
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
7
use ChamiloSession as Session;
8
9
$return = $_POST['return'] ?? $_GET['return'] ?? '/home';
10
11
$canAccept = true;
12
$infoMessage = '';
13
14
$userId = 0;
15
$termData = Session::read('term_and_condition');
16
if (!empty($termData['user_id'])) {
17
    $userId = (int) $termData['user_id'];
18
} else {
19
    $userId = api_get_user_id();
20
}
21
22
$isPlatformAdmin = api_is_platform_admin();
23
if ('true' === api_get_setting('registration.allow_terms_conditions') && !$isPlatformAdmin) {
24
    if ('true' === api_get_setting('ticket.show_terms_if_profile_completed')) {
25
        if (!empty($userId)) {
26
            $userInfo = api_get_user_info($userId);
27
            if ($userInfo && ANONYMOUS != $userInfo['status']) {
28
                $extraFieldValue = new ExtraFieldValue('user');
29
                $value = $extraFieldValue->get_values_by_handler_and_field_variable(
30
                    $userId,
31
                    'termactivated'
32
                );
33
34
                $termActivated = false;
35
                if (isset($value['value'])) {
36
                    $termActivated = !empty($value['value']) && 1 === (int) $value['value'];
37
                }
38
39
                if (false === $termActivated) {
40
                    $canAccept = false;
41
                    $infoMessage = Display::return_message(
42
                        get_lang('The terms and conditions have not yet been validated by your tutor'),
43
                        'warning',
44
                        false
45
                    );
46
                }
47
48
                if ($canAccept && 1 !== (int) $userInfo['profile_completed']) {
49
                    $canAccept = false;
50
                    $infoMessage .= Display::return_message(
51
                        get_lang('You must first fill your profile to enable the terms and conditions validation.'),
52
                        'warning',
53
                        true
54
                    );
55
                }
56
            }
57
        }
58
    }
59
}
60
61
if (
62
    $_SERVER['REQUEST_METHOD'] === 'POST'
63
    && !empty($_POST['legal_accept_type'])
64
    && (isset($_POST['legal_accept']) || api_get_setting('registration.hide_legal_accept_checkbox') === 'true')
65
) {
66
    if (!$canAccept) {
67
        ChamiloHelper::displayLegalTermsPage($return, false, $infoMessage);
68
    }
69
70
    if ($userId > 0) {
71
        ChamiloHelper::saveUserTermsAcceptance($userId, $_POST['legal_accept_type']);
72
73
        // Re-login in Symfony security
74
        $userEntity = api_get_user_entity($userId);
75
        if ($userEntity) {
76
            $token = new UsernamePasswordToken(
77
                $userEntity,
78
                'main',
79
                $userEntity->getRoles()
80
            );
81
82
            $tokenStorage = Container::getTokenStorage();
83
            $tokenStorage->setToken($token);
84
85
            // Save the token to session so the firewall recognizes it on the next request
86
            $session = Container::getSession();
87
            if ($session) {
0 ignored issues
show
$session is of type Symfony\Component\HttpFoundation\Session\Session, thus it always evaluated to true.
Loading history...
88
                $session->set('_security_main', serialize($token));
89
            }
90
        }
91
92
        Session::write('term_and_condition', null);
93
94
        ChamiloHelper::redirectTo($return);
95
    } else {
96
        die('Error: Unable to identify user accepting terms.');
97
    }
98
}
99
100
ChamiloHelper::displayLegalTermsPage($return, $canAccept, $infoMessage);
101