Metrolog/Metrolog-API/Controllers/UserController.cs
2025-02-15 20:43:28 +01:00

156 lines
4.1 KiB
C#

using Metrolog_API.Attributes;
using Metrolog_API.Models;
using Metrolog_API.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace Metrolog_API.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class UserController : ControllerBase
{
private UserService _UserService;
private PasswordService _PasswordService;
public UserController(UserService UserService, PasswordService passwordService)
{
_UserService = UserService;
_PasswordService = passwordService;
}
// GET: api/<UserController>/me
[HttpGet("me")]
public async Task<ActionResult<UserModel>> ActualUserGet()
{
try
{
UserModel user = await _UserService.GetByEmailAsync(HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email).Value);
user.Password = "";
return Ok(user);
}
catch (Exception ex)
{
return NoContent();
}
}
// PUT api/<UserController>/me
[HttpPut("me")]
public async Task<ActionResult<UserModel>> ActualUserPut([FromBody] UserModel newUser)
{
UserModel user = await _UserService.GetByEmailAsync(HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email).Value);
newUser.Password = user.Password;
newUser.Email = user.Email;
newUser.Id = user.Id;
await _UserService.UpdateAsync(user.Id, newUser);
newUser.Password = "";
return Ok(newUser);
}
// PUT api/<UserController>/me
[HttpPut("me/password")]
public async Task<ActionResult> ActualUserPut([FromBody] PasswordChangeModel pass)
{
UserModel user = await _UserService.GetByEmailAsync(HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email).Value);
if (_PasswordService.VerifyPassword(user.Password, pass.OldPassword))
{
user.Password = _PasswordService.HashPassword(pass.NewPassword);
await _UserService.UpdateAsync(user.Id, user);
return Ok();
}
else
{
return BadRequest("Old password not match");
}
}
// DELETE api/<UserController>/me
[HttpDelete("me")]
public async Task<ActionResult> ActualUserDelete()
{
UserModel user = await _UserService.GetByEmailAsync(HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email).Value);
await _UserService.RemoveAsync(user.Id);
return Ok();
}
// GET: api/<UserController>/[page]
[HttpGet("{page}")]
[Permission("User_List")]
public async Task<ActionResult<List<UserModel>>> UserListGet(int page = 1)
{
List<UserModel> users = new();
foreach (UserModel user in await _UserService.GetPageAsync(page))
{
user.Password = "";
users.Add(user);
}
return Ok(users);
}
// GET api/<UserController>/5
[HttpGet("{id}")]
[Permission("User_Detail")]
public async Task<ActionResult<UserModel>> UserDetailGet(string id)
{
UserModel? user = await _UserService.GetByIdAsync(id);
if (user == null)
return NotFound();
user.Password = "";
return Ok(user);
}
// PUT api/<UserController>/{id}
[HttpPut("{id}")]
[Permission("User_Update")]
public async Task<ActionResult<UserModel>> Put(string id, [FromBody] UserModel newUser)
{
UserModel user = await _UserService.GetByIdAsync(id);
newUser.Password = user.Password;
newUser.Id = user.Id;
await _UserService.UpdateAsync(user.Id, newUser);
newUser.Password = "";
return Ok(newUser);
}
// PUT api/<UserController>/{id}/password
[HttpPut("{id}")]
[Permission("User_Update")]
public async Task<ActionResult> PasswordPut(string id, [FromBody] PasswordChangeModel pass)
{
UserModel user = await _UserService.GetByIdAsync(id);
if(user == null)
return NotFound("User not found");
user.Password = _PasswordService.HashPassword(pass.NewPassword);
await _UserService.UpdateAsync(user.Id, user);
return Ok();
}
// DELETE api/<UserController>/5
[HttpDelete("{id}")]
[Permission("User_Remove")]
public async Task<ActionResult> Delete(string id)
{
await _UserService.RemoveAsync(id);
return Ok();
}
}
}