// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see .
using System;
using Kyoo.Abstractions.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
namespace Kyoo.Abstractions.Models.Permissions;
///
/// The kind of permission needed.
///
public enum Kind
{
///
/// Allow the user to read for this kind of data.
///
Read,
///
/// Allow the user to write for this kind of data.
///
Write,
///
/// Allow the user to create this kind of data.
///
Create,
///
/// Allow the user to delete this kind of data.
///
Delete,
///
/// Allow the user to play this file.
///
Play,
}
///
/// The group of the permission.
///
public enum Group
{
///
/// Default group indicating no value.
///
None,
///
/// Allow all operations on basic items types.
///
Overall,
///
/// Allow operation on sensitive items like libraries path, configurations and so on.
///
Admin
}
///
/// Specify permissions needed for the API.
///
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public class PermissionAttribute : Attribute, IFilterFactory
{
///
/// The needed permission as string.
///
public string Type { get; }
///
/// The needed permission kind.
///
public Kind Kind { get; }
///
/// The group of this permission.
///
public Group Group { get; }
///
/// Ask a permission to run an action.
///
///
/// The type of the action
///
///
/// The kind of permission needed.
///
///
/// The group of this permission (allow grouped permission like overall.read
/// for all read permissions of this group).
///
public PermissionAttribute(string type, Kind permission, Group group = Group.Overall)
{
Type = type.ToLower();
Kind = permission;
Group = group;
}
///
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
{
return serviceProvider.GetRequiredService().Create(this);
}
///
public bool IsReusable => true;
///
/// Return this permission attribute as a string.
///
/// The string representation.
public string AsPermissionString()
{
return Type;
}
}