Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Defines classes used for plugin info.
19
 *
20
 * @package    core
21
 * @copyright  2013 Petr Skoda {@link http://skodak.org}
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace core\plugininfo;
25
 
26
use admin_settingpage;
27
use moodle_url;
28
use part_of_admin_tree;
29
 
30
/**
31
 * Class for authentication plugins
32
 */
33
class auth extends base {
34
 
35
    public static function plugintype_supports_disabling(): bool {
36
        return true;
37
    }
38
 
39
    public function is_uninstall_allowed() {
40
        global $DB;
41
 
42
        if (in_array($this->name, array('manual', 'nologin', 'webservice', 'mnet'))) {
43
            return false;
44
        }
45
 
46
        return !$DB->record_exists('user', array('auth'=>$this->name));
47
    }
48
 
49
    /**
50
     * Finds all enabled plugins, the result may include missing plugins.
51
     * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
52
     */
53
    public static function get_enabled_plugins() {
54
        global $CFG;
55
 
56
        // These two are always enabled and can't be disabled.
57
        $enabled = array('nologin'=>'nologin', 'manual'=>'manual');
58
        foreach (explode(',', $CFG->auth) as $auth) {
59
            $enabled[$auth] = $auth;
60
        }
61
 
62
        return $enabled;
63
    }
64
 
65
    public static function enable_plugin(string $pluginname, int $enabled): bool {
66
        global $CFG;
67
 
68
        $haschanged = false;
69
        $plugins = [];
70
        if (!empty($CFG->auth)) {
71
            $plugins = array_flip(explode(',', $CFG->auth));
72
        }
73
        // Only set visibility if it's different from the current value.
74
        if ($enabled && !array_key_exists($pluginname, $plugins)) {
75
            $plugins[$pluginname] = $pluginname;
76
            $haschanged = true;
77
        } else if (!$enabled && array_key_exists($pluginname, $plugins)) {
78
            unset($plugins[$pluginname]);
79
            $haschanged = true;
80
 
81
            if ($pluginname == $CFG->registerauth) {
82
                set_config('registerauth', '');
83
            }
84
        }
85
 
86
        if ($haschanged) {
87
            $new = implode(',', array_flip($plugins));
88
            add_to_config_log('auth', $CFG->auth, $new, 'core');
89
            set_config('auth', $new);
1441 ariadna 90
            \core\session\manager::destroy_by_auth_plugin($pluginname);
1 efrain 91
            // Remove stale sessions.
92
            \core\session\manager::gc();
93
            // Reset caches.
94
            \core_plugin_manager::reset_caches();
95
        }
96
 
97
        return $haschanged;
98
    }
99
 
100
    public function get_settings_section_name() {
101
        return 'authsetting' . $this->name;
102
    }
103
 
104
    public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
105
        global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
106
        /** @var \admin_root $ADMIN */
107
        $ADMIN = $adminroot; // May be used in settings.php.
108
        $plugininfo = $this; // Also can be used inside settings.php.
109
        $auth = $this;       // Also to be used inside settings.php.
110
 
111
        if (!$this->is_installed_and_upgraded()) {
112
            return;
113
        }
114
 
115
        if (!$hassiteconfig) {
116
            return;
117
        }
118
 
119
        $section = $this->get_settings_section_name();
120
 
121
        $settings = null;
122
        if (file_exists($this->full_path('settings.php'))) {
123
            // TODO: finish implementation of common settings - locking, etc.
124
            $settings = new admin_settingpage($section, $this->displayname,
125
                'moodle/site:config', $this->is_enabled() === false);
126
            include($this->full_path('settings.php')); // This may also set $settings to null.
127
        }
128
 
129
        if ($settings) {
130
            $ADMIN->add($parentnodename, $settings);
131
        }
132
    }
133
 
134
    /**
135
     * Return URL used for management of plugins of this type.
136
     * @return moodle_url
137
     */
138
    public static function get_manage_url() {
139
        return new moodle_url('/admin/settings.php', array('section'=>'manageauths'));
140
    }
141
 
142
    /**
143
     * Pre-uninstall hook.
144
     *
145
     * This is intended for disabling of plugin, some DB table purging, etc.
146
     *
147
     * NOTE: to be called from uninstall_plugin() only.
148
     * @private
149
     */
150
    public function uninstall_cleanup() {
151
        global $CFG;
152
 
153
        if (!empty($CFG->auth)) {
154
            $auths = explode(',', $CFG->auth);
155
            $auths = array_unique($auths);
156
        } else {
157
            $auths = array();
158
        }
159
        if (($key = array_search($this->name, $auths)) !== false) {
160
            unset($auths[$key]);
161
            $value = implode(',', $auths);
162
            add_to_config_log('auth', $CFG->auth, $value, 'core');
163
            set_config('auth', $value);
1441 ariadna 164
            \core\session\manager::destroy_by_auth_plugin($this->name);
1 efrain 165
        }
166
 
167
        if (!empty($CFG->registerauth) and $CFG->registerauth === $this->name) {
168
            unset_config('registerauth');
169
        }
170
 
171
        parent::uninstall_cleanup();
172
    }
173
}