forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserController.cs
More file actions
141 lines (125 loc) · 4.37 KB
/
UserController.cs
File metadata and controls
141 lines (125 loc) · 4.37 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
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using System.ComponentModel.DataAnnotations;
namespace BotSharp.OpenAPI.Controllers;
[Authorize]
[ApiController]
public class UserController : ControllerBase
{
private readonly IServiceProvider _services;
private readonly IUserService _userService;
public UserController(IUserService userService, IServiceProvider services)
{
_services = services;
_userService = userService;
}
[AllowAnonymous]
[HttpPost("/token")]
public async Task<ActionResult<Token>> GetToken([FromHeader(Name = "Authorization")][Required] string authcode)
{
if (authcode.Contains(' '))
{
authcode = authcode.Split(' ')[1];
}
var token = await _userService.GetToken(authcode);
if (token == null)
{
return Unauthorized();
}
return Ok(token);
}
[AllowAnonymous]
[HttpGet("/sso/{provider}")]
public async Task<IActionResult> Authorize([FromRoute] string provider,string redirectUrl)
{
return Challenge(new AuthenticationProperties { RedirectUri = redirectUrl }, provider);
}
[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);
}
[AllowAnonymous]
[HttpPost("/user")]
public async Task<UserViewModel> CreateUser(UserCreationModel user)
{
var createdUser = await _userService.CreateUser(user.ToUser());
return UserViewModel.FromUser(createdUser);
}
[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);
}
[HttpGet("/user/me")]
public async Task<UserViewModel> GetMyUserProfile()
{
var user = await _userService.GetMyProfile();
if (user == null)
{
var identiy = _services.GetRequiredService<IUserIdentity>();
var accessor = _services.GetRequiredService<IHttpContextAccessor>();
var claims = accessor.HttpContext.User.Claims;
user = await _userService.CreateUser(new User
{
Email = identiy.Email,
UserName = identiy.UserName,
FirstName = identiy.FirstName,
LastName = identiy.LastName,
Source = claims.First().Issuer,
ExternalId = identiy.Id,
});
}
return UserViewModel.FromUser(user);
}
[HttpGet("/user/name/existing")]
public async Task<bool> VerifyUserUnique([FromQuery] string userName)
{
return await _userService.VerifyUserUnique(userName);
}
[HttpGet("/user/email/existing")]
public async Task<bool> VerifyEmailUnique([FromQuery] string email)
{
return await _userService.VerifyEmailUnique(email);
}
#region Avatar
[HttpPost("/user/avatar")]
public bool UploadUserAvatar([FromBody] BotSharpFile file)
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
return fileService.SaveUserAvatar(file);
}
[HttpGet("/user/avatar")]
public IActionResult GetUserAvatar()
{
var fileService = _services.GetRequiredService<IBotSharpFileService>();
var file = fileService.GetUserAvatar();
if (string.IsNullOrEmpty(file))
{
return NotFound();
}
return BuildFileResult(file);
}
#endregion
#region Private methods
private FileContentResult BuildFileResult(string file)
{
using Stream stream = System.IO.File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read);
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
return File(bytes, "application/octet-stream", Path.GetFileName(file));
}
#endregion
}