Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

design pattern to implement a set of permissions for a user

I'm trying to figure out the correct way to implement and code the following using desing patterns, or a good object oriented solution:

There is an user class which can contains a variable set of permits, each one enables him to do different action on the application. The idea is to be able tell a certain user object to, for example delete an order, if he has any permits that enable him to do so, do it and if not, to raise an exception.

If someone has a place where to read about this, it's helpfull too. thanks

like image 238
mbmihura Avatar asked Dec 04 '11 18:12

mbmihura


People also ask

What are the different access control design patterns?

Access control patterns The two most observed patterns are Discretionary Access control (DAC) and Role-Based Access Control (RBAC). Mandatory Access Control (MAC) is also relatively common, but not as much as the 2 formers.

Which of the design pattern is used to to access the elements?

In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.

Which design pattern can be used to ensure that only one instance is created by application?

The Singleton Design Pattern is a Creational pattern, whose objective is to create only one instance of a class and to provide only one global access point to that object.

What is implementation in design pattern?

Implementation of a design pattern can take many forms according to the programming language being used. Most of the literature presents design patterns in their conventional object-oriented implementations. Several other studies show the implementation in aspect-oriented languages such as AspectJ, EOS, and Caesar.


1 Answers

There are built in functions for permission in C#/.NET.

The access requirements on a function is set through the PrincipalPermissionAttribute class, or inside the code with PrincipalPermission. To prevent a method from being called unless the current user is a member of the Administrators role, the following attribute is used (sample from MSDN):

[PrincipalPermission(SecurityAction.Demand, Role = "Administrators")]
static void CheckAdministrator()
{
    Console.WriteLine("User is an administrator");
}

Both these checks against the current identity of the calling thread. So what you need to do is to implement the IPrincipal interface to allow your users to be set as the thread identity. Then you can use standard .NET PrincipalPermission to check security. It works exactly as you want - if the security demand is not met, an exception is thrown.

like image 90
Anders Abel Avatar answered Oct 24 '22 17:10

Anders Abel