浏览代码

Expand and document IHasPermissions

Patrick Barron 5 年之前
父节点
当前提交
e8b6da3cd7
共有 3 个文件被更改,包括 39 次插入8 次删除
  1. 12 1
      Jellyfin.Data/Entities/Group.cs
  2. 6 7
      Jellyfin.Data/Entities/User.cs
  3. 21 0
      Jellyfin.Data/IHasPermissions.cs

+ 12 - 1
Jellyfin.Data/Entities/Group.cs

@@ -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;
+        }
     }
 }
-

+ 6 - 7
Jellyfin.Data/Entities/User.cs

@@ -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();
     }
 }

+ 21 - 0
Jellyfin.Data/IHasPermissions.cs

@@ -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);
     }
 }