Issues (24)

Model/UniLogin.php (5 issues)

1
<?php
2
App::uses('UniLoginWebserviceAppModel', 'UniLoginWebservice.Model');
3
4
/**
5
 * UniLogin Model.
6
 *
7
 */
8
class UniLogin extends UniLoginWebserviceAppModel {
9
10
/**
11
 * The name of the DataSource connection that this Model uses.
12
 *
13
 * @var string
14
 */
15
	public $useDbConfig = 'uniLoginWebservice';
16
17
/**
18
 * Use table.
19
 *
20
 * @var mixed False or table name
21
 */
22
	public $useTable = false;
23
24
/**
25
 * Returns authentication parameters.
26
 *
27
 * @return array Authentication parameters
28
 */
29
	protected function _getAuthParameters() {
30
		$params = [
31
			'wsBrugerid' => Configure::read('UniLoginWebservice.wsBrugerid'),
0 ignored issues
show
The type Configure was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
			'wsPassword' => Configure::read('UniLoginWebservice.wsPassword')
33
		];
34
		return $params;
35
	}
36
37
/**
38
 * Extracts "return"-property from given object.
39
 *
40
 * @param object $data Data to extract "return"-property from
41
 * @return mixed The extracted property (mixed), or false (bool) on failure
42
 */
43
	protected function _extractResult($data) {
44
		if (is_object($data)) {
45
			$property = 'return';
46
			if (property_exists($data, $property)) {
47
				return $data->{$property};
48
			}
49
		}
50
51
		return false;
52
	}
53
54
/**
55
 * Test method that only requires that the firewall is open for the calling IP-number.
56
 *
57
 * @return string
58
 */
59
	public function helloWorld() {
60
		return $this->query('helloWorld');
61
	}
62
63
/**
64
 * Test method that only requires that the firewall is open for the calling IP-number.
65
 *
66
 * @return string
67
 */
68
	public function helloSOAPFaultDemo() {
69
		return $this->query('helloSOAPFaultDemo');
70
	}
71
72
/**
73
 * Returns most information about an institution.
74
 *
75
 *  Wrapper for API call hentInstitution
76
 *
77
 * @param string $instid 6-char institution number (from Danmarks Statistik, e.g. 101001).
78
 * @return array Institution data
79
 */
80
	public function getInstitution($instid) {
81
		$params = $this->_getAuthParameters();
82
		$params['instid'] = $instid;
83
84
		$result = $this->query('hentInstitution', $params);
85
		$result = $this->_extractResult($result);
86
		if ($result) {
87
			$result = $this->_convertInstitution($result);
88
		}
89
90
		return $result;
91
	}
92
93
/**
94
 * Returns a list of institutions where the user "brugerid" has a relation.
95
 *
96
 *  Wrapper for API call hentInstitutionsliste
97
 *
98
 * @param string $brugerid Unique UNI•Login user id.
99
 * @return array List of institutions
100
 */
101
	public function getInstitutions($brugerid) {
102
		$params = $this->_getAuthParameters();
103
		$params['brugerid'] = $brugerid;
104
105
		$result = $this->query('hentInstitutionsliste', $params);
106
		$result = $this->_extractResult($result);
107
		if ($result) {
108
			$result = $this->_convertInstitutionList($result);
109
		}
110
111
		return $result;
112
	}
113
114
/**
115
 * Returns information about a person "brugerid".
116
 * "Institutionsnummer" is the user’s primary institution and "funktionsmarkering" is the relation to this institution.
117
 * Both may be empty if the user has no primary institution.
118
 * Use the method hentInstitutionsliste() to get a list of the institutions where the user has a relation.
119
 *
120
 *  Wrapper for API call hentPerson
121
 *
122
 * @param string $brugerid Unique UNI•Login user id.
123
 * @return array Person data
124
 */
125
	public function getPerson($brugerid) {
126
		$params = $this->_getAuthParameters();
127
		$params['brugerid'] = $brugerid;
128
129
		$result = $this->query('hentPerson', $params);
130
		$result = $this->_extractResult($result);
131
		if ($result) {
132
			$result = $this->_convertUser($result);
133
		}
134
135
		return $result;
136
	}
137
138
/**
139
 * Returns a list of employees at the institution "instnr".
140
 *
141
 *  Wrapper for API call hentAnsatte
142
 *
143
 * @param string $instid 6-char institution number (from Danmarks Statistik, e.g. 101001).
144
 * @return array List of employees
145
 */
146
	public function getEmployees($instid) {
147
		$params = $this->_getAuthParameters();
148
		$params['instid'] = $instid;
149
150
		$result = $this->query('hentAnsatte', $params);
151
		$result = $this->_extractResult($result);
152
		if ($result) {
153
			$result = $this->_convertUserList($result);
154
		}
155
156
		return $result;
157
	}
158
159
/**
160
 * Returns a list of employees with detailed person information at the institution "instnr".
161
 *
162
 * @param string $instid 6-char institution number (from Danmarks Statistik, e.g. 101001).
163
 * @return array List of employees
164
 */
165
	public function getEmployeesWithDetails($instid) {
166
		$result = [];
167
168
		$employees = $this->getEmployees($instid);
169
		if (!empty($employees)) {
170
			foreach ($employees as $employee) {
171
				$result[] = $this->getPerson($employee['uni_login_key']);
172
			}
173
		}
174
175
		return $result;
176
	}
177
178
/**
179
 * Returns a list of all pupils and students at the institution "instnr".
180
 *
181
 *  Wrapper for API call hentAlleElever
182
 *
183
 * @param string $instid 6-char institution number (from Danmarks Statistik, e.g. 101001).
184
 * @return array List of pupils and students
185
 */
186
	public function getStudents($instid) {
187
		$params = $this->_getAuthParameters();
188
		$params['instid'] = $instid;
189
190
		$result = $this->query('hentAlleElever', $params);
191
		$result = $this->_extractResult($result);
192
		if ($result) {
193
			$result = $this->_convertUserList($result);
194
		}
195
196
		return $result;
197
	}
198
199
/**
200
 * Returns a list of students with detailed person information at the institution "instnr".
201
 *
202
 * @param string $instid 6-char institution number (from Danmarks Statistik, e.g. 101001).
203
 * @return array List of students
204
 */
205
	public function getStudentsWithDetails($instid) {
206
		$result = [];
207
208
		$students = $this->getStudents($instid);
209
		if (!empty($students)) {
210
			foreach ($students as $student) {
211
				$result[] = $this->getPerson($student['uni_login_key']);
212
			}
213
		}
214
215
		return $result;
216
	}
217
218
/**
219
 * Converts array of Uni-Login PersonSimpel objects.
220
 *
221
 * @param array $userList Array of Uni-Login PersonSimpel objects
222
 * @return mixed Converted user data (array), or false (bool) on failure
223
 */
224
	protected function _convertUserList($userList) {
225
		$result = false;
226
		if (is_object($userList)) {
0 ignored issues
show
The condition is_object($userList) is always false.
Loading history...
227
			$property = 'PersonSimpel';
228
			if (property_exists($userList, $property)) {
229
				if (is_array($userList->{$property})) {
230
					$minimal = true;
231
					$result = [];
232
					foreach ($userList->{$property} as $user) {
233
						$item = $this->_convertUser($user, $minimal);
234
						if ($item) {
235
							$result[] = $item;
236
						} else {
237
							$result = false;
238
							break;
239
						}
240
					}
241
				}
242
			}
243
		}
244
245
		return $result;
246
	}
247
248
/**
249
 * Converts Uni-Login Institution object.
250
 *
251
 * @param stdClass $institution Uni-Login Institution object
252
 * @param bool $minimal Whether or the given user object is a PersonSimpel object
253
 * @return mixed Converted institution data (array), or false (bool) on failure
254
 */
255
	protected function _convertInstitution($institution, $minimal = false) {
256
		$mapping = [
257
			'uni_login_key' => 'Instnr',
258
			'name' => 'Navn',
259
			'type' => 'Type',
260
			'type_name' => 'Typenavn',
261
			'address' => 'Adresse',
262
			'city' => 'Bynavn',
263
			'zip_code' => 'Postnr',
264
			'phone_number' => 'Telefonnr',
265
			'fax_number' => 'Faxnr',
266
			'mail_address' => 'Mailadresse',
267
			'website' => 'Www',
268
			'parent_institution_uni_login_key' => 'Hovedinstitutionsnr',
269
			'municipal' => 'Kommunenr',
270
			'municipal_name' => 'Kommune',
271
			'administrating_municipal' => 'Admkommunenr',
272
			'administrating_municipal_name' => 'Admkommune',
273
			'region' => 'Regionsnr',
274
			'region_name' => 'Region',
275
		];
276
		if ($minimal) {
277
			$mapping = [
278
				'uni_login_key' => 'Instnr',
279
				'name' => 'Navn',
280
			];
281
		}
282
283
		$result = false;
284
		if (is_object($institution)) {
285
			$result = [];
286
			foreach ($mapping as $name => $property) {
287
				if (!property_exists($institution, $property)) {
288
					$result = false;
289
					break;
290
				}
291
				$result[$name] = $institution->{$property};
292
			}
293
		}
294
295
		return $result;
296
	}
297
298
/**
299
 * Converts array of Uni-Login InstitutionSimpel objects.
300
 *
301
 * @param array $institutionList Array of Uni-Login InstitutionSimpel objects
302
 * @return mixed Converted institution data (array), or false (bool) on failure
303
 */
304
	protected function _convertInstitutionList($institutionList) {
305
		$result = false;
306
		if (is_object($institutionList)) {
0 ignored issues
show
The condition is_object($institutionList) is always false.
Loading history...
307
			$property = 'InstitutionSimpel';
308
			if (property_exists($institutionList, $property)) {
309
				$minimal = true;
310
				$result = [];
311
				if (is_array($institutionList->{$property})) {
312
					foreach ($institutionList->{$property} as $institution) {
313
						$item = $this->_convertInstitution($institution, $minimal);
314
						if ($item) {
315
							$result[] = $item;
316
						} else {
317
							$result = false;
318
							break;
319
						}
320
					}
321
				} elseif (is_object($institutionList->{$property})) {
322
					$institution = $institutionList->{$property};
323
324
					$result[] = $this->_convertInstitution($institution, $minimal);
325
				}
326
			}
327
		}
328
329
		return $result;
330
	}
331
332
/**
333
 * Converts a Uni-Login role.
334
 *
335
 * @param string $role Uni-Login role
336
 * @return mixed Converted role (string), or false (bool) on failure
337
 */
338
	protected function _convertRole($role) {
339
		$mapping = [
340
			'lærer' => 'teacher',
341
			'tap' => 'technical / administrative employee',
342
			'pæd' => 'educator', // Pædagog
343
			'elev' => 'pupil',
344
			'stud' => 'student',
345
			'kursist' => 'anonymous user with limited lifespan',
346
			'klasse' => 'class',
347
			'skole' => 'common school login',
348
			'Instleder' => 'director at institution',
349
			'Instledelse' => 'board of directors',
350
			'Brugeradm' => 'user administrator',
351
			'brugeradm_sup' => 'additional user administrator',
352
			'Kontakt' => 'contact person at institution',
353
			'uni_server_adm' => 'UNI-Server administrator',
354
			'uni_server_indholds_adm' => 'UNI-Server Content administrator',
355
			'hjpc_ansv' => 'HomePC responsible',
356
			'hjpc_ansv_a' => 'HomePC responsible for A-leg',
357
			'hjpc_ansv_p' => 'HomePC responsible for P-leg'
358
		];
359
		return Hash::get($mapping, $role) ?: false;
360
	}
361
362
/**
363
 * Converts Uni-Login Person or PersonSimpel object.
364
 *
365
 * @param stdClass $user Uni-Login Person or PersonSimpel object
366
 * @param bool $minimal Whether or the given user object is a PersonSimpel object
367
 * @return array mixed Converted user data (array), or false (bool) on failure
368
 */
369
	protected function _convertUser($user, $minimal = false) {
370
		$mapping = [
371
			'uni_login_key' => 'Brugerid',
372
			'full_name' => 'Navn',
373
			'first_name' => 'Fornavn',
374
			'last_name' => 'Efternavn',
375
			'username' => 'SkolekomNavn',
376
			'email' => 'Mailadresse',
377
			'school_uni_login_key' => 'Instnr',
378
			'role' => 'Funktionsmarkering',
379
			'date_of_birth' => 'Foedselsdag'
380
		];
381
382
		if ($minimal) {
383
			$mapping = [
384
				'uni_login_key' => 'Brugerid',
385
				'full_name' => 'Navn'
386
			];
387
		}
388
389
		$result = false;
390
		if (is_object($user)) {
391
			$result = [];
392
			foreach ($mapping as $name => $property) {
393
				if (!property_exists($user, $property)) {
394
					$result = false;
395
					break;
396
				}
397
				$result[$name] = $user->{$property};
398
			}
399
		}
400
401
		if ($result) {
402
			$role = Hash::get($result, 'role');
403
			if ($role) {
404
				$result['uni_login_role'] = $role;
405
				$result['role'] = $this->_convertRole($role);
406
			}
407
408
			$dateOfBirth = Hash::get($result, 'date_of_birth');
409
			if ($dateOfBirth) {
410
				$result['date_of_birth'] = $this->_parseDate($dateOfBirth);
411
			}
412
		}
413
414
		return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
415
	}
416
417
/**
418
 * Parse Uni-Login formatted date string.
419
 *
420
 * @param string $dateString Uni-Login formatted date string (ddmmyy)
421
 * @return string Formatted date string (yyyy-mm-dd)
422
 * @return mixed Formatted date (string), or false (bool) on failure
423
 */
424
	protected function _parseDate($dateString) {
425
		$format = 'dmy';
426
427
		$result = date_create_from_format($format, $dateString);
428
		if ($result) {
429
			$result = date_format($result, 'Y-m-d');
430
		}
431
432
		return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
433
	}
434
435
}
436