Group.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using Jellyfin.Data.Enums;
  6. using Jellyfin.Data.Interfaces;
  7. namespace Jellyfin.Data.Entities
  8. {
  9. /// <summary>
  10. /// An entity representing a group.
  11. /// </summary>
  12. public class Group : IHasPermissions, IHasConcurrencyToken
  13. {
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="Group"/> class.
  16. /// </summary>
  17. /// <param name="name">The name of the group.</param>
  18. public Group(string name)
  19. {
  20. if (string.IsNullOrEmpty(name))
  21. {
  22. throw new ArgumentNullException(nameof(name));
  23. }
  24. Name = name;
  25. Id = Guid.NewGuid();
  26. Permissions = new HashSet<Permission>();
  27. Preferences = new HashSet<Preference>();
  28. }
  29. /// <summary>
  30. /// Gets the id of this group.
  31. /// </summary>
  32. /// <remarks>
  33. /// Identity, Indexed, Required.
  34. /// </remarks>
  35. public Guid Id { get; private set; }
  36. /// <summary>
  37. /// Gets or sets the group's name.
  38. /// </summary>
  39. /// <remarks>
  40. /// Required, Max length = 255.
  41. /// </remarks>
  42. [MaxLength(255)]
  43. [StringLength(255)]
  44. public string Name { get; set; }
  45. /// <inheritdoc />
  46. [ConcurrencyCheck]
  47. public uint RowVersion { get; private set; }
  48. /// <summary>
  49. /// Gets a collection containing the group's permissions.
  50. /// </summary>
  51. public virtual ICollection<Permission> Permissions { get; private set; }
  52. /// <summary>
  53. /// Gets a collection containing the group's preferences.
  54. /// </summary>
  55. public virtual ICollection<Preference> Preferences { get; private set; }
  56. /// <inheritdoc/>
  57. public bool HasPermission(PermissionKind kind)
  58. {
  59. return Permissions.First(p => p.Kind == kind).Value;
  60. }
  61. /// <inheritdoc/>
  62. public void SetPermission(PermissionKind kind, bool value)
  63. {
  64. Permissions.First(p => p.Kind == kind).Value = value;
  65. }
  66. /// <inheritdoc />
  67. public void OnSavingChanges()
  68. {
  69. RowVersion++;
  70. }
  71. }
  72. }