forked from patrickpollet/mahara_plugin_auth_cas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginAuthCas.class.php
365 lines (318 loc) · 13.5 KB
/
PluginAuthCas.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2009 Catalyst IT Ltd (http://www.catalyst.net.nz)
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage auth-cas
* @author Patrick Pollet <pp@patrickpollet.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2011 Catalyst IT Ltd http://catalyst.net.nz
* @copyright (C) 2011 INSA de Lyon France
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Moodle - Modular Object-Oriented Dynamic Learning Environment
* http://moodle.com
*
* Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com
*
* 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 2 of the License, or
* (at your option) any later version.
*
* 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:
*
* http://www.gnu.org/copyleft/gpl.html
*/
/**
* Plugin configuration class
*/
class PluginAuthCas extends PluginAuthLdap {
const NAME = 'cas';
const CAS_HOSTNAME = 'cas_hostname';
const CAS_PORT = 'cas_port';
const CAS_VERSION = 'cas_version';
const CAS_BASEURI = 'cas_baseuri';
const CAS_LANGUAGE = 'cas_language';
const CAS_PROXY = 'cas_proxy';
const CAS_LOGOUT = 'cas_logout';
const CAS_CERTIFICATECHECK = 'cas_certificatecheck';
const CAS_CERTIFICATEPATH = 'cas_certificatepath';
private static $default_config = array(
self::CAS_HOSTNAME => '',
self::CAS_PORT => '443',
self::CAS_VERSION => CAS_VERSION_2_0,
self::CAS_BASEURI => '',
self::CAS_LANGUAGE => 'English',
self::CAS_PROXY => '0',
self::CAS_LOGOUT => '0',
self::CAS_CERTIFICATECHECK => '',
self::CAS_CERTIFICATEPATH => '',
);
public static function get_cron() {
return array(
(object)array(
'callfunction' => 'auth_cas_sync_cron',
'minute' => '30',
'hour' => '0',
),
);
}
/**
* Synchronize users and groups with the LDAP server
*/
public static function auth_cas_sync_cron() {
$auths = get_records_array('auth_instance', 'authname', 'cas', 'id', 'id');
if (!$auths) {
return;
}
foreach ($auths as $auth) {
/* @var $authobj AuthCas */
$authobj = AuthFactory::create($auth->id);
// Each instance will decide for itself whether it should sync users and/or groups
// User sync needs to be called before group sync in order for new users to wind
// up in the correct groups
$authobj->sync_users();
$authobj->sync_groups();
}
}
public static function has_config () {
return false;
}
public static function get_config_options () {
return array();
}
public static function has_instance_config () {
return true;
}
public static function can_be_disabled () {
return true;
}
public static function is_usable() {
// this definitively prohibits adding a second instnace of the pluin
//but the error message is wrong . It display $string['notusable'] from auth/ldap plugin
// and not mine redefined in auth/cas
// Ensure that plugin is not already in use by another institution
if (parent::is_usable() && !count_records('auth_instance', 'authname', self::NAME)) {
return true;
}
return false;
}
public static function get_instance_config_options ($institution, $instance = 0) {
// Define all values for CAS languages.
$CASLANGUAGES = array(
'Catalan' => 'Catalan',
'English' => 'English',
'French' => 'French',
'German' => 'German',
'Greek' => 'Greek',
'Japanese' => 'Japanese',
'Spanish' => 'Spanish'
);
//currently only two versions
$CASVERSIONS = array();
$CASVERSIONS[CAS_VERSION_1_0] = 'CAS 1.0';
$CASVERSIONS[CAS_VERSION_2_0] = 'CAS 2.0';
//get LDAP instance values from parent class
$parent = parent::get_instance_config_options ($institution, $instance);
// change what must be changed
unset($parent['elements']['authname']); // we shall change it to cas
unset($parent['elements']['institution']); //pieforms does not want hidden in fieldsets
unset($parent['elements']['instance']);
// we want the instance name at the 1st item as usual
$first = array_shift ($parent['elements']); // ['instancename'];
// add CAS specific informations
// first read possible values from DB
if ($instance > 0) {
$default = get_record ('auth_instance', 'id', $instance);
if ($default == false) {
throw new SystemException('Could not find data for auth instance ' . $instance);
}
$current_config = get_records_menu ('auth_instance_config', 'instance', $instance, '', 'field, value');
if ($current_config == false) {
$current_config = array();
}
foreach (self::$default_config as $key => $value) {
if (array_key_exists ($key, $current_config)) {
self::$default_config[$key] = $current_config[$key];
}
}
} else {
$default = new stdClass();
$default->instancename = '';
}
$elements = array(
// the name of the instance
'instancename' => $first,
//the 3 hidden fields that cannot be in fieldsets
'instance' => array(
'type' => 'hidden',
'value' => $instance,
),
'institution' => array(
'type' => 'hidden',
'value' => $institution,
),
'authname' => array(
'type' => 'hidden',
'value' => self::NAME,
),
// then two fielddsets
'fsCAS' => array(
'type' => 'fieldset',
'legend' => get_string ('cassettings', 'auth.cas'),
'collapsible' => true,
'collapsed' => true,
'elements' => array(
self::CAS_HOSTNAME => array(
'type' => 'text',
'title' => get_string (self::CAS_HOSTNAME, 'auth.cas'),
'rules' => array(
'required' => true,
),
'defaultvalue' => self::$default_config[self::CAS_HOSTNAME],
'help' => true,
),
self::CAS_BASEURI => array(
'type' => 'text',
'title' => get_string (self::CAS_BASEURI, 'auth.cas'),
'rules' => array(
'required' => false,
),
'defaultvalue' => self::$default_config[self::CAS_BASEURI],
'help' => true,
),
self::CAS_PORT => array(
'type' => 'text',
'title' => get_string (self::CAS_PORT, 'auth.cas'),
'rules' => array(
'required' => true,
),
'defaultvalue' => self::$default_config[self::CAS_PORT],
'help' => true,
),
self::CAS_VERSION => array(
'type' => 'select',
'title' => get_string (self::CAS_VERSION, 'auth.cas'),
'options' => $CASVERSIONS,
'rules' => array(
'required' => true,
),
'defaultvalue' => self::$default_config[self::CAS_VERSION],
'help' => true,
),
self::CAS_VERSION => array(
'type' => 'select',
'title' => get_string (self::CAS_VERSION, 'auth.cas'),
'options' => $CASVERSIONS,
'rules' => array(
'required' => true,
),
'defaultvalue' => self::$default_config[self::CAS_VERSION],
'help' => true,
),
self::CAS_LANGUAGE => array(
'type' => 'select',
'title' => get_string (self::CAS_LANGUAGE, 'auth.cas'),
'options' => $CASLANGUAGES,
'rules' => array(
'required' => true,
),
'defaultvalue' => self::$default_config[self::CAS_LANGUAGE],
'help' => true,
),
self::CAS_PROXY => array(
'type' => 'checkbox',
'title' => get_string (self::CAS_PROXY, 'auth.cas'),
'defaultvalue' => self::$default_config[self::CAS_PROXY],
'help' => true,
),
self::CAS_LOGOUT => array(
'type' => 'checkbox',
'title' => get_string (self::CAS_LOGOUT, 'auth.cas'),
'defaultvalue' => self::$default_config[self::CAS_LOGOUT],
'help' => true,
),
self::CAS_CERTIFICATECHECK => array(
'type' => 'checkbox',
'title' => get_string (self::CAS_CERTIFICATECHECK, 'auth.cas'),
'defaultvalue' => self::$default_config[self::CAS_CERTIFICATECHECK],
'help' => true,
),
self::CAS_CERTIFICATEPATH => array(
'type' => 'text',
'title' => get_string (self::CAS_CERTIFICATEPATH, 'auth.cas'),
'defaultvalue' => self::$default_config[self::CAS_CERTIFICATEPATH],
'help' => true,
),
),
),
);
$elements = array_merge($elements, $parent['elements']);
return array(
'elements' => $elements,
'renderer' => 'table'
);
}
/**
*
* override PluginAuthLdap::save_instance_config_options...
* @param unknown_type $values
* @param unknown_type $form
*/
public static function save_instance_config_options ($values, $form) {
//pp_error_log('values', $values);
// let parent take care of the LDAP settings and of creating the authinstance if needed
$values = parent::save_instance_config_options ($values, $form);
//pp_error_log('values II', $values);
//at this stage the instance does exist
$current = get_records_assoc ('auth_instance_config', 'instance', $values['instance'], '', 'field, value');
if (empty($current)) {
$current = array();
}
self::$default_config = array(
self::CAS_HOSTNAME => $values[self::CAS_HOSTNAME],
self::CAS_PORT => $values[self::CAS_PORT],
self::CAS_VERSION => $values[self::CAS_VERSION],
self::CAS_BASEURI => $values[self::CAS_BASEURI],
self::CAS_LANGUAGE => $values[self::CAS_LANGUAGE],
self::CAS_PROXY => $values[self::CAS_PROXY],
self::CAS_LOGOUT => $values[self::CAS_LOGOUT],
self::CAS_CERTIFICATECHECK => $values[self::CAS_CERTIFICATECHECK],
self::CAS_CERTIFICATEPATH => $values[self::CAS_CERTIFICATEPATH],
);
//pp_error_log('def_conf', self::$default_config);
foreach (self::$default_config as $field => $value) {
$record = new stdClass();
$record->instance = $values['instance'];
$record->field = $field;
$record->value = $value;
if ($values['create'] || !array_key_exists ($field, $current)) {
insert_record ('auth_instance_config', $record);
}
else {
update_record ('auth_instance_config', $record, array('instance' => $values['instance'], 'field' => $field));
}
}
return $values;
}
}