Mtf.Permissions
1.0.0
See the version list below for details.
dotnet add package Mtf.Permissions --version 1.0.0
NuGet\Install-Package Mtf.Permissions -Version 1.0.0
<PackageReference Include="Mtf.Permissions" Version="1.0.0" />
paket add Mtf.Permissions --version 1.0.0
#r "nuget: Mtf.Permissions, 1.0.0"
// Install Mtf.Permissions as a Cake Addin #addin nuget:?package=Mtf.Permissions&version=1.0.0 // Install Mtf.Permissions as a Cake Tool #tool nuget:?package=Mtf.Permissions&version=1.0.0
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:
Add Package:
Run the following command in your project directory:dotnet add package Mtf.Permissions
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
- Assign user permissions during initialization.
- Attach the
Tag
property of controls to the relevant method names. - Use the
RequirePermission
attribute to restrict access to specific methods. - Dynamically enable/disable controls and validate permissions using
PermissionManager
.
Notes
- Error Handling: Ensure to handle exceptions like
UnauthorizedAccessException
orInvalidOperationException
for better user feedback. - Flexibility: Extend
PermissionType
andPermission
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 | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net481 is compatible. |
-
.NETFramework 4.8.1
- System.Drawing.Common (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.