Group.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel.DataAnnotations;
  5. using System.ComponentModel.DataAnnotations.Schema;
  6. using System.Linq;
  7. using Jellyfin.Data.Enums;
  8. namespace Jellyfin.Data.Entities
  9. {
  10. /// <summary>
  11. /// An entity representing a group.
  12. /// </summary>
  13. public partial class Group : IHasPermissions, ISavingChanges
  14. {
  15. /// <summary>
  16. /// Initializes a new instance of the <see cref="Group"/> class.
  17. /// Public constructor with required data.
  18. /// </summary>
  19. /// <param name="name">The name of the group.</param>
  20. public Group(string name)
  21. {
  22. if (string.IsNullOrEmpty(name))
  23. {
  24. throw new ArgumentNullException(nameof(name));
  25. }
  26. Name = name;
  27. Id = Guid.NewGuid();
  28. Permissions = new HashSet<Permission>();
  29. ProviderMappings = new HashSet<ProviderMapping>();
  30. Preferences = new HashSet<Preference>();
  31. Init();
  32. }
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="Group"/> class.
  35. /// Default constructor. Protected due to required properties, but present because EF needs it.
  36. /// </summary>
  37. protected Group()
  38. {
  39. Init();
  40. }
  41. /*************************************************************************
  42. * Properties
  43. *************************************************************************/
  44. /// <summary>
  45. /// Gets or sets the id of this group.
  46. /// </summary>
  47. /// <remarks>
  48. /// Identity, Indexed, Required.
  49. /// </remarks>
  50. [Key]
  51. [Required]
  52. public Guid Id { get; protected set; }
  53. /// <summary>
  54. /// Gets or sets the group's name.
  55. /// </summary>
  56. /// <remarks>
  57. /// Required, Max length = 255.
  58. /// </remarks>
  59. [Required]
  60. [MaxLength(255)]
  61. [StringLength(255)]
  62. public string Name { get; set; }
  63. /// <summary>
  64. /// Gets or sets the row version.
  65. /// </summary>
  66. /// <remarks>
  67. /// Required, Concurrency Token.
  68. /// </remarks>
  69. [ConcurrencyCheck]
  70. [Required]
  71. public uint RowVersion { get; set; }
  72. public void OnSavingChanges()
  73. {
  74. RowVersion++;
  75. }
  76. /*************************************************************************
  77. * Navigation properties
  78. *************************************************************************/
  79. [ForeignKey("Permission_GroupPermissions_Id")]
  80. public virtual ICollection<Permission> Permissions { get; protected set; }
  81. [ForeignKey("ProviderMapping_ProviderMappings_Id")]
  82. public virtual ICollection<ProviderMapping> ProviderMappings { get; protected set; }
  83. [ForeignKey("Preference_Preferences_Id")]
  84. public virtual ICollection<Preference> Preferences { get; protected set; }
  85. /// <summary>
  86. /// Static create function (for use in LINQ queries, etc.)
  87. /// </summary>
  88. /// <param name="name">The name of this group.</param>
  89. public static Group Create(string name)
  90. {
  91. return new Group(name);
  92. }
  93. /// <inheritdoc/>
  94. public bool HasPermission(PermissionKind kind)
  95. {
  96. return Permissions.First(p => p.Kind == kind).Value;
  97. }
  98. /// <inheritdoc/>
  99. public void SetPermission(PermissionKind kind, bool value)
  100. {
  101. Permissions.First(p => p.Kind == kind).Value = value;
  102. }
  103. partial void Init();
  104. }
  105. }