From 0677a4ddbccb91e38f39c0872ffec460c686eb30 Mon Sep 17 00:00:00 2001 From: phoenix Date: Tue, 11 Mar 2025 21:41:44 -0400 Subject: [PATCH] tsk-#76: Adding more code --- Icarus/Controllers/Managers/TokenManager.cs | 2 -- .../Controllers/v1/AccessLevelController.cs | 12 +++++----- .../v1/SongCompressedDataControllers.cs | 14 ++++++++++++ Icarus/Controllers/v1/SongController.cs | 14 ++++++++++++ Icarus/Controllers/v1/SongDataController.cs | 16 +++++++++++--- Icarus/Controllers/v1/SongStreamController.cs | 22 +++++++++++++++++-- 6 files changed, 67 insertions(+), 13 deletions(-) diff --git a/Icarus/Controllers/Managers/TokenManager.cs b/Icarus/Controllers/Managers/TokenManager.cs index 8a48157..bbb79e7 100644 --- a/Icarus/Controllers/Managers/TokenManager.cs +++ b/Icarus/Controllers/Managers/TokenManager.cs @@ -121,8 +121,6 @@ public class TokenManager : BaseManager }; } - // TODO: Make this call to whenver media is being accessed or modified - cover art, genre, - // album, song, et cetera public bool CanAccessSong(string token, Song song, AccessLevel accessLevel) { if (accessLevel!.Level!.Equals(Models.AccessLevel.DefaultLevel().Level)) diff --git a/Icarus/Controllers/v1/AccessLevelController.cs b/Icarus/Controllers/v1/AccessLevelController.cs index 1209df8..1d74488 100644 --- a/Icarus/Controllers/v1/AccessLevelController.cs +++ b/Icarus/Controllers/v1/AccessLevelController.cs @@ -21,7 +21,7 @@ public class AccessLevelController : BaseController { this._logger = logger; this._config = config; - _connectionString = this._config.GetConnectionString("DefaultConnection"); + this._connectionString = this._config.GetConnectionString("DefaultConnection"); } #endregion @@ -29,8 +29,8 @@ public class AccessLevelController : BaseController [HttpGet] public IActionResult GetAccessLevels(int? id, int? songId) { - var accLevel = new Icarus.Models.AccessLevel { Id = 0 }; - var accessLevelContext = new Icarus.Database.Contexts.AccessLevelContext(_connectionString!); + var accLevel = new Models.AccessLevel { Id = 0 }; + var accessLevelContext = new Database.Contexts.AccessLevelContext(_connectionString!); if (id != null) { @@ -38,7 +38,7 @@ public class AccessLevelController : BaseController } else if (songId != null) { - accLevel = accessLevelContext.AccessLevels!.FirstOrDefault(al => al.SongId == songId); + accLevel = accessLevelContext.GetAccessLevel(songId.Value); } var response = new GetAccessLevelsResponse { Data = new List() }; @@ -115,7 +115,7 @@ public class GetAccessLevelsResponse [Newtonsoft.Json.JsonProperty("subject")] public string? Subject { get; set; } [Newtonsoft.Json.JsonProperty("data")] - public List? Data { get; set; } + public List? Data { get; set; } #endregion } @@ -125,7 +125,7 @@ public class UpdateAccessLevelResponse [Newtonsoft.Json.JsonProperty("subject")] public string? Subject { get; set; } [Newtonsoft.Json.JsonProperty("data")] - public List? Data { get; set; } + public List? Data { get; set; } #endregion } #endregion diff --git a/Icarus/Controllers/v1/SongCompressedDataControllers.cs b/Icarus/Controllers/v1/SongCompressedDataControllers.cs index 57dd630..d82864b 100644 --- a/Icarus/Controllers/v1/SongCompressedDataControllers.cs +++ b/Icarus/Controllers/v1/SongCompressedDataControllers.cs @@ -47,6 +47,20 @@ public class SongCompressedDataController : BaseController Console.WriteLine("Starting process of retrieving comrpessed song"); var sng = context.RetrieveRecord(new Song { Id = id }); + + var tokenManager = new TokenManager(this._config!); + var accLvlContext = new AccessLevelContext(this._connectionString!); + var accessLevel = accLvlContext.GetAccessLevel(sng.Id); + var token = tokenManager.GetBearerToken(HttpContext); + if (token == null || accessLevel == null) + { + return BadRequest(); + } + + if (!tokenManager.CanAccessSong(token, sng, accessLevel)) + { + return BadRequest(); + } SongData song = await cmp.RetrieveCompressedSong(sng); var filename = DirectoryManager.GenerateDownloadFilename(10, Constants.FileExtensions.ZIP_EXTENSION, sng.Title!, randomizeFilename); diff --git a/Icarus/Controllers/v1/SongController.cs b/Icarus/Controllers/v1/SongController.cs index 6011260..f59a0c9 100644 --- a/Icarus/Controllers/v1/SongController.cs +++ b/Icarus/Controllers/v1/SongController.cs @@ -86,6 +86,20 @@ public class SongController : BaseController }); } + var tokenManager = new TokenManager(this._config!); + var accLvlContext = new AccessLevelContext(this._connectionString!); + var accessLevel = accLvlContext.GetAccessLevel(song.Id); + var token = tokenManager.GetBearerToken(HttpContext); + if (token == null || accessLevel == null) + { + return BadRequest(); + } + + if (!tokenManager.CanAccessSong(token, song, accessLevel)) + { + return BadRequest(); + } + var songRes = _songMgr.UpdateSong(song); return Ok(songRes); diff --git a/Icarus/Controllers/v1/SongDataController.cs b/Icarus/Controllers/v1/SongDataController.cs index 15dff68..4cbd4b4 100644 --- a/Icarus/Controllers/v1/SongDataController.cs +++ b/Icarus/Controllers/v1/SongDataController.cs @@ -175,11 +175,21 @@ public class SongDataController : BaseController public IActionResult DeleteSong(int id) { var songContext = new SongContext(_connectionString!); + var songMetaData = songContext.RetrieveRecord(new Song { Id = id }); - var songMetaData = new Song { Id = id }; - Console.WriteLine($"Id {songMetaData.Id}"); + var tokenManager = new TokenManager(this._config!); + var accLvlContext = new AccessLevelContext(this._connectionString!); + var accessLevel = accLvlContext.GetAccessLevel(songMetaData.Id); + var token = tokenManager.GetBearerToken(HttpContext); + if (token == null || accessLevel == null) + { + return BadRequest(); + } - songMetaData = songContext.RetrieveRecord(songMetaData); + if (!tokenManager.CanAccessSong(token, songMetaData, accessLevel)) + { + return BadRequest(); + } if (string.IsNullOrEmpty(songMetaData.Title)) { diff --git a/Icarus/Controllers/v1/SongStreamController.cs b/Icarus/Controllers/v1/SongStreamController.cs index 7a347bb..af8fcec 100644 --- a/Icarus/Controllers/v1/SongStreamController.cs +++ b/Icarus/Controllers/v1/SongStreamController.cs @@ -11,6 +11,7 @@ namespace Icarus.Controllers.V1; public class SongStreamController : BaseController { #region Fields + private string? _connectionString; private ILogger? _logger; #endregion @@ -22,8 +23,9 @@ public class SongStreamController : BaseController #region Constructor public SongStreamController(ILogger logger, IConfiguration config) { - _logger = logger; - _config = config; + this._logger = logger; + this._config = config; + this._connectionString = this._config.GetConnectionString("DefaultConnection"); } #endregion @@ -35,6 +37,22 @@ public class SongStreamController : BaseController var context = new SongContext(_config!.GetConnectionString("DefaultConnection")!); var song = context.Songs!.FirstOrDefault(sng => sng.Id == id); + if (song == null) { + return BadRequest(); + } + var tokenManager = new Managers.TokenManager(this._config!); + var accLvlContext = new AccessLevelContext(this._connectionString!); + var accessLevel = accLvlContext.GetAccessLevel(song.Id); + var token = tokenManager.GetBearerToken(HttpContext); + if (token == null || accessLevel == null) + { + return BadRequest(); + } + + if (!tokenManager.CanAccessSong(token, song, accessLevel)) + { + return BadRequest(); + } var stream = new FileStream(song!.SongPath(), FileMode.Open, FileAccess.Read); stream.Position = 0;