2024-02-14 23:45:28 +00:00
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
2024-01-16 07:03:35 +00:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
|
2023-08-19 00:15:02 +00:00
|
|
|
namespace BotSharp.OpenAPI.Controllers;
|
|
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
[ApiController]
|
2023-12-27 15:53:54 +00:00
|
|
|
public class UserController : ControllerBase
|
2023-08-19 00:15:02 +00:00
|
|
|
{
|
2024-02-14 23:45:28 +00:00
|
|
|
private readonly IServiceProvider _services;
|
2023-08-19 00:15:02 +00:00
|
|
|
private readonly IUserService _userService;
|
2024-02-14 23:45:28 +00:00
|
|
|
public UserController(IUserService userService, IServiceProvider services)
|
2023-08-19 00:15:02 +00:00
|
|
|
{
|
2024-02-14 23:45:28 +00:00
|
|
|
_services = services;
|
2023-08-19 00:15:02 +00:00
|
|
|
_userService = userService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
[HttpPost("/token")]
|
2024-01-16 07:03:35 +00:00
|
|
|
public async Task<ActionResult<Token>> GetToken([FromHeader(Name = "Authorization")][Required] string authcode)
|
2023-08-19 00:15:02 +00:00
|
|
|
{
|
2024-01-16 07:03:35 +00:00
|
|
|
if (authcode.Contains(' '))
|
|
|
|
|
{
|
|
|
|
|
authcode = authcode.Split(' ')[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var token = await _userService.GetToken(authcode);
|
|
|
|
|
|
2023-08-19 00:15:02 +00:00
|
|
|
if (token == null)
|
|
|
|
|
{
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
|
|
|
|
return Ok(token);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 23:45:28 +00:00
|
|
|
[AllowAnonymous]
|
|
|
|
|
[HttpGet("/sso/{provider}")]
|
2024-05-30 09:11:42 +00:00
|
|
|
public async Task<IActionResult> Authorize([FromRoute] string provider, string redirectUrl)
|
2024-02-14 23:45:28 +00:00
|
|
|
{
|
2024-02-17 14:16:21 +00:00
|
|
|
return Challenge(new AuthenticationProperties { RedirectUri = redirectUrl }, provider);
|
2024-02-14 23:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
[HttpGet("/signout")]
|
|
|
|
|
[HttpPost("/signout")]
|
|
|
|
|
public IActionResult SignOutCurrentUser()
|
|
|
|
|
{
|
|
|
|
|
// Instruct the cookies middleware to delete the local cookie created
|
|
|
|
|
// when the user agent is redirected from the external identity provider
|
|
|
|
|
// after a successful authentication flow (e.g Google or Facebook).
|
|
|
|
|
return SignOut(new AuthenticationProperties { RedirectUri = "/" },
|
|
|
|
|
CookieAuthenticationDefaults.AuthenticationScheme);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-19 00:15:02 +00:00
|
|
|
[AllowAnonymous]
|
|
|
|
|
[HttpPost("/user")]
|
|
|
|
|
public async Task<UserViewModel> CreateUser(UserCreationModel user)
|
|
|
|
|
{
|
|
|
|
|
var createdUser = await _userService.CreateUser(user.ToUser());
|
|
|
|
|
return UserViewModel.FromUser(createdUser);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 01:34:29 +00:00
|
|
|
[AllowAnonymous]
|
|
|
|
|
[HttpPost("/user/activate")]
|
|
|
|
|
public async Task<ActionResult<Token>> ActivateUser(UserActivationModel model)
|
|
|
|
|
{
|
|
|
|
|
var token = await _userService.ActiveUser(model);
|
|
|
|
|
if (token == null)
|
|
|
|
|
{
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
}
|
|
|
|
|
return Ok(token);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 23:45:28 +00:00
|
|
|
[HttpGet("/user/me")]
|
2023-08-19 00:15:02 +00:00
|
|
|
public async Task<UserViewModel> GetMyUserProfile()
|
|
|
|
|
{
|
|
|
|
|
var user = await _userService.GetMyProfile();
|
2024-02-14 23:45:28 +00:00
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
var identiy = _services.GetRequiredService<IUserIdentity>();
|
|
|
|
|
var accessor = _services.GetRequiredService<IHttpContextAccessor>();
|
|
|
|
|
var claims = accessor.HttpContext.User.Claims;
|
2024-02-16 01:47:02 +00:00
|
|
|
user = await _userService.CreateUser(new User
|
2024-02-14 23:45:28 +00:00
|
|
|
{
|
2024-02-16 01:47:02 +00:00
|
|
|
Email = identiy.Email,
|
|
|
|
|
UserName = identiy.UserName,
|
|
|
|
|
FirstName = identiy.FirstName,
|
|
|
|
|
LastName = identiy.LastName,
|
|
|
|
|
Source = claims.First().Issuer,
|
|
|
|
|
ExternalId = identiy.Id,
|
|
|
|
|
});
|
2024-02-14 23:45:28 +00:00
|
|
|
}
|
2023-08-19 00:15:02 +00:00
|
|
|
return UserViewModel.FromUser(user);
|
|
|
|
|
}
|
2024-05-28 11:47:48 +00:00
|
|
|
|
2024-06-02 02:15:47 +00:00
|
|
|
[AllowAnonymous]
|
2024-05-29 02:50:41 +00:00
|
|
|
[HttpGet("/user/name/existing")]
|
2024-05-30 09:11:42 +00:00
|
|
|
public async Task<bool> VerifyUserNameExisting([FromQuery] string userName)
|
2024-05-28 11:47:48 +00:00
|
|
|
{
|
2024-05-30 09:11:42 +00:00
|
|
|
return await _userService.VerifyUserNameExisting(userName);
|
2024-05-28 11:47:48 +00:00
|
|
|
}
|
|
|
|
|
|
2024-06-02 02:15:47 +00:00
|
|
|
[AllowAnonymous]
|
2024-05-29 02:50:41 +00:00
|
|
|
[HttpGet("/user/email/existing")]
|
2024-05-30 09:11:42 +00:00
|
|
|
public async Task<bool> VerifyEmailExisting([FromQuery] string email)
|
2024-05-28 11:47:48 +00:00
|
|
|
{
|
2024-05-30 09:11:42 +00:00
|
|
|
return await _userService.VerifyEmailExisting(email);
|
2024-05-28 11:47:48 +00:00
|
|
|
}
|
2024-06-28 09:59:49 +00:00
|
|
|
[AllowAnonymous]
|
|
|
|
|
[HttpPost("/user/verifycode")]
|
|
|
|
|
public async Task<bool> SendVerificationCodeResetPassword([FromQuery] UserCreationModel user)
|
|
|
|
|
{
|
|
|
|
|
return await _userService.SendVerificationCodeResetPassword(user.ToUser());
|
|
|
|
|
}
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
[HttpPost("/user/resetpassword")]
|
2024-06-29 10:42:16 +00:00
|
|
|
public async Task<bool> ResetUserPassword([FromBody] UserResetPasswordModel user)
|
2024-06-28 09:59:49 +00:00
|
|
|
{
|
|
|
|
|
return await _userService.ResetUserPassword(user.ToUser());
|
|
|
|
|
}
|
2024-05-30 00:27:38 +00:00
|
|
|
|
2024-07-24 10:49:54 +00:00
|
|
|
[HttpPost("/user/email/modify")]
|
|
|
|
|
public async Task<bool> ModifyUserEmail([FromQuery] string email)
|
|
|
|
|
{
|
|
|
|
|
return await _userService.ModifyUserEmail(email);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("/user/phone/modify")]
|
|
|
|
|
public async Task<bool> ModifyUserPhone([FromQuery] string phone)
|
|
|
|
|
{
|
|
|
|
|
return await _userService.ModifyUserPhone(phone);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 00:27:38 +00:00
|
|
|
#region Avatar
|
|
|
|
|
[HttpPost("/user/avatar")]
|
|
|
|
|
public bool UploadUserAvatar([FromBody] BotSharpFile file)
|
|
|
|
|
{
|
2024-08-08 01:56:29 +00:00
|
|
|
var fileStorage = _services.GetRequiredService<IFileStorageService>();
|
|
|
|
|
return fileStorage.SaveUserAvatar(file);
|
2024-05-30 00:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("/user/avatar")]
|
|
|
|
|
public IActionResult GetUserAvatar()
|
|
|
|
|
{
|
2024-08-08 01:56:29 +00:00
|
|
|
var fileStorage = _services.GetRequiredService<IFileStorageService>();
|
|
|
|
|
var file = fileStorage.GetUserAvatar();
|
2024-05-30 00:27:38 +00:00
|
|
|
if (string.IsNullOrEmpty(file))
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
return BuildFileResult(file);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Private methods
|
|
|
|
|
private FileContentResult BuildFileResult(string file)
|
|
|
|
|
{
|
2024-08-08 01:56:29 +00:00
|
|
|
var fileStorage = _services.GetRequiredService<IFileStorageService>();
|
|
|
|
|
var bytes = fileStorage.GetFileBytes(file);
|
2024-05-30 00:27:38 +00:00
|
|
|
return File(bytes, "application/octet-stream", Path.GetFileName(file));
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2023-08-19 00:15:02 +00:00
|
|
|
}
|