-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserController.cs
More file actions
305 lines (263 loc) · 10.3 KB
/
Copy pathUserController.cs
File metadata and controls
305 lines (263 loc) · 10.3 KB
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
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Statutis.API.Form;
using Statutis.API.Models;
using Statutis.Core.Interfaces.Business;
using Statutis.Entity;
namespace Statutis.API.Controllers;
/// <summary>
/// Controleur sur les utilitseurs
/// </summary>
[Tags("Utilisateurs")]
[Route("api/users")]
[ApiController]
[Authorize]
public class UserController : Controller
{
private readonly IUserService _userService;
private readonly IPasswordHash _passwordHash;
/// <summary>
/// Contructeur
/// </summary>
/// <param name="userService"></param>
/// <param name="passwordHash"></param>
public UserController(IUserService userService, IPasswordHash passwordHash)
{
_userService = userService;
_passwordHash = passwordHash;
}
/// <summary>
/// Récupération de l'utilisateur courant
/// </summary>
/// <returns>Un utilisateur</returns>
/// <response code="401">Si vous n'êtes pas authentifié.</response>
[HttpGet("me")]
[Authorize]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserModel))]
[ProducesResponseType(StatusCodes.Status401Unauthorized, Type = typeof(AuthModel))]
public async Task<IActionResult> Get()
{
if (User.Identity == null)
return StatusCode(StatusCodes.Status401Unauthorized, new AuthModel(null, Url));
User? user = await _userService.GetUserAsync(User);
if (user == null)
return StatusCode(StatusCodes.Status401Unauthorized, new AuthModel(null, Url));
return Ok(new UserModel(user, Url));
}
/// <summary>
/// Récupération d'un utilisateur
/// </summary>
/// <param name="email">Adresse mail de l'utilisaur cible</param>
/// <returns>Un utilisateur</returns>
/// <response code="401">Si vous n'êtes pas authentifié.</response>
/// <response code="404">Si l'utilisateur cible n'existe pas.</response>
[HttpGet("email/{email}")]
[Authorize()]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserModel))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetByEmail([Required] string email)
{
if (email == "")
return StatusCode(StatusCodes.Status400BadRequest, "Query parameter email is required !");
User? user = await _userService.GetByEmail(email);
if (user == null)
return NotFound();
return Ok(new UserModel(user, Url));
}
/// <summary>
/// Récupération de tous les utilisateurs
/// </summary>
/// <returns>Liste des utilisateurs</returns>
/// <response code="401">Si vous n'êtes pas authentifié.</response>
[HttpGet("")]
[Authorize]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<UserModel>))]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> GetAll()
{
List<User> users = await _userService.GetAll();
return Ok(users.Select(x => new UserModel(x, Url)));
}
/// <summary>
/// Modification d'un utilisateur
/// </summary>
/// <param name="form">Information sur l'utilisateur</param>
/// <param name="email">Adresse mail de l'utilisaur cible</param>
/// <returns>Un utilisateur</returns>
/// <response code="401">Si vous n'êtes pas authentifié.</response>
/// <response code="403">Si vous n'êtes pas l'utilisateur cible ou un administrateur.</response>
/// <response code="404">Si l'utilisateur cible n'existe pas.</response>
[HttpPut, Route("{email}")]
[Authorize]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserPutModel))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<IActionResult> Update([FromBody] UserPutModel form, String email)
{
if (User.Identity == null || User.Identity.Name == null)
return StatusCode(401, new AuthModel(null, Url));
var user = await _userService.GetUserAsync(User);
if (user == null)
return StatusCode(StatusCodes.Status401Unauthorized, new AuthModel(null, Url));
if (email != User.Identity.Name && !user.IsAdmin())
return StatusCode(StatusCodes.Status403Forbidden, "You don't have enough permissions");
User? targetUser = await _userService.GetByEmail(email);
if (targetUser == null)
return StatusCode(StatusCodes.Status404NotFound, "User not found");
targetUser.Username = form.Username;
targetUser.Firstname = form.Firstname;
targetUser.Name = form.Name;
targetUser.Password = _passwordHash.Hash(form.Password);
var userUpdated = await _userService.Update(targetUser);
return Ok(new UserModel(userUpdated, Url));
}
/// <summary>
/// Modification d'un utilisateur
/// </summary>
/// <param name="email">Adresse mail de l'utilisaur cible</param>
/// <param name="form">Information sur l'utilisateur</param>
/// <returns>Un utilisateur</returns>
/// <response code="401">Si vous n'êtes pas authentifié.</response>
/// <response code="403">Si vous n'êtes pas l'utilisateur cible ou un administrateur.</response>
/// <response code="404">Si l'utilisateur cible n'existe pas.</response>
[HttpPatch, Route("{email}")]
[Authorize]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserModel))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> Update([FromBody] UserPatchModel form, string email)
{
if (User.Identity == null || User.Identity.Name == null)
return StatusCode(401, new AuthModel(null, Url));
var user = await _userService.GetUserAsync(User);
if (user == null)
return StatusCode(StatusCodes.Status401Unauthorized, new AuthModel(null, Url));
if (email != user.Email && !user.IsAdmin())
return StatusCode(StatusCodes.Status403Forbidden, "You don't have enough permissions");
User? targetUser = await _userService.GetByEmail(email);
if (targetUser == null)
return StatusCode(StatusCodes.Status404NotFound, "User not found");
if (!String.IsNullOrWhiteSpace(form.Username))
{
if (form.Username.Length < 3)
return StatusCode(StatusCodes.Status403Forbidden, "The field Username must be a string a minimum length of '3'.");
targetUser.Username = form.Username;
}
if (!String.IsNullOrWhiteSpace(form.Firstname))
{
if (form.Firstname.Length < 3)
return StatusCode(StatusCodes.Status403Forbidden, "The field Firstname must be a string a minimum length of '3'.");
targetUser.Firstname = form.Firstname;
}
if (!String.IsNullOrWhiteSpace(form.Name))
{
if (form.Name.Length < 3)
return StatusCode(StatusCodes.Status403Forbidden, "The field Name must be a string a minimum length of '3'.");
targetUser.Name = form.Name;
}
if (!String.IsNullOrWhiteSpace(form.Password))
{
if (form.Password.Length < 8)
return StatusCode(StatusCodes.Status403Forbidden, "The field Password must be a string a minimum length of '8'.");
targetUser.Password = _passwordHash.Hash(form.Password);
}
var userUpdated = await _userService.Update(user);
return Ok(new UserModel(userUpdated, Url));
}
/// <summary>
/// Recupération de l'avatar de l'utilisateur courant
/// </summary>
/// <see cref="GetAvatar(String)"/>
/// <returns>L'avatar de l'utilisateur courant</returns>
/// <response code="401">Si vous n'êtes pas authentifié.</response>
[HttpGet]
[Authorize]
[Route("avatar")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> GetAvatar()
{
var email = User.Identity?.Name;
if (email == null)
return Unauthorized();
return await GetAvatar(email);
}
/// <summary>
/// Récupération d'un avatar d'un utilisateur
/// </summary>
/// <param name="email">Identifiant d'un utilisateur cible</param>
/// <returns>Avatar de l'utilisateur</returns>
/// <response code="404">Si l'utilisateur visé n'existe pas ou qu'il ne dispose pas d'avatar.</response>
[HttpGet]
[AllowAnonymous]
[Route("avatar/{email}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetAvatar(String email)
{
var targetUser = await _userService.GetByEmail(email);
if (targetUser == null || targetUser.Avatar == null || targetUser.AvatarContentType == null)
return NotFound();
return File(targetUser.Avatar, targetUser.AvatarContentType);
}
/// <summary>
/// Mise à jour de l'avatar de l'utilisateur courant
/// </summary>
/// <see cref="UploadAvatar(string,Microsoft.AspNetCore.Http.IFormFile?)"/>
/// <returns></returns>
/// <response code="401">Si vous n'êtes pas authentifié.</response>
[HttpPut]
[Authorize]
[Route("avatar")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public Task<IActionResult> UploadAvatar(IFormFile? form = null)
{
return UploadAvatar( User.Identity?.Name ?? String.Empty,form);
}
/// <summary>
/// Mettre à jour l'avatar d'un utilisateur
/// </summary>
/// <param name="email">Identifiant de l'utilisateur cible</param>
/// <param name="form">Informations sur le nouvel avatar (null si l'on souhaite supprimer celui courant)</param>
/// <returns></returns>
/// <response code="404">Si l'utilisateur visé n'existe pas.</response>
/// <response code="403">Vous ne disposez pas des droits suffisants.</response>
/// <response code="401">Si vous n'êtes pas authentifié.</response>
[HttpPut]
[Authorize]
[Route("avatar/{email}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<IActionResult> UploadAvatar(string email, IFormFile? form = null)
{
var user = await _userService.GetUserAsync(User);
if (user == null)
return StatusCode(401, new AuthModel(null, Url));
var targetUser = String.IsNullOrWhiteSpace(email) ? user : await _userService.GetByEmail(email);
if (targetUser == null)
return NotFound();
if (user.Email != targetUser.Email && !user.IsAdmin())
return Forbid();
if (form == null)
{
targetUser.Avatar = null;
targetUser.AvatarContentType = null;
}
else
{
using (var memoryStream = new MemoryStream())
{
await form.CopyToAsync(memoryStream);
targetUser.Avatar = memoryStream.ToArray();
}
targetUser.AvatarContentType = form.ContentType;
}
await _userService.Update(targetUser);
return Ok();
}
}