-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathAccountController.cs
More file actions
193 lines (167 loc) · 7.15 KB
/
AccountController.cs
File metadata and controls
193 lines (167 loc) · 7.15 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
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using HwProj.AuthService.API.Services;
using HwProj.Models.AuthService.DTO;
using HwProj.Models.AuthService.ViewModels;
using HwProj.Models.Result;
using HwProj.Models.Roles;
using HwProj.Utils.Authorization;
using Microsoft.Extensions.Configuration;
using User = HwProj.Models.AuthService.ViewModels.User;
namespace HwProj.AuthService.API.Controllers
{
[Route("api/account")]
[ApiController]
public class AccountController : ControllerBase
{
private readonly IAccountService _accountService;
private readonly IUserManager _userManager;
private readonly IConfiguration _configuration;
private readonly IMapper _mapper;
public AccountController(
IAccountService accountService,
IUserManager userManager,
IMapper mapper)
{
_accountService = accountService;
_userManager = userManager;
_mapper = mapper;
}
[HttpGet("getUserData/{userId}")]
[ProducesResponseType(typeof(AccountDataDto), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetUserDataById(string userId)
{
var accountData = await _accountService.GetAccountDataAsync(userId).ConfigureAwait(false);
return accountData != null
? Ok(accountData) as IActionResult
: NotFound();
}
[HttpGet("getUserDataByEmail/{email}")]
[ProducesResponseType(typeof(AccountDataDto), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetUserDataByEmail(string email)
{
var accountData = await _accountService.GetAccountDataByEmailAsync(email);
return Ok(accountData);
}
[HttpGet("getUsersData")]
public async Task<AccountDataDto?[]> GetUsersData([FromBody] string[] userIds)
{
return await _accountService.GetAccountsDataAsync(userIds);
}
[HttpPost("register")]
[ProducesResponseType(typeof(Result<TokenCredentials>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> Register([FromBody] RegisterViewModel model)
{
var newModel = _mapper.Map<RegisterDataDTO>(model);
var result = await _accountService.RegisterUserAsync(newModel);
return Ok(result);
}
[HttpPost("login")]
[ProducesResponseType(typeof(Result<TokenCredentials>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> Login([FromBody] LoginViewModel model)
{
var tokenMeta = await _accountService.LoginUserAsync(model).ConfigureAwait(false);
return Ok(tokenMeta);
}
[HttpGet("refreshToken")]
[ProducesResponseType(typeof(Result<TokenCredentials>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> RefreshToken(string userId)
{
var tokenMeta = await _accountService.RefreshToken(userId);
return Ok(tokenMeta);
}
[HttpGet("getGuestToken")]
[ProducesResponseType(typeof(TokenCredentials), (int)HttpStatusCode.OK)]
public Task<IActionResult> GetGuestToken([FromQuery] string courseId)
{
var tokenMeta = _accountService.GetGuestToken(courseId);
return Task.FromResult<IActionResult>(Ok(tokenMeta));
}
[HttpPut("edit/{userId}")]
[ProducesResponseType(typeof(Result), (int)HttpStatusCode.OK)]
public async Task<IActionResult> Edit([FromBody] EditAccountViewModel model, string userId)
{
var newModel = _mapper.Map<EditDataDTO>(model);
var result = await _accountService.EditAccountAsync(userId, newModel);
return Ok(result);
}
[HttpPost("inviteNewLecturer")]
[ProducesResponseType(typeof(Result), (int)HttpStatusCode.OK)]
public async Task<IActionResult> InviteNewLecturer(InviteLecturerViewModel model)
{
var result = await _accountService.InviteNewLecturer(model.Email).ConfigureAwait(false);
return Ok(result);
}
[HttpGet("findByEmail/{email}")]
[ProducesResponseType(typeof(User), (int)HttpStatusCode.OK)]
public async Task<IActionResult> FindByEmail(string email)
{
var user = await _userManager.FindByEmailAsync(email);
return Ok(user);
}
[HttpGet("getRole")]
[ProducesResponseType(typeof(string), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetRolesAsync([FromBody] User user)
{
var roles = await _userManager.GetRolesAsync(user);
return Ok(roles[0]);
}
[HttpGet("getAllStudents")]
[ProducesResponseType(typeof(AccountDataDto[]), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetAllStudents()
{
var allStudents = await _accountService.GetUsersInRole(Roles.StudentRole);
var result = allStudents
.Select(u => u.ToAccountDataDto(Roles.StudentRole))
.ToArray();
return Ok(result);
}
[HttpGet("getAllLecturers")]
[ProducesResponseType(typeof(User[]), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetAllLecturers()
{
var allLecturers = await _accountService.GetUsersInRole(Roles.LecturerRole);
var result = allLecturers.ToArray();
return Ok(result);
}
[HttpPost("requestPasswordRecovery")]
public async Task<Result> RequestPasswordRecovery(RequestPasswordRecoveryViewModel model)
{
return await _accountService.RequestPasswordRecovery(model);
}
[HttpPost("resetPassword")]
public async Task<Result> ResetPassword(ResetPasswordViewModel model)
{
return await _accountService.ResetPassword(model);
}
[HttpPost("github/url")]
[ProducesResponseType(typeof(Result<UrlDto>), (int)HttpStatusCode.OK)]
public Task<IActionResult> GetGithubLoginUrl(
[FromServices] IConfiguration configuration,
[FromBody] UrlDto urlDto)
{
var sourceSection = configuration.GetSection("Github");
var clientId = sourceSection["ClientIdGitHub"];
var scope = sourceSection["ScopeGitHub"];
var redirectUrl = urlDto.Url;
var resultUrl =
$"https://github.com/login/oauth/authorize?client_id={clientId}&redirect_uri={redirectUrl}&scope={scope}";
var resultUrlDto = new UrlDto
{
Url = resultUrl
};
return Task.FromResult<IActionResult>(Ok(resultUrlDto));
}
[HttpPost("github/authorize/{userId}")]
[ProducesResponseType(typeof(GithubCredentials), (int)HttpStatusCode.OK)]
public async Task<GithubCredentials> GithubAuthorize(
string userId, [FromQuery] string code)
{
var result = await _accountService.AuthorizeGithub(code, userId);
return result;
}
}
}