From 6f45848b51d6b14da74d73cbb638b64637af106f Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Sun, 27 Mar 2022 19:57:41 -0600 Subject: [PATCH] Safely set/get user permission --- Jellyfin.Data/Entities/User.cs | 42 ++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/Jellyfin.Data/Entities/User.cs b/Jellyfin.Data/Entities/User.cs index e309e54de9..26c46a704f 100644 --- a/Jellyfin.Data/Entities/User.cs +++ b/Jellyfin.Data/Entities/User.cs @@ -362,7 +362,7 @@ namespace Jellyfin.Data.Entities /// True if the user has the specified permission. public bool HasPermission(PermissionKind kind) { - return Permissions.First(p => p.Kind == kind).Value; + return Permissions.FirstOrDefault(p => p.Kind == kind)?.Value ?? false; } /// @@ -372,7 +372,15 @@ namespace Jellyfin.Data.Entities /// The value to set. public void SetPermission(PermissionKind kind, bool value) { - Permissions.First(p => p.Kind == kind).Value = value; + var currentPermission = Permissions.FirstOrDefault(p => p.Kind == kind); + if (currentPermission == null) + { + Permissions.Add(new Permission(kind, value)); + } + else + { + currentPermission.Value = value; + } } /// @@ -382,9 +390,9 @@ namespace Jellyfin.Data.Entities /// A string array containing the user's preferences. public string[] GetPreference(PreferenceKind preference) { - var val = Preferences.First(p => p.Kind == preference).Value; + var val = Preferences.FirstOrDefault(p => p.Kind == preference)?.Value; - return Equals(val, string.Empty) ? Array.Empty() : val.Split(Delimiter); + return string.IsNullOrEmpty(val) ? Array.Empty() : val.Split(Delimiter); } /// @@ -395,7 +403,7 @@ namespace Jellyfin.Data.Entities /// A {T} array containing the user's preference. public T[] GetPreferenceValues(PreferenceKind preference) { - var val = Preferences.First(p => p.Kind == preference).Value; + var val = Preferences.FirstOrDefault(p => p.Kind == preference)?.Value; if (string.IsNullOrEmpty(val)) { return Array.Empty(); @@ -432,8 +440,16 @@ namespace Jellyfin.Data.Entities /// The values. public void SetPreference(PreferenceKind preference, string[] values) { - Preferences.First(p => p.Kind == preference).Value - = string.Join(Delimiter, values); + var value = string.Join(Delimiter, values); + var currentPreference = Preferences.FirstOrDefault(p => p.Kind == preference); + if (currentPreference == null) + { + Preferences.Add(new Preference(preference, value)); + } + else + { + currentPreference.Value = value; + } } /// @@ -444,8 +460,16 @@ namespace Jellyfin.Data.Entities /// The type of value. public void SetPreference(PreferenceKind preference, T[] values) { - Preferences.First(p => p.Kind == preference).Value - = string.Join(Delimiter, values); + var value = string.Join(Delimiter, values); + var currentPreference = Preferences.FirstOrDefault(p => p.Kind == preference); + if (currentPreference == null) + { + Preferences.Add(new Preference(preference, value)); + } + else + { + currentPreference.Value = value; + } } ///