Group.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #pragma warning disable CA2227
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel.DataAnnotations;
  5. using System.Linq;
  6. using Jellyfin.Data.Enums;
  7. using Jellyfin.Data.Interfaces;
  8. namespace Jellyfin.Data.Entities
  9. {
  10. /// <summary>
  11. /// An entity representing a group.
  12. /// </summary>
  13. public class Group : IHasPermissions, IHasConcurrencyToken
  14. {
  15. /// <summary>
  16. /// Initializes a new instance of the <see cref="Group"/> class.
  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. Preferences = new HashSet<Preference>();
  29. }
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="Group"/> class.
  32. /// </summary>
  33. /// <remarks>
  34. /// Default constructor. Protected due to required properties, but present because EF needs it.
  35. /// </remarks>
  36. protected Group()
  37. {
  38. }
  39. /// <summary>
  40. /// Gets or sets the id of this group.
  41. /// </summary>
  42. /// <remarks>
  43. /// Identity, Indexed, Required.
  44. /// </remarks>
  45. public Guid Id { get; protected set; }
  46. /// <summary>
  47. /// Gets or sets the group's name.
  48. /// </summary>
  49. /// <remarks>
  50. /// Required, Max length = 255.
  51. /// </remarks>
  52. [Required]
  53. [MaxLength(255)]
  54. [StringLength(255)]
  55. public string Name { get; set; }
  56. /// <inheritdoc />
  57. [ConcurrencyCheck]
  58. public uint RowVersion { get; set; }
  59. /// <summary>
  60. /// Gets or sets a collection containing the group's permissions.
  61. /// </summary>
  62. public virtual ICollection<Permission> Permissions { get; protected set; }
  63. /// <summary>
  64. /// Gets or sets a collection containing the group's preferences.
  65. /// </summary>
  66. public virtual ICollection<Preference> Preferences { get; protected set; }
  67. /// <inheritdoc/>
  68. public bool HasPermission(PermissionKind kind)
  69. {
  70. return Permissions.First(p => p.Kind == kind).Value;
  71. }
  72. /// <inheritdoc/>
  73. public void SetPermission(PermissionKind kind, bool value)
  74. {
  75. Permissions.First(p => p.Kind == kind).Value = value;
  76. }
  77. /// <inheritdoc />
  78. public void OnSavingChanges()
  79. {
  80. RowVersion++;
  81. }
  82. }
  83. }