-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseController.cs
209 lines (175 loc) · 7 KB
/
BaseController.cs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ATGV3.Business.Model;
using ATGV3.Common;
using ATGV3.Web.Helpers;
using Microsoft.AspNet.Identity;
using Newtonsoft.Json;
using ATGV3.Business;
using System.Security.Principal;
using Microsoft.Owin.Security;
namespace ATGV3.Web.Controllers
{
[Authorize]
public class BaseController : Controller
{
protected ApplicationServices _applicationServices = ApplicationHelper.Instance.GetApplicationServices();
protected ApplicationServicesSetup _applicationServicesSetup = ApplicationHelper.Instance.GetApplicationServicesSetup();
protected AspNetUser _user;
protected const int VALIDATION_ERROR_RESPONSE_CODE = 409;
protected const int ERROR_RESPONSE_CODE = 500;
protected ServerEventsHelper EventDispatcher;
public BaseController()
{
_SetAppUser();
EventDispatcher = new ServerEventsHelper();
ViewBag.User = _user;
}
private void _SetAppUser()
{
var loggedInUserName = System.Web.HttpContext.Current.User.Identity.GetUserName();
if (!string.IsNullOrEmpty(loggedInUserName))
{
//_user = ConvertObjectFromCache<AspNetUser>(_applicationServices.CacheService().Get(loggedInUserName));
//get user from database
if (_user == null)
{
using (UserBusiness ub = new UserBusiness(_applicationServicesSetup))
{
_user = ub.GetUser(loggedInUserName, new[]{
Globals.Related.User.AspNetRoles,
Globals.Related.User.Groups,
Globals.Related.User.Settings,
Globals.Related.User.SavedSearches,
Globals.Related.User.Permissions
}, false);
//todo: put cache time into database
//_applicationServices.CacheService().Set(loggedInUserName, ConvertObjectToCache(_user), new DateTimeOffset(DateTime.Now.AddMinutes(600)));
}
if (!_user.Active)
{
RedirectToAction("Login", new { Code = "Locked" });
}
}
}
}
public static T ConvertObjectFromCache<T>(Object cacheObject)
{
if (cacheObject == null)
{
return default(T);
}
if (String.IsNullOrEmpty(cacheObject.ToString()))
{
return default(T);
}
return JsonConvert.DeserializeObject<T>(cacheObject.ToString(), new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, PreserveReferencesHandling = PreserveReferencesHandling.Objects });
}
public static string ConvertObjectToCache(object cacheObject)
{
return JsonConvert.SerializeObject(cacheObject, Formatting.None, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto, PreserveReferencesHandling = PreserveReferencesHandling.Objects });
}
protected override void Dispose(bool disposing)
{
_applicationServices = null;
_applicationServicesSetup = null;
_user = null;
base.Dispose(disposing);
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Trace.Write("(Controller)Action Executing: " +
filterContext.ActionDescriptor.ActionName);
base.OnActionExecuting(filterContext);
}
protected string ConvertToJson(List<string> list)
{
return JsonConvert.SerializeObject(list);
}
protected ActionResult ValidationErrorResponse(string message)
{
HttpContext.Response.StatusCode = VALIDATION_ERROR_RESPONSE_CODE;
return Json(new List<string>() { message }, JsonRequestBehavior.AllowGet);
}
protected ActionResult ValidationErrorResponse(IEnumerable<string> messages)
{
HttpContext.Response.StatusCode = VALIDATION_ERROR_RESPONSE_CODE;
return Json(messages, JsonRequestBehavior.AllowGet);
}
protected ActionResult SuccessMessage(dynamic data = null, string message = null)
{
return Json(new {
Success = true,
data = data,
Message = message
}, JsonRequestBehavior.AllowGet);
}
[PreventDirectAccess]
protected ActionResult ErrorMessage(string message)
{
return Json(new {
Success = false,
Message = message
}, JsonRequestBehavior.AllowGet);
}
protected ActionResult ErrorMessage(IEnumerable<string> messages)
{
return Json(new
{
Success = false,
Message = ConvertToJson(messages.ToList())
}, JsonRequestBehavior.AllowGet);
}
[PreventDirectAccess]
public ActionResult ServerError()
{
HttpContext.Response.StatusCode = 500;
if(Request.IsAjaxRequest())
return ErrorMessage("An error occured processing your last request");
else
return Redirect("~/500.html");
}
[PreventDirectAccess]
public ActionResult AccessDenied()
{
HttpContext.Response.StatusCode = 403;
if(Request.IsAjaxRequest())
return ErrorMessage("Access to this Page is Denied");
else
return Redirect("~/403.html");
}
[PreventDirectAccess]
public ActionResult NotFound()
{
HttpContext.Response.StatusCode = 404;
if (Request.IsAjaxRequest())
{
return ErrorMessage("Page Not Found");
}else
return Redirect("~/404.html");
}
[PreventDirectAccess]
public ActionResult PDF(string url = null, List<string> otherUrls = null)
{
if (url != null)
url = "http://" + Request.Url.Authority + url;
if (otherUrls == null)
otherUrls = new List<string>();
return File(
_applicationServices.CreatePDF( url, otherUrls.ToArray()),
"application/pdf"
);
}
[PreventDirectAccess]
public ActionResult PDF(byte[] pdf)
{
return File(pdf, "application/pdf");
}
protected AspNetUser GetSystemUser(){
return ApplicationHelper.Instance.GetSystemUser();
}
}
}