forked from microsoftgraph/aspnet-snippets-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UsersController.cs
250 lines (214 loc) · 9.83 KB
/
UsersController.cs
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
/*
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
* See LICENSE in the source repository root for complete license information.
*/
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Threading.Tasks;
using Microsoft.Graph;
using Microsoft_Graph_ASPNET_Snippets.Helpers;
using Microsoft_Graph_ASPNET_Snippets.Models;
using System.Linq;
using System.IO;
using Resources;
namespace Microsoft_Graph_ASPNET_Snippets.Controllers.Users
{
[Authorize]
public class UsersController : Controller
{
UsersService usersService = new UsersService();
// Load the view.
public ActionResult Index()
{
return View("Users");
}
// Get all users.
public async Task<ActionResult> GetUsers()
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get users.
results.Items = await usersService.GetUsers(graphClient);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Users", results);
}
// Get the current user's profile.
public async Task<ActionResult> GetMe()
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get the current user's profile.
results.Items = await usersService.GetMe(graphClient);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Users", results);
}
// Get the current user's manager.
public async Task<ActionResult> GetMyManager()
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get the current user's manager.
results.Items = await usersService.GetMyManager(graphClient);
}
// Throws exception if manager is null, with Request_ResourceNotFound code.
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Users", results);
}
// Get the current user's photo.
public async Task<ActionResult> GetMyPhoto()
{
ResultsViewModel results = new ResultsViewModel(false);
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get my photo.
results.Items = await usersService.GetMyPhoto(graphClient);
}
// Throws exception if photo is null, with itemNotFound code.
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Users", results);
}
// Create a new user in the signed-in user's tenant.
// This snippet requires an admin work account.
public async Task<ActionResult> CreateUser()
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Add the user.
results.Items = await usersService.CreateUser(graphClient);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Users", results);
}
// Get a specified user.
public async Task<ActionResult> GetUser(string id)
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get the user.
results.Items = await usersService.GetUser(graphClient, id);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Users", results);
}
// Get a specified user's photo.
public async Task<ActionResult> GetUserPhoto(string id)
{
ResultsViewModel results = new ResultsViewModel(false);
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get the user's photo.
results.Items = await usersService.GetUserPhoto(graphClient, id);
}
// Throws an exception when requesting the photo for unlicensed users (such as those created by this sample), with message "The requested user '<user-name>' is invalid."
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Users", results);
}
// Get the direct reports of a specified user.
public async Task<ActionResult> GetDirectReports(string id)
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get user's direct reports.
results.Items = await usersService.GetDirectReports(graphClient, id);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Users", results);
}
// Update a user.
// This snippet changes the user's display name.
// This snippet requires an admin work account.
public async Task<ActionResult> UpdateUser(string id, string name)
{
ResultsViewModel results = new ResultsViewModel(false);
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Change user display name.
results.Items = await usersService.UpdateUser(graphClient, id, name);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Users", results);
}
// Delete a user. Warning: This operation cannot be undone.
// This snippet requires an admin work account.
public async Task<ActionResult> DeleteUser(string id)
{
ResultsViewModel results = new ResultsViewModel(false);
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Make sure that the current user is not selected.
results.Items = await usersService.DeleteUser(graphClient, id);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Users", results);
}
}
}