Expand and document IHasPermissions

This commit is contained in:
Patrick Barron 2020-05-30 20:49:31 -04:00
parent 4cff9b8512
commit e8b6da3cd7
3 changed files with 39 additions and 8 deletions

View File

@ -2,6 +2,8 @@ using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using Jellyfin.Data.Enums;
namespace Jellyfin.Data.Entities
{
@ -96,6 +98,15 @@ namespace Jellyfin.Data.Entities
[ForeignKey("Preference_Preferences_Id")]
public virtual ICollection<Preference> Preferences { get; protected set; }
public bool HasPermission(PermissionKind kind)
{
return Permissions.First(p => p.Kind == kind).Value;
}
public void SetPermission(PermissionKind kind, bool value)
{
Permissions.First(p => p.Kind == kind).Value = value;
}
}
}

View File

@ -389,16 +389,14 @@ namespace Jellyfin.Data.Entities
RowVersion++;
}
partial void Init();
/// <summary>
/// Checks whether the user has the specified permission.
/// </summary>
/// <param name="permission">The permission kind.</param>
/// <param name="kind">The permission kind.</param>
/// <returns><c>True</c> if the user has the specified permission.</returns>
public bool HasPermission(PermissionKind permission)
public bool HasPermission(PermissionKind kind)
{
return Permissions.First(p => p.Kind == permission).Value;
return Permissions.First(p => p.Kind == kind).Value;
}
/// <summary>
@ -408,8 +406,7 @@ namespace Jellyfin.Data.Entities
/// <param name="value">The value to set.</param>
public void SetPermission(PermissionKind kind, bool value)
{
var permissionObj = Permissions.First(p => p.Kind == kind);
permissionObj.Value = value;
Permissions.First(p => p.Kind == kind).Value = value;
}
/// <summary>
@ -501,5 +498,7 @@ namespace Jellyfin.Data.Entities
Preferences.Add(new Preference(val, string.Empty));
}
}
partial void Init();
}
}

View File

@ -1,10 +1,31 @@
using System.Collections.Generic;
using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums;
namespace Jellyfin.Data
{
/// <summary>
/// An abstraction representing an entity that has permissions.
/// </summary>
public interface IHasPermissions
{
/// <summary>
/// Gets a collection containing this entity's permissions.
/// </summary>
ICollection<Permission> Permissions { get; }
/// <summary>
/// Checks whether this entity has the specified permission kind.
/// </summary>
/// <param name="kind">The kind of permission.</param>
/// <returns><c>true</c> if this entity has the specified permission, <c>false</c> otherwise.</returns>
bool HasPermission(PermissionKind kind);
/// <summary>
/// Sets the specified permission to the provided value.
/// </summary>
/// <param name="kind">The kind of permission.</param>
/// <param name="value">The value to set.</param>
void SetPermission(PermissionKind kind, bool value);
}
}