From 655c9ea7a173add232c737d1a648ad697c19f297 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 3 Feb 2015 16:06:56 -0500 Subject: [PATCH] enforce user access to offline items --- MediaBrowser.Controller/Entities/BaseItem.cs | 17 +++++++ MediaBrowser.Model/Sync/SyncDataRequest.cs | 2 + MediaBrowser.Model/Sync/SyncDataResponse.cs | 2 + .../LiveTv/LiveTvManager.cs | 6 +-- .../Sync/SyncManager.cs | 47 +++++++++++++++++++ .../Sync/SyncRepository.cs | 6 +-- Nuget/MediaBrowser.Common.Internal.nuspec | 4 +- Nuget/MediaBrowser.Common.nuspec | 2 +- Nuget/MediaBrowser.Model.Signed.nuspec | 2 +- Nuget/MediaBrowser.Server.Core.nuspec | 4 +- 10 files changed, 80 insertions(+), 12 deletions(-) diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index 1e46aa9e56..4925bcd8ad 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -1083,6 +1083,23 @@ namespace MediaBrowser.Controller.Entities return IsParentalAllowed(user); } + public virtual bool IsVisibleStandalone(User user) + { + if (!IsVisible(user)) + { + return false; + } + + if (Parents.Any(i => !i.IsVisible(user))) + { + return false; + } + + // TODO: Need some work here, e.g. is in user library, for channels, can user access channel, etc. + + return true; + } + /// /// Gets a value indicating whether this instance is folder. /// diff --git a/MediaBrowser.Model/Sync/SyncDataRequest.cs b/MediaBrowser.Model/Sync/SyncDataRequest.cs index 3eb447b3f2..dc33239a04 100644 --- a/MediaBrowser.Model/Sync/SyncDataRequest.cs +++ b/MediaBrowser.Model/Sync/SyncDataRequest.cs @@ -5,12 +5,14 @@ namespace MediaBrowser.Model.Sync public class SyncDataRequest { public List LocalItemIds { get; set; } + public List OfflineUserIds { get; set; } public string TargetId { get; set; } public SyncDataRequest() { LocalItemIds = new List(); + OfflineUserIds = new List(); } } } diff --git a/MediaBrowser.Model/Sync/SyncDataResponse.cs b/MediaBrowser.Model/Sync/SyncDataResponse.cs index ac7ff5c84a..3799e94557 100644 --- a/MediaBrowser.Model/Sync/SyncDataResponse.cs +++ b/MediaBrowser.Model/Sync/SyncDataResponse.cs @@ -5,10 +5,12 @@ namespace MediaBrowser.Model.Sync public class SyncDataResponse { public List ItemIdsToRemove { get; set; } + public Dictionary> ItemUserAccess { get; set; } public SyncDataResponse() { ItemIdsToRemove = new List(); + ItemUserAccess = new Dictionary>(); } } } diff --git a/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs b/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs index 74069e5801..33c3b97dc9 100644 --- a/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs +++ b/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs @@ -147,7 +147,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv var currentUser = user; channels = channels - .Where(i => i.IsParentalAllowed(currentUser)) + .Where(i => i.IsVisible(currentUser)) .OrderBy(i => { double number = 0; @@ -679,7 +679,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv { // Avoid implicitly captured closure var currentUser = user; - programs = programs.Where(i => i.IsParentalAllowed(currentUser)); + programs = programs.Where(i => i.IsVisible(currentUser)); } var programList = programs.ToList(); @@ -714,7 +714,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv // Avoid implicitly captured closure var currentUser = user; - programs = programs.Where(i => i.IsParentalAllowed(currentUser)); + programs = programs.Where(i => i.IsVisible(currentUser)); if (query.IsAiring.HasValue) { diff --git a/MediaBrowser.Server.Implementations/Sync/SyncManager.cs b/MediaBrowser.Server.Implementations/Sync/SyncManager.cs index 6a4bc9cc44..749730f827 100644 --- a/MediaBrowser.Server.Implementations/Sync/SyncManager.cs +++ b/MediaBrowser.Server.Implementations/Sync/SyncManager.cs @@ -314,6 +314,10 @@ namespace MediaBrowser.Server.Implementations.Sync try { _fileSystem.DeleteDirectory(path, true); + } + catch (DirectoryNotFoundException) + { + } catch (Exception ex) { @@ -664,9 +668,48 @@ namespace MediaBrowser.Server.Implementations.Sync response.ItemIdsToRemove = response.ItemIdsToRemove.Distinct(StringComparer.OrdinalIgnoreCase).ToList(); + var itemsOnDevice = request.LocalItemIds + .Except(response.ItemIdsToRemove) + .ToList(); + + SetUserAccess(request, response, itemsOnDevice); + return response; } + private void SetUserAccess(SyncDataRequest request, SyncDataResponse response, List itemIds) + { + var users = request.OfflineUserIds + .Select(_userManager.GetUserById) + .Where(i => i != null) + .ToList(); + + foreach (var itemId in itemIds) + { + var item = _libraryManager.GetItemById(itemId); + + if (item != null) + { + var usersWithAccess = new List(); + + foreach (var user in users) + { + if (IsUserVisible(item, user)) + { + usersWithAccess.Add(user); + } + } + + response.ItemUserAccess[itemId] = users.Select(i => i.Id.ToString("N")).ToList(); + } + } + } + + private bool IsUserVisible(BaseItem item, User user) + { + return item.IsVisibleStandalone(user); + } + private bool IsLibraryItemAvailable(BaseItem item) { if (item == null) @@ -723,6 +766,10 @@ namespace MediaBrowser.Server.Implementations.Sync try { _fileSystem.DeleteDirectory(path, true); + } + catch (DirectoryNotFoundException) + { + } catch (Exception ex) { diff --git a/MediaBrowser.Server.Implementations/Sync/SyncRepository.cs b/MediaBrowser.Server.Implementations/Sync/SyncRepository.cs index a101f13eb3..85317cb237 100644 --- a/MediaBrowser.Server.Implementations/Sync/SyncRepository.cs +++ b/MediaBrowser.Server.Implementations/Sync/SyncRepository.cs @@ -407,7 +407,6 @@ namespace MediaBrowser.Server.Implementations.Sync if (!string.IsNullOrWhiteSpace(query.TargetId)) { whereClauses.Add("TargetId=@TargetId"); - cmd.Parameters.Add(cmd, "@TargetId", DbType.String).Value = query.TargetId; } if (!string.IsNullOrWhiteSpace(query.UserId)) { @@ -422,7 +421,7 @@ namespace MediaBrowser.Server.Implementations.Sync var startIndex = query.StartIndex ?? 0; if (startIndex > 0) { - whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM SyncJobs ORDER BY DateLastModified DESC LIMIT {0})", + whereClauses.Add(string.Format("Id NOT IN (SELECT Id FROM SyncJobs ORDER BY (Select Max(DateLastModified) from SyncJobs where TargetId=@TargetId) DESC, DateLastModified DESC LIMIT {0})", startIndex.ToString(_usCulture))); } @@ -431,7 +430,8 @@ namespace MediaBrowser.Server.Implementations.Sync cmd.CommandText += " where " + string.Join(" AND ", whereClauses.ToArray()); } - cmd.CommandText += " ORDER BY DateLastModified DESC"; + cmd.CommandText += " ORDER BY (Select Max(DateLastModified) from SyncJobs where TargetId=@TargetId) DESC, DateLastModified DESC"; + cmd.Parameters.Add(cmd, "@TargetId", DbType.String).Value = query.TargetId; if (query.Limit.HasValue) { diff --git a/Nuget/MediaBrowser.Common.Internal.nuspec b/Nuget/MediaBrowser.Common.Internal.nuspec index 271bc7864a..3a5cf888da 100644 --- a/Nuget/MediaBrowser.Common.Internal.nuspec +++ b/Nuget/MediaBrowser.Common.Internal.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Common.Internal - 3.0.551 + 3.0.552 MediaBrowser.Common.Internal Luke ebr,Luke,scottisafool @@ -12,7 +12,7 @@ Contains common components shared by Media Browser Theater and Media Browser Server. Not intended for plugin developer consumption. Copyright © Media Browser 2013 - + diff --git a/Nuget/MediaBrowser.Common.nuspec b/Nuget/MediaBrowser.Common.nuspec index 31d2c918c1..d879b37f1d 100644 --- a/Nuget/MediaBrowser.Common.nuspec +++ b/Nuget/MediaBrowser.Common.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Common - 3.0.551 + 3.0.552 MediaBrowser.Common Media Browser Team ebr,Luke,scottisafool diff --git a/Nuget/MediaBrowser.Model.Signed.nuspec b/Nuget/MediaBrowser.Model.Signed.nuspec index e202c66fa5..04b6126bba 100644 --- a/Nuget/MediaBrowser.Model.Signed.nuspec +++ b/Nuget/MediaBrowser.Model.Signed.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Model.Signed - 3.0.551 + 3.0.552 MediaBrowser.Model - Signed Edition Media Browser Team ebr,Luke,scottisafool diff --git a/Nuget/MediaBrowser.Server.Core.nuspec b/Nuget/MediaBrowser.Server.Core.nuspec index 34df8a8548..443e07efe0 100644 --- a/Nuget/MediaBrowser.Server.Core.nuspec +++ b/Nuget/MediaBrowser.Server.Core.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Server.Core - 3.0.551 + 3.0.552 Media Browser.Server.Core Media Browser Team ebr,Luke,scottisafool @@ -12,7 +12,7 @@ Contains core components required to build plugins for Media Browser Server. Copyright © Media Browser 2013 - +