-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphServiceAccessHelper.php
232 lines (230 loc) · 12.8 KB
/
GraphServiceAccessHelper.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
<?php
//Helper file updated to pull configuration settings from WordPress.
//Original sourced from https://github.com/Azure-Samples/active-directory-php-graphapi-web/commit/6da6cd2f8ab14976aa4243d28fa7517d99176297
//Require other files.
require_once 'AuthorizationHelperForGraph.php';
class GraphServiceAccessHelper
{
// Constructs a Http GET request to a feed passed in as paremeter.
// Returns the json decoded respone as the objects that were recieved in feed.
public static function getFeed($feedName){
// initiaze curl which is used to make the http request.
$ch = curl_init();
// Add authorization and other headers. Also set some common settings.
self::AddRequiredHeadersAndSettings($ch);
// set url
$feedURL = "https://graph.windows.net/".B2C_Settings::$tenant."/".$feedName;
$feedURL = $feedURL."?".B2C_Settings::$api_version;
curl_setopt($ch, CURLOPT_URL, $feedURL);
//Enable fiddler to capture request
//curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$jsonOutput = json_decode($output);
// There is a field for odata metadata that we ignore and just consume the value
return $jsonOutput->{'value'};
}
// Constructs a Http GET request to a linked feed based on the source entry and navigation property name.
public static function getLinkedFeed($sourceFeedName, $sourceEntryId, $navigationPropertyName){
//initiaze curl which is used to make the http request
$ch = curl_init();
// Add authorization and other headers. Also set some common settings.
self::AddRequiredHeadersAndSettings($ch);
// create URL to the linked feed by using the navigation property name and the key value for the source entry.
$feedURL = "https://graph.windows.net/".B2C_Settings::$tenant."/".$sourceFeedName .'(\''. $sourceEntryId . '\')' .'/'.$navigationPropertyName;
$feedURL = $feedURL."?".B2C_Settings::$api_version;
curl_setopt($ch, CURLOPT_URL, $feedURL);
//Enable fiddler to capture request
//curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$jsonOutput = json_decode($output);
// There is a field for odata metadata that we ignore and just consume the value
return $jsonOutput->{'value'};
}
// Constructs a Http GET request to a feed passed in as paremeter and uses the field information as the filter clause.
// Returns the json decoded respone as the objects that were recieved in feed.
public static function getFeedWithFilterClause($feedName, $fieldName, $fieldValue){
//initiaze curl which is used to make the http request
$ch = curl_init();
// Add authorization and other headers. Also set some common settings.
self::AddRequiredHeadersAndSettings($ch);
// set url based on the filter clause. This uses the standard OData syntax to create the filter clause.
$feedURL = "https://graph.windows.net/".B2C_Settings::$tenant."/".$feedName .'?$filter='.$fieldName.urlencode(' eq ')
.'(\''.urlencode($fieldValue).'\')';
$feedURL = $feedURL."&api-version=2013-04-05";
curl_setopt($ch, CURLOPT_URL, $feedURL);
//Enable fiddler to capture request
//curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$jsonOutput = json_decode($output);
// There is a field for odata metadata that we ignore and just consume the value
return $jsonOutput->{'value'};
}
// Constructs a Http GET request to fetch an entry based on the feed name and the key value passed in.
// Returns the json decoded respone as the objects that were recieved in feed.
public static function getEntry($feedName, $keyValue){
// initiaze curl which is used to make the http request
$ch = curl_init();
// Add authorization and other headers. Also set some common settings.
self::AddRequiredHeadersAndSettings($ch);
// Create url for the entry based on the feedname and the key value
$feedURL = 'https://graph.windows.net/'.B2C_Settings::$tenant.'/'.$feedName.'(\''. $keyValue .'\')';
$feedURL = $feedURL."?".B2C_Settings::$api_version;
curl_setopt($ch, CURLOPT_URL, $feedURL);
//Enable fiddler to capture request
//curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$jsonOutput = json_decode($output);
return $jsonOutput;
}
// Constructs a HTTP POST request for creating and adding an entry
// to a feed based on the feed name and data passed in.
public static function addEntryToFeed($feedName, $entry){
//initiaze curl which is used to make the http request
$ch = curl_init();
// Add authorization and other headers. Also set some common settings.
self::AddRequiredHeadersAndSettings($ch);
// set url
$feedURL = "https://graph.windows.net/".B2C_Settings::$tenant.'/'.$feedName;
$feedURL = $feedURL."?".B2C_Settings::$api_version;
curl_setopt($ch, CURLOPT_URL, $feedURL);
// Mark as Post request
curl_setopt($ch, CURLOPT_POST, 1);
$data = json_encode($entry);
// Set the data for the post request
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//Enable fiddler to capture request
//curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
// read the output from the post request
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
// decode the response json decoder
$createdEntry = json_decode($output);
return $createdEntry;
}
// Constructs a HTTP POST request for creating and adding a link
// between two existing entries using the source and target URLs and the navigation property name.
public static function addLinkForEntries($sourceEntryUrl, $targetEntryUrl, $navigationPropertyName){
//initiaze curl which is used to make the http request
$ch = curl_init();
// Add authorization and other headers. Also set some common settings.
self::AddRequiredHeadersAndSettings($ch);
// set url
$feedURL = 'https://graph.windows.net/'.B2C_Settings::$tenant.'/'.$sourceEntryUrl.'/$links/'.$navigationPropertyName;
$feedURL = $feedURL."?".B2C_Settings::$api_version;
curl_setopt($ch, CURLOPT_URL, $feedURL);
// Mark as Post request
curl_setopt($ch, CURLOPT_POST, 1);
$data = json_encode($targetEntryUrl);
// Set the data for the post request
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//Enable fiddler to capture request
//curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
// read the output from the post request
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
// decode the response json decoder
$createdEntry = json_decode($output);
return $createdEntry;
}
// Constructs a HTTP PATCH request for updating an entry.
public static function updateEntry($feedName, $keyValue, $entry){
//initiaze curl which is used to make the http request
$ch = curl_init();
self::AddRequiredHeadersAndSettings($ch);
// set url
$feedURL = "https://graph.windows.net/".B2C_Settings::$tenant.'/'.$feedName.'(\''. $keyValue .'\')';
$feedURL = $feedURL."?".B2C_Settings::$api_version;
curl_setopt($ch, CURLOPT_URL, $feedURL);
// Mark as Patch request
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
//Enable fiddler to capture request
//curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
$data = json_encode($entry);
// Set the data for the request
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// read the output from the request
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
// decode the response json decoder
$udpatedEntry = json_decode($output);
return $udpatedEntry;
}
// Constructs a HTTP DELETE request for deleting an entry.
public static function deleteEntry($feedName, $keyValue){
//initiaze curl which is used to make the http request
$ch = curl_init();
self::AddRequiredHeadersAndSettings($ch);
// set url
$feedURL = "https://graph.windows.net/".B2C_Settings::$tenant.'/'.$feedName.'(\''. $keyValue .'\')';
$feedURL = $feedURL."?".B2C_Settings::$api_version;
curl_setopt($ch, CURLOPT_URL, $feedURL);
//Enable fiddler to capture request
//curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
// Mark as Post request
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
// read the output from the post request
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
// decode the response json decoder
$deletedEntry = json_decode($output);
return $deletedEntry;
}
// Constructs a Http GET request get changes for the type passed in.
// Returns the json decoded respone as the objects that were recieved in feed.
public static function getDeltaLinkFeed($feedURL, $feedName){
if ($feedName == NULL)
{
$feedName = "directoryObjects";
}
// initiaze curl which is used to make the http request.
$ch = curl_init();
// Add authorization and other headers. Also set some common settings.
self::AddRequiredHeadersAndSettings($ch);
// set url
if ($feedURL == NULL)
{
$feedURL = "https://graph.windows.net/".B2C_Settings::$tenant."/".$feedName;
$feedURL = $feedURL."?deltaLink=";
}
// add api-version always to indicate the target version
$feedURL = $feedURL."&api-version=2013-04-05";
curl_setopt($ch, CURLOPT_URL, $feedURL);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$jsonOutput = json_decode($output);
// There is a field for odata metadata that we ignore and just consume the value
return $jsonOutput;
}
// Add required headers like authorization header, service version etc.
public static function AddRequiredHeadersAndSettings($ch)
{
//Generate the authentication header
$authHeader = AuthorizationHelperForAADGraphService::GetAuthenticationHeader(B2C_Settings::$tenant, B2C_Settings::$graphID, B2C_Settings::$clientSecret);
// Add authorization header, request/response format header( for json) and a header to request content for Update and delete operations.
curl_setopt($ch, CURLOPT_HTTPHEADER, array($authHeader, 'Accept:application/json;odata=minimalmetadata',
'Content-Type:application/json;odata=minimalmetadata', 'Prefer:return-content'));
// Set the option to recieve the response back as string.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// By default https does not work for CURL.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
}
?>