Passed
Push — master ( 2361e4...a90ea1 )
by Julito
09:52
created

AzureActiveDirectory::install()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 13
rs 9.9332
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
    const SETTING_ENABLE = 'enable';
16
    const SETTING_APP_ID = 'app_id';
17
    const SETTING_APP_SECRET = 'app_secret';
18
    const SETTING_BLOCK_NAME = 'block_name';
19
    const SETTING_FORCE_LOGOUT_BUTTON = 'force_logout';
20
    const SETTING_MANAGEMENT_LOGIN_ENABLE = 'management_login_enable';
21
    const SETTING_MANAGEMENT_LOGIN_NAME = 'management_login_name';
22
23
    const URL_TYPE_AUTHORIZE = 'login';
24
    const URL_TYPE_LOGOUT = 'logout';
25
26
    const EXTRA_FIELD_ORGANISATION_EMAIL = 'organisationemail';
27
    const EXTRA_FIELD_AZURE_ID = 'azure_id';
28
29
    /**
30
     * AzureActiveDirectory constructor.
31
     */
32
    protected function __construct()
33
    {
34
        $settings = [
35
            self::SETTING_ENABLE => 'boolean',
36
            self::SETTING_APP_ID => 'text',
37
            self::SETTING_APP_SECRET => 'text',
38
            self::SETTING_BLOCK_NAME => 'text',
39
            self::SETTING_FORCE_LOGOUT_BUTTON => 'boolean',
40
            self::SETTING_MANAGEMENT_LOGIN_ENABLE => 'boolean',
41
            self::SETTING_MANAGEMENT_LOGIN_NAME => 'text',
42
        ];
43
44
        parent::__construct('2.1', 'Angel Fernando Quiroz Campos', $settings);
45
    }
46
47
    /**
48
     * Instance the plugin.
49
     *
50
     * @staticvar null $result
51
     *
52
     * @return $this
53
     */
54
    public static function create()
55
    {
56
        static $result = null;
57
58
        return $result ? $result : $result = new self();
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function get_name()
65
    {
66
        return 'azure_active_directory';
67
    }
68
69
    /**
70
     * @return Azure
71
     */
72
    public function getProvider()
73
    {
74
        $provider = new Azure([
75
            'clientId' => $this->get(self::SETTING_APP_ID),
76
            'clientSecret' => $this->get(self::SETTING_APP_SECRET),
77
            'redirectUri' => api_get_path(WEB_PLUGIN_PATH).'azure_active_directory/src/callback.php',
78
        ]);
79
80
        return $provider;
81
    }
82
83
    /**
84
     * @param string $urlType Type of URL to generate
85
     *
86
     * @return string
87
     */
88
    public function getUrl($urlType)
89
    {
90
        if (self::URL_TYPE_LOGOUT === $urlType) {
91
            $provider = $this->getProvider();
92
93
            return $provider->getLogoutUrl(
94
                api_get_path(WEB_PATH)
95
            );
96
        }
97
98
        return api_get_path(WEB_PLUGIN_PATH).$this->get_name().'/src/callback.php';
99
    }
100
101
    /**
102
     * Create extra fields for user when installing.
103
     */
104
    public function install()
105
    {
106
        UserManager::create_extra_field(
107
            self::EXTRA_FIELD_ORGANISATION_EMAIL,
108
            ExtraField::FIELD_TYPE_TEXT,
109
            $this->get_lang('OrganisationEmail'),
110
            ''
111
        );
112
        UserManager::create_extra_field(
113
            self::EXTRA_FIELD_AZURE_ID,
114
            ExtraField::FIELD_TYPE_TEXT,
115
            $this->get_lang('AzureId'),
116
            ''
117
        );
118
    }
119
}
120