Merge pull request #7506 from crobibero/set-permissions

Safely get/set User permission/preference
This commit is contained in:
Claus Vium 2022-03-28 23:02:42 +02:00 committed by GitHub
commit d2ce315c1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -362,7 +362,7 @@ namespace Jellyfin.Data.Entities
/// <returns><c>True</c> if the user has the specified permission.</returns> /// <returns><c>True</c> if the user has the specified permission.</returns>
public bool HasPermission(PermissionKind kind) public bool HasPermission(PermissionKind kind)
{ {
return Permissions.First(p => p.Kind == kind).Value; return Permissions.FirstOrDefault(p => p.Kind == kind)?.Value ?? false;
} }
/// <summary> /// <summary>
@ -372,7 +372,15 @@ namespace Jellyfin.Data.Entities
/// <param name="value">The value to set.</param> /// <param name="value">The value to set.</param>
public void SetPermission(PermissionKind kind, bool value) 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;
}
} }
/// <summary> /// <summary>
@ -382,9 +390,9 @@ namespace Jellyfin.Data.Entities
/// <returns>A string array containing the user's preferences.</returns> /// <returns>A string array containing the user's preferences.</returns>
public string[] GetPreference(PreferenceKind preference) 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<string>() : val.Split(Delimiter); return string.IsNullOrEmpty(val) ? Array.Empty<string>() : val.Split(Delimiter);
} }
/// <summary> /// <summary>
@ -395,7 +403,7 @@ namespace Jellyfin.Data.Entities
/// <returns>A {T} array containing the user's preference.</returns> /// <returns>A {T} array containing the user's preference.</returns>
public T[] GetPreferenceValues<T>(PreferenceKind preference) public T[] GetPreferenceValues<T>(PreferenceKind preference)
{ {
var val = Preferences.First(p => p.Kind == preference).Value; var val = Preferences.FirstOrDefault(p => p.Kind == preference)?.Value;
if (string.IsNullOrEmpty(val)) if (string.IsNullOrEmpty(val))
{ {
return Array.Empty<T>(); return Array.Empty<T>();
@ -432,8 +440,16 @@ namespace Jellyfin.Data.Entities
/// <param name="values">The values.</param> /// <param name="values">The values.</param>
public void SetPreference(PreferenceKind preference, string[] values) public void SetPreference(PreferenceKind preference, string[] values)
{ {
Preferences.First(p => p.Kind == preference).Value var value = string.Join(Delimiter, values);
= 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;
}
} }
/// <summary> /// <summary>
@ -444,8 +460,16 @@ namespace Jellyfin.Data.Entities
/// <typeparam name="T">The type of value.</typeparam> /// <typeparam name="T">The type of value.</typeparam>
public void SetPreference<T>(PreferenceKind preference, T[] values) public void SetPreference<T>(PreferenceKind preference, T[] values)
{ {
Preferences.First(p => p.Kind == preference).Value var value = string.Join(Delimiter, values);
= 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;
}
} }
/// <summary> /// <summary>