-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseController.cs
More file actions
121 lines (88 loc) · 3.06 KB
/
Copy pathBaseController.cs
File metadata and controls
121 lines (88 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using CfMembership;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using static System.Collections.Specialized.BitVector32;
namespace Cf.Controllers
{
public class UserManagement
{
public CfIdentityEntities E = new CfIdentityEntities();
public void login(string username, string password)
{
HttpContext.Current.Session["user"] = E.Users.Where(c => c.UserName == username && c.Password == password).FirstOrDefault();
}
public bool IsAllow(string controller, string action)
{
if (HttpContext.Current.Session["user"] == null)
{
return false;
}
CfMembership.User uu = HttpContext.Current.Session["user"] as CfMembership.User;
var x = from u in E.UserRoles
join r in E.Roles on u.RoleId equals r.Id
join rp in E.RolePrivileges on r.Id equals rp.RoleId
join
p in E.Privileges on rp.PrivilegeId equals p.Id
where
(p.Controller == controller && p.Action == action && u.UserId == uu.Id)
select p;
if (x.Count() == 0)
{
return false;
}
else
{
return true;
}
}
}
public class BaseController : Controller
{
public int CurrentUserId
{
get
{
if(Session["userId"]==null)
{
Session["userId"] = -1;
}
return (int)Session["userId"];
}
}
public User CurrentUser
{
get
{
return Session["user"] as CfMembership.User;
}
}
protected override void Initialize(RequestContext requestContext)
{
base.Initialize(requestContext);
var rd = requestContext.RouteData;
string currentAction = rd.GetRequiredString("action");
string currentController = rd.GetRequiredString("controller");
Session["backAction"] = Session["currentAction"];
Session["backController"] = Session["currentController"];
Session["currentAction"] = currentAction;
Session["currentController"] = currentController;
if (currentAction != "Edit") {
UserManagement u = new UserManagement();
if (!u.IsAllow(currentController, currentAction) && currentController != "Home")
{
requestContext.HttpContext.Response.Clear();
if (!Response.IsRequestBeingRedirected)
requestContext.HttpContext.Response.Redirect(Url.Action("AccessDenied", "Home"));
requestContext.HttpContext.Response.End();
}
}
}
}
}