Group.cs 3.6 KB

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