Group.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. using Jellyfin.Data.Interfaces;
  9. namespace Jellyfin.Data.Entities
  10. {
  11. /// <summary>
  12. /// An entity representing a group.
  13. /// </summary>
  14. public partial class Group : IHasPermissions, IHasConcurrencyToken
  15. {
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="Group"/> class.
  18. /// Public constructor with required data.
  19. /// </summary>
  20. /// <param name="name">The name of the group.</param>
  21. public Group(string name)
  22. {
  23. if (string.IsNullOrEmpty(name))
  24. {
  25. throw new ArgumentNullException(nameof(name));
  26. }
  27. Name = name;
  28. Id = Guid.NewGuid();
  29. Permissions = new HashSet<Permission>();
  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("Preference_Preferences_Id")]
  82. public virtual ICollection<Preference> Preferences { get; protected set; }
  83. /// <summary>
  84. /// Static create function (for use in LINQ queries, etc.)
  85. /// </summary>
  86. /// <param name="name">The name of this group.</param>
  87. public static Group Create(string name)
  88. {
  89. return new Group(name);
  90. }
  91. /// <inheritdoc/>
  92. public bool HasPermission(PermissionKind kind)
  93. {
  94. return Permissions.First(p => p.Kind == kind).Value;
  95. }
  96. /// <inheritdoc/>
  97. public void SetPermission(PermissionKind kind, bool value)
  98. {
  99. Permissions.First(p => p.Kind == kind).Value = value;
  100. }
  101. partial void Init();
  102. }
  103. }