Tolitech.CleanArchitecture.Application.Authorization 1.0.0-alpha02

This is a prerelease version of Tolitech.CleanArchitecture.Application.Authorization.
dotnet add package Tolitech.CleanArchitecture.Application.Authorization --version 1.0.0-alpha02                
NuGet\Install-Package Tolitech.CleanArchitecture.Application.Authorization -Version 1.0.0-alpha02                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tolitech.CleanArchitecture.Application.Authorization" Version="1.0.0-alpha02" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tolitech.CleanArchitecture.Application.Authorization --version 1.0.0-alpha02                
#r "nuget: Tolitech.CleanArchitecture.Application.Authorization, 1.0.0-alpha02"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Tolitech.CleanArchitecture.Application.Authorization as a Cake Addin
#addin nuget:?package=Tolitech.CleanArchitecture.Application.Authorization&version=1.0.0-alpha02&prerelease

// Install Tolitech.CleanArchitecture.Application.Authorization as a Cake Tool
#tool nuget:?package=Tolitech.CleanArchitecture.Application.Authorization&version=1.0.0-alpha02&prerelease                

Tolitech.CleanArchitecture.Application.Authorization

Overview

Tolitech.CleanArchitecture.Application.Authorization is a lightweight .NET library designed to facilitate the definition and retrieval of application permissions using marker interfaces and reflection. This library helps you manage permissions in a clean and organized manner by leveraging marker interfaces to identify classes that contain permission definitions.

Features

  • Marker Interface for Permissions: Use the IPermissionDefinitions interface to mark classes that define permissions.

  • Reflection-Based Retrieval: Retrieve all permission definitions from specified assemblies using reflection.

  • Easy Integration: Seamlessly integrate with your existing .NET applications.

Getting Started

Installation

You can install the package via NuGet:

dotnet add package Tolitech.CleanArchitecture.Application.Authorization

Usage

Define Permissions

Create classes that implement the IPermissionDefinitions interface to define your permissions. For example:

namespace MyApplication.Permissions
{
    public class UserPermissions : IPermissionDefinitions
    {
        public const string ViewUsers = "Permissions.Users.View";
        public const string EditUsers = "Permissions.Users.Edit";
    }

    public class AdminPermissions : IPermissionDefinitions
    {
        public const string ManageRoles = "Permissions.Admin.ManageRoles";
        public const string ViewLogs = "Permissions.Admin.ViewLogs";
    }
}

Retrieve Permissions

Use the PermissionDefinitions.GetAllPermissions method to retrieve all permissions defined in the specified assembly:

using System.Reflection;
using Tolitech.CleanArchitecture.Application.Authorization;

Assembly assembly = Assembly.GetExecutingAssembly();
ReadOnlyCollection<string> permissions = PermissionDefinitions.GetAllPermissions(assembly);

foreach (var permission in permissions)
{
    Console.WriteLine(permission);
}

This will output:

Permissions.Users.View
Permissions.Users.Edit
Permissions.Admin.ManageRoles
Permissions.Admin.ViewLogs

Detailed Explanation

Marker Interface

The IPermissionDefinitions interface serves as a marker to identify classes that contain permission definitions. This makes it easy to locate and process permission-related classes within your application.

Reflection-Based Retrieval

The PermissionDefinitions.GetAllPermissions method uses reflection to find all classes that implement the IPermissionDefinitions interface within a given assembly. It then retrieves the values of all public static string fields within those classes, which are treated as permission names.

Benefits

  • Organized Permission Management: Keeps your permission definitions organized and easily accessible.
  • Reduced Hardcoding: Centralizes permission definitions, reducing the need to hardcode permission strings throughout your application.
  • Scalability: Easily add new permissions by simply adding new classes or fields implementing the IPermissionDefinitions interface.
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Tolitech.CleanArchitecture.Application.Authorization:

Package Downloads
Tolitech.CleanArchitecture.Infrastructure.Authorization

The library provides implementations for managing authorization logic. It includes classes and utilities for handling permissions, attribute-based authorization, and configuring authorization policies.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0-alpha02 81 7/4/2024
1.0.0-alpha01 54 6/28/2024