1. Data structure
The Mad_popedom is the permission table, the control record controller name, and the action record name.
Mad_role is the role table.
2. Implementation of permission control
This is a simpler AOP approach, implemented with the filter of MVC, with the following code
Copy Code code as follows:
Using System.Collections.Generic;
Using SYSTEM.WEB.MVC;
Using Madnet.Model.MadAdmin;
Using Madnet.BLL.MadAdmin;
Namespace Madnet.Controllers.MadAdmin
{
public class Supportfilterattribute:actionfilterattribute
{
private bool _islogin = true;
<summary>
Whether you need to log on
</summary>
public bool IsLogin
{
Set
{
_islogin = value;
}
Get
{
if (system.configuration.configurationmanager.appsettings["IsLogin"]!= null)
{
bool. TryParse (system.configuration.configurationmanager.appsettings["IsLogin"). ToString (), out _islogin);
}
return _islogin;
}
}
public override void OnActionExecuting (ActionExecutingContext filtercontext)
{
String controllername = (string) filtercontext.routedata.values["Controller"];
String actionname = (string) filtercontext.routedata.values["action";
if (islogin && filtercontext.httpcontext.session["login_user"] = = null)
{
FilterContext.HttpContext.Response.Redirect (New Urlhelper (Filtercontext.requestcontext). Action ("Login", "Default"));
Filtercontext.result = new Emptyresult ();
}
else if (islogin && filtercontext.httpcontext.session["Login_user"]!= null)
{
Mad_user User = filtercontext.httpcontext.session["Login_user"] as Mad_user;
if (!user.is_super)
{
if (! Getpopedom (user). Exists (p => p.controller_name = = Controllername.tolower () && p.action_name = Actionname.tolower ()))
{
FilterContext.HttpContext.Response.Write ("no authority");
Filtercontext.result = new Emptyresult ();
}
}
}
}
<summary>
Get all actions that the current user has permission to perform
</summary>
<returns></returns>
Public list<atmodel> getpopedom (Mad_user User)
{
list<atmodel> ATS = new list<atmodel> ();
list<mad_popedom> pops = Mad_popedombll.getpopedombyuser (user.user_id);
foreach (Mad_popedom pop in POPs)
{
Ats. ADD (New Atmodel () {controller_name = pop. Control, Action_name = pop. Action});
}
return ATS;
}
}
}
To explain, the code above is to get the controller-action that the logged-on user can run before executing, and then compare it to the controller-action that is currently required, if it exists, that is, or not.
3. Add Permissions for actions
For simplicity, I am independent of a class library for the controller layer, and the advantage is that when you add permissions to a role, we do not need to enter it manually, as long as the reflection DLL is available.
As the figure shows, any function that requires permission control needs only to add the [Supportfilter] attribute, which can only be controlled to the action level.
4. Add Permissions for role amount
This is relatively simple, just need to associate the role and permissions on it, here I use Reflection Controller layer DLL implementation.
Web.config
Global.asax.cs
Madnet.Controllers.Test is the DLL for the controller layer
Test is controller name, index is the action name, select the action that Role2 can access, submit to the database. This figure indicates that Role2 has Test1controller access, but does not have test2controller,test3controller access rights.
5. End
The above 4 steps are complete with basic permissions control. This can be based on the user group, users, menus and other management, can achieve "user-role-permissions" of the free combination, a simple general background is probably the case.