-
Notifications
You must be signed in to change notification settings - Fork 105
/
GraphRunnerGUI.html
2016 lines (1701 loc) · 71.8 KB
/
GraphRunnerGUI.html
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GraphRunner</title>
<style>
body {
font-family: Courier, sans-serif;
margin: 0;
padding: 0;
background-color: #303030;
color: #ffffff;
}
.container {
max-width: 1000px;
margin: 0 auto;
padding: 20px;
background-color: #505050;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
}
h1 {
text-align: center;
margin-bottom: 20px;
color: #FFFFFF;
}
h3 {
text-align: center;
margin-bottom: 20px;
color: #FFFFFF;
}
label {
font-weight: bold;
}
input[type="text"]{
padding: 8px;
font-size: 14px;
border-radius: 4px;
border: 1px solid #ccc;
background-color: #fff;
width: 100%;
max-width: 400px;
}
input[type="submit"],
textarea,
select {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="file"] {
padding: 5px;
}
button {
padding: 10px 20px;
background-color: #357EC7;
color: #000;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #B4CFEC;
}
.response {
border: 1px solid #ccc;
padding: 4px;
border-radius: 4px;
background-color: #303030;
white-space: pre-wrap;
}
.item-list {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.item-card {
border: 1px solid #ccc;
border-radius: 8px;
padding: 10px;
width: 200px;
background-color: #fff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
}
.item-card:hover {
transform: translateY(-5px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.item-thumbnail {
max-width: 100%;
height: auto;
border-radius: 4px;
}
.item-title {
font-size: 18px;
font-weight: bold;
margin: 8px 0;
}
.item-description {
font-size: 14px;
color: #666;
}
.item-action {
display: inline-block;
padding: 6px 12px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 4px;
text-decoration: none;
font-size: 14px;
transition: background-color 0.2s ease-in-out;
}
.item-action:hover {
background-color: #0056b3;
}
/* Email Viewer Styles */
.email-controls {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 20px;
}
.search-input {
flex: 1;
padding: 10px;
border: 1px solid #ccc;
border-radius: 0px;
font-size: 14px;
margin-right: 10px;
max-width: 40%;
}
.search-button {
padding: 10px 20px;
background-color: #357EC7;
color: #000;
border: none;
border-radius: 4px;
cursor: pointer;
}
.search-button:hover {
background-color: #B4CFEC;
}
.email-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(600px, 1fr));
gap: 8px;
}
.email-summary {
padding: 10px;
border: 1px solid #ccc;
background-color: #303030;
border-radius: 4px;
cursor: pointer;
transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
}
.email-summary:hover {
transform: translateY(-5px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.email-detail {
margin-top: 20px;
padding: 4px;
border: 1px solid #ccc;
background-color: #dddddd;
border-radius: 4px;
color: #000000;
}
.email-detail h3 {
margin-bottom: 10px;
color: #000000;
}
.email-detail strong {
font-weight: bold;
}
.email-detail p {
margin: 10px 0;
font-size: 14px;
color: #FFFFFF;
}
.email-detail a {
color: #B0E0E6;
text-decoration: none;
transition: color 0.2s ease-in-out;
}
.email-detail a:hover {
color: #0056b3;
}
select#httpMethod {
padding: 8px;
font-size: 14px;
border-radius: 4px;
border: 1px solid #ccc;
background-color: #fff;
width: 100%;
max-width: 80px;
}
#chatViewer {
margin-bottom: 20px;
}
.chat-summary {
border: 1px solid #ccc;
border-radius: 8px;
padding: 10px;
width: 100%;
background-color: #303030;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
cursor: pointer;
margin-bottom: 10px;
}
.chat-summary:hover {
transform: translateY(-5px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
#chatMessages {
border: 1px solid #ccc;
padding: 10px;
background-color: #f9f9f9;
border-radius: 4px;
margin-top: 20px;
color: #000000;
}
.message {
border: 1px solid #ccc;
border-radius: 4px;
margin-bottom: 10px;
padding: 10px;
background-color: #f9f9f9;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.message-header {
font-weight: bold;
margin-bottom: 5px;
}
.message-content {
margin-top: 5px;
}
/* Style for image attachments */
.message-content img {
max-width: 100%;
height: auto;
border: 1px solid #ccc;
border-radius: 4px;
margin-top: 5px;
}
/* Style for file links */
.message-content a {
color: #007bff;
text-decoration: none;
transition: color 0.2s ease-in-out;
}
.message-content a:hover {
color: #0056b3;
}
.file-card {
border: 1px solid #ccc;
padding: 10px;
margin: 10px 0;
background-color: #f9f9f9;
border-radius: 5px;
box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1);
}
.file-name {
font-weight: bold;
color: #333;
}
.file-details {
margin-top: 5px;
font-size: 14px;
color: #666;
}
.file-link {
color: #007bff;
text-decoration: none;
}
.item-name {
font-weight: bold;
color: #333;
}
.item-details {
margin-top: 5px;
font-size: 14px;
color: #666;
}
.sharepoint-drive-summary {
border: 1px solid #ccc;
border-radius: 8px;
padding: 10px;
width: 100%;
background-color: #303030;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
cursor: pointer;
margin-bottom: 10px;
}
.sharepoint-drive-summary:hover {
transform: translateY(-5px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.sharepoint-item-summary {
border: 1px solid #ccc;
border-radius: 8px;
padding: 10px;
width: 100%;
background-color: #303030;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
cursor: pointer;
margin-bottom: 10px;
}
.sharepoint-item-summary:hover {
transform: translateY(-5px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body>
<div class="container">
<h1>GraphRunner</h1>
<h3>A GUI for the Microsoft Graph API</h3>
<!-- Token Input -->
<label for="token">Access Token:</label>
<input type="text" id="token" name="token" required>
<button id="parseTokenButton">Parse Token</button>
<pre class="response" id="jwtFields"></pre>
<hr>
<!-- API Request Form -->
<h2>Custom API Request</h2>
<form id="apiRequestForm">
<label for="apiEndpoint">API Endpoint:</label>
<input type="text" id="apiEndpoint" name="apiEndpoint" value="https://graph.microsoft.com/v1.0/me" required>
<!-- Dropdown for HTTP Method -->
<label for="httpMethod">HTTP Method:</label>
<select id="httpMethod" name="httpMethod">
<option value="GET">GET</option>
<option value="POST">POST</option>
<option value="PUT">PUT</option>
<option value="OPTIONS">OPTIONS</option>
</select>
<!-- Additional input for POST/PUT request body -->
<label for="requestBody" id="requestBodyLabel">Request Body:</label>
<textarea id="requestBody" name="requestBody" rows="4"></textarea>
<button type="submit">Send Request</button>
</form>
<pre class="response" id="apiResponse"></pre>
<hr>
<!-- List All Users Button -->
<h2>Directory - Users</h2>
<p><strong>Required Perms:</strong> User.ReadBasic.All, User.Read.All, User.ReadWrite.All, Directory.Read.All, or Directory.ReadWrite.All</p>
<button id="listUsersButton">List Users</button>
<button id="exportUsersButton">Export</button>
<pre class="response" id="listUsersResponse"></pre>
<hr>
<div id="groupExplorerContainer" class="group-explorer">
<div class="group-explorer-header">
<h2>Directory - Groups</h2>
<p><strong>Required Perms:</strong> GroupMember.Read.All, Group.Read.All, Directory.Read.All, Group.ReadWrite.All, or Directory.ReadWrite.All</p>
<button id="listGroupsButton">List Groups</button>
<button id="exportButton">Export</button>
</div>
<div id="groupList" class="group-list"></div>
<div id="groupMembers" class="group-members"></div>
</div>
<!-- Email Viewer -->
<h2>Email Viewer (Current User)</h2>
<p><strong>Required Perms:</strong> Mail.ReadBasic, Mail.Read, or Mail.ReadWrite</p>
<button id="fetchEmailsButton">Fetch Emails</button>
<button id="exportEmailsButton">Export</button>
<button id="searchButton" class="search-button">Search</button>
<br></br>
<div class="email-controls">
<input type="text" id="searchInput" class="search-input" placeholder="Search emails...">
</div>
<div class="email-list" id="emailList"></div>
<div class="email-detail" id="emailDetail"></div>
<hr>
<!-- Other User Email Viewer -->
<h2>Email Viewer (Other Users)</h2>
<p><strong>Required Perms:</strong> Mail.Read.Shared or Mail.ReadWrite.Shared</p>
<form id="customEmailViewerForm">
<label for="customUserId">User ID:</label>
<br>
<input type="text" id="customUserId" name="customUserId" required>
<br></br>
<label for="customFolder">Folder:</label>
<br>
<input type="text" id="customFolder" name="customFolder" value="Inbox" required>
<br></br>
<button type="submit">Fetch Emails</button>
</form>
<div id="customEmailList"></div>
<div id="customEmailDetail"></div>
<hr>
<!-- Send Email Form -->
<h2>Send Email</h2>
<p><strong>Required Perms:</strong> Mail.Send</p>
<form id="sendEmailForm">
<label for="to">To:</label>
<input type="text" id="to" name="to" required>
<label for="subject">Subject:</label>
<input type="text" id="subject" name="subject" required>
<label for="body">Body:</label>
<textarea id="body" name="body" rows="4" required></textarea>
<label for="attachments">Attachments:</label>
<input type="file" id="attachments" name="attachments" multiple>
<button type="submit">Send Email</button>
</form>
<div id="emailSuccessMessage"></div>
<div id="emailErrorMessage"></div>
<hr>
<!-- Button to Fetch Teams Chats -->
<h2>Teams Chat Viewer (Direct Messages and Group Chat)</h2>
<p><strong>Required Perms:</strong> Chat.ReadBasic, Chat.Read, or Chat.ReadWrite</p>
<div id="chatViewer">
<!-- Fetch Chats Button -->
<button id="fetchChatsButton">Fetch Teams Chats</button>
<!-- Chat List -->
<div id="chatList" class="item-list"></div>
<!-- Chat Messages -->
<div id="chatMessages" class="item-list"></div>
<!-- Send Message Form -->
<h4>Send Message to Teams Chat:</h4>
<p><strong>Required Perms:</strong> ChatMessage.Send or Chat.ReadWrite</p>
<input type="text" id="messageTextBox" placeholder="Type your message...">
<button id="sendMessageButton">Send Message</button>
</div>
<hr>
<!-- List Public Teams Chats -->
<h2>Teams Chat Viewer (Org Teams Channels)</h2>
<p><strong>Required Perms:</strong> Team.ReadBasic.All, TeamSettings.Read.All, TeamSettings.ReadWrite.All, User.Read.All, or User.ReadWrite.All</p>
<button id="listPublicChatsButton">List Channels</button>
<div id="publicChatList"></div>
<!-- Chat Messages -->
<div id="publicChatMessages"></div>
<hr>
<!-- List OneDrive Files and Folders -->
<h2>OneDrive My Files</h2>
<p><strong>Required Perms:</strong> Files.Read, Files.ReadWrite, Files.Read.All, Files.ReadWrite.All, Sites.Read.All, or Sites.ReadWrite.All</p>
<button id="listFilesButton">List Files</button>
<div id="fileList"></div>
<hr>
<!-- List OneDrive Shared Files -->
<h2>OneDrive Shared Files</h2>
<p><strong>Required Perms:</strong> Files.Read, Files.ReadWrite, Files.Read.All, Files.ReadWrite.All, Sites.Read.All, or Sites.ReadWrite.All</p>
<button id="listSharedFilesButton">List Shared Files</button>
<div id="sharedFileList"></div>
<hr>
<!-- List SharePoint Files and Folders -->
<h2>SharePoint</h2>
<p><strong>Required Perms:</strong>Sites.Read.All or Sites.ReadWrite.All AND Files.Read, Files.ReadWrite, Files.Read.All, or Files.ReadWrite.All</p>
<button id="listDrivesButton">List SharePoint Root Drives</button>
<h4>Drive List</h4>
<div id="driveList"></div>
<h4>Files and Folders</h4>
<div id="sharePointDriveFileList"></div>
<br>
<button id="listSitesButton">List SharePoint Sites</button>
<h4>Site List</h4>
<div id="siteList"></div>
<h4>Files and Folders</h4>
<div id="sharePointfileList"></div>
<hr>
</div>
<script>
const tokenInput = document.getElementById('token');
const apiRequestForm = document.getElementById('apiRequestForm');
const apiResponse = document.getElementById('apiResponse');
const fetchEmailsButton = document.getElementById('fetchEmailsButton');
const emailList = document.getElementById('emailList');
const emailDetail = document.getElementById('emailDetail');
const sendEmailForm = document.getElementById('sendEmailForm');
const sendEmailResponse = document.getElementById('sendEmailResponse');
const FileReader = window.FileReader;
// CUSTOM GRAPH API QUERY
apiRequestForm.addEventListener('submit', async function(event) {
event.preventDefault();
const token = tokenInput.value;
const apiEndpoint = document.getElementById('apiEndpoint').value;
const httpMethod = document.getElementById('httpMethod').value;
const headers = {
'Authorization': `Bearer ${token}`
};
// Check if POST or PUT method selected
let requestBody = null;
if (httpMethod === 'POST' || httpMethod === 'PUT') {
requestBody = document.getElementById('requestBody').value;
headers['Content-Type'] = 'application/json';
}
try {
const requestOptions = {
method: httpMethod,
headers
};
if (requestBody) {
requestOptions.body = requestBody;
}
const response = await fetch(apiEndpoint, requestOptions);
const data = await response.json();
apiResponse.textContent = JSON.stringify(data, null, 2);
} catch (error) {
apiResponse.textContent = 'Error: ' + error.message;
}
});
//MAIL - VIEWER
// Function to export emails to a CSV file
function exportEmailsToCSV(emails) {
const csvContent = [
'From,To,Subject,Date,Body'
];
emails.forEach(email => {
const from = email.from.emailAddress.name + ' (' + email.from.emailAddress.address + ')';
const to = email.toRecipients.map(recipient => recipient.emailAddress.address).join(', ');
const subject = email.subject;
const date = new Date(email.sentDateTime).toLocaleString();
const body = email.bodyPreview;
csvContent.push(`"${from}","${to}","${subject}","${date}","${body}"`);
});
const csvText = csvContent.join('\n');
const blob = new Blob([csvText], { type: 'text/csv' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'emails.csv';
a.click();
}
// Fetch and display emails
fetchEmailsButton.addEventListener('click', async function() {
const token = tokenInput.value;
const apiEndpoint = 'https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messages';
const headers = {
'Authorization': `Bearer ${token}`
};
try {
const response = await fetch(apiEndpoint, { headers });
const data = await response.json();
emailList.innerHTML = '';
data.value.forEach(email => {
const emailSummary = `
<div class="email-summary" data-id="${email.id}">
<strong>From:</strong> ${email.from.emailAddress.name} (${email.from.emailAddress.address})<br>
<strong>Subject:</strong> ${email.subject}<br>
<strong>Date:</strong> ${new Date(email.sentDateTime).toLocaleString()}<br>
<strong>Preview:</strong> ${email.bodyPreview}<br>
</div>
<hr>
`;
emailList.innerHTML += emailSummary;
});
// MAIL VIEWER - Event listener for email summaries
const emailSummaries = document.querySelectorAll('.email-summary');
emailSummaries.forEach(emailSummary => {
emailSummary.addEventListener('click', async () => {
const emailId = emailSummary.getAttribute('data-id');
const emailDetailEndpoint = `https://graph.microsoft.com/v1.0/me/messages/${emailId}`;
try {
const emailDetailResponse = await fetch(emailDetailEndpoint, { headers });
const emailDetailData = await emailDetailResponse.json();
emailDetail.innerHTML = `
<h3>Email Details</h3>
<strong>From:</strong> ${emailDetailData.from.emailAddress.name} (${emailDetailData.from.emailAddress.address})<br>
<strong>Subject:</strong> ${emailDetailData.subject}<br>
<strong>Date:</strong> ${new Date(emailDetailData.sentDateTime).toLocaleString()}<br>
<strong>Body:</strong> ${emailDetailData.body.content}
`;
} catch (error) {
emailDetail.innerHTML = `Error fetching email details: ${error.message}`;
}
});
});
// Store email data for exporting
window.emailsForExport = data.value;
} catch (error) {
emailList.innerHTML = `Error fetching emails: ${error.message}`;
}
});
// Export Emails
exportEmailsButton.addEventListener('click', function() {
if (window.emailsForExport) {
exportEmailsToCSV(window.emailsForExport);
} else {
alert('No email data available for export.');
}
});
// Search Emails
searchButton.addEventListener('click', async function() {
const token = tokenInput.value;
const searchInput = document.getElementById('searchInput').value.trim();
if (searchInput) {
const apiEndpoint = `https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messages?$search="${searchInput}"`;
const headers = {
'Authorization': `Bearer ${token}`
};
try {
const response = await fetch(apiEndpoint, { headers });
const data = await response.json();
// Store search results for exporting
window.emailsForExport = data.value;
// Call displayEmails with search results and headers
displayEmails(data.value, headers);
} catch (error) {
emailList.innerHTML = `Error searching emails: ${error.message}`;
}
} else {
alert('Please enter a search term.');
}
});
// Function to search display emails
function displayEmails(emails, headers) {
emailList.innerHTML = '';
emails.forEach(email => {
const emailSummary = `
<div class="email-summary" data-id="${email.id}">
<strong>From:</strong> ${email.from.emailAddress.name} (${email.from.emailAddress.address})<br>
<strong>Subject:</strong> ${email.subject}<br>
<strong>Date:</strong> ${new Date(email.sentDateTime).toLocaleString()}<br>
<strong>Preview:</strong> ${email.bodyPreview}<br>
</div>
`;
emailList.innerHTML += emailSummary;
});
// Add event listener for email summaries
const emailSummaries = document.querySelectorAll('.email-summary');
emailSummaries.forEach(emailSummary => {
emailSummary.addEventListener('click', async () => {
const emailId = emailSummary.getAttribute('data-id');
const emailDetailEndpoint = `https://graph.microsoft.com/v1.0/me/messages/${emailId}`;
try {
const emailDetailResponse = await fetch(emailDetailEndpoint, { headers });
const emailDetailData = await emailDetailResponse.json();
emailDetail.innerHTML = `
<h3>Email Details</h3>
<strong>From:</strong> ${emailDetailData.from.emailAddress.name} (${emailDetailData.from.emailAddress.address})<br>
<strong>Subject:</strong> ${emailDetailData.subject}<br>
<strong>Date:</strong> ${new Date(emailDetailData.sentDateTime).toLocaleString()}<br>
<strong>Body:</strong> ${emailDetailData.body.content}
`;
} catch (error) {
emailDetail.innerHTML = `Error fetching email details: ${error.message}`;
}
});
});
}
// LIST USERS
// Function to export users to a text file
function exportUsersToFile(users) {
const userText = users.join('\n');
const blob = new Blob([userText], { type: 'text/plain' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'users.txt';
a.click();
}
// List Users
listUsersButton.addEventListener('click', async function() {
const token = tokenInput.value;
try {
const allUsers = await fetchAllUsers(token);
// Display userPrincipalName fields only
const userPrincipalNames = allUsers.map(user => user.userPrincipalName);
listUsersResponse.textContent = userPrincipalNames.join('\n');
// Store userPrincipalNames for exporting
window.userPrincipalNamesForExport = userPrincipalNames;
} catch (error) {
listUsersResponse.textContent = 'Error: ' + error.message;
}
});
// Function to fetch all users using pagination
async function fetchAllUsers(token) {
const initialUrl = 'https://graph.microsoft.com/v1.0/users';
let allUsers = [];
let url = initialUrl;
while (url) {
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${token}`
}
});
const data = await response.json();
if (data.value) {
allUsers = allUsers.concat(data.value);
}
url = data['@odata.nextLink'];
}
return allUsers;
}
// Export Users
exportUsersButton.addEventListener('click', function() {
if (window.userPrincipalNamesForExport) {
exportUsersToFile(window.userPrincipalNamesForExport);
} else {
alert('No user data available for export.');
}
});
// DIRECTORY GROUPS
const groupApiEndpoint = 'https://graph.microsoft.com/v1.0/groups';
// Function to list groups
async function listGroups(token) {
const headers = {
'Authorization': `Bearer ${token}`
};
try {
const response = await fetch(groupApiEndpoint, { headers });
const data = await response.json();
return data.value;
} catch (error) {
console.error('Error listing groups:', error);
return [];
}
}
// Function to generate group list
function generateGroupList(parentNode, groups) {
const ul = document.createElement('ul');
parentNode.appendChild(ul);
groups.forEach(group => {
const li = document.createElement('li');
ul.appendChild(li);
const groupNode = document.createElement('div');
groupNode.classList.add('group-node');
groupNode.textContent = group.displayName;
li.appendChild(groupNode);
groupNode.addEventListener('click', async () => {
const members = await fetchGroupMembers(group.id, tokenInput.value);
showGroupMembers(members);
});
});
}
// Function to fetch group members
async function fetchGroupMembers(groupId, token) {
const membersApiEndpoint = `https://graph.microsoft.com/v1.0/groups/${groupId}/members`;
const headers = {
'Authorization': `Bearer ${token}`
};
try {
const response = await fetch(membersApiEndpoint, { headers });
const data = await response.json();
return data.value;
} catch (error) {
console.error('Error fetching group members:', error);
return [];
}
}
// Function to display group members
function showGroupMembers(members) {
const groupMembersContainer = document.getElementById('groupMembers');
groupMembersContainer.innerHTML = '';
const membersList = document.createElement('ul');
groupMembersContainer.appendChild(membersList);
members.forEach(member => {
const memberItem = document.createElement('li');
memberItem.innerHTML = `<strong>Name:</strong> ${member.displayName}<br><strong>Email:</strong> ${member.userPrincipalName}<br><strong>ID:</strong> ${member.id}`;
membersList.appendChild(memberItem);
});
}
// List Groups button click event
const listGroupsButton = document.getElementById('listGroupsButton');
const groupList = document.getElementById('groupList');
listGroupsButton.addEventListener('click', async () => {
groupList.innerHTML = ''; // Clear existing content
const groups = await listGroups(tokenInput.value); // Replace with your token retrieval logic
generateGroupList(groupList, groups);
});
function exportGroupMembersToCSV(members) {
// Create CSV content
let csvContent = "ID,Name,Email\n";
members.forEach(member => {
csvContent += `${member.id},${member.displayName},${member.userPrincipalName}\n`;
});
// Create a Blob containing the CSV content
const blob = new Blob([csvContent], { type: 'text/csv' });
// Create a download link for the Blob
const downloadLink = document.createElement('a');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'group_members.csv';
downloadLink.click();
// Revoke the object URL to free up resources
URL.revokeObjectURL(downloadLink.href);
}
// Attach export button click event
const exportButton = document.getElementById('exportButton');
exportButton.addEventListener('click', async function () {
const displayedGroupMembers = await fetchDisplayedGroupMembers();
if (displayedGroupMembers.length > 0) {
exportGroupMembersToCSV(displayedGroupMembers);
} else {
alert('No group members data available for export.');
}
});
// Function to fetch the currently displayed group's members
async function fetchDisplayedGroupMembers() {
const groupMembersContainer = document.getElementById('groupMembers');
const memberItems = groupMembersContainer.getElementsByTagName('li');
const token = tokenInput.value;
const members = [];
for (const memberItem of memberItems) {
const name = memberItem.querySelector('strong:nth-child(1)').textContent.split(':')[1].trim();
const email = memberItem.querySelector('strong:nth-child(2)').textContent.split(':')[1].trim();
const id = memberItem.querySelector('strong:nth-child(3)').textContent.split(':')[1].trim();
members.push({ id, displayName: name, userPrincipalName: email });
}
return members;
}
// MAIL - CUSTOM EMAIL VIEWER
const customEmailViewerForm = document.getElementById('customEmailViewerForm');
const customEmailList = document.getElementById('customEmailList');
const customEmailDetail = document.getElementById('customEmailDetail');
customEmailViewerForm.addEventListener('submit', async function(event) {
event.preventDefault();
const token = tokenInput.value;
const customUserId = document.getElementById('customUserId').value;
const customFolder = document.getElementById('customFolder').value;
const customApiEndpoint = `https://graph.microsoft.com/v1.0/users/${customUserId}/mailFolders/${customFolder}/messages`;
const headers = {
'Authorization': `Bearer ${token}`
};
try {
const response = await fetch(customApiEndpoint, { headers });
const data = await response.json();
customEmailList.innerHTML = '';
data.value.forEach(email => {
const customEmailSummary = `
<div class="email-summary" data-id="${email.id}">
<strong>From:</strong> ${email.sender ? `${email.sender.emailAddress.name} (${email.sender.emailAddress.address})` : 'Unknown'}<br>
<strong>Subject:</strong> ${email.subject}<br>
<strong>Date:</strong> ${new Date(email.sentDateTime).toLocaleString()}<br>
<strong>Preview:</strong> ${email.bodyPreview}<br>
</div>
<hr>
`;
customEmailList.innerHTML += customEmailSummary;
});
// MAIL - CUSTOM EMAIL VIEWER - Add event listener to custom email summaries
const customEmailSummaries = document.querySelectorAll('.email-summary');
customEmailSummaries.forEach(customEmailSummary => {
customEmailSummary.addEventListener('click', async () => {
const customEmailId = customEmailSummary.getAttribute('data-id');
const customEmailDetailEndpoint = `https://graph.microsoft.com/v1.0/users/${customUserId}/messages/${customEmailId}`;
try {
const customEmailDetailResponse = await fetch(customEmailDetailEndpoint, { headers });
const customEmailDetailData = await customEmailDetailResponse.json();
customEmailDetail.innerHTML = `
<h3>Email Details</h3>
<strong>From:</strong> ${customEmailDetailData.sender ? `${customEmailDetailData.sender.emailAddress.name} (${customEmailDetailData.sender.emailAddress.address})` : 'Unknown'}<br>
<strong>Subject:</strong> ${customEmailDetailData.subject}<br>
<strong>Date:</strong> ${new Date(customEmailDetailData.sentDateTime).toLocaleString()}<br>
<strong>Body:</strong> ${customEmailDetailData.body.content}
`;
} catch (error) {
customEmailDetail.innerHTML = `Error fetching email details: ${error.message}`;
}
});
});
} catch (error) {