Group.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. ArgumentException.ThrowIfNullOrEmpty(name);
  21. Name = name;
  22. Id = Guid.NewGuid();
  23. Permissions = new HashSet<Permission>();
  24. Preferences = new HashSet<Preference>();
  25. }
  26. /// <summary>
  27. /// Gets the id of this group.
  28. /// </summary>
  29. /// <remarks>
  30. /// Identity, Indexed, Required.
  31. /// </remarks>
  32. public Guid Id { get; private set; }
  33. /// <summary>
  34. /// Gets or sets the group's name.
  35. /// </summary>
  36. /// <remarks>
  37. /// Required, Max length = 255.
  38. /// </remarks>
  39. [MaxLength(255)]
  40. [StringLength(255)]
  41. public string Name { get; set; }
  42. /// <inheritdoc />
  43. [ConcurrencyCheck]
  44. public uint RowVersion { get; private set; }
  45. /// <summary>
  46. /// Gets a collection containing the group's permissions.
  47. /// </summary>
  48. public virtual ICollection<Permission> Permissions { get; private set; }
  49. /// <summary>
  50. /// Gets a collection containing the group's preferences.
  51. /// </summary>
  52. public virtual ICollection<Preference> Preferences { get; private set; }
  53. /// <inheritdoc/>
  54. public bool HasPermission(PermissionKind kind)
  55. {
  56. return Permissions.First(p => p.Kind == kind).Value;
  57. }
  58. /// <inheritdoc/>
  59. public void SetPermission(PermissionKind kind, bool value)
  60. {
  61. Permissions.First(p => p.Kind == kind).Value = value;
  62. }
  63. /// <inheritdoc />
  64. public void OnSavingChanges()
  65. {
  66. RowVersion++;
  67. }
  68. }
  69. }