Добавлено управление сессиями
This commit is contained in:
@@ -13,12 +13,14 @@ public class AccountController : ControllerBase
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
private readonly JwtService _jwtService;
|
||||
private readonly UserSessionService _userSessionService;
|
||||
|
||||
public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, JwtService jwtService)
|
||||
public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, JwtService jwtService, UserSessionService userSessionService)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_signInManager = signInManager;
|
||||
_jwtService = jwtService;
|
||||
_userSessionService = userSessionService;
|
||||
}
|
||||
|
||||
[HttpPost("register")]
|
||||
@@ -56,6 +58,8 @@ public class AccountController : ControllerBase
|
||||
|
||||
private async Task<ActionResult<ApiResponse<LoginResponse>>> GenerateTokenResponse(ApplicationUser user)
|
||||
{
|
||||
await _userSessionService.GetOrCreateCurrentSessionAsync(user.Id);
|
||||
|
||||
var (token, refreshToken, expiration) = await _jwtService.GenerateTokenAsync(user);
|
||||
return Ok(ApiResponse<LoginResponse>.Ok(new LoginResponse
|
||||
{
|
||||
|
||||
@@ -14,17 +14,20 @@ public class OpenIdController : ControllerBase
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly JwtService _jwtService;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly UserSessionService _userSessionService;
|
||||
|
||||
public OpenIdController(
|
||||
SignInManager<ApplicationUser> signInManager,
|
||||
UserManager<ApplicationUser> userManager,
|
||||
JwtService jwtService,
|
||||
IConfiguration configuration)
|
||||
IConfiguration configuration,
|
||||
UserSessionService userSessionService)
|
||||
{
|
||||
_signInManager = signInManager;
|
||||
_userManager = userManager;
|
||||
_jwtService = jwtService;
|
||||
_configuration = configuration;
|
||||
_userSessionService = userSessionService;
|
||||
}
|
||||
|
||||
[HttpGet("login")]
|
||||
@@ -70,6 +73,7 @@ public class OpenIdController : ControllerBase
|
||||
}
|
||||
|
||||
await _signInManager.SignInAsync(user, isPersistent: false);
|
||||
await _userSessionService.GetOrCreateCurrentSessionAsync(user.Id);
|
||||
var (token, refreshToken, _) = await _jwtService.GenerateTokenAsync(user);
|
||||
return Redirect($"{_configuration["Client:BaseUrl"]}/auth-callback?token={token}&refreshToken={refreshToken}");
|
||||
}
|
||||
|
||||
@@ -16,18 +16,24 @@ public class SharedPlaylistController : ControllerBase
|
||||
private readonly SharedPlaylistService _sharedService;
|
||||
private readonly YandexMusicService _yandexService;
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly TrackAdditionLogService _trackLogService;
|
||||
private readonly UserSessionService _userSessionService;
|
||||
private readonly TrackAdditionLogService _trackAdditionLogService;
|
||||
private readonly TrackRemovalLogService _trackRemovalLogService;
|
||||
|
||||
public SharedPlaylistController(
|
||||
SharedPlaylistService sharedService,
|
||||
YandexMusicService yandexService,
|
||||
UserManager<ApplicationUser> userManager,
|
||||
TrackAdditionLogService trackLogService)
|
||||
TrackAdditionLogService trackAdditionLogService,
|
||||
TrackRemovalLogService trackRemovalLogService,
|
||||
UserSessionService userSessionService)
|
||||
{
|
||||
_sharedService = sharedService;
|
||||
_yandexService = yandexService;
|
||||
_userManager = userManager;
|
||||
_trackLogService = trackLogService;
|
||||
_trackAdditionLogService = trackAdditionLogService;
|
||||
_trackRemovalLogService = trackRemovalLogService;
|
||||
_userSessionService = userSessionService;
|
||||
}
|
||||
|
||||
// GET /api/sharedplaylist/{token}
|
||||
@@ -113,6 +119,13 @@ public class SharedPlaylistController : ControllerBase
|
||||
if (updatedPlaylist == null)
|
||||
return StatusCode(500, ApiResponse<object>.Fail(new ErrorResponse { StatusCode = 500, Message = "Ошибка при добавлении треков" }));
|
||||
|
||||
var session = await _userSessionService.GetOrCreateCurrentSessionAsync(currentUserId);
|
||||
var sessionId = session.SessionId;
|
||||
foreach (var trackId in request.TrackIds)
|
||||
{
|
||||
await _trackAdditionLogService.LogAdditionAsync(playlist.Id, trackId, currentUserId, sessionId);
|
||||
}
|
||||
|
||||
return Ok(ApiResponse<object>.Ok(new { message = "Треки добавлены" }));
|
||||
}
|
||||
|
||||
@@ -125,9 +138,12 @@ public class SharedPlaylistController : ControllerBase
|
||||
if (playlist == null)
|
||||
return NotFound(ApiResponse<object>.Fail(new ErrorResponse { StatusCode = 404, Message = "Плейлист не найден" }));
|
||||
|
||||
var session = await _userSessionService.GetOrCreateCurrentSessionAsync(currentUserId);
|
||||
var sessionId = session.SessionId;
|
||||
|
||||
foreach (var trackId in request.TrackIds)
|
||||
{
|
||||
if (!await _sharedService.CanRemoveTrackAsync(playlist, currentUserId, trackId))
|
||||
if (!await _sharedService.CanRemoveTrackAsync(playlist, currentUserId, trackId, sessionId))
|
||||
return StatusCode(403, ApiResponse<object>.Fail(new ErrorResponse { StatusCode = 403, Message = $"Недостаточно прав для удаления трека {trackId}" }));
|
||||
}
|
||||
|
||||
@@ -140,7 +156,10 @@ public class SharedPlaylistController : ControllerBase
|
||||
return StatusCode(500, ApiResponse<object>.Fail(new ErrorResponse { StatusCode = 500, Message = "Ошибка при удалении треков" }));
|
||||
|
||||
foreach (var trackId in request.TrackIds)
|
||||
await _trackLogService.RemoveLogsForTrackAsync(playlist.Id, trackId);
|
||||
{
|
||||
await _trackRemovalLogService.LogRemovalAsync(playlist.Id, trackId, currentUserId, sessionId);
|
||||
await _trackAdditionLogService.RemoveLogsForTrackAsync(playlist.Id, trackId);
|
||||
}
|
||||
|
||||
return Ok(ApiResponse<object>.Ok(new { message = "Треки удалены" }));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user