jellyfin/Jellyfin.Data/Entities/Group.cs

95 lines
2.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
2020-05-30 17:49:31 -07:00
using System.Linq;
using Jellyfin.Data.Enums;
using Jellyfin.Data.Interfaces;
namespace Jellyfin.Data.Entities
{
2020-05-30 18:53:56 -07:00
/// <summary>
/// An entity representing a group.
/// </summary>
2020-09-01 08:16:09 -07:00
public class Group : IHasPermissions, IHasConcurrencyToken
2020-05-02 14:56:05 -07:00
{
/// <summary>
2020-05-30 18:53:56 -07:00
/// Initializes a new instance of the <see cref="Group"/> class.
2020-05-02 14:56:05 -07:00
/// </summary>
2020-05-30 18:53:56 -07:00
/// <param name="name">The name of the group.</param>
public Group(string name)
2020-05-02 14:56:05 -07:00
{
2020-05-15 14:24:01 -07:00
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException(nameof(name));
}
2020-05-02 14:56:05 -07:00
2020-05-30 18:53:56 -07:00
Name = name;
Id = Guid.NewGuid();
2020-05-02 14:56:05 -07:00
2020-05-30 18:53:56 -07:00
Permissions = new HashSet<Permission>();
Preferences = new HashSet<Preference>();
2020-05-02 14:56:05 -07:00
}
/// <summary>
2020-05-30 18:53:56 -07:00
/// Initializes a new instance of the <see cref="Group"/> class.
2020-05-02 14:56:05 -07:00
/// </summary>
2020-09-01 08:16:09 -07:00
/// <remarks>
/// Default constructor. Protected due to required properties, but present because EF needs it.
/// </remarks>
2020-05-30 18:53:56 -07:00
protected Group()
2020-05-02 14:56:05 -07:00
{
}
/// <summary>
2020-05-30 18:53:56 -07:00
/// Gets or sets the id of this group.
2020-05-02 14:56:05 -07:00
/// </summary>
2020-05-30 18:53:56 -07:00
/// <remarks>
/// Identity, Indexed, Required.
/// </remarks>
2020-05-15 14:24:01 -07:00
public Guid Id { get; protected set; }
2020-05-02 14:56:05 -07:00
/// <summary>
2020-05-30 18:53:56 -07:00
/// Gets or sets the group's name.
2020-05-02 14:56:05 -07:00
/// </summary>
2020-05-30 18:53:56 -07:00
/// <remarks>
/// Required, Max length = 255.
/// </remarks>
2020-05-02 14:56:05 -07:00
[Required]
[MaxLength(255)]
[StringLength(255)]
public string Name { get; set; }
2020-09-01 08:16:09 -07:00
/// <inheritdoc />
2020-05-02 14:56:05 -07:00
[ConcurrencyCheck]
public uint RowVersion { get; set; }
2020-09-01 08:16:09 -07:00
/// <summary>
/// Gets or sets a collection containing the group's permissions.
/// </summary>
2020-05-19 16:05:17 -07:00
public virtual ICollection<Permission> Permissions { get; protected set; }
2020-05-02 14:56:05 -07:00
2020-05-30 18:53:56 -07:00
/// <summary>
2020-09-01 08:16:09 -07:00
/// Gets or sets a collection containing the group's preferences.
2020-05-30 18:53:56 -07:00
/// </summary>
2020-09-01 08:16:09 -07:00
public virtual ICollection<Preference> Preferences { get; protected set; }
2020-05-30 18:53:56 -07:00
/// <inheritdoc/>
2020-05-30 17:49:31 -07:00
public bool HasPermission(PermissionKind kind)
{
return Permissions.First(p => p.Kind == kind).Value;
}
2020-05-30 18:53:56 -07:00
/// <inheritdoc/>
2020-05-30 17:49:31 -07:00
public void SetPermission(PermissionKind kind, bool value)
{
Permissions.First(p => p.Kind == kind).Value = value;
}
2020-05-30 18:53:56 -07:00
2020-09-01 08:16:09 -07:00
/// <inheritdoc />
public void OnSavingChanges()
{
RowVersion++;
}
2020-05-02 14:56:05 -07:00
}
}