-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGraphHelper.php
120 lines (90 loc) · 3.51 KB
/
GraphHelper.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
<?php
class AADSSO_GraphHelper {
public static $ch;
public static $settings;
public static $token;
public static $tenant_id;
// TODO: validation if tenat_id is not set
public static function getResourceUrl() {
return self::$settings->resourceURI . '/' . self::$tenant_id;
}
public static function getUsers() {
$url = self::getResourceUrl() . '/users/' . '?api-version=' . self::$settings->graphVersion;
return self::getRequest( $url );
}
public static function getUserMemberOf( $id ) {
$url = self::getResourceUrl() . '/users/' . $id . '/$links/memberOf?api-version=' . self::$settings->graphVersion;
return self::getRequest( $url );
}
public static function getGroups() {
return self::getRequest( self::getResourceUrl() . '/groups?api-version=' . self::$settings->graphVersion );
}
public static function userCheckMemberGroups( $id, $group_ids ) {
$group_ids = array_filter( $group_ids ); //remove empty elements
$url = self::getResourceUrl() . '/users/' . $id . '/checkMemberGroups?api-version=' . self::$settings->graphVersion;
return self::postRequest( $url, array('groupIds' => $group_ids) );
}
public static function getUser( $id ) {
$url = self::getResourceUrl() . '/users/' . $id . '?api-version=' . self::$settings->graphVersion;
return self::getRequest( $url );
}
public static function updateUser( $id, $data ){
return self::patchRequest(
self::getResourceUrl() . '/users/' . $id . '?api-version=' . self::$settings->graphVersion, $data);
}
public static function getMe(){
return self::getRequest( self::getResourceUrl() . '/me' . '?api-version=' . self::$settings->graphVersion );
}
public static function updateMe( $data ){
return self::patchRequest(
self::getResourceUrl() . '/me' . '?api-version=' . self::$settings->graphVersion, $data);
}
public static function getRequest( $url ) {
return self::request( 'GET', $url );
}
public static function patchRequest( $url, $data ) {
$response = self::postRequest( $url, $data );
// Legacy hack
$payload = json_encode( $data );
$_SESSION['last_request'] = array('method' => 'PATCH', 'url' => $url, 'payload' => $payload);
return $response;
}
public static function postRequest( $url, $data ) {
return self::request( 'POST', $url, $data );
}
public static function request( $method, $url, $data = '' ) {
$headers = self::AddRequiredHeadersAndSettings();
if ( is_wp_error( $headers ) ) {
return $headers;
}
$args = array(
'headers' => $headers,
'method' => $method,
);
$payload = '';
if ( $data ) {
$payload = json_encode( $data );
$args['body'] = $payload;
}
$response = wp_remote_post( $url, $args );
if ( is_wp_error( $response ) ) {
return $response;
}
$output = json_decode( wp_remote_retrieve_body( $response ) );
$_SESSION['last_request'] = array( 'method' => $method, 'url' => $url, 'payload' => $payload );
$_SESSION['last_request']['response'] = $output;
return $output;
}
// Add required headers like authorization header, service version etc.
public static function AddRequiredHeadersAndSettings() {
if ( ! isset( $_SESSION['token_type'], $_SESSION['access_token'] ) ) {
return new WP_Error( 'session_data_missing', 'ERROR: Session data missing (token_type, and/or access_token).' );
}
return array(
'Authorization' => $_SESSION['token_type'] . ' ' . $_SESSION['access_token'],
'Accept' => 'application/json;odata=minimalmetadata',
'Content-Type' => 'application/json;odata=minimalmetadata',
'Prefer' => 'return-content',
);
}
}