-
Notifications
You must be signed in to change notification settings - Fork 1
/
addusertogroup.php
70 lines (60 loc) · 2.26 KB
/
addusertogroup.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
<?php
require_once 'kpasinc/dataporten.inc';
require_once 'kpasinc/vars.inc';
require_once 'kpasinc/curlutility.inc';
require_once 'kpasinc/canvas.inc';
require_once 'kpasinc/utility.inc';
require_once 'kpasinc/kpasapi.inc';
$result = "";
$status_message = "Success";
$token = "";
$jsonGroup = "";
try {
if(!isset($_SERVER['HTTP_X_DATAPORTEN_TOKEN'])) {
throw new Exception("Dataporten token not specified.");
}
$token = $_SERVER['HTTP_X_DATAPORTEN_TOKEN'];
if(!isset($_POST["group"])) {
throw new Exception("Group parameter not specified.");
}
$jsonGroup = $_POST["group"];
$group = json_decode($jsonGroup, true);
if(!$group) {
throw new Exception("Group parameter has invalid syntax.");
}
$jsonUnenrollFrom = $_POST["unenrollFrom"];
mydbg("Unenroll from " . $jsonUnenrollFrom);
$unenrollFrom = json_decode($jsonUnenrollFrom, true);
if($unenrollFrom == FALSE) {
$errCode = json_last_error();
throw new Exception("unenrollFrom parameter has invalid syntax, error_code:" . $errCode);
}
myvardump($unenrollFrom);
//First get Feide ID from dataporten.
$dataportenUserInfo = KPASAPI_GetUserInfo();
if(array_key_exists('error', $dataportenUserInfo))
{
$errorMessage = "Dataporten: " . $dataportenUserInfo["error"] . " " . $dataportenUserInfo["message"];
throw new Exception($errorMessage);
}
//Now search for that Feide ID in Canvas.
$feideid = getFeideIdFromDataportenUserInfo($dataportenUserInfo);
$canvasUser = getCanvasUserFromFeideId($feideid);
if(array_key_exists('errors', $canvasUser)) {
myvardump($canvasUser);
$errorMessage = "Canvas: " . $canvasUser["errors"][0]["message"];
throw new Exception($errorMessage);
}
//Check that we found the Canvas user.
$user_id = getCanvasUserIdFromCanvasUser($canvasUser, $feideid);
if(!$user_id) {
throw new Exception("Kunne ikke finne brukeren " . $feideid . " i Canvas.");
}
//We can add the user to the specified group.
$result = AddUserToGroup($user_id, $group, $unenrollFrom["unenrollmentIds"]);
} catch (Exception $e) {
$result = $e->getMessage();
$status_message = "Failure";
}
KPASAPI_Response(200,$status_message,$result);
?>