Group.cs 2.3 KB

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