/
UniLogin.php
435 lines (387 loc) · 11.1 KB
/
UniLogin.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
App::uses('UniLoginWebserviceAppModel', 'UniLoginWebservice.Model');
/**
* UniLogin Model.
*
*/
class UniLogin extends UniLoginWebserviceAppModel {
/**
* The name of the DataSource connection that this Model uses.
*
* @var string
*/
public $useDbConfig = 'uniLoginWebservice';
/**
* Use table.
*
* @var mixed False or table name
*/
public $useTable = false;
/**
* Returns authentication parameters.
*
* @return array Authentication parameters
*/
protected function _getAuthParameters() {
$params = [
'wsBrugerid' => Configure::read('UniLoginWebservice.wsBrugerid'),
'wsPassword' => Configure::read('UniLoginWebservice.wsPassword')
];
return $params;
}
/**
* Extracts "return"-property from given object.
*
* @param object $data Data to extract "return"-property from
* @return mixed The extracted property (mixed), or false (bool) on failure
*/
protected function _extractResult($data) {
if (is_object($data)) {
$property = 'return';
if (property_exists($data, $property)) {
return $data->{$property};
}
}
return false;
}
/**
* Test method that only requires that the firewall is open for the calling IP-number.
*
* @return string
*/
public function helloWorld() {
return $this->query('helloWorld');
}
/**
* Test method that only requires that the firewall is open for the calling IP-number.
*
* @return string
*/
public function helloSOAPFaultDemo() {
return $this->query('helloSOAPFaultDemo');
}
/**
* Returns most information about an institution.
*
* Wrapper for API call hentInstitution
*
* @param string $instid 6-char institution number (from Danmarks Statistik, e.g. 101001).
* @return array Institution data
*/
public function getInstitution($instid) {
$params = $this->_getAuthParameters();
$params['instid'] = $instid;
$result = $this->query('hentInstitution', $params);
$result = $this->_extractResult($result);
if ($result) {
$result = $this->_convertInstitution($result);
}
return $result;
}
/**
* Returns a list of institutions where the user "brugerid" has a relation.
*
* Wrapper for API call hentInstitutionsliste
*
* @param string $brugerid Unique UNI•Login user id.
* @return array List of institutions
*/
public function getInstitutions($brugerid) {
$params = $this->_getAuthParameters();
$params['brugerid'] = $brugerid;
$result = $this->query('hentInstitutionsliste', $params);
$result = $this->_extractResult($result);
if ($result) {
$result = $this->_convertInstitutionList($result);
}
return $result;
}
/**
* Returns information about a person "brugerid".
* "Institutionsnummer" is the user’s primary institution and "funktionsmarkering" is the relation to this institution.
* Both may be empty if the user has no primary institution.
* Use the method hentInstitutionsliste() to get a list of the institutions where the user has a relation.
*
* Wrapper for API call hentPerson
*
* @param string $brugerid Unique UNI•Login user id.
* @return array Person data
*/
public function getPerson($brugerid) {
$params = $this->_getAuthParameters();
$params['brugerid'] = $brugerid;
$result = $this->query('hentPerson', $params);
$result = $this->_extractResult($result);
if ($result) {
$result = $this->_convertUser($result);
}
return $result;
}
/**
* Returns a list of employees at the institution "instnr".
*
* Wrapper for API call hentAnsatte
*
* @param string $instid 6-char institution number (from Danmarks Statistik, e.g. 101001).
* @return array List of employees
*/
public function getEmployees($instid) {
$params = $this->_getAuthParameters();
$params['instid'] = $instid;
$result = $this->query('hentAnsatte', $params);
$result = $this->_extractResult($result);
if ($result) {
$result = $this->_convertUserList($result);
}
return $result;
}
/**
* Returns a list of employees with detailed person information at the institution "instnr".
*
* @param string $instid 6-char institution number (from Danmarks Statistik, e.g. 101001).
* @return array List of employees
*/
public function getEmployeesWithDetails($instid) {
$result = [];
$employees = $this->getEmployees($instid);
if (!empty($employees)) {
foreach ($employees as $employee) {
$result[] = $this->getPerson($employee['uni_login_key']);
}
}
return $result;
}
/**
* Returns a list of all pupils and students at the institution "instnr".
*
* Wrapper for API call hentAlleElever
*
* @param string $instid 6-char institution number (from Danmarks Statistik, e.g. 101001).
* @return array List of pupils and students
*/
public function getStudents($instid) {
$params = $this->_getAuthParameters();
$params['instid'] = $instid;
$result = $this->query('hentAlleElever', $params);
$result = $this->_extractResult($result);
if ($result) {
$result = $this->_convertUserList($result);
}
return $result;
}
/**
* Returns a list of students with detailed person information at the institution "instnr".
*
* @param string $instid 6-char institution number (from Danmarks Statistik, e.g. 101001).
* @return array List of students
*/
public function getStudentsWithDetails($instid) {
$result = [];
$students = $this->getStudents($instid);
if (!empty($students)) {
foreach ($students as $student) {
$result[] = $this->getPerson($student['uni_login_key']);
}
}
return $result;
}
/**
* Converts array of Uni-Login PersonSimpel objects.
*
* @param array $userList Array of Uni-Login PersonSimpel objects
* @return mixed Converted user data (array), or false (bool) on failure
*/
protected function _convertUserList($userList) {
$result = false;
if (is_object($userList)) {
$property = 'PersonSimpel';
if (property_exists($userList, $property)) {
if (is_array($userList->{$property})) {
$minimal = true;
$result = [];
foreach ($userList->{$property} as $user) {
$item = $this->_convertUser($user, $minimal);
if ($item) {
$result[] = $item;
} else {
$result = false;
break;
}
}
}
}
}
return $result;
}
/**
* Converts Uni-Login Institution object.
*
* @param stdClass $institution Uni-Login Institution object
* @param bool $minimal Whether or the given user object is a PersonSimpel object
* @return mixed Converted institution data (array), or false (bool) on failure
*/
protected function _convertInstitution($institution, $minimal = false) {
$mapping = [
'uni_login_key' => 'Instnr',
'name' => 'Navn',
'type' => 'Type',
'type_name' => 'Typenavn',
'address' => 'Adresse',
'city' => 'Bynavn',
'zip_code' => 'Postnr',
'phone_number' => 'Telefonnr',
'fax_number' => 'Faxnr',
'mail_address' => 'Mailadresse',
'website' => 'Www',
'parent_institution_uni_login_key' => 'Hovedinstitutionsnr',
'municipal' => 'Kommunenr',
'municipal_name' => 'Kommune',
'administrating_municipal' => 'Admkommunenr',
'administrating_municipal_name' => 'Admkommune',
'region' => 'Regionsnr',
'region_name' => 'Region',
];
if ($minimal) {
$mapping = [
'uni_login_key' => 'Instnr',
'name' => 'Navn',
];
}
$result = false;
if (is_object($institution)) {
$result = [];
foreach ($mapping as $name => $property) {
if (!property_exists($institution, $property)) {
$result = false;
break;
}
$result[$name] = $institution->{$property};
}
}
return $result;
}
/**
* Converts array of Uni-Login InstitutionSimpel objects.
*
* @param array $institutionList Array of Uni-Login InstitutionSimpel objects
* @return mixed Converted institution data (array), or false (bool) on failure
*/
protected function _convertInstitutionList($institutionList) {
$result = false;
if (is_object($institutionList)) {
$property = 'InstitutionSimpel';
if (property_exists($institutionList, $property)) {
$minimal = true;
$result = [];
if (is_array($institutionList->{$property})) {
foreach ($institutionList->{$property} as $institution) {
$item = $this->_convertInstitution($institution, $minimal);
if ($item) {
$result[] = $item;
} else {
$result = false;
break;
}
}
} elseif (is_object($institutionList->{$property})) {
$institution = $institutionList->{$property};
$result[] = $this->_convertInstitution($institution, $minimal);
}
}
}
return $result;
}
/**
* Converts a Uni-Login role.
*
* @param string $role Uni-Login role
* @return mixed Converted role (string), or false (bool) on failure
*/
protected function _convertRole($role) {
$mapping = [
'lærer' => 'teacher',
'tap' => 'technical / administrative employee',
'pæd' => 'educator', // Pædagog
'elev' => 'pupil',
'stud' => 'student',
'kursist' => 'anonymous user with limited lifespan',
'klasse' => 'class',
'skole' => 'common school login',
'Instleder' => 'director at institution',
'Instledelse' => 'board of directors',
'Brugeradm' => 'user administrator',
'brugeradm_sup' => 'additional user administrator',
'Kontakt' => 'contact person at institution',
'uni_server_adm' => 'UNI-Server administrator',
'uni_server_indholds_adm' => 'UNI-Server Content administrator',
'hjpc_ansv' => 'HomePC responsible',
'hjpc_ansv_a' => 'HomePC responsible for A-leg',
'hjpc_ansv_p' => 'HomePC responsible for P-leg'
];
return Hash::get($mapping, $role) ?: false;
}
/**
* Converts Uni-Login Person or PersonSimpel object.
*
* @param stdClass $user Uni-Login Person or PersonSimpel object
* @param bool $minimal Whether or the given user object is a PersonSimpel object
* @return array mixed Converted user data (array), or false (bool) on failure
*/
protected function _convertUser($user, $minimal = false) {
$mapping = [
'uni_login_key' => 'Brugerid',
'full_name' => 'Navn',
'first_name' => 'Fornavn',
'last_name' => 'Efternavn',
'username' => 'SkolekomNavn',
'email' => 'Mailadresse',
'school_uni_login_key' => 'Instnr',
'role' => 'Funktionsmarkering',
'date_of_birth' => 'Foedselsdag'
];
if ($minimal) {
$mapping = [
'uni_login_key' => 'Brugerid',
'full_name' => 'Navn'
];
}
$result = false;
if (is_object($user)) {
$result = [];
foreach ($mapping as $name => $property) {
if (!property_exists($user, $property)) {
$result = false;
break;
}
$result[$name] = $user->{$property};
}
}
if ($result) {
$role = Hash::get($result, 'role');
if ($role) {
$result['uni_login_role'] = $role;
$result['role'] = $this->_convertRole($role);
}
$dateOfBirth = Hash::get($result, 'date_of_birth');
if ($dateOfBirth) {
$result['date_of_birth'] = $this->_parseDate($dateOfBirth);
}
}
return $result;
}
/**
* Parse Uni-Login formatted date string.
*
* @param string $dateString Uni-Login formatted date string (ddmmyy)
* @return string Formatted date string (yyyy-mm-dd)
* @return mixed Formatted date (string), or false (bool) on failure
*/
protected function _parseDate($dateString) {
$format = 'dmy';
$result = date_create_from_format($format, $dateString);
if ($result) {
$result = date_format($result, 'Y-m-d');
}
return $result;
}
}