addauthority.php 6.61 KB
Newer Older
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
5
 *
Francois Marier's avatar
Francois Marier committed
6
7
8
9
 * 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.
10
 *
Francois Marier's avatar
Francois Marier committed
11
12
13
14
 * 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.
15
 *
Francois Marier's avatar
Francois Marier committed
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20
 *
 * @package    mahara
 * @subpackage admin
21
 * @author     Catalyst IT Ltd
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
23
 * @copyright  (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 *
 */
define('INTERNAL', 1);
define('ADMIN', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
define('TITLE', get_string('institutions', 'admin'));
require_once('pieforms/pieform.php');

// 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');
41
$plugin      = param_variable('p');
42
43
44
45
46
47
$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:
48
if ($institution && $plugin) {
49
50
    $classname = 'PluginAuth' . ucfirst(strtolower($plugin));
    safe_require('auth', strtolower($plugin));
51

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

        // 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.
58
59
60
61
62
63
64
65
66
67
68
69
70
        $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;
        }

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

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

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

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

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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");
        }
    }
}

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

    if (false == $form->has_errors()) {
133
        if (array_key_exists('create', $values) && $values['create']) {
134
135
136
137
138
            execute_javascript_and_close('window.opener.addAuthority('.$values['instance'].', "'.addslashes($values['instancename']).'", "'.$values['authname'].'");');
        } else {
            execute_javascript_and_close();
        }
        exit;
139
    }
140
    redirect($_SERVER['REQUEST_URI']);
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
}

// TODO: move to lib if people want this:
function execute_javascript_and_close($js='') {
    echo '<html>
    <head>
        <title>You may close this window</title>
        <script language="Javascript">
            function closeMe() { 
                '.$js.'
                window.close();
            }
        </script>
    </head>
    <body onLoad="closeMe();" style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; text-align: center;">This window should close automatically</body>'.
    "\n</html>";
    exit;
}

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
$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;
}
addLoadEvent(
    function() {
        connect('auth_config_parent', 'onchange', authloginmsgVisibility);
        authloginmsgVisibility();
    }
);
EOF;

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