Passed
Pull Request — 1.11.x (#5763)
by Angel Fernando Quiroz
16:44
created

AzureActiveDirectory::get_name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/* For license terms, see /license.txt */
3
4
use TheNetworg\OAuth2\Client\Provider\Azure;
5
6
/**
7
 * AzureActiveDirectory plugin class.
8
 *
9
 * @author Angel Fernando Quiroz Campos <[email protected]>
10
 *
11
 * @package chamilo.plugin.azure_active_directory
12
 */
13
class AzureActiveDirectory extends Plugin
14
{
15
    public const SETTING_ENABLE = 'enable';
16
    public const SETTING_APP_ID = 'app_id';
17
    public const SETTING_APP_SECRET = 'app_secret';
18
    public const SETTING_BLOCK_NAME = 'block_name';
19
    public const SETTING_FORCE_LOGOUT_BUTTON = 'force_logout';
20
    public const SETTING_MANAGEMENT_LOGIN_ENABLE = 'management_login_enable';
21
    public const SETTING_MANAGEMENT_LOGIN_NAME = 'management_login_name';
22
    public const SETTING_PROVISION_USERS = 'provisioning';
23
    public const SETTING_GROUP_ID_ADMIN = 'group_id_admin';
24
    public const SETTING_GROUP_ID_SESSION_ADMIN = 'group_id_session_admin';
25
    public const SETTING_GROUP_ID_TEACHER = 'group_id_teacher';
26
    public const SETTING_EXISTING_USER_VERIFICATION_ORDER = 'existing_user_verification_order';
27
28
    public const URL_TYPE_AUTHORIZE = 'login';
29
    public const URL_TYPE_LOGOUT = 'logout';
30
31
    public const EXTRA_FIELD_ORGANISATION_EMAIL = 'organisationemail';
32
    public const EXTRA_FIELD_AZURE_ID = 'azure_id';
33
    public const EXTRA_FIELD_AZURE_UID = 'azure_uid';
34
35
    /**
36
     * AzureActiveDirectory constructor.
37
     */
38
    protected function __construct()
39
    {
40
        $settings = [
41
            self::SETTING_ENABLE => 'boolean',
42
            self::SETTING_APP_ID => 'text',
43
            self::SETTING_APP_SECRET => 'text',
44
            self::SETTING_BLOCK_NAME => 'text',
45
            self::SETTING_FORCE_LOGOUT_BUTTON => 'boolean',
46
            self::SETTING_MANAGEMENT_LOGIN_ENABLE => 'boolean',
47
            self::SETTING_MANAGEMENT_LOGIN_NAME => 'text',
48
            self::SETTING_PROVISION_USERS => 'boolean',
49
            self::SETTING_GROUP_ID_ADMIN => 'text',
50
            self::SETTING_GROUP_ID_SESSION_ADMIN => 'text',
51
            self::SETTING_GROUP_ID_TEACHER => 'text',
52
            self::SETTING_EXISTING_USER_VERIFICATION_ORDER => 'text',
53
        ];
54
55
        parent::__construct('2.3', 'Angel Fernando Quiroz Campos, Yannick Warnier', $settings);
56
    }
57
58
    /**
59
     * Instance the plugin.
60
     *
61
     * @staticvar null $result
62
     *
63
     * @return $this
64
     */
65
    public static function create()
66
    {
67
        static $result = null;
68
69
        return $result ? $result : $result = new self();
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function get_name()
76
    {
77
        return 'azure_active_directory';
78
    }
79
80
    /**
81
     * @return Azure
82
     */
83
    public function getProvider()
84
    {
85
        $provider = new Azure([
86
            'clientId' => $this->get(self::SETTING_APP_ID),
87
            'clientSecret' => $this->get(self::SETTING_APP_SECRET),
88
            'redirectUri' => api_get_path(WEB_PLUGIN_PATH).'azure_active_directory/src/callback.php',
89
        ]);
90
91
        return $provider;
92
    }
93
94
    /**
95
     * @param string $urlType Type of URL to generate
96
     *
97
     * @return string
98
     */
99
    public function getUrl($urlType)
100
    {
101
        if (self::URL_TYPE_LOGOUT === $urlType) {
102
            $provider = $this->getProvider();
103
104
            return $provider->getLogoutUrl(
105
                api_get_path(WEB_PATH)
106
            );
107
        }
108
109
        return api_get_path(WEB_PLUGIN_PATH).$this->get_name().'/src/callback.php';
110
    }
111
112
    /**
113
     * Create extra fields for user when installing.
114
     */
115
    public function install()
116
    {
117
        UserManager::create_extra_field(
118
            self::EXTRA_FIELD_ORGANISATION_EMAIL,
119
            ExtraField::FIELD_TYPE_TEXT,
120
            $this->get_lang('OrganisationEmail'),
121
            ''
122
        );
123
        UserManager::create_extra_field(
124
            self::EXTRA_FIELD_AZURE_ID,
125
            ExtraField::FIELD_TYPE_TEXT,
126
            $this->get_lang('AzureId'),
127
            ''
128
        );
129
        UserManager::create_extra_field(
130
            self::EXTRA_FIELD_AZURE_UID,
131
            ExtraField::FIELD_TYPE_TEXT,
132
            $this->get_lang('AzureUid'),
133
            ''
134
        );
135
    }
136
137
    public function getExistingUserVerificationOrder(): array
138
    {
139
        $defaultOrder = [1, 2, 3];
140
141
        $settingValue = $this->get(self::SETTING_EXISTING_USER_VERIFICATION_ORDER);
142
        $selectedOrder = array_filter(
143
            array_map(
144
                'trim',
145
                explode(',', $settingValue)
146
            )
147
        );
148
        $selectedOrder = array_map('intval', $selectedOrder);
149
        $selectedOrder = array_filter(
150
            $selectedOrder,
151
            function ($position) use ($defaultOrder): bool {
152
                return in_array($position, $defaultOrder);
153
            }
154
        );
155
156
        if ($selectedOrder) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $selectedOrder of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
157
            return $selectedOrder;
158
        }
159
160
        return $defaultOrder;
161
    }
162
163
    public function getUserIdByVerificationOrder(array $azureUserData): ?int
164
    {
165
        $selectedOrder = $this->getExistingUserVerificationOrder();
166
167
        $extraFieldValue = new ExtraFieldValue('user');
168
        $positionsAndFields = [
169
            1 => $extraFieldValue->get_item_id_from_field_variable_and_field_value(
170
                AzureActiveDirectory::EXTRA_FIELD_ORGANISATION_EMAIL,
171
                $azureUserData['mail']
172
            ),
173
            2 => $extraFieldValue->get_item_id_from_field_variable_and_field_value(
174
                AzureActiveDirectory::EXTRA_FIELD_AZURE_ID,
175
                $azureUserData['mailNickname']
176
            ),
177
            3 => $extraFieldValue->get_item_id_from_field_variable_and_field_value(
178
                AzureActiveDirectory::EXTRA_FIELD_AZURE_UID,
179
                $azureUserData['id']
180
            ),
181
        ];
182
183
        foreach ($selectedOrder as $position) {
184
            if (!empty($positionsAndFields[$position]) && isset($positionsAndFields[$position]['item_id'])) {
185
                return (int) $positionsAndFields[$position]['item_id'];
186
            }
187
        }
188
189
        return null;
190
    }
191
}
192