addauthority.php 6.83 KB
Newer Older
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
6
 *
Francois Marier's avatar
Francois Marier committed
7
8
9
10
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
11
 *
Francois Marier's avatar
Francois Marier committed
12
13
14
15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
16
 *
Francois Marier's avatar
Francois Marier committed
17
18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
21
 *
 * @package    mahara
 * @subpackage admin
22
 * @author     Catalyst IT Ltd
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
25
26
27
28
29
30
31
 *
 */
define('INTERNAL', 1);
define('ADMIN', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
define('TITLE', get_string('institutions', 'admin'));
require_once('pieforms/pieform.php');
32
require_once(get_config('docroot') . '/lib/htmloutput.php');
33
34
35
36
37
38
39
40
41
42

// CHECK FOR CANCEL BEFORE THE 'REQUIRED' PARAMS:
$cancel = param_boolean('c');

if ($cancel) {
    execute_javascript_and_close();
}

// NOT CANCELLING? OK - OTHER PARAMS THEN:
$institution = param_variable('i');
43
$plugin      = param_variable('p');
44
45
46
47
48
49
$add         = param_boolean('add');
$edit        = param_boolean('edit');
$json        = param_boolean('j');
$instanceid  = param_variable('id', 0);

// IF WE'RE EDITING OR CREATING AN AUTHORITY:
50
if ($institution && $plugin) {
51
52
    $classname = 'PluginAuth' . ucfirst(strtolower($plugin));
    safe_require('auth', strtolower($plugin));
53

54
55
    $has_instance_config = call_static_method($classname, 'has_instance_config');
    if (false == $has_instance_config && $add) {
56
57
58
59

        // We've been asked to add an instance of an auth plugin that has no
        // config options. We've been called by an AJAX request, so we just
        // add the instance and generate an acknowledgement.
60
61
62
63
64
65
66
67
68
69
70
71
72
        $authinstance = new stdClass();

        // Get the auth instance with the highest priority number (which is
        // the instance with the lowest priority).
        // TODO: rethink 'priority' as a fieldname... it's backwards!!
        $lastinstance = get_records_array('auth_instance', 'institution', $institution, 'priority DESC', '*', '0', '1');

        if ($lastinstance == false) {
            $authinstance->priority = 0;
        } else {
            $authinstance->priority = $lastinstance[0]->priority + 1;
        }

73
        $authinstance->instancename = $plugin;
74
        $authinstance->institution  = $institution;
75
        $authinstance->authname     = $plugin;
76
77
78
79
80
        $authinstance->id           = insert_record('auth_instance', $authinstance, 'id', true);
        json_reply(false, array('id' => $authinstance->id, 'name' => ucfirst($authinstance->authname), 'authname' => $authinstance->authname));
        exit;
    }

81
    $authclass = new $classname();
82
    $form = $authclass->get_instance_config_options($institution, $instanceid);
83
84
    $form['name'] = 'auth_config';
    $form['plugintype'] = 'auth';
85
    $form['pluginname'] = strtolower($plugin);
86
87
88
89
90
91
92

    $form['elements']['submit'] = array(
        'type' => 'submitcancel',
        'value' => array(get_string('submit'), get_string('cancel')),
        'goto'  => 'addauthority.php?c=1'
    );

93
    $form = pieform($form);
94
    $smarty = smarty();
95
96
97
98
99
    if ($add) {
        $smarty->assign('PAGETITLE', get_string('addauthority', 'auth'));
    } else {
        $smarty->assign('PAGETITLE', get_string('editauthority', 'auth'));
    }
100
    $smarty->assign('auth_imap_form', $form);
101
102
}

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
function auth_config_validate(Pieform $form, $values) {
    $plugin = $values['authname'];
    $classname = 'PluginAuth' . ucfirst(strtolower($plugin));

    if (!method_exists($classname, 'validate_config_options')) {
        return;
    }
    safe_require('auth', strtolower($plugin));

    try {
        $values = call_static_method($classname, 'validate_config_options', $values, $form);
    } catch (Exception $e) {
        if (!$form->has_errors()) {
            $form->set_error('instancename', "An unknown error occurred while processing this form");
        }
    }
}

121
function auth_config_submit(Pieform $form, $values) {
122
    global $SESSION;
123
124
125
    $plugin = $values['authname'];
    $classname = 'PluginAuth' . ucfirst(strtolower($plugin));
    safe_require('auth', strtolower($plugin));
126
127
128
    try {
        $values = call_static_method($classname, 'save_config_options', $values, $form);
    } catch (Exception $e) {
129
130
131
        log_info($e->getMessage());
        log_info($e->getTrace());
        $SESSION->add_error_msg("An error occurred while processing this form: " . $e->getMessage());
132
        redirect('/admin/users/addauthority.php?'. $_SERVER['QUERY_STRING']);
133
134
    }

135
136
137
138
    if (array_key_exists('create', $values) && $values['create']) {
        execute_javascript_and_close('window.opener.addAuthority('.$values['instance'].', "'.addslashes($values['instancename']).'", "'.$values['authname'].'");');
    } else {
        execute_javascript_and_close();
139
    }
140
    exit;
141
142
}

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
$js = <<<EOF
function authloginmsgVisibility() {
    // If Parent authority is 'None'
    if ($('auth_config_parent').value != 0) {
      addElementClass('auth_config_authloginmsg_container', 'hidden');
      addElementClass(nextSiblingTR($('auth_config_authloginmsg_container')), 'hidden');
    }
    else {
      removeElementClass('auth_config_authloginmsg_container', 'hidden');
      removeElementClass(nextSiblingTR($('auth_config_authloginmsg_container')), 'hidden');
    }
}
function nextSiblingTR(node) {
    while (node.nextSibling.tagName != 'TR') {
        node = node.nextSibling;
    }
    return node.nextSibling;
}
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var ssoAllOptions = {
    'updateuserinfoonlogin': 'theyssoin',
    'weautocreateusers': 'theyssoin',
    'theyautocreateusers': 'wessoout',
    'weimportcontent': 'theyssoin'
};
function updateSsoOptions() {
    var current = $('auth_config_ssodirection').value;
    for (var opt in ssoAllOptions) {
        if (ssoAllOptions[opt] == current) {
            removeElementClass('auth_config_' + opt + '_container', 'hidden');
        }
        else {
            addElementClass('auth_config_' + opt + '_container', 'hidden');
        }
    }
}
178
179
180
181
addLoadEvent(
    function() {
        connect('auth_config_parent', 'onchange', authloginmsgVisibility);
        authloginmsgVisibility();
182
183
184
185
        if ($('auth_config_ssodirection')) {
            connect('auth_config_ssodirection', 'onchange', updateSsoOptions);
        }
        updateSsoOptions();
186
187
188
189
190
    }
);
EOF;

$smarty->assign('INLINEJAVASCRIPT', $js);
191
$smarty->display('admin/users/addauthority.tpl');
192
?>