Mtf.Permissions 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Mtf.Permissions --version 1.0.1                
NuGet\Install-Package Mtf.Permissions -Version 1.0.1                
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="Mtf.Permissions" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Mtf.Permissions --version 1.0.1                
#r "nuget: Mtf.Permissions, 1.0.1"                
#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 Mtf.Permissions as a Cake Addin
#addin nuget:?package=Mtf.Permissions&version=1.0.1

// Install Mtf.Permissions as a Cake Tool
#tool nuget:?package=Mtf.Permissions&version=1.0.1                

Mtf.Permissions Library Documentation

Overview

The Mtf.Permissions library provides functionality for managing user permissions in applications. It includes tools for defining, checking, and applying permissions on methods and UI controls. This document explains installation, main classes, attributes, and usage examples to integrate permissions management into your .NET applications.

Installation

To use Mtf.Permissions, add the package to your project:

  1. Add Package:
    Run the following command in your project directory:

    dotnet add package Mtf.Permissions
    
  2. Include the Namespace:
    Add the following namespace at the beginning of your code:

    using Mtf.Permissions;
    

Key Components

Class: PermissionManager

The PermissionManager class provides methods for applying permissions to UI controls and verifying user access to specific methods.

Properties
Property Type Description
currentUser User Represents the current user and their permissions.
Methods
Method Description
SetUser(Control container, User currentUser) Sets the current user and applies their permissions to all controls in the specified container.
ApplyPermissionsOnControls(Control container) Iterates through the container's controls and enables or disables them based on the user's permissions.
EnsurePermissions() Ensures the current user has permission to execute the calling method. Throws an exception if permission is denied.
Example Usage
var permissionManager = new PermissionManager();
permissionManager.SetUser(mainForm, currentUser);
permissionManager.ApplyPermissionsOnControls(mainForm);

Attribute: RequirePermissionAttribute

The RequirePermissionAttribute is used to annotate methods that require specific permissions.

Properties
Property Type Description
PermissionType PermissionType The type of permission required.
Example Usage
[RequirePermission(PermissionType.Admin)]
private void PerformAdminAction()
{
    // Method logic here
}

Class: User

The User class represents a system user and their permissions.

Properties
Property Type Description
Username string The username of the user.
IndividualPermissions List<Permission> A list of permissions assigned directly to the user.
Groups List<Group> A list of groups the user belongs to, each group containing permissions.

Class: Permission

The Permission class defines a specific type of permission.

Properties
Property Type Description
PermissionType PermissionType The type of permission.

Enum: PermissionType

Defines various permission types used in the application.


Example Application

Scenario

A Windows Forms application where users (Guest, Member, Admin) have different levels of access to UI controls and functionality.

MainForm Code Example
public partial class MainForm : Form
{
    private PermissionManager permissionManager;
    private User currentUser;

    public MainForm()
    {
        InitializeComponent();
        permissionManager = new PermissionManager();

        // Define users and their permissions
        var guest = new User { Username = "Guest", IndividualPermissions = new() };
		var member = new User { Username = "Group member", Groups = [ new Group { Permissions = [new Permission { PermissionType = PermissionType.Admin }] } ] };
        var admin = new User { Username = "Admin", IndividualPermissions = new() { new Permission { PermissionType = PermissionType.Admin } } };

        // Assign control tags for permissions
        someButton.Tag = "PerformAdminAction";

        // Apply permissions based on current user
        permissionManager.SetUser(this, admin);
    }

    [RequirePermission(PermissionType.Admin)]
    private void PerformAdminAction()
    {
        // Admin-only action
        MessageBox.Show("Admin action performed.");
    }
}
Example Flow
  1. Assign user permissions during initialization.
  2. Attach the Tag property of controls to the relevant method names.
  3. Use the RequirePermission attribute to restrict access to specific methods.
  4. Dynamically enable/disable controls and validate permissions using PermissionManager.

Notes

  • Error Handling: Ensure to handle exceptions like UnauthorizedAccessException or InvalidOperationException for better user feedback.
  • Flexibility: Extend PermissionType and Permission classes as needed to support custom permission logic.
  • Optimization: For large UI forms, consider caching the permission results to improve performance.

For additional questions or contributions, refer to the package repository.

Product Compatible and additional computed target framework versions.
.NET net8.0-windows7.0 is compatible.  net9.0-windows7.0 is compatible. 
.NET Framework net481 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.8.1

    • No dependencies.
  • net8.0-windows7.0

    • No dependencies.
  • net9.0-windows7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.12 65 12/23/2024
3.0.11 78 12/16/2024
3.0.10 67 12/16/2024
3.0.9 71 11/27/2024
3.0.8 66 11/27/2024
3.0.7 78 11/27/2024
3.0.6 89 11/26/2024
2.0.5 94 11/26/2024
2.0.4 83 11/26/2024
1.0.3 84 11/26/2024
1.0.2 94 11/26/2024
1.0.1 101 11/21/2024
1.0.0 83 11/21/2024