lib.php 6.04 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage auth-browserid
 * @author     Francois Marier <francois@catalyst.net.nz>
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
Aaron Wells's avatar
Aaron Wells committed
9
 *
10
11
12
13
14
15
16
17
 */

defined('INTERNAL') || die();
require_once(get_config('docroot') . 'auth/lib.php');
require_once(get_config('docroot') . 'lib/institution.php');

class AuthBrowserid extends Auth {

18
    public function __construct($id = null) {
19
20
21
        if (!empty($id)) {
            return $this->init($id);
        }
22
        $this->ready = true;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
        return true;
    }

    public function can_auto_create_users() {
        return false;
    }
}

class PluginAuthBrowserid extends PluginAuth {

    public static function has_config() {
        return true;
    }

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    public static function get_config_options() {
        // Find out how many active users there are, with which instances,
        // in which institutions.
        $instances = get_records_sql_array(
            'SELECT
                i.displayname as displayname,
                i.name as name,
                (
                    SELECT COUNT(*)
                    FROM {usr} u
                    WHERE
                        u.authinstance = ai.id
                        AND deleted = 0
                ) AS numusers
            FROM
                {auth_instance} ai
                INNER JOIN {institution} i
                    ON ai.institution = i.name
            WHERE
                ai.authname=\'browserid\'
            ORDER BY
                i.displayname
            '
60
61
        );

62
63
64
        $elements = array();
        $elements['helptext'] = array(
            'type' => 'html',
65
            'value' => get_string('deprecatedmsg1', 'auth.browserid')
66
67
        );

68
69
70
71
72
73
74
75
76
77
78
79
        if ($instances) {
            $smarty = smarty_core();
            $smarty->assign('instances', $instances);
            $tablehtml = $smarty->fetch('auth:browserid:statustable.tpl');
            $elements['statustable'] = array(
                'type' => 'html',
                'value' => $tablehtml
            );

            $elements['migrate'] = array(
                'type' => 'switchbox',
                'title' => get_string('migratetitle', 'auth.browserid'),
80
                'description' => get_string('migratedesc1', 'auth.browserid'),
81
82
83
                'defaultvalue' => false,
                'help' => true,
            );
84
85
        }
        else {
86
87
88
89
            $elements['noaction'] = array(
                'type' => 'html',
                'value' => get_string('nobrowseridinstances', 'auth.browserid')
            );
90
91
        }

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
        $form = array(
            'elements' => $elements
        );
        if ($instances) {
            $form['elements']['js'] = array(
                'type' => 'html',
                'value' => <<<HTML
<script type="text/javascript">
if (typeof auth_browserid_reload_page === "undefined") {
    var auth_browserid_reload_page = function() {
        window.location.reload(true);
    }
}
</script>
HTML
            );
            $form['jssuccesscallback'] = 'auth_browserid_reload_page';
        }
        return $form;
    }

    public static function save_config_options(Pieform $form, $values) {
        if (!empty($values['migrate'])) {
            $instances = get_records_array('auth_instance', 'authname', 'browserid', 'id');
            foreach ($instances as $authinst) {
                // Are there any users with this auth instance?
                if (record_exists('usr', 'authinstance', $authinst->id)) {

                    // Find the internal auth instance for this institution
                    $internal = get_field('auth_instance', 'id', 'authname', 'internal', 'institution', $authinst->institution);
                    if (!$internal) {
                        // Institution has no internal auth instance. Create one.
                        $todb = new stdClass();
                        $todb->instancename = 'internal';
                        $todb->authname = 'internal';
                        $todb->institution = $authinst->institution;
                        $todb->priority = $authinst->priority;
                        $internal = insert_record('auth_instance', $todb, 'id', true);
                    }
131

132
133
134
135
136
137
138
139
140
141
142
143
144
                    // Set the password & salt for Persona users to "*", which means "no password set"
                    update_record(
                        'usr',
                        (object)array(
                            'password' => '*',
                            'salt' => '*'
                        ),
                        array(
                            'authinstance' => $authinst->id
                        )
                    );
                    set_field('usr', 'authinstance', $internal, 'authinstance', $authinst->id);
                }
145

146
147
148
149
150
151
                // Delete the Persona auth instance
                delete_records('auth_remote_user', 'authinstance', $authinst->id);
                delete_records('auth_instance_config', 'instance', $authinst->id);
                delete_records('auth_instance', 'id', $authinst->id);
                // Make it no longer be the parent authority to any auth instances
                delete_records('auth_instance_config', 'field', 'parent', 'value', $authinst->id);
152
            }
153
            set_field('auth_installed', 'active', 0, 'name', 'browserid');
154
155
156
        }
    }

157
158
    public static function has_instance_config() {
        return false;
159
160
161
    }

    /**
162
163
164
     * Implement the function is_usable()
     *
     * @return boolean true if the BrowserID verifier is usable, false otherwise
165
     */
166
    public static function is_usable() {
167
168
        return false;
    }
169
170

    public static function postinst($fromversion) {
171
172
        // Always deactivate this plugin, if it has been activated somehow.
        set_field('auth_installed', 'active', 0, 'name', 'browserid');
173
174
175
176
177
    }

    public static function can_be_disabled() {
        return true;
    }
178
179
180

    public static function is_deprecated() {
        return true;
181
    }
182
}